Skip to content

Commit

Permalink
Injecting fake LEAVE message to avoid waiting 15 seconds for the real…
Browse files Browse the repository at this point in the history
… one from the server.
  • Loading branch information
maratal committed Dec 4, 2023
1 parent 900708a commit 415810b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
10 changes: 4 additions & 6 deletions Test/Test Utilities/TestUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
11 changes: 11 additions & 0 deletions Test/Tests/RealtimeClientPresenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 415810b

Please sign in to comment.