diff --git a/Test/Test Utilities/Test.swift b/Test/Test Utilities/Test.swift index 1832e53ff..bda7a6b98 100644 --- a/Test/Test Utilities/Test.swift +++ b/Test/Test Utilities/Test.swift @@ -1,13 +1,19 @@ /** Represents an execution of a test case method. */ -struct Test { +struct Test: CustomStringConvertible { var id = UUID() - private var function: StaticString + var fileID: String + var function: String - init(function: StaticString = #function) { + init(fileID: String = #fileID, function: String = #function) { + self.fileID = fileID self.function = function - NSLog("Created test \(id) for function \(function)") + NSLog("Created test \(id) for function \(function) in file \(fileID)") + } + + var description: String { + return "Test(id: \(id), fileID: \(fileID), function: \(function))" } func uniqueChannelName(prefix: String = "", diff --git a/Test/Test Utilities/TestProxyTransportFactory.swift b/Test/Test Utilities/TestProxyTransportFactory.swift index 65d0a5ef5..43d9c5e07 100644 --- a/Test/Test Utilities/TestProxyTransportFactory.swift +++ b/Test/Test Utilities/TestProxyTransportFactory.swift @@ -1,6 +1,12 @@ import Ably.Private class TestProxyTransportFactory: RealtimeTransportFactory { + let internalQueue: DispatchQueue + + init(internalQueue: DispatchQueue) { + self.internalQueue = internalQueue + } + // This value will be used by all TestProxyTransportFactory instances created by this factory (including those created before this property is updated). var fakeNetworkResponse: FakeNetworkResponse? @@ -17,7 +23,8 @@ class TestProxyTransportFactory: RealtimeTransportFactory { resumeKey: resumeKey, connectionSerial: connectionSerial, logger: logger, - webSocketFactory: webSocketFactory + webSocketFactory: webSocketFactory, + internalQueue: internalQueue ) } } diff --git a/Test/Test Utilities/TestUtilities.swift b/Test/Test Utilities/TestUtilities.swift index 985a3ed5f..782ac350e 100644 --- a/Test/Test Utilities/TestUtilities.swift +++ b/Test/Test Utilities/TestUtilities.swift @@ -23,6 +23,34 @@ class AblyTestsConfiguration: NSObject, XCTestObservation { performedPreFirstTestCaseSetup = true } } + + func testSuiteDidFinish(_ testSuite: XCTestSuite) { + let activeQueueIdentities = AblyTests.QueueIdentity.active + + if activeQueueIdentities.isEmpty { + print("No active queue identities.") + } else { + print("\(activeQueueIdentities.count) active queue \(activeQueueIdentities.count == 1 ? "identity" : "identities"):") + + let activeQueueIdentitiesGroupedByFileID = Dictionary(grouping: activeQueueIdentities, by: \.test.fileID) + let sortedFileIDs = activeQueueIdentitiesGroupedByFileID.keys.sorted() + for fileID in sortedFileIDs { + print("\t\(fileID):") + let activeQueueIdentitiesForFileID = activeQueueIdentitiesGroupedByFileID[fileID]! + + let activeQueueIdentitiesForFileIDGroupedByFunction = Dictionary(grouping: activeQueueIdentitiesForFileID, by: \.test.function) + let sortedFunctions = activeQueueIdentitiesForFileIDGroupedByFunction.keys.sorted() + for function in sortedFunctions { + print("\t\t\(function):") + let activeQueueIdentitiesForFunction = activeQueueIdentitiesForFileIDGroupedByFunction[function]! + + for queueIdentity in activeQueueIdentitiesForFunction { + print("\t\t\t\(queueIdentity.label)") + } + } + } + } + } private func preFirstTestCaseSetup() { // This is code that, when we were using the Quick testing @@ -82,21 +110,60 @@ class AblyTests { static var testApplication: [String: Any]? - struct QueueIdentity { + class QueueIdentity: CustomStringConvertible, Hashable { let label: String + let test: Test + + private static let semaphore = DispatchSemaphore(value: 1) + private static var _active: Set = [] + + static var active: Set { + semaphore.wait() + let active = _active + semaphore.signal() + return active + } + + init(label: String, test: Test) { + self.label = label + self.test = test + Self.semaphore.wait() + Self._active.insert(self) + Self.semaphore.signal() + NSLog("Created QueueIdentity \(label)") + } + + deinit { + Self.semaphore.wait() + Self._active.remove(self) + Self.semaphore.signal() + NSLog("deinit QueueIdentity \(label)") + } + + var description: String { + return "QueueIdentity(label: \(label), test: \(test))" + } + + static func == (lhs: AblyTests.QueueIdentity, rhs: AblyTests.QueueIdentity) -> Bool { + return ObjectIdentifier(lhs) == ObjectIdentifier(rhs) + } + + func hash(into hasher: inout Hasher) { + hasher.combine(ObjectIdentifier(self)) + } } static let queueIdentityKey = DispatchSpecificKey() - static var queue: DispatchQueue = { - let queue = DispatchQueue(label: "io.ably.tests", qos: .userInitiated) - queue.setSpecific(key: queueIdentityKey, value: QueueIdentity(label: queue.label)) + static func createInternalQueue(for test: Test) -> DispatchQueue { + let queue = DispatchQueue(label: "io.ably.tests.\(test.id).\(UUID().uuidString)", qos: .userInitiated) + queue.setSpecific(key: queueIdentityKey, value: QueueIdentity(label: queue.label, test: test)) return queue - }() + } static func createUserQueue(for test: Test) -> DispatchQueue { let queue = DispatchQueue(label: "io.ably.tests.callbacks.\(test.id).\(UUID().uuidString)", qos: .userInitiated) - queue.setSpecific(key: queueIdentityKey, value: QueueIdentity(label: queue.label)) + queue.setSpecific(key: queueIdentityKey, value: QueueIdentity(label: queue.label, test: test)) return queue } @@ -154,7 +221,7 @@ class AblyTests { options.token = try getTestToken(for: test) } options.dispatchQueue = DispatchQueue.main - options.internalDispatchQueue = queue + options.internalDispatchQueue = createInternalQueue(for: test) return options } @@ -188,7 +255,7 @@ class AblyTests { let autoConnect = modifiedOptions.autoConnect modifiedOptions.autoConnect = false - let transportFactory = TestProxyTransportFactory() + let transportFactory = TestProxyTransportFactory(internalQueue: modifiedOptions.internalDispatchQueue) modifiedOptions.testOptions.transportFactory = transportFactory let realtime = ARTRealtime(options: modifiedOptions) realtime.internal.setReachabilityClass(TestReachability.self) @@ -794,10 +861,6 @@ class MockHTTP: ARTHttp { private var rule: Rule? private var count: Int = 0 - init(logger: InternalLog) { - super.init(queue: AblyTests.queue, logger: logger) - } - func setNetworkState(network: FakeNetworkResponse, resetAfter numberOfRequests: Int) { queue.async { self.networkState = network @@ -984,9 +1047,9 @@ class TestProxyHTTPExecutor: NSObject, ARTHTTPExecutor { private var callbackAfterRequest: ((URLRequest) -> Void)? private var callbackProcessingDataResponse: ((Data?) -> Data)? - init(logger: InternalLog) { + init(queue: DispatchQueue, logger: InternalLog) { self.logger = logger - self.http = ARTHttp(queue: AblyTests.queue, logger: logger) + self.http = ARTHttp(queue: queue, logger: logger) } init(http: ARTHttp, logger: InternalLog) { @@ -1101,8 +1164,11 @@ class TestProxyTransport: ARTWebSocketTransport { return _factory } - init(factory: TestProxyTransportFactory, rest: ARTRestInternal, options: ARTClientOptions, resumeKey: String?, connectionSerial: NSNumber?, logger: InternalLog, webSocketFactory: WebSocketFactory) { + private var internalQueue: DispatchQueue + + init(factory: TestProxyTransportFactory, rest: ARTRestInternal, options: ARTClientOptions, resumeKey: String?, connectionSerial: NSNumber?, logger: InternalLog, webSocketFactory: WebSocketFactory, internalQueue: DispatchQueue) { self._factory = factory + self.internalQueue = internalQueue super.init(rest: rest, options: options, resumeKey: resumeKey, connectionSerial: connectionSerial, logger: logger, webSocketFactory: webSocketFactory) } @@ -1145,7 +1211,7 @@ class TestProxyTransport: ARTWebSocketTransport { var actionsIgnored = [ARTProtocolMessageAction]() var queue: DispatchQueue { - return websocket?.delegateDispatchQueue ?? AblyTests.queue + return websocket?.delegateDispatchQueue ?? internalQueue } private var callbackBeforeProcessingIncomingMessage: ((ARTProtocolMessage) -> Void)? @@ -1553,23 +1619,23 @@ extension ARTRealtime { } } - func simulateNoInternetConnection(transportFactory: TestProxyTransportFactory) { + func simulateNoInternetConnection(transportFactory: TestProxyTransportFactory, internalQueue: DispatchQueue) { guard let reachability = self.internal.reachability as? TestReachability else { fatalError("Expected test reachability") } - AblyTests.queue.async { + internalQueue.async { transportFactory.fakeNetworkResponse = .noInternet reachability.simulate(false) } } - func simulateRestoreInternetConnection(after seconds: TimeInterval? = nil, transportFactory: TestProxyTransportFactory) { + func simulateRestoreInternetConnection(after seconds: TimeInterval? = nil, transportFactory: TestProxyTransportFactory, internalQueue: DispatchQueue) { guard let reachability = self.internal.reachability as? TestReachability else { fatalError("Expected test reachability") } - AblyTests.queue.asyncAfter(deadline: .now() + (seconds ?? 0)) { + internalQueue.asyncAfter(deadline: .now() + (seconds ?? 0)) { transportFactory.fakeNetworkResponse = nil reachability.simulate(true) } diff --git a/Test/Tests/AuthTests.swift b/Test/Tests/AuthTests.swift index 8fe2dc92b..8c876d9af 100644 --- a/Test/Tests/AuthTests.swift +++ b/Test/Tests/AuthTests.swift @@ -86,7 +86,7 @@ class AuthTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) let client = ARTRest(options: options) - let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) client.internal.httpExecutor = testHTTPExecutor waitUntil(timeout: testTimeout) { done in @@ -123,7 +123,7 @@ class AuthTests: XCTestCase { let options = try AblyTests.clientOptions(for: test, requestToken: true) options.tls = false let clientHTTP = ARTRest(options: options) - let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) clientHTTP.internal.httpExecutor = testHTTPExecutor waitUntil(timeout: testTimeout) { done in @@ -142,7 +142,7 @@ class AuthTests: XCTestCase { let options = try AblyTests.clientOptions(for: test, requestToken: true) options.tls = true let clientHTTPS = ARTRest(options: options) - let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) clientHTTPS.internal.httpExecutor = testHTTPExecutor waitUntil(timeout: testTimeout) { done in @@ -164,7 +164,7 @@ class AuthTests: XCTestCase { options.token = try getTestToken(for: test) let client = ARTRest(options: options) - let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) client.internal.httpExecutor = testHTTPExecutor waitUntil(timeout: testTimeout) { done in @@ -191,7 +191,7 @@ class AuthTests: XCTestCase { let options = try AblyTests.clientOptions(for: test) options.token = try getTestToken(for: test) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } @@ -241,7 +241,7 @@ class AuthTests: XCTestCase { XCTAssertNil(rest.internal.options.key) XCTAssertNil(rest.internal.options.authCallback) XCTAssertNil(rest.internal.options.authUrl) - let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) rest.internal.httpExecutor = testHTTPExecutor let channel = rest.channels.get(test.uniqueChannelName()) @@ -264,7 +264,7 @@ class AuthTests: XCTestCase { let options = try AblyTests.clientOptions(for: test) options.tokenDetails = try getTestTokenDetails(for: test, ttl: 0.1) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) // Token will expire, expecting 40142 waitUntil(timeout: testTimeout) { done in @@ -307,7 +307,7 @@ class AuthTests: XCTestCase { } let rest = ARTRest(options: options) - let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) rest.internal.httpExecutor = testHTTPExecutor let channel = rest.channels.get(test.uniqueChannelName()) @@ -335,7 +335,7 @@ class AuthTests: XCTestCase { options.useTokenAuth = true let rest = ARTRest(options: options) - let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) rest.internal.httpExecutor = testHTTPExecutor let channel = rest.channels.get(test.uniqueChannelName()) @@ -435,7 +435,7 @@ class AuthTests: XCTestCase { options.key = nil let rest = ARTRest(options: options) - let proxyHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let proxyHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) // Sync server time offset let authOptions = ARTAuthOptions(key: testKey) @@ -486,7 +486,7 @@ class AuthTests: XCTestCase { options.key = nil let rest = ARTRest(options: options) - let proxyHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let proxyHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) rest.internal.httpExecutor = proxyHTTPExecutor // No server time offset @@ -884,7 +884,7 @@ class AuthTests: XCTestCase { options.clientId = expectedClientId let client = ARTRest(options: options) - let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) client.internal.httpExecutor = testHTTPExecutor waitUntil(timeout: testTimeout) { done in @@ -917,7 +917,7 @@ class AuthTests: XCTestCase { let options = try AblyTests.commonAppSetup(for: test) options.clientId = expectedClientId options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } @@ -1060,7 +1060,7 @@ class AuthTests: XCTestCase { let options = try AblyTests.commonAppSetup(for: test) let rest = ARTRest(options: options) - let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) rest.internal.httpExecutor = testHTTPExecutor waitUntil(timeout: testTimeout) { done in @@ -1093,7 +1093,7 @@ class AuthTests: XCTestCase { let options = try AblyTests.commonAppSetup(for: test) let rest = ARTRest(options: options) - let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) rest.internal.httpExecutor = testHTTPExecutor waitUntil(timeout: testTimeout) { done in @@ -1125,7 +1125,7 @@ class AuthTests: XCTestCase { let options = try AblyTests.commonAppSetup(for: test) options.clientId = "mary" let rest = ARTRest(options: options) - let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) rest.internal.httpExecutor = testHTTPExecutor let channel = rest.channels.get(test.uniqueChannelName()) waitUntil(timeout: testTimeout) { done in @@ -1151,7 +1151,7 @@ class AuthTests: XCTestCase { options.clientId = "client_string" let client = ARTRest(options: options) - let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) client.internal.httpExecutor = testHTTPExecutor waitUntil(timeout: testTimeout) { done in @@ -1289,7 +1289,7 @@ class AuthTests: XCTestCase { options.useTokenAuth = true let client = ARTRest(options: options) - let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) client.internal.httpExecutor = testHTTPExecutor // TokenDetails @@ -1482,7 +1482,7 @@ class AuthTests: XCTestCase { options.authParams!.append(URLQueryItem(name: "body", value: testToken)) let rest = ARTRest(options: options) - let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) rest.internal.httpExecutor = testHTTPExecutor waitUntil(timeout: testTimeout) { done in @@ -1510,7 +1510,7 @@ class AuthTests: XCTestCase { options.authParams?.append(URLQueryItem(name: "body", value: jsonTokenDetails.toUTF8String)) let rest = ARTRest(options: options) - let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHTTPExecutor = TestProxyHTTPExecutor(queue: AblyTests.createInternalQueue(for: test), logger: .init(clientOptions: options)) rest.internal.httpExecutor = testHTTPExecutor waitUntil(timeout: testTimeout) { done in @@ -1563,7 +1563,7 @@ class AuthTests: XCTestCase { options.authParams?.append(URLQueryItem(name: "body", value: jsonTokenRequest.toUTF8String)) rest = ARTRest(options: options) - let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) rest.internal.httpExecutor = testHTTPExecutor waitUntil(timeout: testTimeout) { done in @@ -1673,6 +1673,7 @@ class AuthTests: XCTestCase { // RSA8c2 func test__067__requestToken__authUrl__TokenParams_should_take_precedence_over_any_configured_authParams_when_a_name_conflict_occurs() { + let test = Test() let options = ARTClientOptions() options.clientId = "john" options.authUrl = URL(string: "http://auth.ably.io") @@ -1688,7 +1689,7 @@ class AuthTests: XCTestCase { tokenParams.clientId = "tester" let client = ARTRest(options: options) - let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHTTPExecutor = TestProxyHTTPExecutor(queue: AblyTests.createInternalQueue(for: test), logger: .init(clientOptions: options)) client.internal.httpExecutor = testHTTPExecutor waitUntil(timeout: testTimeout) { done in @@ -1870,7 +1871,7 @@ class AuthTests: XCTestCase { let options = try AblyTests.commonAppSetup(for: test) options.token = try getTestToken(for: test, clientId: nil) let rest = ARTRest(options: options) - let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) rest.internal.httpExecutor = testHTTPExecutor let channel = rest.channels.get(test.uniqueChannelName()) @@ -1933,7 +1934,7 @@ class AuthTests: XCTestCase { } } - let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) rest.internal.httpExecutor = testHTTPExecutor let channel = rest.channels.get(test.uniqueChannelName()) @@ -2695,7 +2696,7 @@ class AuthTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) let rest = ARTRest(options: options) - let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) rest.internal.httpExecutor = testHTTPExecutor let auth = rest.auth @@ -4187,7 +4188,7 @@ class AuthTests: XCTestCase { options.authParams?.append(URLQueryItem(name: "keySecret", value: keys["keySecret"])) options.authParams?.append(URLQueryItem(name: "expiresIn", value: String(UInt(tokenDuration)))) options.autoConnect = false // Prevent auto connection so we can set the transport proxy - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } @@ -4480,7 +4481,7 @@ class AuthTests: XCTestCase { #if TARGET_OS_IOS XCTAssertNil(rest.device.clientId) #endif - let testHttpExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHttpExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) rest.internal.httpExecutor = testHttpExecutor let channel = rest.channels.get(channelName) diff --git a/Test/Tests/ObjectLifetimesTests.swift b/Test/Tests/ObjectLifetimesTests.swift index 8a64e57a8..f779ebb6b 100644 --- a/Test/Tests/ObjectLifetimesTests.swift +++ b/Test/Tests/ObjectLifetimesTests.swift @@ -123,7 +123,7 @@ class ObjectLifetimesTests: XCTestCase { } waitUntil(timeout: testTimeout) { done in - AblyTests.queue.async { + options.internalDispatchQueue.async { client = nil // should enqueue a release channel = nil // should enqueue a release done() @@ -131,7 +131,7 @@ class ObjectLifetimesTests: XCTestCase { } waitUntil(timeout: testTimeout) { done in - AblyTests.queue.async { + options.internalDispatchQueue.async { XCTAssertNil(weakClient) XCTAssertNil(weakChannel) done() diff --git a/Test/Tests/PushAdminTests.swift b/Test/Tests/PushAdminTests.swift index 7fc3bdba8..a1bba2b8f 100644 --- a/Test/Tests/PushAdminTests.swift +++ b/Test/Tests/PushAdminTests.swift @@ -673,7 +673,7 @@ class PushAdminTests: XCTestCase { let options = try AblyTests.commonAppSetup(for: test) let realtime = ARTRealtime(options: options) defer { realtime.dispose(); realtime.close() } - let testProxyHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testProxyHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) realtime.internal.rest.httpExecutor = testProxyHTTPExecutor waitUntil(timeout: testTimeout) { done in @@ -820,7 +820,7 @@ class PushAdminTests: XCTestCase { let options = try AblyTests.commonAppSetup(for: test) let realtime = ARTRealtime(options: options) defer { realtime.dispose(); realtime.close() } - let testProxyHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testProxyHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) realtime.internal.rest.httpExecutor = testProxyHTTPExecutor waitUntil(timeout: testTimeout) { done in diff --git a/Test/Tests/PushChannelTests.swift b/Test/Tests/PushChannelTests.swift index 59312ba43..19aeb7df6 100644 --- a/Test/Tests/PushChannelTests.swift +++ b/Test/Tests/PushChannelTests.swift @@ -13,7 +13,7 @@ class PushChannelTests: XCTestCase { let options = ARTClientOptions(key: "xxxx:xxxx") userQueue = AblyTests.createUserQueue(for: test) options.dispatchQueue = userQueue - options.internalDispatchQueue = AblyTests.queue + options.internalDispatchQueue = AblyTests.createInternalQueue(for: test) rest = ARTRest(options: options) rest.internal.options.clientId = "tester" rest.internal.httpExecutor = mockHttpExecutor diff --git a/Test/Tests/PushTests.swift b/Test/Tests/PushTests.swift index 70fbcd1a8..3bb1494ef 100644 --- a/Test/Tests/PushTests.swift +++ b/Test/Tests/PushTests.swift @@ -257,7 +257,7 @@ class PushTests: XCTestCase { func test__013__LocalDevice__when_getting_a_client_ID_from_CONNECTED_message__new_clientID_is_set() { let options = ARTClientOptions(key: "fake:key") options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let realtime = ARTRealtime(options: options) XCTAssertNil(realtime.device.clientId) diff --git a/Test/Tests/RealtimeClientChannelTests.swift b/Test/Tests/RealtimeClientChannelTests.swift index 918374298..f4f1cfb8d 100644 --- a/Test/Tests/RealtimeClientChannelTests.swift +++ b/Test/Tests/RealtimeClientChannelTests.swift @@ -48,7 +48,7 @@ private func testHandlesDecodingErrorInFixture(_ cryptoFixtureFileName: String, let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false options.logHandler = ARTLog(capturingOutput: true) - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { client.dispose(); client.close() } @@ -113,7 +113,7 @@ private func testWithUntilAttach(_ untilAttach: Bool, for test: Test, channelNam defer { client.dispose(); client.close() } let channel = client.channels.get(channelName) - let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) client.internal.rest.httpExecutor = testHTTPExecutor let query = ARTRealtimeHistoryQuery() @@ -443,7 +443,8 @@ class RealtimeClientChannelTests: XCTestCase { // RTL2c func test__009__Channel__EventEmitter__channel_states_and_events__should_contain_an_ErrorInfo_object_with_details_when_an_error_occurs() throws { let test = Test() - let client = ARTRealtime(options: try AblyTests.commonAppSetup(for: test)) + let options = try AblyTests.commonAppSetup(for: test) + let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } let channel = client.channels.get(test.uniqueChannelName()) @@ -457,7 +458,7 @@ class RealtimeClientChannelTests: XCTestCase { XCTAssertEqual(channel.errorReason, pmError.error) done() } - AblyTests.queue.async { + options.internalDispatchQueue.async { channel.internal.onError(pmError) } } @@ -466,7 +467,8 @@ class RealtimeClientChannelTests: XCTestCase { // RTL2d func test__010__Channel__EventEmitter__channel_states_and_events__a_ChannelStateChange_is_emitted_as_the_first_argument_for_every_channel_state_change() throws { let test = Test() - let client = ARTRealtime(options: try AblyTests.commonAppSetup(for: test)) + let options = try AblyTests.commonAppSetup(for: test) + let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } let channel = client.channels.get(test.uniqueChannelName()) @@ -491,7 +493,7 @@ class RealtimeClientChannelTests: XCTestCase { XCTAssertEqual(stateChange.previous, ARTRealtimeChannelState.attached) done() } - AblyTests.queue.async { + options.internalDispatchQueue.async { channel.internal.onError(AblyTests.newErrorProtocolMessage()) } } @@ -578,7 +580,7 @@ class RealtimeClientChannelTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { client.dispose(); client.close() } @@ -635,7 +637,7 @@ class RealtimeClientChannelTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { client.dispose(); client.close() } @@ -661,7 +663,7 @@ class RealtimeClientChannelTests: XCTestCase { partialDone() } - AblyTests.queue.async { + options.internalDispatchQueue.async { let pmError = AblyTests.newErrorProtocolMessage() client.internal.onError(pmError) partialDone() @@ -705,7 +707,7 @@ class RealtimeClientChannelTests: XCTestCase { let options = try AblyTests.commonAppSetup(for: test) options.queueMessages = false options.autoConnect = false - let transportFactory = TestProxyTransportFactory() + let transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) options.testOptions.transportFactory = transportFactory let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } @@ -738,7 +740,7 @@ class RealtimeClientChannelTests: XCTestCase { expect(stateChange.reason?.message).to(satisfyAnyOf(contain("unreachable host"), contain("network is down"))) done() } - client.simulateNoInternetConnection(transportFactory: transportFactory) + client.simulateNoInternetConnection(transportFactory: transportFactory, internalQueue: options.internalDispatchQueue) } waitUntil(timeout: testTimeout) { done in @@ -746,7 +748,7 @@ class RealtimeClientChannelTests: XCTestCase { XCTAssertEqual(stateChange.previous, .connecting) done() } - client.simulateRestoreInternetConnection(transportFactory: transportFactory) + client.simulateRestoreInternetConnection(transportFactory: transportFactory, internalQueue: options.internalDispatchQueue) } channel.off() @@ -759,7 +761,7 @@ class RealtimeClientChannelTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { client.dispose(); client.close() } @@ -799,7 +801,7 @@ class RealtimeClientChannelTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { client.dispose(); client.close() } @@ -832,7 +834,7 @@ class RealtimeClientChannelTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { client.dispose(); client.close() } @@ -858,7 +860,7 @@ class RealtimeClientChannelTests: XCTestCase { partialDone() } - AblyTests.queue.async { + options.internalDispatchQueue.async { client.internal.onSuspended() partialDone() } @@ -925,13 +927,13 @@ class RealtimeClientChannelTests: XCTestCase { options.suspendedRetryTimeout = 3.0 options.channelRetryTimeout = 0.5 options.autoConnect = false - let transportFactory = TestProxyTransportFactory() + let transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) options.testOptions.transportFactory = transportFactory let client = ARTRealtime(options: options) client.internal.setReachabilityClass(TestReachability.self) defer { - client.simulateRestoreInternetConnection(transportFactory: transportFactory) + client.simulateRestoreInternetConnection(transportFactory: transportFactory, internalQueue: options.internalDispatchQueue) client.dispose() client.close() } @@ -957,10 +959,10 @@ class RealtimeClientChannelTests: XCTestCase { expect(error.message).to(satisfyAnyOf(contain("network is down"), contain("unreachable host"))) done() } - client.simulateNoInternetConnection(transportFactory: transportFactory) + client.simulateNoInternetConnection(transportFactory: transportFactory, internalQueue: options.internalDispatchQueue) } - AblyTests.queue.async { + options.internalDispatchQueue.async { // Do not resume client.simulateLostConnectionAndState() } @@ -970,7 +972,7 @@ class RealtimeClientChannelTests: XCTestCase { XCTAssertEqual(stateChange.reason?.code, ARTErrorCode.unableToRecoverConnectionExpired.intValue) // didn't resumed done() } - client.simulateRestoreInternetConnection(after: 1.0, transportFactory: transportFactory) + client.simulateRestoreInternetConnection(after: 1.0, transportFactory: transportFactory, internalQueue: options.internalDispatchQueue) } waitUntil(timeout: testTimeout) { done in @@ -1113,7 +1115,7 @@ class RealtimeClientChannelTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { client.dispose(); client.close() } @@ -1256,7 +1258,7 @@ class RealtimeClientChannelTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { client.dispose(); client.close() } @@ -1550,7 +1552,8 @@ class RealtimeClientChannelTests: XCTestCase { // RTL4j1 func test__047__Channel__attach__attach_resume__should_have_correct_AttachResume_value() throws { let test = Test() - let client = ARTRealtime(options: try AblyTests.commonAppSetup(for: test)) + let options = try AblyTests.commonAppSetup(for: test) + let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } let channel = client.channels.get(test.uniqueChannelName()) @@ -1561,7 +1564,7 @@ class RealtimeClientChannelTests: XCTestCase { channel.once(.failed) { _ in done() } - AblyTests.queue.async { + options.internalDispatchQueue.async { channel.internal.onError(AblyTests.newErrorProtocolMessage()) } } @@ -1780,7 +1783,7 @@ class RealtimeClientChannelTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { client.dispose(); client.close() } @@ -1869,7 +1872,7 @@ class RealtimeClientChannelTests: XCTestCase { let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false options.testOptions.realtimeRequestTimeout = 1.0 - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { client.dispose(); client.close() } @@ -1905,7 +1908,7 @@ class RealtimeClientChannelTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { client.dispose(); client.close() } @@ -2593,7 +2596,8 @@ class RealtimeClientChannelTests: XCTestCase { // RTL6c5 func test__070__Channel__publish__Connection_state_conditions__publish_should_not_trigger_an_implicit_attach() throws { let test = Test() - let client = ARTRealtime(options: try AblyTests.commonAppSetup(for: test)) + let options = try AblyTests.commonAppSetup(for: test) + let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } expect(client.connection.state).toEventually(equal(ARTRealtimeConnectionState.connected), timeout: testTimeout) let channel = client.channels.get(test.uniqueChannelName()) @@ -2610,7 +2614,7 @@ class RealtimeClientChannelTests: XCTestCase { } } XCTAssertEqual(channel.state, ARTRealtimeChannelState.initialized) - AblyTests.queue.async { + options.internalDispatchQueue.async { channel.internal.onError(protocolError) } } @@ -2824,7 +2828,7 @@ class RealtimeClientChannelTests: XCTestCase { let expectationMessageBundling = XCTestExpectation(description: "message-bundling") - AblyTests.queue.async { + options.internalDispatchQueue.async { let queue: [ARTQueuedMessage] = client.internal.queuedMessages as! [ARTQueuedMessage] for i in 0 ... 10 { for message in queue[i].msg.messages! { @@ -2949,7 +2953,7 @@ class RealtimeClientChannelTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { client.dispose(); client.close() } @@ -2992,7 +2996,7 @@ class RealtimeClientChannelTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { client.dispose(); client.close() } @@ -3031,7 +3035,7 @@ class RealtimeClientChannelTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { client.dispose(); client.close() } @@ -3070,7 +3074,7 @@ class RealtimeClientChannelTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { client.dispose(); client.close() } @@ -3106,7 +3110,7 @@ class RealtimeClientChannelTests: XCTestCase { let options = try AblyTests.commonAppSetup(for: test) options.clientId = "client_string" options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { client.dispose(); client.close() } @@ -3375,7 +3379,7 @@ class RealtimeClientChannelTests: XCTestCase { let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false options.logHandler = ARTLog(capturingOutput: true) - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { client.dispose(); client.close() } @@ -4009,7 +4013,7 @@ class RealtimeClientChannelTests: XCTestCase { options.channelRetryTimeout = 1.0 options.autoConnect = false options.testOptions.realtimeRequestTimeout = 1.0 - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() @@ -4469,7 +4473,7 @@ class RealtimeClientChannelTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let clientSender = ARTRealtime(options: options) defer { clientSender.close() } @@ -4585,7 +4589,8 @@ class RealtimeClientChannelTests: XCTestCase { // TM2a func test__139__message_attributes__if_the_message_does_not_contain_an_id__it_should_be_set_to_protocolMsgId_index() throws { let test = Test() - let client = ARTRealtime(options: try AblyTests.commonAppSetup(for: test)) + let options = try AblyTests.commonAppSetup(for: test) + let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } let p = ARTProtocolMessage() p.id = "protocolId" @@ -4602,7 +4607,7 @@ class RealtimeClientChannelTests: XCTestCase { XCTAssertEqual(message.id, "protocolId:0") done() } - AblyTests.queue.async { + options.internalDispatchQueue.async { channel.internal.onMessage(p) } } diff --git a/Test/Tests/RealtimeClientConnectionTests.swift b/Test/Tests/RealtimeClientConnectionTests.swift index ff04af1d4..997da4804 100644 --- a/Test/Tests/RealtimeClientConnectionTests.swift +++ b/Test/Tests/RealtimeClientConnectionTests.swift @@ -27,7 +27,7 @@ private func testUsesAlternativeHostOnResponse(_ caseTest: FakeNetworkResponse, let options = ARTClientOptions(key: "xxxx:xxxx") options.autoConnect = false options.testOptions.realtimeRequestTimeout = 1.0 - let transportFactory = TestProxyTransportFactory() + let transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) options.testOptions.transportFactory = transportFactory let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } @@ -66,7 +66,7 @@ private func testUsesAlternativeHostOnResponse(_ caseTest: FakeNetworkResponse, private func testMovesToDisconnectedWithNetworkingError(_ error: Error, for test: Test) throws { let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = AblyTests.newRealtime(options).client defer { client.dispose() @@ -81,7 +81,7 @@ private func testMovesToDisconnectedWithNetworkingError(_ error: Error, for test } var _transport: ARTWebSocketTransport? - AblyTests.queue.sync { + options.internalDispatchQueue.sync { _transport = client.internal.transport as? ARTWebSocketTransport } @@ -165,7 +165,7 @@ class RealtimeClientConnectionTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) let defaultMaxMessageSize = ARTDefault.maxMessageSize() XCTAssertEqual(defaultMaxMessageSize, 65536) @@ -192,7 +192,7 @@ class RealtimeClientConnectionTests: XCTestCase { func test__017__Connection__url__should_connect_to_the_default_host() { let options = ARTClientOptions(key: "keytest:secret") options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() @@ -209,7 +209,7 @@ class RealtimeClientConnectionTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() @@ -246,7 +246,7 @@ class RealtimeClientConnectionTests: XCTestCase { options.useTokenAuth = true options.autoConnect = false options.echoMessages = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() @@ -366,7 +366,7 @@ class RealtimeClientConnectionTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() @@ -767,7 +767,7 @@ class RealtimeClientConnectionTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { @@ -809,7 +809,7 @@ class RealtimeClientConnectionTests: XCTestCase { let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false options.clientId = "client_string" - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { client.dispose(); client.close() } @@ -839,7 +839,7 @@ class RealtimeClientConnectionTests: XCTestCase { let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false options.clientId = "client_string" - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { client.dispose(); client.close() } @@ -882,7 +882,7 @@ class RealtimeClientConnectionTests: XCTestCase { let channelName = test.uniqueChannelName() options.token = try getTestToken(for: test, key: options.key, capability: "{ \"\(options.testOptions.channelNamePrefix!)-\(channelName)\":[\"subscribe\"] }") options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { client.dispose(); client.close() } @@ -912,7 +912,7 @@ class RealtimeClientConnectionTests: XCTestCase { let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false options.clientId = "client_string" - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { client.dispose(); client.close() } @@ -955,7 +955,7 @@ class RealtimeClientConnectionTests: XCTestCase { let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false options.clientId = "client_string" - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { client.dispose(); client.close() } @@ -1199,7 +1199,7 @@ class RealtimeClientConnectionTests: XCTestCase { let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false options.clientId = "client_string" - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { client.dispose(); client.close() } @@ -1238,7 +1238,7 @@ class RealtimeClientConnectionTests: XCTestCase { let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false options.clientId = "client_string" - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { client.dispose(); client.close() } @@ -1266,7 +1266,7 @@ class RealtimeClientConnectionTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { @@ -1702,7 +1702,7 @@ class RealtimeClientConnectionTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { @@ -1746,7 +1746,7 @@ class RealtimeClientConnectionTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { @@ -1798,7 +1798,7 @@ class RealtimeClientConnectionTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { @@ -2053,7 +2053,7 @@ class RealtimeClientConnectionTests: XCTestCase { } let tokenTtl = 3.0 options.token = try getTestToken(for: test, key: options.key, ttl: tokenTtl) - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) defer { @@ -2139,7 +2139,7 @@ class RealtimeClientConnectionTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let tokenTtl = 3.0 let tokenDetails = try getTestTokenDetails(for: test, key: options.key, capability: nil, ttl: tokenTtl) options.token = tokenDetails.token @@ -2180,7 +2180,7 @@ class RealtimeClientConnectionTests: XCTestCase { options.autoConnect = false let tokenTtl = 1.0 options.token = try getTestToken(for: test, ttl: tokenTtl) - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) // Let the token expire waitUntil(timeout: testTimeout) { done in @@ -2355,13 +2355,13 @@ class RealtimeClientConnectionTests: XCTestCase { options.disconnectedRetryTimeout = 0.5 options.suspendedRetryTimeout = 2.0 options.autoConnect = false - let transportFactory = TestProxyTransportFactory() + let transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) options.testOptions.transportFactory = transportFactory let client = ARTRealtime(options: options) client.internal.setReachabilityClass(TestReachability.self) defer { - client.simulateRestoreInternetConnection(transportFactory: transportFactory) + client.simulateRestoreInternetConnection(transportFactory: transportFactory, internalQueue: options.internalDispatchQueue) client.dispose() client.close() } @@ -2381,7 +2381,7 @@ class RealtimeClientConnectionTests: XCTestCase { client.connection.on { stateChange in events.append(stateChange.current) } - client.simulateNoInternetConnection(transportFactory: transportFactory) + client.simulateNoInternetConnection(transportFactory: transportFactory, internalQueue: options.internalDispatchQueue) expect(events).toEventually(equal([ .disconnected, @@ -2402,7 +2402,7 @@ class RealtimeClientConnectionTests: XCTestCase { ]), timeout: testTimeout) events.removeAll() - client.simulateRestoreInternetConnection(after: 7.0, transportFactory: transportFactory) + client.simulateRestoreInternetConnection(after: 7.0, transportFactory: transportFactory, internalQueue: options.internalDispatchQueue) expect(events).toEventually(equal([ .connecting, // 2.0 - 1 @@ -2878,7 +2878,7 @@ class RealtimeClientConnectionTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() @@ -2953,7 +2953,7 @@ class RealtimeClientConnectionTests: XCTestCase { fail("Shouldn't be called") } } - AblyTests.queue.async { + options.internalDispatchQueue.async { client.internal.onDisconnected() } client.connection.once(.connected) { _ in @@ -3086,7 +3086,7 @@ class RealtimeClientConnectionTests: XCTestCase { } let tokenTtl = 2.0 options.token = try getTestToken(for: test, key: options.key, ttl: tokenTtl) - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) defer { @@ -3167,7 +3167,7 @@ class RealtimeClientConnectionTests: XCTestCase { callback(tokenDetails, nil) // Return the same expired token again. } } - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) defer { @@ -3497,7 +3497,7 @@ class RealtimeClientConnectionTests: XCTestCase { let options = ARTClientOptions(key: "xxxx:xxxx") options.autoConnect = false options.testOptions.realtimeRequestTimeout = 1.0 - let transportFactory = TestProxyTransportFactory() + let transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) options.testOptions.transportFactory = transportFactory let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } @@ -3542,7 +3542,7 @@ class RealtimeClientConnectionTests: XCTestCase { options.autoConnect = false options.fallbackHosts = ["f.ably-realtime.com", "g.ably-realtime.com", "h.ably-realtime.com", "i.ably-realtime.com", "j.ably-realtime.com"] options.testOptions.realtimeRequestTimeout = 1.0 - let transportFactory = TestProxyTransportFactory() + let transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) options.testOptions.transportFactory = transportFactory let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } @@ -3617,7 +3617,7 @@ class RealtimeClientConnectionTests: XCTestCase { let options = ARTClientOptions(key: "xxxx:xxxx") options.autoConnect = false options.testOptions.realtimeRequestTimeout = 1.0 - let transportFactory = TestProxyTransportFactory() + let transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) options.testOptions.transportFactory = transportFactory let client = ARTRealtime(options: options) let channel = client.channels.get(test.uniqueChannelName()) @@ -3651,7 +3651,7 @@ class RealtimeClientConnectionTests: XCTestCase { let options = ARTClientOptions(key: "xxxx:xxxx") options.autoConnect = false options.testOptions.realtimeRequestTimeout = 1.0 - let transportFactory = TestProxyTransportFactory() + let transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) options.testOptions.transportFactory = transportFactory let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } @@ -3706,13 +3706,13 @@ class RealtimeClientConnectionTests: XCTestCase { options.autoConnect = false options.testOptions.realtimeRequestTimeout = 5.0 options.testOptions.shuffleArray = shuffleArrayInExpectedHostOrder - let transportFactory = TestProxyTransportFactory() + let transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) options.testOptions.transportFactory = transportFactory let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } client.channels.get(test.uniqueChannelName()) - let testHttpExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHttpExecutor = TestProxyHTTPExecutor(queue: AblyTests.createInternalQueue(for: test), logger: .init(clientOptions: options)) client.internal.rest.httpExecutor = testHttpExecutor transportFactory.fakeNetworkResponse = .hostUnreachable @@ -3781,14 +3781,14 @@ class RealtimeClientConnectionTests: XCTestCase { let options = ARTClientOptions(key: "xxxx:xxxx") options.autoConnect = false options.testOptions.realtimeRequestTimeout = 1.0 - let transportFactory = TestProxyTransportFactory() + let transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) options.testOptions.transportFactory = transportFactory let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } client.channels.get(test.uniqueChannelName()) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) let testHttpExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.rest.httpExecutor = testHttpExecutor @@ -3832,13 +3832,13 @@ class RealtimeClientConnectionTests: XCTestCase { options.fallbackHosts = expectedFallbackHosts.sorted() // will be picked "randomly" as of expectedHostOrder options.testOptions.realtimeRequestTimeout = 5.0 options.testOptions.shuffleArray = shuffleArrayInExpectedHostOrder - let transportFactory = TestProxyTransportFactory() + let transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) options.testOptions.transportFactory = transportFactory let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } client.channels.get(test.uniqueChannelName()) - let testHttpExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHttpExecutor = TestProxyHTTPExecutor(queue: AblyTests.createInternalQueue(for: test), logger: .init(clientOptions: options)) client.internal.rest.httpExecutor = testHttpExecutor transportFactory.fakeNetworkResponse = .hostUnreachable @@ -3903,12 +3903,12 @@ class RealtimeClientConnectionTests: XCTestCase { let options = ARTClientOptions(key: "xxxx:xxxx") options.autoConnect = false options.fallbackHosts = [] - let transportFactory = TestProxyTransportFactory() + let transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) options.testOptions.transportFactory = transportFactory let client = ARTRealtime(options: options) let channel = client.channels.get(test.uniqueChannelName()) - let testHttpExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHttpExecutor = TestProxyHTTPExecutor(queue: AblyTests.createInternalQueue(for: test), logger: .init(clientOptions: options)) client.internal.rest.httpExecutor = testHttpExecutor transportFactory.fakeNetworkResponse = .hostUnreachable @@ -3935,13 +3935,14 @@ class RealtimeClientConnectionTests: XCTestCase { // RTN17e func test__096__Connection__Host_Fallback__client_is_connected_to_a_fallback_host_endpoint_should_do_HTTP_requests_to_the_same_data_centre() { + let test = Test() let options = ARTClientOptions(key: "xxxx:xxxx") options.autoConnect = false - let transportFactory = TestProxyTransportFactory() + let transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) options.testOptions.transportFactory = transportFactory let client = ARTRealtime(options: options) - let testHttpExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHttpExecutor = TestProxyHTTPExecutor(queue: AblyTests.createInternalQueue(for: test), logger: .init(clientOptions: options)) client.internal.rest.httpExecutor = testHttpExecutor transportFactory.fakeNetworkResponse = .hostUnreachable @@ -4079,7 +4080,7 @@ class RealtimeClientConnectionTests: XCTestCase { } XCTAssertEqual(channel.state, ARTRealtimeChannelState.attaching) transport.ignoreSends = false - AblyTests.queue.async { + options.internalDispatchQueue.async { client.internal.onDisconnected() } } @@ -4245,7 +4246,7 @@ class RealtimeClientConnectionTests: XCTestCase { let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false options.useTokenAuth = true - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } diff --git a/Test/Tests/RealtimeClientPresenceTests.swift b/Test/Tests/RealtimeClientPresenceTests.swift index ddb4c3d99..6fae8166f 100644 --- a/Test/Tests/RealtimeClientPresenceTests.swift +++ b/Test/Tests/RealtimeClientPresenceTests.swift @@ -4,15 +4,16 @@ import Nimble import XCTest // RTP16c -private func testResultsInErrorWithConnectionState(_ connectionState: ARTRealtimeConnectionState, for test: Test, channelName: String, performMethod: @escaping (ARTRealtime) -> Void) throws { - let client = ARTRealtime(options: try AblyTests.commonAppSetup(for: test)) +private func testResultsInErrorWithConnectionState(_ connectionState: ARTRealtimeConnectionState, for test: Test, channelName: String, performMethod: @escaping (ARTRealtime, ARTClientOptions) -> Void) throws { + let options = try AblyTests.commonAppSetup(for: test) + let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } let channel = client.channels.get(channelName) XCTAssertTrue(client.internal.options.queueMessages) waitUntil(timeout: testTimeout) { done in channel.attach { _ in - performMethod(client) + performMethod(client, options) done() } } @@ -110,7 +111,7 @@ class RealtimeClientPresenceTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { client.dispose(); client.close() } @@ -144,7 +145,7 @@ class RealtimeClientPresenceTests: XCTestCase { disposable += [AblyTests.addMembersSequentiallyToChannel(channelName, members: 250, options: options)] options.autoConnect = false - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) client.connect() defer { client.dispose(); client.close() } @@ -573,7 +574,8 @@ class RealtimeClientPresenceTests: XCTestCase { func skipped__test__019__Presence__Channel_state_change_side_effects__if_the_channel_enters_the_FAILED_state__should_clear_the_PresenceMap_including_local_members_and_does_not_emit_any_presence_events() throws { let test = Test() - let client = ARTRealtime(options: try AblyTests.commonAppSetup(for: test)) + let options = try AblyTests.commonAppSetup(for: test) + let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } let channel = client.channels.get(test.uniqueChannelName()) waitUntil(timeout: testTimeout) { done in @@ -603,7 +605,7 @@ class RealtimeClientPresenceTests: XCTestCase { expect(channel.internal.presenceMap.localMembers).to(beEmpty()) done() } - AblyTests.queue.async { + options.internalDispatchQueue.async { channel.internal.onError(AblyTests.newErrorProtocolMessage()) } } @@ -932,7 +934,8 @@ class RealtimeClientPresenceTests: XCTestCase { // RTP6c func test__028__Presence__subscribe__should_result_in_an_error_if_the_channel_moves_to_the_FAILED_state() throws { let test = Test() - let client = ARTRealtime(options: try AblyTests.commonAppSetup(for: test)) + let options = try AblyTests.commonAppSetup(for: test) + let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } let channel = client.channels.get(test.uniqueChannelName()) @@ -946,7 +949,7 @@ class RealtimeClientPresenceTests: XCTestCase { done() }) { _ in } }) { _ in } - AblyTests.queue.async { + options.internalDispatchQueue.async { channel.internal.onError(error) } } @@ -1166,7 +1169,7 @@ class RealtimeClientPresenceTests: XCTestCase { let channel = client.channels.get(test.uniqueChannelName()) waitUntil(timeout: testTimeout) { done in - AblyTests.queue.async { + options.internalDispatchQueue.async { channel.internal.onError(AblyTests.newErrorProtocolMessage()) done() } @@ -1311,7 +1314,7 @@ class RealtimeClientPresenceTests: XCTestCase { waitUntil(timeout: testTimeout) { done in let protocolError = AblyTests.newErrorProtocolMessage() channel.once(.attaching) { _ in - AblyTests.queue.async { + options.internalDispatchQueue.async { channel.internal.onError(protocolError) } } @@ -2061,7 +2064,7 @@ class RealtimeClientPresenceTests: XCTestCase { defer { client.dispose(); client.close() } let channel = client.channels.get(test.uniqueChannelName()) - AblyTests.queue.async { + options.internalDispatchQueue.async { channel.internal.onError(AblyTests.newErrorProtocolMessage()) } @@ -2273,7 +2276,7 @@ class RealtimeClientPresenceTests: XCTestCase { defer { client.dispose(); client.close() } let channel = client.channels.get(test.uniqueChannelName()) - AblyTests.queue.async { + options.internalDispatchQueue.async { channel.internal.onError(AblyTests.newErrorProtocolMessage()) } @@ -2376,7 +2379,7 @@ class RealtimeClientPresenceTests: XCTestCase { } } - AblyTests.queue.async { + options.internalDispatchQueue.async { channel.internal.onError(AblyTests.newErrorProtocolMessage()) } @@ -2446,12 +2449,13 @@ class RealtimeClientPresenceTests: XCTestCase { // RTP6c func test__076__Presence__subscribe__should_result_in_an_error_if_the_channel_is_in_the_FAILED_state() throws { let test = Test() - let client = ARTRealtime(options: try AblyTests.commonAppSetup(for: test)) + let options = try AblyTests.commonAppSetup(for: test) + let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } let channel = client.channels.get(test.uniqueChannelName()) let protocolError = AblyTests.newErrorProtocolMessage() - AblyTests.queue.async { + options.internalDispatchQueue.async { channel.internal.onError(protocolError) } @@ -2469,7 +2473,8 @@ class RealtimeClientPresenceTests: XCTestCase { // RTP6c func test__077__Presence__subscribe__should_result_in_an_error_if_the_channel_moves_to_the_FAILED_state() throws { let test = Test() - let client = ARTRealtime(options: try AblyTests.commonAppSetup(for: test)) + let options = try AblyTests.commonAppSetup(for: test) + let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } let channel = client.channels.get(test.uniqueChannelName()) @@ -2482,7 +2487,7 @@ class RealtimeClientPresenceTests: XCTestCase { }, callback: { _ in fail("Should not be called") }) - AblyTests.queue.async { + options.internalDispatchQueue.async { channel.internal.onError(error) } } @@ -2991,12 +2996,13 @@ class RealtimeClientPresenceTests: XCTestCase { func test__should_result_in_an_error_if_the_channel_is_in_the_FAILED_state() throws { contextBeforeEach?() - let client = ARTRealtime(options: try AblyTests.commonAppSetup(for: test)) + let options = try AblyTests.commonAppSetup(for: test) + let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } let channel = client.channels.get(test.uniqueChannelName()) let expectedErrorMessage = "Something has failed" - AblyTests.queue.async { + options.internalDispatchQueue.async { channel.internal.onError(AblyTests.newErrorProtocolMessage(message: expectedErrorMessage)) } @@ -3019,14 +3025,15 @@ class RealtimeClientPresenceTests: XCTestCase { func test__should_result_in_an_error_if_the_channel_moves_to_the_FAILED_state() throws { contextBeforeEach?() - let client = AblyTests.newRealtime(try AblyTests.commonAppSetup(for: test)).client + let options = try AblyTests.commonAppSetup(for: test) + let client = AblyTests.newRealtime(options).client defer { client.dispose(); client.close() } let channel = client.channels.get(test.uniqueChannelName()) waitUntil(timeout: testTimeout) { done in let error = AblyTests.newErrorProtocolMessage() channel.once(.attaching) { _ in - AblyTests.queue.async { + options.internalDispatchQueue.async { channel.internal.onError(error) } } @@ -3230,8 +3237,8 @@ class RealtimeClientPresenceTests: XCTestCase { func test__097__Presence__Connection_state_conditions__should_result_in_an_error_if_the_connection_state_is__suspended() throws { let test = Test() - try testResultsInErrorWithConnectionState(.suspended, for: test, channelName: test.uniqueChannelName()) { client in - AblyTests.queue.async { + try testResultsInErrorWithConnectionState(.suspended, for: test, channelName: test.uniqueChannelName()) { client, options in + options.internalDispatchQueue.async { client.internal.onSuspended() } } @@ -3239,15 +3246,15 @@ class RealtimeClientPresenceTests: XCTestCase { func test__098__Presence__Connection_state_conditions__should_result_in_an_error_if_the_connection_state_is__closed() throws { let test = Test() - try testResultsInErrorWithConnectionState(.closed, for: test, channelName: test.uniqueChannelName()) { client in + try testResultsInErrorWithConnectionState(.closed, for: test, channelName: test.uniqueChannelName()) { client, _ in client.close() } } func test__099__Presence__Connection_state_conditions__should_result_in_an_error_if_the_connection_state_is__failed() throws { let test = Test() - try testResultsInErrorWithConnectionState(.failed, for: test, channelName: test.uniqueChannelName()) { client in - AblyTests.queue.async { + try testResultsInErrorWithConnectionState(.failed, for: test, channelName: test.uniqueChannelName()) { client, options in + options.internalDispatchQueue.async { client.internal.onError(AblyTests.newErrorProtocolMessage()) } } @@ -3326,12 +3333,13 @@ class RealtimeClientPresenceTests: XCTestCase { // RTP11b func test__102__Presence__get__should_result_in_an_error_if_the_channel_is_in_the_FAILED_state() throws { let test = Test() - let client = ARTRealtime(options: try AblyTests.commonAppSetup(for: test)) + let options = try AblyTests.commonAppSetup(for: test) + let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } let channel = client.channels.get(test.uniqueChannelName()) let pm = AblyTests.newErrorProtocolMessage() - AblyTests.queue.async { + options.internalDispatchQueue.async { channel.internal.onError(pm) } @@ -3358,7 +3366,8 @@ class RealtimeClientPresenceTests: XCTestCase { // RTP11b func test__103__Presence__get__should_result_in_an_error_if_the_channel_moves_to_the_FAILED_state() throws { let test = Test() - let client = AblyTests.newRealtime(try AblyTests.commonAppSetup(for: test)).client + let options = try AblyTests.commonAppSetup(for: test) + let client = AblyTests.newRealtime(options).client defer { client.dispose(); client.close() } let channel = client.channels.get(test.uniqueChannelName()) @@ -3369,7 +3378,7 @@ class RealtimeClientPresenceTests: XCTestCase { } (client.internal.transport as! TestProxyTransport).actionsIgnored += [.attached] channel.once(.attaching) { _ in - AblyTests.queue.async { + options.internalDispatchQueue.async { channel.internal.onError(pm) } } @@ -3992,7 +4001,7 @@ class RealtimeClientPresenceTests: XCTestCase { XCTAssertEqual(message.id, "protocolId:0") done() } - AblyTests.queue.async { + options.internalDispatchQueue.async { channel.internal.onPresence(protocolMessage) } } diff --git a/Test/Tests/RealtimeClientTests.swift b/Test/Tests/RealtimeClientTests.swift index 9a2af6076..e7bbdd512 100644 --- a/Test/Tests/RealtimeClientTests.swift +++ b/Test/Tests/RealtimeClientTests.swift @@ -433,7 +433,7 @@ class RealtimeClientTests: XCTestCase { let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false options.useTokenAuth = true - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } @@ -491,7 +491,7 @@ class RealtimeClientTests: XCTestCase { options.autoConnect = false let testToken = try getTestToken(for: test) options.token = testToken - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } @@ -552,7 +552,7 @@ class RealtimeClientTests: XCTestCase { options.autoConnect = false let testToken = try getTestToken(for: test, capability: "{\"test\":[\"subscribe\"]}") options.token = testToken - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } @@ -644,7 +644,7 @@ class RealtimeClientTests: XCTestCase { options.autoConnect = false let testToken = try getTestToken(for: test) options.token = testToken - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } @@ -706,7 +706,7 @@ class RealtimeClientTests: XCTestCase { options.autoConnect = false options.clientId = "ios" options.useTokenAuth = true - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } @@ -799,7 +799,7 @@ class RealtimeClientTests: XCTestCase { let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false options.useTokenAuth = true - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } @@ -834,7 +834,7 @@ class RealtimeClientTests: XCTestCase { let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false options.useTokenAuth = true - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } @@ -922,7 +922,7 @@ class RealtimeClientTests: XCTestCase { let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false options.useTokenAuth = true - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } @@ -973,7 +973,7 @@ class RealtimeClientTests: XCTestCase { let options = try AblyTests.commonAppSetup(for: test) options.autoConnect = false options.useTokenAuth = true - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } @@ -1061,7 +1061,7 @@ class RealtimeClientTests: XCTestCase { options.autoConnect = false let testToken = try getTestToken(for: test) options.token = testToken - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } @@ -1117,7 +1117,7 @@ class RealtimeClientTests: XCTestCase { options.autoConnect = false let testToken = try getTestToken(for: test) options.token = testToken - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } @@ -1173,7 +1173,7 @@ class RealtimeClientTests: XCTestCase { options.autoConnect = false let testToken = try getTestToken(for: test) options.token = testToken - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } @@ -1229,7 +1229,7 @@ class RealtimeClientTests: XCTestCase { options.autoConnect = false let testToken = try getTestToken(for: test) options.token = testToken - options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.transportFactory = TestProxyTransportFactory(internalQueue: options.internalDispatchQueue) let client = ARTRealtime(options: options) defer { client.dispose(); client.close() } diff --git a/Test/Tests/RestClientChannelTests.swift b/Test/Tests/RestClientChannelTests.swift index 24154c2bb..171490fb0 100644 --- a/Test/Tests/RestClientChannelTests.swift +++ b/Test/Tests/RestClientChannelTests.swift @@ -136,7 +136,7 @@ class RestClientChannelTests: XCTestCase { init(test: Test) throws { let options = try AblyTests.commonAppSetup(for: test) client = ARTRest(options: options) - testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) } } @@ -357,7 +357,7 @@ class RestClientChannelTests: XCTestCase { let options = try AblyTests.commonAppSetup(for: test) options.clientId = "john-doe" let client = ARTRest(options: options) - let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) client.internal.httpExecutor = testHTTPExecutor waitUntil(timeout: testTimeout) { done in client.channels.get(test.uniqueChannelName(prefix: "RSA7e1")) @@ -914,7 +914,7 @@ class RestClientChannelTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) let client = ARTRest(options: options) - let proxyHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let proxyHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) client.internal.httpExecutor = proxyHTTPExecutor let channel = client.channels.get(test.uniqueChannelName()) diff --git a/Test/Tests/RestClientTests.swift b/Test/Tests/RestClientTests.swift index 081a8dd4d..a96b41c92 100644 --- a/Test/Tests/RestClientTests.swift +++ b/Test/Tests/RestClientTests.swift @@ -23,11 +23,11 @@ private let shuffleArrayInExpectedHostOrder = { (array: NSMutableArray) in private let _fallbackHosts = ["f.ably-realtime.com", "g.ably-realtime.com", "h.ably-realtime.com", "i.ably-realtime.com", "j.ably-realtime.com"] -private func testUsesAlternativeHost(_ caseTest: FakeNetworkResponse, channelName: String) { +private func testUsesAlternativeHost(_ caseTest: FakeNetworkResponse, for test: Test, channelName: String) { let options = ARTClientOptions(key: "xxxx:xxxx") let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: caseTest, resetAfter: 1) @@ -47,11 +47,11 @@ private func testUsesAlternativeHost(_ caseTest: FakeNetworkResponse, channelNam XCTAssertTrue(NSRegularExpression.match(testHTTPExecutor.requests[1].url!.absoluteString, pattern: "//[a-e].ably-realtime.com")) } -private func testStoresSuccessfulFallbackHostAsDefaultHost(_ caseTest: FakeNetworkResponse, channelName: String) { +private func testStoresSuccessfulFallbackHostAsDefaultHost(_ caseTest: FakeNetworkResponse, for test: Test, channelName: String) { let options = ARTClientOptions(key: "xxxx:xxxx") let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: caseTest, resetAfter: 1) @@ -83,13 +83,13 @@ private func testStoresSuccessfulFallbackHostAsDefaultHost(_ caseTest: FakeNetwo XCTAssertEqual(usedFallbackURL.host, reusedURL.host) } -private func testRestoresDefaultPrimaryHostAfterTimeoutExpires(_ caseTest: FakeNetworkResponse, channelName: String) { +private func testRestoresDefaultPrimaryHostAfterTimeoutExpires(_ caseTest: FakeNetworkResponse, for test: Test, channelName: String) { let options = ARTClientOptions(key: "xxxx:xxxx") options.logLevel = .debug options.fallbackRetryTimeout = 1 let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: caseTest, resetAfter: 1) @@ -113,13 +113,13 @@ private func testRestoresDefaultPrimaryHostAfterTimeoutExpires(_ caseTest: FakeN XCTAssertEqual(testHTTPExecutor.requests[2].url!.host, "rest.ably.io") } -private func testUsesAnotherFallbackHost(_ caseTest: FakeNetworkResponse, channelName: String) { +private func testUsesAnotherFallbackHost(_ caseTest: FakeNetworkResponse, for test: Test, channelName: String) { let options = ARTClientOptions(key: "xxxx:xxxx") options.fallbackRetryTimeout = 10 options.logLevel = .debug let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: caseTest, resetAfter: 2) @@ -152,7 +152,7 @@ class RestClientTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) let client = ARTRest(options: options) - testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) client.internal.httpExecutor = testHTTPExecutor let channel = client.channels.get(test.uniqueChannelName()) waitUntil(timeout: testTimeout) { done in @@ -309,7 +309,7 @@ class RestClientTests: XCTestCase { let options = ARTClientOptions(key: "fake:key") options.restHost = "fake.ably.io" let client = ARTRest(options: options) - testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + testHTTPExecutor = TestProxyHTTPExecutor(queue: AblyTests.createInternalQueue(for: test), logger: .init(clientOptions: options)) client.internal.httpExecutor = testHTTPExecutor publishTestMessage(client, channelName: test.uniqueChannelName(), failOnError: false) @@ -323,7 +323,7 @@ class RestClientTests: XCTestCase { options.environment = "test" options.restHost = "fake.ably.io" let client = ARTRest(options: options) - testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + testHTTPExecutor = TestProxyHTTPExecutor(queue: AblyTests.createInternalQueue(for: test), logger: .init(clientOptions: options)) client.internal.httpExecutor = testHTTPExecutor publishTestMessage(client, channelName: test.uniqueChannelName(), failOnError: false) @@ -337,7 +337,7 @@ class RestClientTests: XCTestCase { let options = ARTClientOptions(key: "fake:key") options.environment = "myEnvironment" let client = ARTRest(options: options) - testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + testHTTPExecutor = TestProxyHTTPExecutor(queue: AblyTests.createInternalQueue(for: test), logger: .init(clientOptions: options)) client.internal.httpExecutor = testHTTPExecutor publishTestMessage(client, channelName: test.uniqueChannelName(), failOnError: false) @@ -349,7 +349,7 @@ class RestClientTests: XCTestCase { let test = Test() let options = ARTClientOptions(key: "fake:key") let client = ARTRest(options: options) - testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + testHTTPExecutor = TestProxyHTTPExecutor(queue: AblyTests.createInternalQueue(for: test),logger: .init(clientOptions: options)) client.internal.httpExecutor = testHTTPExecutor publishTestMessage(client, channelName: test.uniqueChannelName(), failOnError: false) @@ -362,7 +362,7 @@ class RestClientTests: XCTestCase { let options = try AblyTests.clientOptions(for: test, requestToken: true) options.tls = false let client = ARTRest(options: options) - testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) client.internal.httpExecutor = testHTTPExecutor publishTestMessage(client, channelName: test.uniqueChannelName(), failOnError: false) @@ -410,7 +410,7 @@ class RestClientTests: XCTestCase { options.httpMaxRetryCount = 1 let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: .hostUnreachable) @@ -438,7 +438,7 @@ class RestClientTests: XCTestCase { options.httpMaxRetryDuration = 1.0 let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: .requestTimeout(timeout: 0.1)) @@ -472,7 +472,7 @@ class RestClientTests: XCTestCase { options.restHost = "rest.ably.test" XCTAssertEqual(options.restHost, "rest.ably.test") let client = ARTRest(options: options) - testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + testHTTPExecutor = TestProxyHTTPExecutor(queue: AblyTests.createInternalQueue(for: test), logger: .init(clientOptions: options)) client.internal.httpExecutor = testHTTPExecutor waitUntil(timeout: testTimeout) { done in client.channels.get(test.uniqueChannelName()).publish(nil, data: "message") { error in @@ -508,7 +508,7 @@ class RestClientTests: XCTestCase { let options = try AblyTests.commonAppSetup(for: test) let clientHttps = ARTRest(options: options) - testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) clientHttps.internal.httpExecutor = testHTTPExecutor let channelName = test.uniqueChannelName() @@ -527,7 +527,7 @@ class RestClientTests: XCTestCase { options.useTokenAuth = true options.tls = false let clientHttp = ARTRest(options: options) - testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) clientHttp.internal.httpExecutor = testHTTPExecutor waitUntil(timeout: testTimeout) { done in @@ -577,7 +577,7 @@ class RestClientTests: XCTestCase { let options = try AblyTests.commonAppSetup(for: test) options.token = try getTestToken(for: test, ttl: 0.5) let client = ARTRest(options: options) - testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) client.internal.httpExecutor = testHTTPExecutor let auth = client.auth @@ -607,7 +607,7 @@ class RestClientTests: XCTestCase { let options = try AblyTests.clientOptions(for: test) options.token = try getTestToken(for: test, capability: "{ \"main\":[\"subscribe\"] }") let client = ARTRest(options: options) - testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) client.internal.httpExecutor = testHTTPExecutor waitUntil(timeout: testTimeout) { done in @@ -730,7 +730,7 @@ class RestClientTests: XCTestCase { } let rest = ARTRest(options: options) - testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) rest.internal.httpExecutor = testHTTPExecutor waitUntil(timeout: testTimeout) { done in @@ -794,7 +794,7 @@ class RestClientTests: XCTestCase { ) let rest = ARTRest(options: options) - testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) rest.internal.httpExecutor = testHTTPExecutor // Delay for token expiration @@ -894,7 +894,7 @@ class RestClientTests: XCTestCase { let options = ARTClientOptions(key: "xxxx:xxxx") let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: .hostUnreachable, resetAfter: 2) @@ -922,7 +922,7 @@ class RestClientTests: XCTestCase { options.restHost = "fake.ably.io" let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: .hostUnreachable) @@ -949,7 +949,7 @@ class RestClientTests: XCTestCase { options.port = 999 let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: .hostUnreachable) @@ -975,7 +975,7 @@ class RestClientTests: XCTestCase { options.tlsPort = 999 let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: .hostUnreachable) @@ -1001,7 +1001,7 @@ class RestClientTests: XCTestCase { options.fallbackHosts = ["a.cocoa.ably", "b.cocoa.ably"] let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: .hostUnreachable) @@ -1029,7 +1029,7 @@ class RestClientTests: XCTestCase { options.fallbackHostsUseDefault = true let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: .hostUnreachable, resetAfter: 2) @@ -1056,7 +1056,7 @@ class RestClientTests: XCTestCase { options.restHost = "fake.ably.io" let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: .hostUnreachable) @@ -1079,7 +1079,7 @@ class RestClientTests: XCTestCase { options.fallbackHosts = ["f.ably-realtime.com"] let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: .hostUnreachable) @@ -1105,7 +1105,7 @@ class RestClientTests: XCTestCase { options.environment = "test" let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: .hostUnreachable, resetAfter: 2) @@ -1132,7 +1132,7 @@ class RestClientTests: XCTestCase { options.environment = "production" let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: .hostUnreachable) @@ -1160,7 +1160,7 @@ class RestClientTests: XCTestCase { options.environment = "" let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: .hostUnreachable) @@ -1190,7 +1190,7 @@ class RestClientTests: XCTestCase { options.fallbackHostsUseDefault = true let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: .hostUnreachable, resetAfter: 1) @@ -1215,7 +1215,7 @@ class RestClientTests: XCTestCase { options.fallbackHosts = [] // to test TO3k6 let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: .hostUnreachable) @@ -1239,7 +1239,7 @@ class RestClientTests: XCTestCase { options.fallbackHosts = nil let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: .hostUnreachable, resetAfter: 1) @@ -1268,7 +1268,7 @@ class RestClientTests: XCTestCase { options.fallbackRetryTimeout = 1 // RSC15j exception let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: .hostUnreachable, resetAfter: 1) @@ -1322,7 +1322,7 @@ class RestClientTests: XCTestCase { let client = ARTRest(options: options) options.httpMaxRetryCount = 3 let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: .hostUnreachable) @@ -1358,7 +1358,7 @@ class RestClientTests: XCTestCase { options.testOptions.shuffleArray = shuffleArrayInExpectedHostOrder let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: .hostUnreachable) @@ -1388,7 +1388,7 @@ class RestClientTests: XCTestCase { options.testOptions.shuffleArray = shuffleArrayInExpectedHostOrder let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: .hostUnreachable) @@ -1420,7 +1420,7 @@ class RestClientTests: XCTestCase { let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: .hostUnreachable) @@ -1453,7 +1453,7 @@ class RestClientTests: XCTestCase { let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: .hostUnreachable) @@ -1485,7 +1485,7 @@ class RestClientTests: XCTestCase { let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: .hostUnreachable) @@ -1518,7 +1518,7 @@ class RestClientTests: XCTestCase { let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: .hostUnreachable) @@ -1538,17 +1538,17 @@ class RestClientTests: XCTestCase { func test__074__RestClient__Host_Fallback__should_use_an_alternative_host_when___hostUnreachable() { let test = Test() - testUsesAlternativeHost(.hostUnreachable, channelName: test.uniqueChannelName()) + testUsesAlternativeHost(.hostUnreachable, for: test, channelName: test.uniqueChannelName()) } func test__075__RestClient__Host_Fallback__should_use_an_alternative_host_when___requestTimeout_timeout__0_1_() { let test = Test() - testUsesAlternativeHost(.requestTimeout(timeout: 0.1), channelName: test.uniqueChannelName()) + testUsesAlternativeHost(.requestTimeout(timeout: 0.1), for: test, channelName: test.uniqueChannelName()) } func test__076__RestClient__Host_Fallback__should_use_an_alternative_host_when___hostInternalError_code__501_() { let test = Test() - testUsesAlternativeHost(.hostInternalError(code: 501), channelName: test.uniqueChannelName()) + testUsesAlternativeHost(.hostInternalError(code: 501), for: test, channelName: test.uniqueChannelName()) } // RSC15d @@ -1557,7 +1557,7 @@ class RestClientTests: XCTestCase { let options = ARTClientOptions(key: "xxxx:xxxx") let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: .host400BadRequest, resetAfter: 1) @@ -1577,47 +1577,47 @@ class RestClientTests: XCTestCase { func test__077__RestClient__Host_Fallback__should_store_successful_fallback_host_as_default_host___hostUnreachable() { let test = Test() - testStoresSuccessfulFallbackHostAsDefaultHost(.hostUnreachable, channelName: test.uniqueChannelName()) + testStoresSuccessfulFallbackHostAsDefaultHost(.hostUnreachable, for: test, channelName: test.uniqueChannelName()) } func test__078__RestClient__Host_Fallback__should_store_successful_fallback_host_as_default_host___requestTimeout_timeout__0_1_() { let test = Test() - testStoresSuccessfulFallbackHostAsDefaultHost(.requestTimeout(timeout: 0.1), channelName: test.uniqueChannelName()) + testStoresSuccessfulFallbackHostAsDefaultHost(.requestTimeout(timeout: 0.1), for: test, channelName: test.uniqueChannelName()) } func test__079__RestClient__Host_Fallback__should_store_successful_fallback_host_as_default_host___hostInternalError_code__501_() { let test = Test() - testStoresSuccessfulFallbackHostAsDefaultHost(.hostInternalError(code: 501), channelName: test.uniqueChannelName()) + testStoresSuccessfulFallbackHostAsDefaultHost(.hostInternalError(code: 501), for: test, channelName: test.uniqueChannelName()) } func test__080__RestClient__Host_Fallback__should_store_successful_fallback_host_as_default_host__should_restore_default_primary_host_after_fallbackRetryTimeout_expired___hostUnreachable() { let test = Test() - testRestoresDefaultPrimaryHostAfterTimeoutExpires(.hostUnreachable, channelName: test.uniqueChannelName()) + testRestoresDefaultPrimaryHostAfterTimeoutExpires(.hostUnreachable, for: test, channelName: test.uniqueChannelName()) } func test__081__RestClient__Host_Fallback__should_store_successful_fallback_host_as_default_host__should_restore_default_primary_host_after_fallbackRetryTimeout_expired___requestTimeout_timeout__0_1_() { let test = Test() - testRestoresDefaultPrimaryHostAfterTimeoutExpires(.requestTimeout(timeout: 0.1), channelName: test.uniqueChannelName()) + testRestoresDefaultPrimaryHostAfterTimeoutExpires(.requestTimeout(timeout: 0.1), for: test, channelName: test.uniqueChannelName()) } func test__082__RestClient__Host_Fallback__should_store_successful_fallback_host_as_default_host__should_restore_default_primary_host_after_fallbackRetryTimeout_expired___hostInternalError_code__501_() { let test = Test() - testRestoresDefaultPrimaryHostAfterTimeoutExpires(.hostInternalError(code: 501), channelName: test.uniqueChannelName()) + testRestoresDefaultPrimaryHostAfterTimeoutExpires(.hostInternalError(code: 501), for: test, channelName: test.uniqueChannelName()) } func test__083__RestClient__Host_Fallback__should_store_successful_fallback_host_as_default_host__should_use_another_fallback_host_if_previous_fallback_request_failed_and_store_it_as_default_if_current_fallback_request_succseeded___hostUnreachable() { let test = Test() - testUsesAnotherFallbackHost(.hostUnreachable, channelName: test.uniqueChannelName()) + testUsesAnotherFallbackHost(.hostUnreachable, for: test, channelName: test.uniqueChannelName()) } func test__084__RestClient__Host_Fallback__should_store_successful_fallback_host_as_default_host__should_use_another_fallback_host_if_previous_fallback_request_failed_and_store_it_as_default_if_current_fallback_request_succseeded___requestTimeout_timeout__0_1_() { let test = Test() - testUsesAnotherFallbackHost(.requestTimeout(timeout: 0.1), channelName: test.uniqueChannelName()) + testUsesAnotherFallbackHost(.requestTimeout(timeout: 0.1), for: test, channelName: test.uniqueChannelName()) } func test__085__RestClient__Host_Fallback__should_store_successful_fallback_host_as_default_host__should_use_another_fallback_host_if_previous_fallback_request_failed_and_store_it_as_default_if_current_fallback_request_succseeded___hostInternalError_code__501_() { let test = Test() - testUsesAnotherFallbackHost(.hostInternalError(code: 501), channelName: test.uniqueChannelName()) + testUsesAnotherFallbackHost(.hostInternalError(code: 501), for: test, channelName: test.uniqueChannelName()) } // RSC8a @@ -1627,7 +1627,7 @@ class RestClientTests: XCTestCase { XCTAssertTrue(options.useBinaryProtocol) let rest = ARTRest(options: options) - testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) rest.internal.httpExecutor = testHTTPExecutor waitUntil(timeout: testTimeout) { done in rest.channels.get(test.uniqueChannelName(prefix: "rest")).publish(nil, data: "message") { _ in @@ -1665,7 +1665,7 @@ class RestClientTests: XCTestCase { options.useBinaryProtocol = false let rest = ARTRest(options: options) - testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) rest.internal.httpExecutor = testHTTPExecutor waitUntil(timeout: testTimeout) { done in rest.channels.get(test.uniqueChannelName(prefix: "rest")).publish(nil, data: "message") { _ in @@ -1699,7 +1699,7 @@ class RestClientTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) let client = ARTRest(options: options) - testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) client.internal.httpExecutor = testHTTPExecutor waitUntil(timeout: testTimeout) { done in client.channels.get(test.uniqueChannelName()).publish(nil, data: "message") { error in @@ -1725,7 +1725,7 @@ class RestClientTests: XCTestCase { let test = Test() let options = try AblyTests.commonAppSetup(for: test) let client = ARTRest(options: options) - testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) client.internal.httpExecutor = testHTTPExecutor let channel = client.channels.get(test.uniqueChannelName()) waitUntil(timeout: testTimeout) { done in @@ -1954,7 +1954,7 @@ class RestClientTests: XCTestCase { } } - let proxyHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let proxyHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) rest.internal.httpExecutor = proxyHTTPExecutor var httpPaginatedResponse: ARTHTTPPaginatedResponse! @@ -2004,7 +2004,7 @@ class RestClientTests: XCTestCase { } } - let proxyHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let proxyHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) rest.internal.httpExecutor = proxyHTTPExecutor waitUntil(timeout: testTimeout) { done in @@ -2115,7 +2115,7 @@ class RestClientTests: XCTestCase { let client = ARTRest(options: options) let internalLog = InternalLog(clientOptions: options) - let mockHTTP = MockHTTP(logger: internalLog) + let mockHTTP = MockHTTP(queue: AblyTests.createInternalQueue(for: test), logger: internalLog) testHTTPExecutor = TestProxyHTTPExecutor(http: mockHTTP, logger: internalLog) client.internal.httpExecutor = testHTTPExecutor mockHTTP.setNetworkState(network: .hostUnreachable) diff --git a/Test/Tests/UtilitiesTests.swift b/Test/Tests/UtilitiesTests.swift index e1d2a642e..b67994faa 100644 --- a/Test/Tests/UtilitiesTests.swift +++ b/Test/Tests/UtilitiesTests.swift @@ -5,7 +5,7 @@ import XCTest private var jsonEncoder: ARTJsonLikeEncoder! -private var eventEmitter = ARTInternalEventEmitter(queue: AblyTests.queue) +private var eventEmitter: ARTInternalEventEmitter! private var receivedFoo1: Int? private var receivedFoo2: Int? private var receivedBar: Int? @@ -38,6 +38,8 @@ class UtilitiesTests: XCTestCase { return super.defaultTestSuite } + private var eventEmitterQueue: DispatchQueue! + func beforeEach__Utilities__JSON_Encoder() { jsonEncoder = ARTJsonLikeEncoder() jsonEncoder.delegate = ARTJsonEncoder() @@ -207,7 +209,7 @@ class UtilitiesTests: XCTestCase { let options = try AblyTests.commonAppSetup(for: test) let rest = ARTRest(options: options) - let testHTTPExecutor = TestProxyHTTPExecutor(logger: .init(clientOptions: options)) + let testHTTPExecutor = TestProxyHTTPExecutor(queue: options.internalDispatchQueue, logger: .init(clientOptions: options)) rest.internal.httpExecutor = testHTTPExecutor let channel = rest.channels.get(test.uniqueChannelName()) @@ -236,8 +238,9 @@ class UtilitiesTests: XCTestCase { } } - func beforeEach__Utilities__EventEmitter() { - eventEmitter = ARTInternalEventEmitter(queue: AblyTests.queue) + func beforeEach__Utilities__EventEmitter(for test: Test) { + eventEmitterQueue = AblyTests.createInternalQueue(for: test) + eventEmitter = ARTInternalEventEmitter(queue: eventEmitterQueue) receivedFoo1 = nil receivedFoo2 = nil receivedBar = nil @@ -252,7 +255,8 @@ class UtilitiesTests: XCTestCase { } func test__009__Utilities__EventEmitter__should_emit_events_to_all_relevant_listeners() { - beforeEach__Utilities__EventEmitter() + let test = Test() + beforeEach__Utilities__EventEmitter(for: test) eventEmitter.emit("foo", with: 123 as AnyObject?) @@ -274,7 +278,8 @@ class UtilitiesTests: XCTestCase { } func test__010__Utilities__EventEmitter__should_only_call_once_listeners_once_for_its_event() { - beforeEach__Utilities__EventEmitter() + let test = Test() + beforeEach__Utilities__EventEmitter(for: test) eventEmitter.emit("foo", with: 123 as AnyObject?) @@ -293,7 +298,8 @@ class UtilitiesTests: XCTestCase { } func test__011__Utilities__EventEmitter__calling_off_with_a_single_listener_argument__should_stop_receiving_events_when_calling_off_with_a_single_listener_argument() { - beforeEach__Utilities__EventEmitter() + let test = Test() + beforeEach__Utilities__EventEmitter(for: test) eventEmitter.off(listenerFoo1!) eventEmitter.emit("foo", with: 123 as AnyObject?) @@ -314,21 +320,23 @@ class UtilitiesTests: XCTestCase { } func test__012__Utilities__EventEmitter__calling_off_with_a_single_listener_argument__should_remove_the_timeout() { - beforeEach__Utilities__EventEmitter() + let test = Test() + beforeEach__Utilities__EventEmitter(for: test) listenerFoo1!.setTimer(0.1, onTimeout: { fail("onTimeout callback shouldn't have been called") }).startTimer() eventEmitter.off(listenerFoo1!) - waitUntil(timeout: testTimeout) { done in - AblyTests.queue.asyncAfter(deadline: DispatchTime.now() + 0.3) { + waitUntil(timeout: testTimeout) { [eventEmitterQueue] done in + eventEmitterQueue!.asyncAfter(deadline: DispatchTime.now() + 0.3) { done() } } } func test__013__Utilities__EventEmitter__calling_off_with_listener_and_event_arguments__should_still_receive_events_if_off_doesn_t_match_the_listener_s_criteria() { - beforeEach__Utilities__EventEmitter() + let test = Test() + beforeEach__Utilities__EventEmitter(for: test) eventEmitter.off("foo", listener: listenerAll!) eventEmitter.emit("foo", with: 111 as AnyObject?) @@ -338,7 +346,8 @@ class UtilitiesTests: XCTestCase { } func test__014__Utilities__EventEmitter__calling_off_with_listener_and_event_arguments__should_stop_receive_events_if_off_matches_the_listener_s_criteria() { - beforeEach__Utilities__EventEmitter() + let test = Test() + beforeEach__Utilities__EventEmitter(for: test) eventEmitter.off("foo", listener: listenerFoo1!) eventEmitter.emit("foo", with: 111 as AnyObject?) @@ -348,7 +357,8 @@ class UtilitiesTests: XCTestCase { } func test__015__Utilities__EventEmitter__calling_off_with_no_arguments__should_remove_all_listeners() { - beforeEach__Utilities__EventEmitter() + let test = Test() + beforeEach__Utilities__EventEmitter(for: test) eventEmitter.off() eventEmitter.emit("foo", with: 111 as AnyObject?) @@ -365,7 +375,8 @@ class UtilitiesTests: XCTestCase { } func test__016__Utilities__EventEmitter__calling_off_with_no_arguments__should_allow_listening_again() { - beforeEach__Utilities__EventEmitter() + let test = Test() + beforeEach__Utilities__EventEmitter(for: test) eventEmitter.off() eventEmitter.on("foo", callback: { receivedFoo1 = $0 as? Int }) @@ -374,7 +385,8 @@ class UtilitiesTests: XCTestCase { } func test__017__Utilities__EventEmitter__calling_off_with_no_arguments__should_remove_all_timeouts() { - beforeEach__Utilities__EventEmitter() + let test = Test() + beforeEach__Utilities__EventEmitter(for: test) listenerFoo1!.setTimer(0.1, onTimeout: { fail("onTimeout callback shouldn't have been called") @@ -383,23 +395,24 @@ class UtilitiesTests: XCTestCase { fail("onTimeout callback shouldn't have been called") }).startTimer() eventEmitter.off() - waitUntil(timeout: DispatchTimeInterval.milliseconds(300)) { done in - AblyTests.queue.asyncAfter(deadline: .now() + 0.15) { + waitUntil(timeout: DispatchTimeInterval.milliseconds(300)) { [eventEmitterQueue] done in + eventEmitterQueue!.asyncAfter(deadline: .now() + 0.15) { done() } } } func test__018__Utilities__EventEmitter__the_timed_method__should_not_call_onTimeout_if_the_deadline_isn_t_reached() { - beforeEach__Utilities__EventEmitter() + let test = Test() + beforeEach__Utilities__EventEmitter(for: test) weak var timer = listenerFoo1!.setTimer(0.2, onTimeout: { fail("onTimeout callback shouldn't have been called") }) - waitUntil(timeout: DispatchTimeInterval.seconds(1)) { done in + waitUntil(timeout: DispatchTimeInterval.seconds(1)) { [eventEmitterQueue] done in timer?.startTimer() eventEmitter.emit("foo", with: 123 as AnyObject?) - AblyTests.queue.asyncAfter(deadline: .now() + 0.3) { + eventEmitterQueue!.asyncAfter(deadline: .now() + 0.3) { XCTAssertNotNil(receivedFoo1) done() } @@ -407,7 +420,8 @@ class UtilitiesTests: XCTestCase { } func test__019__Utilities__EventEmitter__the_timed_method__should_call_onTimeout_and_off_the_listener_if_the_deadline_is_reached() { - beforeEach__Utilities__EventEmitter() + let test = Test() + beforeEach__Utilities__EventEmitter(for: test) var calledOnTimeout = false let beforeEmitting = NSDate() @@ -415,8 +429,8 @@ class UtilitiesTests: XCTestCase { calledOnTimeout = true expect(NSDate()).to(beCloseTo(beforeEmitting.addingTimeInterval(0.3), within: 0.2)) }).startTimer() - waitUntil(timeout: DispatchTimeInterval.milliseconds(500)) { done in - AblyTests.queue.asyncAfter(deadline: .now() + 0.35) { + waitUntil(timeout: DispatchTimeInterval.milliseconds(500)) { [eventEmitterQueue] done in + eventEmitterQueue!.asyncAfter(deadline: .now() + 0.35) { XCTAssertTrue(calledOnTimeout) eventEmitter.emit("foo", with: 123 as AnyObject?) XCTAssertNil(receivedFoo1) @@ -427,7 +441,8 @@ class UtilitiesTests: XCTestCase { // RTE6a func test__020__Utilities__EventEmitter__set_of_listeners__should_not_change_over_the_course_of_the_emit() { - beforeEach__Utilities__EventEmitter() + let test = Test() + beforeEach__Utilities__EventEmitter(for: test) var firstCallbackCalled = false var secondCallbackCalled = false