Skip to content

Commit

Permalink
Moved helpers to dedicated structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
maratal committed Jan 12, 2025
1 parent c544caa commit 444de76
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 53 deletions.
65 changes: 12 additions & 53 deletions Tests/AblyChatTests/DefaultRoomPresenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,6 @@ import Ably
import Testing

struct DefaultRoomPresenceTests {
// MARK: - Test helpers

let fakeNetworkDelay: UInt64 = 10 // milliseconds; without this delay (or with a very low value such as 1) most of the time attach happens before lifecycleManager has a chance to start waiting.

private func createManager(
forTestingWhatHappensWhenCurrentlyIn status: DefaultRoomLifecycleManager<MockRoomLifecycleContributor>.Status? = nil,
forTestingWhatHappensWhenHasPendingDiscontinuityEvents pendingDiscontinuityEvents: [MockRoomLifecycleContributor.ID: DiscontinuityEvent]? = nil,
forTestingWhatHappensWhenHasTransientDisconnectTimeoutForTheseContributorIDs idsOfContributorsWithTransientDisconnectTimeout: Set<MockRoomLifecycleContributor.ID>? = nil,
contributors: [MockRoomLifecycleContributor] = [],
clock: SimpleClock = MockSimpleClock()
) async -> DefaultRoomLifecycleManager<MockRoomLifecycleContributor> {
await .init(
testsOnly_status: status,
testsOnly_pendingDiscontinuityEvents: pendingDiscontinuityEvents,
testsOnly_idsOfContributorsWithTransientDisconnectTimeout: idsOfContributorsWithTransientDisconnectTimeout,
contributors: contributors,
logger: TestLogger(),
clock: clock
)
}

private func createContributor(
initialState: ARTRealtimeChannelState = .initialized,
initialErrorReason: ARTErrorInfo? = nil,
feature: RoomFeature = .messages, // Arbitrarily chosen, its value only matters in test cases where we check which error is thrown
attachBehavior: MockRoomLifecycleContributorChannel.AttachOrDetachBehavior? = nil,
detachBehavior: MockRoomLifecycleContributorChannel.AttachOrDetachBehavior? = nil,
subscribeToStateBehavior: MockRoomLifecycleContributorChannel.SubscribeToStateBehavior? = nil
) -> MockRoomLifecycleContributor {
.init(
feature: feature,
channel: .init(
initialState: initialState,
initialErrorReason: initialErrorReason,
attachBehavior: attachBehavior,
detachBehavior: detachBehavior,
subscribeToStateBehavior: subscribeToStateBehavior
)
)
}

// MARK: CHA-PR1

// @spec CHA-PR1
Expand Down Expand Up @@ -88,8 +47,8 @@ struct DefaultRoomPresenceTests {
@Test
func usersMayEnterPresenceWhileAttaching() async throws {
// Given: A DefaultRoomLifecycleManager, with an ATTACH operation in progress and hence in the ATTACHING status
let contributor = createContributor(feature: .presence, attachBehavior: .completeAndChangeState(.success, newState: .attached, delayInMilliseconds: fakeNetworkDelay))
let lifecycleManager = await createManager(contributors: [contributor])
let contributor = RoomLifecycleHelper.createContributor(feature: .presence, attachBehavior: .completeAndChangeState(.success, newState: .attached, delayInMilliseconds: RoomLifecycleHelper.fakeNetworkDelay))
let lifecycleManager = await RoomLifecycleHelper.createManager(contributors: [contributor])

// Given: A DefaultPresence with DefaultFeatureChannel and MockRoomLifecycleContributor
let realtimePresence = MockRealtimePresence(["client1"].map { .init(clientId: $0) })
Expand Down Expand Up @@ -122,8 +81,8 @@ struct DefaultRoomPresenceTests {
let attachError = ARTErrorInfo(domain: "SomeDomain", code: 123)

// Given: A DefaultRoomLifecycleManager, with an ATTACH operation in progress and hence in the ATTACHING status
let contributor = createContributor(feature: .presence, attachBehavior: .completeAndChangeState(.failure(attachError), newState: .failed, delayInMilliseconds: fakeNetworkDelay)) // Without this delay most of the time attach fail happens before lifecycleManager has a chance to start waiting. I tried to use SignallableChannelOperation, but looks like `await #expect(...)` doesn't understand `let async x/try await x` syntax.
let lifecycleManager = await createManager(contributors: [contributor])
let contributor = RoomLifecycleHelper.createContributor(feature: .presence, attachBehavior: .completeAndChangeState(.failure(attachError), newState: .failed, delayInMilliseconds: RoomLifecycleHelper.fakeNetworkDelay)) // Without this delay most of the time attach fail happens before lifecycleManager has a chance to start waiting. I tried to use SignallableChannelOperation, but looks like `await #expect(...)` doesn't understand `let async x/try await x` syntax.
let lifecycleManager = await RoomLifecycleHelper.createManager(contributors: [contributor])

// Given: A DefaultPresence with DefaultFeatureChannel and MockRoomLifecycleContributor
let realtimePresence = MockRealtimePresence(["client1"].map { .init(clientId: $0) })
Expand Down Expand Up @@ -208,8 +167,8 @@ struct DefaultRoomPresenceTests {
@Test
func usersMayUpdatePresenceWhileAttaching() async throws {
// Given: A DefaultRoomLifecycleManager, with an ATTACH operation in progress and hence in the ATTACHING status
let contributor = createContributor(feature: .presence, attachBehavior: .completeAndChangeState(.success, newState: .attached, delayInMilliseconds: fakeNetworkDelay))
let lifecycleManager = await createManager(contributors: [contributor])
let contributor = RoomLifecycleHelper.createContributor(feature: .presence, attachBehavior: .completeAndChangeState(.success, newState: .attached, delayInMilliseconds: RoomLifecycleHelper.fakeNetworkDelay))
let lifecycleManager = await RoomLifecycleHelper.createManager(contributors: [contributor])

// Given: A DefaultPresence with DefaultFeatureChannel and MockRoomLifecycleContributor
let realtimePresence = MockRealtimePresence(["client1"].map { .init(clientId: $0) })
Expand Down Expand Up @@ -242,8 +201,8 @@ struct DefaultRoomPresenceTests {
let attachError = ARTErrorInfo(domain: "SomeDomain", code: 123)

// Given: A DefaultRoomLifecycleManager, with an ATTACH operation in progress and hence in the ATTACHING status
let contributor = createContributor(feature: .presence, attachBehavior: .completeAndChangeState(.failure(attachError), newState: .failed, delayInMilliseconds: fakeNetworkDelay))
let lifecycleManager = await createManager(contributors: [contributor])
let contributor = RoomLifecycleHelper.createContributor(feature: .presence, attachBehavior: .completeAndChangeState(.failure(attachError), newState: .failed, delayInMilliseconds: RoomLifecycleHelper.fakeNetworkDelay))
let lifecycleManager = await RoomLifecycleHelper.createManager(contributors: [contributor])

// Given: A DefaultPresence with DefaultFeatureChannel and MockRoomLifecycleContributor
let realtimePresence = MockRealtimePresence(["client1"].map { .init(clientId: $0) })
Expand Down Expand Up @@ -386,8 +345,8 @@ struct DefaultRoomPresenceTests {
@Test
func retrieveAllTheMembersOfThePresenceSetWhileAttaching() async throws {
// Given: A DefaultRoomLifecycleManager, with an ATTACH operation in progress and hence in the ATTACHING status
let contributor = createContributor(feature: .presence, attachBehavior: .completeAndChangeState(.success, newState: .attached, delayInMilliseconds: fakeNetworkDelay))
let lifecycleManager = await createManager(contributors: [contributor])
let contributor = RoomLifecycleHelper.createContributor(feature: .presence, attachBehavior: .completeAndChangeState(.success, newState: .attached, delayInMilliseconds: RoomLifecycleHelper.fakeNetworkDelay))
let lifecycleManager = await RoomLifecycleHelper.createManager(contributors: [contributor])

// Given: A DefaultPresence with DefaultFeatureChannel and MockRoomLifecycleContributor
let realtimePresence = MockRealtimePresence(["client1"].map { .init(clientId: $0) })
Expand Down Expand Up @@ -420,8 +379,8 @@ struct DefaultRoomPresenceTests {
let attachError = ARTErrorInfo(domain: "SomeDomain", code: 123)

// Given: A DefaultRoomLifecycleManager, with an ATTACH operation in progress and hence in the ATTACHING status
let contributor = createContributor(feature: .presence, attachBehavior: .completeAndChangeState(.failure(attachError), newState: .failed, delayInMilliseconds: fakeNetworkDelay))
let lifecycleManager = await createManager(contributors: [contributor])
let contributor = RoomLifecycleHelper.createContributor(feature: .presence, attachBehavior: .completeAndChangeState(.failure(attachError), newState: .failed, delayInMilliseconds: RoomLifecycleHelper.fakeNetworkDelay))
let lifecycleManager = await RoomLifecycleHelper.createManager(contributors: [contributor])

// Given: A DefaultPresence with DefaultFeatureChannel and MockRoomLifecycleContributor
let realtimePresence = MockRealtimePresence(["client1"].map { .init(clientId: $0) })
Expand Down
42 changes: 42 additions & 0 deletions Tests/AblyChatTests/Helpers/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,45 @@ extension Array where Element == PresenceEventType {
PresenceEventType.update
]
}

struct RoomLifecycleHelper {

static let fakeNetworkDelay: UInt64 = 10 // milliseconds; without this delay (or with a very low value such as 1) most of the time attach happens before lifecycleManager has a chance to start waiting.

static func createManager(
forTestingWhatHappensWhenCurrentlyIn status: DefaultRoomLifecycleManager<MockRoomLifecycleContributor>.Status? = nil,
forTestingWhatHappensWhenHasPendingDiscontinuityEvents pendingDiscontinuityEvents: [MockRoomLifecycleContributor.ID: DiscontinuityEvent]? = nil,
forTestingWhatHappensWhenHasTransientDisconnectTimeoutForTheseContributorIDs idsOfContributorsWithTransientDisconnectTimeout: Set<MockRoomLifecycleContributor.ID>? = nil,
contributors: [MockRoomLifecycleContributor] = [],
clock: SimpleClock = MockSimpleClock()
) async -> DefaultRoomLifecycleManager<MockRoomLifecycleContributor> {
await .init(
testsOnly_status: status,
testsOnly_pendingDiscontinuityEvents: pendingDiscontinuityEvents,
testsOnly_idsOfContributorsWithTransientDisconnectTimeout: idsOfContributorsWithTransientDisconnectTimeout,
contributors: contributors,
logger: TestLogger(),
clock: clock
)
}

static func createContributor(
initialState: ARTRealtimeChannelState = .initialized,
initialErrorReason: ARTErrorInfo? = nil,
feature: RoomFeature = .messages, // Arbitrarily chosen, its value only matters in test cases where we check which error is thrown
attachBehavior: MockRoomLifecycleContributorChannel.AttachOrDetachBehavior? = nil,
detachBehavior: MockRoomLifecycleContributorChannel.AttachOrDetachBehavior? = nil,
subscribeToStateBehavior: MockRoomLifecycleContributorChannel.SubscribeToStateBehavior? = nil
) -> MockRoomLifecycleContributor {
.init(
feature: feature,
channel: .init(
initialState: initialState,
initialErrorReason: initialErrorReason,
attachBehavior: attachBehavior,
detachBehavior: detachBehavior,
subscribeToStateBehavior: subscribeToStateBehavior
)
)
}
}

0 comments on commit 444de76

Please sign in to comment.