Skip to content

Commit

Permalink
Test for CHA-PR3d spec (WIP).
Browse files Browse the repository at this point in the history
  • Loading branch information
maratal committed Jan 3, 2025
1 parent 62d1e23 commit 2b6e76f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
11 changes: 11 additions & 0 deletions Sources/AblyChat/Room.swift
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,14 @@ internal actor DefaultRoom<LifecycleManagerFactory: RoomLifecycleManagerFactory>
}
}
}

#if DEBUG
extension DefaultRoom {
var testsOnly_lifecycleManager: DefaultRoomLifecycleManager<DefaultRoomLifecycleContributor> {
guard let lifecycleManager = lifecycleManager as? DefaultRoomLifecycleManager<DefaultRoomLifecycleContributor> else {
preconditionFailure("DefaultRoomLifecycleManager is expected here.")
}
return lifecycleManager
}
}
#endif
26 changes: 26 additions & 0 deletions Tests/AblyChatTests/DefaultRoomPresenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,32 @@ struct DefaultRoomPresenceTests {
}
}

// @specPartial CHA-PR3d (WIP: needs failure case)
@Test
func usersMayEnterPresenceWhileAttaching() async throws {
// Given
let realtimePresence = MockRealtimePresence(["client1"].map { .init(clientId: $0) })
let channelsList = [
MockRealtimeChannel(name: "basketball::$chat::$chatMessages", attachResult: .delayedSuccess, mockPresence: realtimePresence),
]
let channels = MockChannels(channels: channelsList)
let realtime = MockRealtime.create(channels: channels)
let room = try await DefaultRoom(realtime: realtime, chatAPI: ChatAPI(realtime: realtime), roomID: "basketball", options: .init(presence: .init()), logger: TestLogger(), lifecycleManagerFactory: DefaultRoomLifecycleManagerFactory())

let lifecycleManager = await room.testsOnly_lifecycleManager
let attachingStatusWaitSubscription = await lifecycleManager.testsOnly_subscribeToStatusChangeWaitEvents()

// When: The room is in the attaching state and presence enter is called
Task {
try await lifecycleManager.performAttachOperation()
}
try await room.presence.enter()

// Then: The manager waits for its room status to change
_ = try #require(await attachingStatusWaitSubscription.first { _ in true })
#expect(await room.status == .attached)
}

// @spec CHA-PR7a
// @spec CHA-PR7b
// @spec CHA-PR7c
Expand Down
11 changes: 10 additions & 1 deletion Tests/AblyChatTests/Mocks/MockRealtimeChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,20 @@ final class MockRealtimeChannel: NSObject, RealtimeChannelProtocol {
case success
case failure(ARTErrorInfo)

case delayedSuccess
case delayedFailure(ARTErrorInfo)

func performCallback(_ callback: ARTCallback?) {
switch self {
case .success:
callback?(nil)
case .delayedSuccess:
callback?(nil)
case let .failure(error):
sleep(10) // arbitrary
callback?(error)
case let .delayedFailure(error):
sleep(10) // arbitrary
callback?(error)
}
}
Expand Down Expand Up @@ -184,7 +193,7 @@ final class MockRealtimeChannel: NSObject, RealtimeChannelProtocol {
}

func on(_: @escaping (ARTChannelStateChange) -> Void) -> ARTEventListener {
fatalError("Not implemented")
ARTEventListener()
}

func once(_: ARTChannelEvent, callback _: @escaping (ARTChannelStateChange) -> Void) -> ARTEventListener {
Expand Down

0 comments on commit 2b6e76f

Please sign in to comment.