Skip to content

Commit

Permalink
* Removed retain cycles in SubscribeRouterTests
Browse files Browse the repository at this point in the history
* Fixes in unit tests after enabling EE by default
  • Loading branch information
jguz-pubnub committed Feb 20, 2024
1 parent 36dcec4 commit 50654d9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
10 changes: 8 additions & 2 deletions Tests/PubNubTests/Integration/SubscriptionIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class SubscriptionIntegrationTests: XCTestCase {
let configuration = PubNubConfiguration(
publishKey: "",
subscribeKey: "",
userId: UUID().uuidString
userId: UUID().uuidString,
enableEventEngine: false
)
let eeConfiguration = PubNubConfiguration(
publishKey: "",
Expand All @@ -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
Expand All @@ -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")
}
Expand Down
21 changes: 10 additions & 11 deletions Tests/PubNubTests/Networking/Routers/SubscribeRouterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
}
Expand All @@ -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)
Expand All @@ -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")
}
Expand All @@ -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)
Expand All @@ -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")
}
Expand All @@ -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)
Expand Down
12 changes: 10 additions & 2 deletions Tests/PubNubTests/Subscription/SubscriptionSessionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit 50654d9

Please sign in to comment.