From 50654d976aa8e7f65d095198558c5282c85a51e1 Mon Sep 17 00:00:00 2001 From: jguz-pubnub Date: Tue, 20 Feb 2024 18:38:35 +0100 Subject: [PATCH] * Removed retain cycles in SubscribeRouterTests * Fixes in unit tests after enabling EE by default --- .../SubscriptionIntegrationTests.swift | 10 +++++++-- .../Routers/SubscribeRouterTests.swift | 21 +++++++++---------- .../SubscriptionSessionTests.swift | 12 +++++++++-- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/Tests/PubNubTests/Integration/SubscriptionIntegrationTests.swift b/Tests/PubNubTests/Integration/SubscriptionIntegrationTests.swift index f3b356d5..b0ab938c 100644 --- a/Tests/PubNubTests/Integration/SubscriptionIntegrationTests.swift +++ b/Tests/PubNubTests/Integration/SubscriptionIntegrationTests.swift @@ -19,7 +19,8 @@ class SubscriptionIntegrationTests: XCTestCase { let configuration = PubNubConfiguration( publishKey: "", subscribeKey: "", - userId: UUID().uuidString + userId: UUID().uuidString, + enableEventEngine: false ) let eeConfiguration = PubNubConfiguration( publishKey: "", @@ -32,10 +33,13 @@ class SubscriptionIntegrationTests: XCTestCase { XCTContext.runActivity(named: "Testing configuration with enableEventEngine=\(config.enableEventEngine)") { _ in let subscribeExpect = expectation(description: "Subscribe Expectation") let connectingExpect = expectation(description: "Connecting Expectation") + let disconnectedExpect = expectation(description: "Disconnected Expectation") + disconnectedExpect.assertForOverFulfill = true + disconnectedExpect.expectedFulfillmentCount = 1 // Should return subscription key error - let pubnub = PubNub(configuration: configuration) + let pubnub = PubNub(configuration: config) let listener = SubscriptionListener() listener.didReceiveSubscription = { event in @@ -46,6 +50,8 @@ class SubscriptionIntegrationTests: XCTestCase { connectingExpect.fulfill() case .disconnectedUnexpectedly: disconnectedExpect.fulfill() + case .connectionError(_): + disconnectedExpect.fulfill() default: XCTFail("Only should emit these two states") } diff --git a/Tests/PubNubTests/Networking/Routers/SubscribeRouterTests.swift b/Tests/PubNubTests/Networking/Routers/SubscribeRouterTests.swift index ec7b7a2f..8c53a1e9 100644 --- a/Tests/PubNubTests/Networking/Routers/SubscribeRouterTests.swift +++ b/Tests/PubNubTests/Networking/Routers/SubscribeRouterTests.swift @@ -16,7 +16,8 @@ final class SubscribeRouterTests: XCTestCase { let config = PubNubConfiguration( publishKey: "FakeTestString", subscribeKey: "FakeTestString", - userId: UUID().uuidString + userId: UUID().uuidString, + enableEventEngine: false ) let eeEnabledConfig = PubNubConfiguration( publishKey: "FakeTestString", @@ -514,7 +515,7 @@ extension SubscribeRouterTests { } } - listener.didReceiveObjectMetadataEvent = { event in + listener.didReceiveObjectMetadataEvent = { [unowned subscription] event in switch event { case let .setChannel(changeset): XCTAssertEqual(changeset.metadataId, "TestSpaceID") @@ -712,7 +713,7 @@ extension SubscribeRouterTests { } } - listener.didReceiveObjectMetadataEvent = { event in + listener.didReceiveObjectMetadataEvent = { [unowned subscription] event in switch event { case let .removedMembership(membership): XCTAssertEqual(try? membership.transcode(), testMembership) @@ -1102,7 +1103,7 @@ extension SubscribeRouterTests { let messageExpect = XCTestExpectation(description: "Message Event") guard let session = try? MockURLSession.mockSession( - for: ["subscription_message_success", "cancelled"] + for: ["subscription_handshake_success", "subscription_message_success", "cancelled"] ).session else { return XCTFail("Could not create mock url session") } @@ -1117,10 +1118,9 @@ extension SubscribeRouterTests { configuration: config, subscribeSession: session ) - let listener = SubscriptionListener() - listener.didReceiveMessage = { [weak self, pubNubWithMockedSession] message in + listener.didReceiveMessage = { [weak self, unowned pubNubWithMockedSession] message in XCTAssertEqual(message.channel, self?.testChannel) XCTAssertEqual(message.payload.stringOptional, "Test Message") XCTAssertTrue(message.error?.reason == .decryptionFailure) @@ -1139,7 +1139,7 @@ extension SubscribeRouterTests { let messageExpect = XCTestExpectation(description: "Message Event") guard let session = try? MockURLSession.mockSession( - for: ["subscription_encrypted_message_success", "cancelled"] + for: ["subscription_handshake_success", "subscription_encrypted_message_success", "cancelled"] ).session else { return XCTFail("Could not create mock url session") } @@ -1154,10 +1154,9 @@ extension SubscribeRouterTests { configuration: config, subscribeSession: session ) - let listener = SubscriptionListener() - listener.didReceiveMessage = { [weak self, pubNubWithMockedSession] message in + listener.didReceiveMessage = { [weak self, unowned pubNubWithMockedSession] message in XCTAssertEqual(message.channel, self?.testChannel) XCTAssertEqual(message.payload.stringOptional, "Test Message") XCTAssertNil(message.error) @@ -1176,7 +1175,7 @@ extension SubscribeRouterTests { let messageExpect = XCTestExpectation(description: "Message Event") guard let session = try? MockURLSession.mockSession( - for: ["subscription_encrypted_message_success", "cancelled"] + for: ["subscription_handshake_success", "subscription_encrypted_message_success", "cancelled"] ).session else { return XCTFail("Could not create mock url session") } @@ -1194,7 +1193,7 @@ extension SubscribeRouterTests { let listener = SubscriptionListener() - listener.didReceiveMessage = { [weak self, pubNubWithMockedSession] message in + listener.didReceiveMessage = { [weak self, unowned pubNubWithMockedSession] message in XCTAssertEqual(message.channel, self?.testChannel) XCTAssertEqual(message.payload.stringOptional, "UE5FRAFBQ1JIEGOmGQMIMXD+91V+5hTxm7p7uEUhEEYohYLQz5fEGITC") XCTAssertTrue(message.error?.reason == .decryptionFailure) diff --git a/Tests/PubNubTests/Subscription/SubscriptionSessionTests.swift b/Tests/PubNubTests/Subscription/SubscriptionSessionTests.swift index 9c2192bf..6ff2a66f 100644 --- a/Tests/PubNubTests/Subscription/SubscriptionSessionTests.swift +++ b/Tests/PubNubTests/Subscription/SubscriptionSessionTests.swift @@ -92,7 +92,9 @@ class SubscriptionSessionTests: XCTestCase { for configuration in [config, eeEnabledConfig] { XCTContext.runActivity(named: "Testing with enableEventEngine=\(configuration.enableEventEngine)") { _ in let statusExpect = XCTestExpectation(description: "Status Event") - + statusExpect.assertForOverFulfill = true + statusExpect.expectedFulfillmentCount = configuration.enableEventEngine ? 2 : 1 + guard let session = try? MockURLSession.mockSession( for: ["badURL", "cancelled"] ).session else { @@ -102,11 +104,17 @@ class SubscriptionSessionTests: XCTestCase { let subscription = SubscribeSessionFactory.shared.getSession(from: config, with: session) let listener = SubscriptionListener() - listener.didReceiveStatus = { status in + listener.didReceiveStatus = { [unowned subscription] status in if case .failure(_) = status { XCTAssertNil(subscription.previousTokenResponse) statusExpect.fulfill() } + if case .success(let newStatus) = status { + if newStatus == .connectionError(PubNubError(.invalidURL)) { + XCTAssertNil(subscription.previousTokenResponse) + statusExpect.fulfill() + } + } } subscription.add(listener)