From 7b97b312e67bc13a570a660d11f816a9ed49735a Mon Sep 17 00:00:00 2001 From: Lawrence Forooghian Date: Tue, 18 Apr 2023 12:19:59 -0300 Subject: [PATCH] WIP work on not calling default local device fetcher in tests TODO are there tests that rely on using the same fetcher in both cases? TODO run the whole test suite with breakpoints --- Test/Test Utilities/TestUtilities.swift | 1 + .../PushActivationStateMachineTests.swift | 24 +++++++++++-------- Test/Tests/PushChannelTests.swift | 2 +- Test/Tests/PushTests.swift | 23 ++++++++++++------ 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/Test/Test Utilities/TestUtilities.swift b/Test/Test Utilities/TestUtilities.swift index 7eb8e48bf..d20e53b27 100644 --- a/Test/Test Utilities/TestUtilities.swift +++ b/Test/Test Utilities/TestUtilities.swift @@ -171,6 +171,7 @@ class AblyTests { } options.dispatchQueue = DispatchQueue.main options.internalDispatchQueue = queue + options.testOptions.localDeviceFetcher = MockLocalDeviceFetcher() return options } diff --git a/Test/Tests/PushActivationStateMachineTests.swift b/Test/Tests/PushActivationStateMachineTests.swift index 1bcac9470..6eafc493d 100644 --- a/Test/Tests/PushActivationStateMachineTests.swift +++ b/Test/Tests/PushActivationStateMachineTests.swift @@ -31,7 +31,10 @@ class PushActivationStateMachineTests: XCTestCase { override func setUp() { super.setUp() - rest = ARTRest(key: "xxxx:xxxx") + let options = ARTClientOptions(key: "xxxx:xxxx") + options.testOptions.localDeviceFetcher = MockLocalDeviceFetcher() + + rest = ARTRest(options: options) httpExecutor = MockHTTPExecutor() rest.internal.httpExecutor = httpExecutor storage = MockDeviceStorage() @@ -39,12 +42,6 @@ class PushActivationStateMachineTests: XCTestCase { initialStateMachine = ARTPushActivationStateMachine(rest: rest.internal, delegate: StateMachineDelegate(), logger: .init(core: MockInternalLogCore())) } - override func tearDown() { - DefaultLocalDeviceFetcher.sharedInstance.resetDevice() - - super.tearDown() - } - func test__002__Activation_state_machine__should_set_NotActivated_state_as_current_state_when_disk_is_empty() { expect(initialStateMachine.current).to(beAKindOf(ARTPushActivationStateNotActivated.self)) } @@ -119,7 +116,8 @@ class PushActivationStateMachineTests: XCTestCase { func test__014__Activation_state_machine__State_NotActivated__on_Event_CalledActivate__local_device__should_have_a_generated_id() { beforeEach__Activation_state_machine__State_NotActivated() - DefaultLocalDeviceFetcher.sharedInstance.resetDevice() + // TODO was the reset here in a special place? + XCTAssertEqual(rest.device.id.count, 36) } @@ -138,6 +136,7 @@ class PushActivationStateMachineTests: XCTestCase { let options = ARTClientOptions(key: "xxxx:xxxx") options.clientId = "deviceClient" + options.testOptions.localDeviceFetcher = MockLocalDeviceFetcher() let rest = ARTRest(options: options) rest.internal.storage = storage XCTAssertEqual(rest.device.clientId, "deviceClient") @@ -473,7 +472,7 @@ class PushActivationStateMachineTests: XCTestCase { func test__028__Activation_state_machine__State_WaitingForDeviceRegistration__on_Event_GotDeviceRegistration() { beforeEach__Activation_state_machine__State_WaitingForDeviceRegistration() - DefaultLocalDeviceFetcher.sharedInstance.resetDevice() + // TODO was the reset here in a special place? var activatedCallbackCalled = false let hook = stateMachine.testSuite_injectIntoMethod(after: NSSelectorFromString("callActivatedCallback:")) { @@ -907,11 +906,12 @@ class PushActivationStateMachineTests: XCTestCase { expect(stateMachine.current).to(beAKindOf(ARTPushActivationStateWaitingForPushDeviceDetails.self)) } + // TODO what is this a test of? func test__001__should_remove_identityTokenDetails_from_cache_and_storage() { let storage = MockDeviceStorage() rest.internal.storage = storage rest.device.setAndPersistIdentityTokenDetails(nil) - DefaultLocalDeviceFetcher.sharedInstance.resetDevice() +// DefaultLocalDeviceFetcher.sharedInstance.resetDevice() XCTAssertNil(rest.device.identityTokenDetails) XCTAssertEqual(rest.device.isRegistered(), false) XCTAssertNil(storage.object(forKey: ARTDeviceIdentityTokenKey)) @@ -930,14 +930,18 @@ class PushActivationStateMachineTests: XCTestCase { func test__the_local_device_has_id_and_deviceIdentityToken__emits_a_SyncRegistrationFailed_event_with_code_61002_if_client_IDs_don_t_match() { contextBeforeEach?() + let localDeviceFetcher = MockLocalDeviceFetcher() + let options = ARTClientOptions(key: "xxxx:xxxx") options.clientId = "deviceClient" + options.testOptions.localDeviceFetcher = localDeviceFetcher let rest = ARTRest(options: options) rest.internal.storage = storage XCTAssertEqual(rest.device.clientId, "deviceClient") let newOptions = ARTClientOptions(key: "xxxx:xxxx") newOptions.clientId = "instanceClient" + newOptions.testOptions.localDeviceFetcher = localDeviceFetcher let newRest = ARTRest(options: newOptions) newRest.internal.storage = storage let stateMachine = ARTPushActivationStateMachine(rest: newRest.internal, delegate: StateMachineDelegate(), logger: .init(core: MockInternalLogCore())) diff --git a/Test/Tests/PushChannelTests.swift b/Test/Tests/PushChannelTests.swift index 6107f7420..a357c5cbc 100644 --- a/Test/Tests/PushChannelTests.swift +++ b/Test/Tests/PushChannelTests.swift @@ -24,10 +24,10 @@ class PushChannelTests: XCTestCase { userQueue = AblyTests.createUserQueue() options.dispatchQueue = userQueue options.internalDispatchQueue = AblyTests.queue + options.testOptions.localDeviceFetcher = MockLocalDeviceFetcher() rest = ARTRest(options: options) rest.internal.options.clientId = "tester" rest.internal.httpExecutor = mockHttpExecutor - DefaultLocalDeviceFetcher.sharedInstance.resetDevice() } // RSH7 diff --git a/Test/Tests/PushTests.swift b/Test/Tests/PushTests.swift index cbb429c31..3ff691a79 100644 --- a/Test/Tests/PushTests.swift +++ b/Test/Tests/PushTests.swift @@ -27,8 +27,10 @@ class PushTests: XCTestCase { override func setUp() { super.setUp() - rest = ARTRest(key: "xxxx:xxxx") - DefaultLocalDeviceFetcher.sharedInstance.resetDevice() + let options = ARTClientOptions(key: "xxxx:xxxx") + options.testOptions.localDeviceFetcher = MockLocalDeviceFetcher() + + rest = ARTRest(options: options) mockHttpExecutor = MockHTTPExecutor() rest.internal.httpExecutor = mockHttpExecutor storage = MockDeviceStorage() @@ -126,8 +128,6 @@ class PushTests: XCTestCase { let storage = MockDeviceStorage() rest.internal.storage = storage - DefaultLocalDeviceFetcher.sharedInstance.resetDevice() - var stateMachine: ARTPushActivationStateMachine! waitUntil(timeout: testTimeout) { done in rest.push.internal.getActivationMachine { machine in @@ -203,8 +203,11 @@ class PushTests: XCTestCase { // RSH8 func test__008__LocalDevice__has_a_device_method_that_returns_a_LocalDevice() { - let _: ARTLocalDevice = ARTRest(key: "fake:key").device - let _: ARTLocalDevice = ARTRealtime(key: "fake:key").device + let options = ARTClientOptions(key: "fake:key") + options.testOptions.localDeviceFetcher = MockLocalDeviceFetcher() + + let _: ARTLocalDevice = ARTRest(options: options).device + let _: ARTLocalDevice = ARTRealtime(options: options).device } // RSH8a @@ -218,7 +221,10 @@ class PushTests: XCTestCase { clientId: "" ) - let rest = ARTRest(key: "fake:key") + let options = ARTClientOptions(key: "fake:key") + options.testOptions.localDeviceFetcher = MockLocalDeviceFetcher() + + let rest = ARTRest(options: options) rest.internal.storage = storage storage.simulateOnNextRead(string: testToken, for: ARTAPNSDeviceTokenKey) storage.simulateOnNextRead(data: testIdentity.archive(withLogger: nil), for: ARTDeviceIdentityTokenKey) @@ -239,6 +245,7 @@ class PushTests: XCTestCase { callback(ARTTokenDetails(token: "fake:token", expires: nil, issued: nil, capability: nil, clientId: "testClient"), nil) } } + options.testOptions.localDeviceFetcher = MockLocalDeviceFetcher() let realtime = ARTRealtime(options: options) XCTAssertNil(realtime.device.clientId) @@ -258,6 +265,7 @@ class PushTests: XCTestCase { let options = ARTClientOptions(key: "fake:key") options.autoConnect = false options.testOptions.transportFactory = TestProxyTransportFactory() + options.testOptions.localDeviceFetcher = MockLocalDeviceFetcher() let realtime = ARTRealtime(options: options) XCTAssertNil(realtime.device.clientId) @@ -295,6 +303,7 @@ class PushTests: XCTestCase { callback(ARTTokenDetails(token: "fake:token", expires: nil, issued: nil, capability: nil, clientId: expectedClient), nil) } } + options.testOptions.localDeviceFetcher = MockLocalDeviceFetcher() let realtime = ARTRealtime(options: options) let mockHttpExecutor = MockHTTPExecutor()