Skip to content

Commit

Permalink
Allow mock attach/detach function to transition state
Browse files Browse the repository at this point in the history
Will use in an upcoming commit.
  • Loading branch information
lawrence-forooghian committed Nov 14, 2024
1 parent 2c8c0c2 commit bcb173f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
8 changes: 4 additions & 4 deletions Tests/AblyChatTests/DefaultRoomLifecycleManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct DefaultRoomLifecycleManagerTests {
self.continuation = continuation

behavior = .fromFunction { _ in
await (stream.first { _ in true })!
await .complete((stream.first { _ in true })!)
}
}

Expand Down Expand Up @@ -445,7 +445,7 @@ struct DefaultRoomLifecycleManagerTests {
// - and for whom subsequently calling `detach` will fail on the first attempt and succeed on the second
// 1. a channel for whom calling `attach` will fail, putting it in the FAILED state (we won’t make any assertions about this channel; it’s just to trigger the room’s channel detach behaviour)

let detachResult = { @Sendable (callCount: Int) async -> MockRoomLifecycleContributorChannel.AttachOrDetachResult in
let detachResult = { @Sendable (callCount: Int) async -> MockRoomLifecycleContributorChannel.AttachOrDetachBehavior in
if callCount == 1 {
return .failure(.create(withCode: 123, message: ""))
} else {
Expand Down Expand Up @@ -648,7 +648,7 @@ struct DefaultRoomLifecycleManagerTests {
//
// - the first two times `detach` is called, it throws an error, leaving it in the ATTACHED state
// - the third time `detach` is called, it succeeds
let detachImpl = { @Sendable (callCount: Int) async -> MockRoomLifecycleContributorChannel.AttachOrDetachResult in
let detachImpl = { @Sendable (callCount: Int) async -> MockRoomLifecycleContributorChannel.AttachOrDetachBehavior in
if callCount < 3 {
return .failure(ARTErrorInfo(domain: "SomeDomain", code: 123)) // exact error is unimportant
}
Expand Down Expand Up @@ -828,7 +828,7 @@ struct DefaultRoomLifecycleManagerTests {
// Given: A DefaultRoomLifecycleManager, with a contributor for which:
// - the first two times that `detach()` is called, it fails, leaving the contributor in a non-FAILED state
// - the third time that `detach()` is called, it succeeds
let detachImpl = { @Sendable (callCount: Int) async -> MockRoomLifecycleContributorChannel.AttachOrDetachResult in
let detachImpl = { @Sendable (callCount: Int) async -> MockRoomLifecycleContributorChannel.AttachOrDetachBehavior in
if callCount < 3 {
return .failure(ARTErrorInfo(domain: "SomeDomain", code: 123)) // exact error is unimportant
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final actor MockRoomLifecycleContributorChannel: RoomLifecycleContributorChannel

enum AttachOrDetachBehavior {
/// Receives an argument indicating how many times (including the current call) the method for which this is providing a mock implementation has been called.
case fromFunction(@Sendable (Int) async -> AttachOrDetachResult)
case fromFunction(@Sendable (Int) async -> AttachOrDetachBehavior)
case complete(AttachOrDetachResult)
case completeAndChangeState(AttachOrDetachResult, newState: ARTRealtimeChannelState)

Expand Down Expand Up @@ -76,7 +76,9 @@ final actor MockRoomLifecycleContributorChannel: RoomLifecycleContributorChannel
let result: AttachOrDetachResult
switch behavior {
case let .fromFunction(function):
result = await function(callCount)
let behavior = await function(callCount)
try await performBehavior(behavior, callCount: callCount)
return
case let .complete(completeResult):
result = completeResult
case let .completeAndChangeState(completeResult, newState):
Expand Down

0 comments on commit bcb173f

Please sign in to comment.