Skip to content

Commit

Permalink
add otcmarketsorder test
Browse files Browse the repository at this point in the history
ref test: remove not needed wait(seconds:)
  • Loading branch information
kosyloa committed Mar 26, 2024
1 parent 0c0ad28 commit 53106ac
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 115 deletions.
4 changes: 4 additions & 0 deletions DXFeedFramework.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@
646D19E32A3C994100C82315 /* DXFTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 646D19E02A3C970700C82315 /* DXFTimer.swift */; };
647426AD2ABC85F20012F793 /* Arguments.swift in Sources */ = {isa = PBXBuildFile; fileRef = 647426AC2ABC85F20012F793 /* Arguments.swift */; };
647426AF2ABC93900012F793 /* EventCode+String.swift in Sources */ = {isa = PBXBuildFile; fileRef = 647426AE2ABC93900012F793 /* EventCode+String.swift */; };
64820AAF2BB2E26100BDFD0B /* DXOtcMarketOrderTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64820AAE2BB2E26100BDFD0B /* DXOtcMarketOrderTest.swift */; };
648468AC2B0D06FA008297AE /* DXDateOffset.swift in Sources */ = {isa = PBXBuildFile; fileRef = 648468AB2B0D06FA008297AE /* DXDateOffset.swift */; };
6486B9582AD00BDC00D8D5FA /* Summary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6486B9572AD00BDC00D8D5FA /* Summary.swift */; };
6486B95B2AD015B400D8D5FA /* PriceType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6486B95A2AD015B400D8D5FA /* PriceType.swift */; };
Expand Down Expand Up @@ -717,6 +718,7 @@
646D19E02A3C970700C82315 /* DXFTimer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DXFTimer.swift; sourceTree = "<group>"; };
647426AC2ABC85F20012F793 /* Arguments.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Arguments.swift; sourceTree = "<group>"; };
647426AE2ABC93900012F793 /* EventCode+String.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "EventCode+String.swift"; sourceTree = "<group>"; };
64820AAE2BB2E26100BDFD0B /* DXOtcMarketOrderTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DXOtcMarketOrderTest.swift; sourceTree = "<group>"; };
648468AB2B0D06FA008297AE /* DXDateOffset.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DXDateOffset.swift; sourceTree = "<group>"; };
6486B9572AD00BDC00D8D5FA /* Summary.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Summary.swift; sourceTree = "<group>"; };
6486B95A2AD015B400D8D5FA /* PriceType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PriceType.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1610,6 +1612,7 @@
646064E92B4D8973009201E2 /* DXLastEventTest.swift */,
64EAA1A12B7A38F8005087BC /* DXPromiseTest.swift */,
64EAA1A52B838ED3005087BC /* DXSnapshotProcessorTest.swift */,
64820AAE2BB2E26100BDFD0B /* DXOtcMarketOrderTest.swift */,
);
path = DXFeedFrameworkTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -2553,6 +2556,7 @@
64ACBCCF2A27851C00032C53 /* FeedTest.swift in Sources */,
648C72472B18ABFC00E2FEF3 /* DXConnectionTest.swift in Sources */,
6486B95D2AD0287E00D8D5FA /* DateTests.swift in Sources */,
64820AAF2BB2E26100BDFD0B /* DXOtcMarketOrderTest.swift in Sources */,
6447A5ED2A8FCC2200739CCF /* UtilsTest.swift in Sources */,
64278C6C2A602CA20074B5AA /* CandleTests.swift in Sources */,
6498E6B72AB1DACE0093A065 /* ScheduleTest.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ class OtcMarketsOrderMapper: Mapper {

func fromNative(native: UnsafeMutablePointer<dxfg_event_type_t>) -> MarketEvent? {
let event = native.withMemoryRebound(to: type, capacity: 1) { native in
print("ASD")
let native123 = native.pointee
return OtcMarketsOrder(otcNative: native.pointee)
}
return event
Expand Down
37 changes: 17 additions & 20 deletions DXFeedFrameworkTests/DXConnectionStateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import XCTest
@testable import DXFeedFramework

final class DXConnectionStateTests: XCTestCase {
static let port = 7301
static let port = Int.random(in: 7800..<7900)
static let endpointAddress = "localhost:\(port)"

static var publisherEndpoint: DXEndpoint?
Expand Down Expand Up @@ -49,43 +49,40 @@ final class DXConnectionStateTests: XCTestCase {
let expsNotConnected = Array(expectations.filter({ element in
element.key == .notConnected
}).values)
wait(for: expsNotConnected, timeout: 5)
wait(for: expsNotConnected, timeout: 1)
}

func testListenerDealloc() throws {
var endpoint: DXEndpoint? = try DXEndpoint.builder().withRole(.feed).withProperty("test", "value").build()
XCTAssertNotNil(endpoint, "Endpoint should be not nil")
_ = try endpoint?.connect(Self.endpointAddress)
var state = try? endpoint?.getState()
try endpoint?.close()
try endpoint?.disconnect()
wait(seconds: 2)
// Isolate.shared.callGC()
state = try? endpoint?.getState()
try endpoint?.close()
state = try? endpoint?.getState()
// Isolate.shared.callGC()
try endpoint?.close()
state = try? endpoint?.getState()
// Isolate.shared.callGC()
state = try? endpoint?.getState()
endpoint = nil
endpoint = try DXEndpoint.builder().withRole(.feed).withProperty("test", "value").build()
XCTAssertNotNil(endpoint, "Endpoint should be not nil")
_ = try endpoint?.connect(Self.endpointAddress)
try endpoint?.disconnect()
print("\(state ?? .notConnected)")
wait(seconds: 3)
XCTAssertEqual(try endpoint?.getState(), .notConnected)
}

func testReconnect() throws {
let endpoint = try DXEndpoint.builder().withRole(.feed).withProperty("test", "value").build()
XCTAssertNotNil(endpoint, "Endpoint should be not nil")
try endpoint.connect(Self.endpointAddress)
wait(seconds: 1)
wait(for: [expectation(for: NSPredicate(block: { object, _ in
let state = try? (object as? DXEndpoint)?.getState()
return state! == .connected
}), evaluatedWith: endpoint)], timeout: 2)
XCTAssertEqual(try endpoint.getState(), .connected)
try endpoint.reconnect()
wait(seconds: 1)
try endpoint.close()
XCTAssertEqual(try endpoint.getState(), .closed)
}

func testCreationPublisher() throws {
Expand All @@ -94,22 +91,22 @@ final class DXConnectionStateTests: XCTestCase {
DXEndpointState.connecting: expectation(description: "Connecting")]
let listener = TestListener(expectations: expectations)
endpoint?.add(listener: listener)
try endpoint?.connect(":4777")
let exps = Array(expectations.filter({ element in
element.key != .notConnected
}).values)
wait(for: exps, timeout: 1)
try endpoint?.connect(":\(Int.random(in: 7800..<7900))")
wait(for: Array(expectations.values), timeout: 1)
endpoint = nil
wait(seconds: 2)
wait(seconds: 1)
}

func testDisconnectAndClear() throws {
let endpoint = try DXEndpoint.builder().withRole(.feed).withProperty("test", "value").build()
XCTAssertNotNil(endpoint, "Endpoint should be not nil")
try endpoint.connect(Self.endpointAddress)
wait(seconds: 1)
wait(for: [expectation(for: NSPredicate(block: { object, _ in
let state = try? (object as? DXEndpoint)?.getState()
return state! == .connected
}), evaluatedWith: endpoint)], timeout: 2)
try endpoint.disconnectAndClear()
wait(seconds: 1)
XCTAssertEqual(try endpoint.getState(), .notConnected)
}

}
10 changes: 6 additions & 4 deletions DXFeedFrameworkTests/DXLastEventTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,8 @@ final class DXLastEventTest: XCTestCase {
}

func getLastEvents(symbol: String, events: [MarketEvent], fetching: ((DXFeed?) -> Void)) {
let port = Int.random(in: 7500..<7600)
do {
let endpoint: DXEndpoint? = try DXEndpoint.create(.publisher)
try endpoint?.connect(":\(port)")
let endpoint: DXEndpoint? = try DXEndpoint.create(.localHub)
let publisher = endpoint?.getPublisher()
let connectedExpectation = expectation(description: "Connected")
let stateListener: TestEndpoointStateListener? = TestEndpoointStateListener { listener in
Expand Down Expand Up @@ -141,8 +139,12 @@ final class DXLastEventTest: XCTestCase {
Series.self,
OptionSale.self]
let subscription = try feedEndpoint.getFeed()?.createSubscription(allTypes)
try feedEndpoint.connect("localhost:\(port)")
try subscription?.addSymbols(symbol)
DispatchQueue.global(qos: .background).asyncAfter(deadline: .now() + 0.3) {
print(Thread.current.threadName)
try? publisher?.publish(events: events)
connectedExpectation.fulfill()
}
wait(for: [connectedExpectation], timeout: 1)
fetching(feedEndpoint.getFeed())
} catch {
Expand Down
7 changes: 2 additions & 5 deletions DXFeedFrameworkTests/DXObservableSubscriptionTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,16 @@ final class DXObservableSubscriptionTest: XCTestCase {

func testCreateSubscription() throws {
let events = Profile.self
let port = Int.random(in: 7800..<7900)
let address = ":\(port)"

let endpoint = try DXEndpoint.create(.publisher).connect(address)
let endpoint = try DXEndpoint.create(.localHub)
let publisher = endpoint.getPublisher()
let listener = PublishListener(publisher: publisher!)
let subscription = try publisher?.getSubscription(events)
try subscription?.addChangeListener(listener)

let receivedEventsExpectation = expectation(description: "Received events")

let feedEndpoint = try DXEndpoint.create().connect("localhost:\(port)")
let feedSubscription = try feedEndpoint.getFeed()?.createSubscription(events)
let feedSubscription = try endpoint.getFeed()?.createSubscription(events)
let eventListener = AnonymousClass { anonymCl in
anonymCl.callback = { events in
events.forEach { mEvent in
Expand Down
117 changes: 117 additions & 0 deletions DXFeedFrameworkTests/DXOtcMarketOrderTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//
//
// Copyright (C) 2024 Devexperts LLC. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//

import XCTest
@testable import DXFeedFramework

final class DXOtcMarketOrderTest: XCTestCase {

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}

func testExample() throws {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
// Any test you write for XCTest can be annotated as throws and async.
// Mark your test throws to produce an unexpected failure when your test encounters an uncaught error.
// Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards.
}

func testPerformanceExample() throws {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}

func testOtcPublishing() throws {
throw XCTSkip("skip. otc not ready")

let SYMBOL1 = "AAPL_TEST1"
let SYMBOL2 = "AAPL_TEST2"
let order1 = OtcMarketsOrder(SYMBOL1)
order1.orderSide = .buy
order1.marketMaker = "MM1"
order1.scope = .order
order1.price = 10.0
order1.size = 1
order1.index = 1
order1.quoteAccessPayment = -30
order1.isOpen = true
order1.isUnsolicited = true
order1.otcMarketsPriceType = .actual
order1.isSaturated = true
order1.isAutoExecution = true
order1.isNmsConditional = true

let order2 = OtcMarketsOrder(SYMBOL2)
order2.orderSide = .buy
order2.marketMaker = "MM2"
order2.scope = .order
order2.price = 10.0
order2.size = 1
order2.index = 1
order2.quoteAccessPayment = -30
order2.isOpen = true
order2.isUnsolicited = true
order2.otcMarketsPriceType = .wanted
order2.isSaturated = true
order2.isAutoExecution = false
order2.isNmsConditional = false

let endpoint = try DXEndpoint.create(.localHub)
let feed = endpoint.getFeed()
let sub = try feed!.createSubscription([OtcMarketsOrder.self])
let receivedEvent1Exp = expectation(description: "Received events \(SYMBOL1)")
receivedEvent1Exp.expectedFulfillmentCount = 2

let testEventListenr = AnonymousClass { anonymCl in
anonymCl.callback = { events in
if events.count > 0 {
let event = events.first!.otcMarketsOrder
if event.eventSymbol == SYMBOL1 {
XCTAssertEqual(event.marketMaker, "MM1")
XCTAssertEqual(event.price, 10)
XCTAssertEqual(event.quoteAccessPayment, -30)
XCTAssertEqual(event.isOpen, true)
XCTAssertEqual(event.isUnsolicited, true)
XCTAssertEqual(event.otcMarketsPriceType, .actual)
XCTAssertEqual(event.isSaturated, true)
XCTAssertEqual(event.isAutoExecution, true)
XCTAssertEqual(event.isNmsConditional, true)
XCTAssertEqual(event.otcMarketsFlags, order1.otcMarketsFlags)
receivedEvent1Exp.fulfill()
} else if event.eventSymbol == SYMBOL2 {
XCTAssertEqual(event.marketMaker, "MM2")
XCTAssertEqual(event.price, 10)
XCTAssertEqual(event.quoteAccessPayment, -30)
XCTAssertEqual(event.isOpen, true)
XCTAssertEqual(event.isUnsolicited, true)
XCTAssertEqual(event.otcMarketsPriceType, .actual)
XCTAssertEqual(event.isSaturated, true)
XCTAssertEqual(event.isAutoExecution, false)
XCTAssertEqual(event.isNmsConditional, false)
XCTAssertEqual(event.otcMarketsFlags, order2.otcMarketsFlags)
receivedEvent1Exp.fulfill()
}
}
}
return anonymCl
}
try sub.add(listener: testEventListenr)
let publisher = endpoint.getPublisher()
try sub.addSymbols([SYMBOL1, SYMBOL2])
try publisher?.publish(events: [order1, order2])
wait(for: [receivedEvent1Exp], timeout: 1)

}
}
1 change: 0 additions & 1 deletion DXFeedFrameworkTests/DXPromiseTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ final class DXPromiseTest: XCTestCase {
})
wait(for: [receivedEventExp], timeout: 1)
promises.removeAll()
wait(seconds: 1)
} catch {
XCTAssert(false, "testAllOffPromises \(error)")
}
Expand Down
83 changes: 0 additions & 83 deletions DXFeedFrameworkTests/PublisherTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,87 +75,4 @@ final class PublisherTest: XCTestCase {
print("\(error)")
}
}

func testOtcPublishing() throws {
throw XCTSkip("skip. otc not ready")

let SYMBOL1 = "AAPL_TEST1"
let SYMBOL2 = "AAPL_TEST2"
let order1 = OtcMarketsOrder(SYMBOL1)
order1.orderSide = .buy
order1.marketMaker = "MM1"
order1.scope = .order
order1.price = 10.0
order1.size = 1
order1.index = 1
order1.quoteAccessPayment = -30
order1.isOpen = true
order1.isUnsolicited = true
order1.otcMarketsPriceType = .actual
order1.isSaturated = true
order1.isAutoExecution = true
order1.isNmsConditional = true

let order2 = OtcMarketsOrder(SYMBOL2)
order2.orderSide = .buy
order2.marketMaker = "MM1"
order2.scope = .order
order2.price = 10.0
order2.size = 1
order2.index = 1
order2.quoteAccessPayment = -30
order2.isOpen = true
order2.isUnsolicited = true
order2.otcMarketsPriceType = .wanted
order2.isSaturated = true
order2.isAutoExecution = false
order2.isNmsConditional = false

let endpoint = try DXEndpoint.create(.localHub)
let feed = endpoint.getFeed()
let sub = try feed!.createSubscription([OtcMarketsOrder.self])
let receivedEvent1Exp = expectation(description: "Received events \(SYMBOL1)")
let receivedEvent2Exp = expectation(description: "Received events \(SYMBOL2)")

let testEventListenr = AnonymousClass { anonymCl in
anonymCl.callback = { events in
if events.count > 0 {
let event = events.first!.otcMarketsOrder

if event.eventSymbol == SYMBOL1 {
XCTAssert(event.price == order1.price)
XCTAssert(event.quoteAccessPayment == order1.quoteAccessPayment)
XCTAssert(event.isOpen == order1.isOpen)
XCTAssert(event.isUnsolicited == order1.isUnsolicited)
XCTAssert(event.otcMarketsPriceType == order1.otcMarketsPriceType)
XCTAssert(event.isSaturated == order1.isSaturated)
XCTAssert(event.isAutoExecution == order1.isAutoExecution)
XCTAssert(event.isNmsConditional == order1.isNmsConditional)
XCTAssert(event.otcMarketsFlags == order1.otcMarketsFlags)
receivedEvent1Exp.fulfill()
} else if event.eventSymbol == SYMBOL2 {
XCTAssert(event.price == order2.price)

XCTAssert(event.quoteAccessPayment == order2.quoteAccessPayment)
XCTAssert(event.isOpen == order2.isOpen)
XCTAssert(event.isUnsolicited == order2.isUnsolicited)
XCTAssert(event.otcMarketsPriceType == order2.otcMarketsPriceType)
XCTAssert(event.isSaturated == order2.isSaturated)
XCTAssert(event.isAutoExecution == order2.isAutoExecution)
XCTAssert(event.isNmsConditional == order2.isNmsConditional)
XCTAssert(event.otcMarketsFlags == order2.otcMarketsFlags)
receivedEvent2Exp.fulfill()
}
print(event)
}
}
return anonymCl
}
try sub.add(listener: testEventListenr)
let publisher = endpoint.getPublisher()
try sub.addSymbols([SYMBOL1, SYMBOL2])
try publisher?.publish(events: [order1, order2])
wait(for: [receivedEvent1Exp, receivedEvent2Exp], timeout: 1)

}
}

0 comments on commit 53106ac

Please sign in to comment.