From 36ddbe3a78c06fdd96d573807d92cae4d5a2680d Mon Sep 17 00:00:00 2001 From: AKosylo Date: Wed, 6 Sep 2023 17:27:05 +0200 Subject: [PATCH] rename and refactoring --- DXFeedFramework/Api/DXEndpoint.swift | 83 ++++++++++--------- DXFeedFramework/Api/DXFeedSubcription.swift | 4 +- .../Live/DXInstrumentProfileCollector.swift | 4 +- .../Live/DXInstrumentProfileConnection.swift | 4 +- DXFeedFrameworkTests/CandleTests.swift | 6 +- .../EndpointPublisherTest.swift | 2 +- DXFeedFrameworkTests/EndpointTest.swift | 4 +- DXFeedFrameworkTests/FeedTest.swift | 4 +- DXFeedFrameworkTests/IPFTests.swift | 6 +- DXFeedFrameworkTests/SystemPropertyTest.swift | 12 --- 10 files changed, 59 insertions(+), 70 deletions(-) diff --git a/DXFeedFramework/Api/DXEndpoint.swift b/DXFeedFramework/Api/DXEndpoint.swift index 5a54bdc58..2e88379c9 100644 --- a/DXFeedFramework/Api/DXEndpoint.swift +++ b/DXFeedFramework/Api/DXEndpoint.swift @@ -11,7 +11,7 @@ public typealias Role = DXEndpoint.Role public class DXEndpoint { - public enum Role: UInt32 { + public enum Role: UInt32 { case feed = 0 case onDemandFeed case streamFeed @@ -66,17 +66,17 @@ public class DXEndpoint { try native.addListener(self) } - public func add(_ observer: O) + public func add(observer: O) where O: DXEndpointObserver, O: Hashable { - observersSet.insert(observer) - } + observersSet.insert(observer) + } - public func remove(_ observer: O) + public func remove(observer: O) where O: DXEndpointObserver, O: Hashable { - observersSet.remove(observer) - } + observersSet.remove(observer) + } public static func builder() -> Builder { Builder() @@ -144,56 +144,57 @@ public class DXEndpoint { } } -// only for testing + // only for testing func callGC() throws { try endpointNative.callGC() } -} -public class Builder { - var role = Role.feed - var props = [String: String]() + public class Builder { + var role = Role.feed + var props = [String: String]() - var instancesNumerator = Int64(0) + var instancesNumerator = Int64(0) - private lazy var nativeBuilder: NativeBuilder? = { - try? NativeBuilder() - }() + private lazy var nativeBuilder: NativeBuilder? = { + try? NativeBuilder() + }() - deinit { - } + deinit { + } - fileprivate init() { + public init() { - } + } - public func withRole(_ role: Role) throws -> Self { - self.role = role - _ = try nativeBuilder?.withRole(role) - return self - } + public func withRole(_ role: Role) throws -> Self { + self.role = role + _ = try nativeBuilder?.withRole(role) + return self + } - public func isSupported(property: String) throws -> Bool { - return try nativeBuilder?.isSuppored(property: property) ?? false - } + public func isSupported(_ property: String) throws -> Bool { + return try nativeBuilder?.isSuppored(property: property) ?? false + } - public func withProperty(_ key: String, _ value: String) throws -> Self { - props[key] = value - try nativeBuilder?.withProperty(key, value) - return self - } + public func withProperty(_ key: String, _ value: String) throws -> Self { + props[key] = value + try nativeBuilder?.withProperty(key, value) + return self + } - public func build() throws -> DXEndpoint { - return try DXEndpoint(native: try nativeBuilder!.build(), role: role, name: getOrCreateEndpointName()) - } + public func build() throws -> DXEndpoint { + return try DXEndpoint(native: try nativeBuilder!.build(), role: role, name: getOrCreateEndpointName()) + } - private func getOrCreateEndpointName() -> String { - if let name = props[DXEndpoint.Property.name.rawValue] { - return name + private func getOrCreateEndpointName() -> String { + if let name = props[DXEndpoint.Property.name.rawValue] { + return name + } + let value = OSAtomicIncrement64(&instancesNumerator) + return "qdnet_\(value == 0 ? "" : "-\(value)")" } - let value = OSAtomicIncrement64(&instancesNumerator) - return "qdnet_\(value == 0 ? "" : "-\(value)")" } + } extension DXEndpoint: EndpointListener { diff --git a/DXFeedFramework/Api/DXFeedSubcription.swift b/DXFeedFramework/Api/DXFeedSubcription.swift index 6e726115d..c488a9b80 100644 --- a/DXFeedFramework/Api/DXFeedSubcription.swift +++ b/DXFeedFramework/Api/DXFeedSubcription.swift @@ -21,7 +21,7 @@ public class DXFeedSubcription { self.events = Set(events) } - public func add(_ observer: O) throws + public func add(observer: O) throws where O: DXEventListener, O: Hashable { try listeners.reader { [weak self] in @@ -32,7 +32,7 @@ public class DXFeedSubcription { listeners.insert(observer) } - public func remove(_ observer: O) + public func remove(observer: O) where O: DXEventListener, O: Hashable { listeners.remove(observer) diff --git a/DXFeedFramework/Ipf/Live/DXInstrumentProfileCollector.swift b/DXFeedFramework/Ipf/Live/DXInstrumentProfileCollector.swift index 02e05128e..0897e54cb 100644 --- a/DXFeedFramework/Ipf/Live/DXInstrumentProfileCollector.swift +++ b/DXFeedFramework/Ipf/Live/DXInstrumentProfileCollector.swift @@ -15,7 +15,7 @@ public class DXInstrumentProfileCollector { self.native = try NativeInstrumentProfileCollector() } - public func add(_ observer: O) throws + public func add(observer: O) throws where O: DXInstrumentProfileUpdateListener, O: Hashable { try listeners.reader { [weak self] in @@ -26,7 +26,7 @@ public class DXInstrumentProfileCollector { listeners.insert(observer) } - public func remove(_ observer: O) + public func remove(observer: O) where O: DXInstrumentProfileUpdateListener, O: Hashable { listeners.remove(observer) diff --git a/DXFeedFramework/Ipf/Live/DXInstrumentProfileConnection.swift b/DXFeedFramework/Ipf/Live/DXInstrumentProfileConnection.swift index 97c46c859..7a77ef051 100644 --- a/DXFeedFramework/Ipf/Live/DXInstrumentProfileConnection.swift +++ b/DXFeedFramework/Ipf/Live/DXInstrumentProfileConnection.swift @@ -46,13 +46,13 @@ public class DXInstrumentProfileConnection { try native.close() } - public func add(_ observer: O) + public func add(observer: O) where O: DXInstrumentProfileConnectionObserver, O: Hashable { observersSet.insert(observer) } - public func remove(_ observer: O) + public func remove(observer: O) where O: DXInstrumentProfileConnectionObserver, O: Hashable { observersSet.remove(observer) diff --git a/DXFeedFrameworkTests/CandleTests.swift b/DXFeedFrameworkTests/CandleTests.swift index efca8dfc3..d5fafd926 100644 --- a/DXFeedFrameworkTests/CandleTests.swift +++ b/DXFeedFrameworkTests/CandleTests.swift @@ -55,7 +55,7 @@ final class CandleTests: XCTestCase { let subscription = try endpoint?.getFeed()?.createSubscription(code) let receivedEventExp = expectation(description: "Received events \(code)") receivedEventExp.assertForOverFulfill = false - try subscription?.add(AnonymousClass { anonymCl in + try subscription?.add(observer: AnonymousClass { anonymCl in anonymCl.callback = { events in if events.count > 0 { events.forEach { event in @@ -166,7 +166,7 @@ final class CandleTests: XCTestCase { let subscription = try endpoint?.getFeed()?.createSubscription(code) let beginEventsExp = expectation(description: "Begin events \(code)") let endEventsExp = expectation(description: "End events \(code)") - try subscription?.add(AnonymousClass { anonymCl in + try subscription?.add(observer: AnonymousClass { anonymCl in anonymCl.callback = { events in if events.count > 0 { events.forEach { event in @@ -216,7 +216,7 @@ final class CandleTests: XCTestCase { testDelegate.wasSnapshotExpect = snapshotExpect testDelegate.wasUpdateExpect = updateExpect snapshotProcessor.add(testDelegate) - try subscription?.add(snapshotProcessor) + try subscription?.add(observer: snapshotProcessor) try subscription?.addSymbols(symbol) wait(for: [snapshotExpect, updateExpect], timeout: 10) try? endpoint?.disconnect() diff --git a/DXFeedFrameworkTests/EndpointPublisherTest.swift b/DXFeedFrameworkTests/EndpointPublisherTest.swift index a401cd520..120d1ad7b 100644 --- a/DXFeedFrameworkTests/EndpointPublisherTest.swift +++ b/DXFeedFrameworkTests/EndpointPublisherTest.swift @@ -14,7 +14,7 @@ final class EndpointPublisherTest: XCTestCase { let expectations = [DXEndpointState.connected: expectation(description: "Connected"), DXEndpointState.connecting: expectation(description: "Connecting")] let listener = TestListener(expectations: expectations) - endpoint?.add(listener) + endpoint?.add(observer: listener) try endpoint?.connect(":4700") let exps = Array(expectations.filter({ element in element.key != .notConnected diff --git a/DXFeedFrameworkTests/EndpointTest.swift b/DXFeedFrameworkTests/EndpointTest.swift index 0f1aca761..e2bff8286 100644 --- a/DXFeedFrameworkTests/EndpointTest.swift +++ b/DXFeedFrameworkTests/EndpointTest.swift @@ -51,7 +51,7 @@ final class EndpointTest: XCTestCase { DXEndpointState.connecting: expectation(description: "Connecting"), DXEndpointState.notConnected: expectation(description: "NotConnected")] let listener = TestListener(expectations: expectations) - endpoint?.add(listener) + endpoint?.add(observer: listener) try endpoint?.connect(endpointAddress) let exps = Array(expectations.filter({ element in element.key != .notConnected @@ -84,7 +84,7 @@ final class EndpointTest: XCTestCase { func testSupportProperty() throws { let builder = DXEndpoint.builder() func isSupportedProperty(_ prop: String, _ expected: Bool) { - XCTAssert(try builder.isSupported(property: prop) == expected, "Graal doesn't support property \(prop)") + XCTAssert(try builder.isSupported(prop) == expected, "Graal doesn't support property \(prop)") } DXEndpoint.Property.allCases.forEach { prop in isSupportedProperty(prop.rawValue, true) diff --git a/DXFeedFrameworkTests/FeedTest.swift b/DXFeedFrameworkTests/FeedTest.swift index 64d27be40..280b6c30b 100644 --- a/DXFeedFrameworkTests/FeedTest.swift +++ b/DXFeedFrameworkTests/FeedTest.swift @@ -65,7 +65,7 @@ final class FeedTest: XCTestCase { let receivedEventExp = expectation(description: "Received events \(code)") receivedEventExp.assertForOverFulfill = false - try subscription?.add(AnonymousClass { anonymCl in + try subscription?.add(observer: AnonymousClass { anonymCl in anonymCl.callback = { events in events.forEach { event in differentSymbols.insert(event.quote.eventSymbol) @@ -151,7 +151,7 @@ final class FeedTest: XCTestCase { let subscription = try endpoint.getFeed()?.createSubscription(code) let receivedEventExp = expectation(description: "Received events \(code)") receivedEventExp.assertForOverFulfill = false - try subscription?.add(AnonymousClass { anonymCl in + try subscription?.add(observer: AnonymousClass { anonymCl in anonymCl.callback = { events in if events.count > 0 { let event = events.first diff --git a/DXFeedFrameworkTests/IPFTests.swift b/DXFeedFrameworkTests/IPFTests.swift index 8afea6941..d67f4c14a 100644 --- a/DXFeedFrameworkTests/IPFTests.swift +++ b/DXFeedFrameworkTests/IPFTests.swift @@ -177,7 +177,7 @@ STOCK,EREGL:TR,EREĞLİ DEMİR VE ÇELİK FABRİKALARI1 T.A.Ş.,TR,XIST,XIST,TRY let newProfile = InstrumentProfile() newProfile.symbol = "TEST_123" - try collector.add(AnonymousProfileListener { anonymCl in + try collector.add(observer: AnonymousProfileListener { anonymCl in anonymCl.callback = { profiles in if profiles.count == 1 { let profile = profiles.first @@ -212,7 +212,7 @@ STOCK,EREGL:TR,EREĞLİ DEMİR VE ÇELİK FABRİKALARI1 T.A.Ş.,TR,XIST,XIST,TRY let expectationCollector = expectation(description: "Collector") expectationCollector.assertForOverFulfill = false let collector = try DXInstrumentProfileCollector() - try collector.add(AnonymousProfileListener { anonymCl in + try collector.add(observer: AnonymousProfileListener { anonymCl in anonymCl.callback = { profiles in if profiles.count > 0 { expectationCollector.fulfill() @@ -223,7 +223,7 @@ STOCK,EREGL:TR,EREĞLİ DEMİR VE ÇELİK FABRİKALARI1 T.A.Ş.,TR,XIST,XIST,TRY let expectationConnection = expectation(description: "Connection") expectationConnection.expectedFulfillmentCount = 3 // connecting, connected, completed let connection = try DXInstrumentProfileConnection(address, collector) - connection.add(AnonymousConnectionListener { anonymCl in + connection.add(observer: AnonymousConnectionListener { anonymCl in anonymCl.callback = { _, new in switch new { case .notConnected: diff --git a/DXFeedFrameworkTests/SystemPropertyTest.swift b/DXFeedFrameworkTests/SystemPropertyTest.swift index 47a90f10c..417e6ae60 100644 --- a/DXFeedFrameworkTests/SystemPropertyTest.swift +++ b/DXFeedFrameworkTests/SystemPropertyTest.swift @@ -59,16 +59,4 @@ final class SystemPropertyTest: XCTestCase { } XCTAssert(value == SystemProperty.getProperty(key), "Couldn't read value") } - - func testException() { - do { - try SystemProperty.test() - } catch { - print("Just test exception: \(error)") - } - } - - func testSupportedProperties() { - - } }