From 415810bf9347dc876d19cc38c12efccdd2bd955a Mon Sep 17 00:00:00 2001 From: Marat Al Date: Mon, 4 Dec 2023 01:28:12 +0100 Subject: [PATCH] Injecting fake LEAVE message to avoid waiting 15 seconds for the real one from the server. --- Test/Test Utilities/TestUtilities.swift | 10 ++++------ Test/Tests/RealtimeClientPresenceTests.swift | 11 +++++++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Test/Test Utilities/TestUtilities.swift b/Test/Test Utilities/TestUtilities.swift index 5f9b75d26..fba40151b 100644 --- a/Test/Test Utilities/TestUtilities.swift +++ b/Test/Test Utilities/TestUtilities.swift @@ -165,16 +165,14 @@ class AblyTests { return protocolMessage } - class func newPresenceProtocolMessage(_ channel: String, action: ARTPresenceAction, clientId: String) -> ARTProtocolMessage { + class func newPresenceProtocolMessage(id: String, channel: String, action: ARTPresenceAction, clientId: String, connectionId: String) -> ARTProtocolMessage { let protocolMessage = ARTProtocolMessage() protocolMessage.action = .presence protocolMessage.channel = channel protocolMessage.timestamp = Date() - let presenceMessage = ARTPresenceMessage() - presenceMessage.action = action - presenceMessage.clientId = clientId - presenceMessage.timestamp = Date() - protocolMessage.presence = [presenceMessage] + protocolMessage.presence = [ + ARTPresenceMessage(clientId: clientId, action: action, connectionId: connectionId, id: id, timestamp: Date()) + ] return protocolMessage } diff --git a/Test/Tests/RealtimeClientPresenceTests.swift b/Test/Tests/RealtimeClientPresenceTests.swift index ce00aea4f..d30a2bac4 100644 --- a/Test/Tests/RealtimeClientPresenceTests.swift +++ b/Test/Tests/RealtimeClientPresenceTests.swift @@ -774,6 +774,8 @@ class RealtimeClientPresenceTests: XCTestCase { let leavesChannel = leavesClient.channels.get(channelName) let mainChannel = mainClient.channels.get(channelName) + var oldConnectionId = "" + waitUntil(timeout: testTimeout) { done in let partialDone = AblyTests.splitDone(4, done: done) mainChannel.presence.subscribe { message in @@ -788,6 +790,7 @@ class RealtimeClientPresenceTests: XCTestCase { } mainChannel.presence.enter(nil) { error in XCTAssertNil(error) + oldConnectionId = mainChannel.internal.connectionId partialDone() } leavesChannel.presence.enter(nil) { error in @@ -815,6 +818,7 @@ class RealtimeClientPresenceTests: XCTestCase { waitUntil(timeout: testTimeout) { done in let partialDone = AblyTests.splitDone(4, done: done) mainChannel.presence.subscribe { presence in + guard presence.clientId != mainClient.clientId else { return } // ignore events from themselves presenceEvents += [presence] delay(1) { partialDone() // Wait a bit to make sure we don't receive any other presence messages @@ -844,6 +848,13 @@ class RealtimeClientPresenceTests: XCTestCase { mainChannel.presence.unsubscribe() + guard let transport = mainClient.internal.transport as? TestProxyTransport else { + fail("TestProxyTransport is not set"); return + } + + // Same can be achieved with sleep for more than 15 seconds for the Realtime to send LEAVE for the main client + transport.receive(AblyTests.newPresenceProtocolMessage(id: "\(mainChannel.internal.connectionId):0:0", channel: mainChannel.name, action: .leave, clientId: mainClient.clientId!, connectionId: oldConnectionId)) + waitUntil(timeout: testTimeout) { done in mainChannel.presence.get { members, error in XCTAssertNil(error)