Skip to content

Commit

Permalink
Enhance contributor mock’s state change handling
Browse files Browse the repository at this point in the history
Add an option for it to emit a state change when you subscribe to its
state. Will use in an upcoming commit.
  • Loading branch information
lawrence-forooghian committed Nov 18, 2024
1 parent 25847e4 commit fd22099
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 4 additions & 2 deletions Tests/AblyChatTests/DefaultRoomLifecycleManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,16 @@ struct DefaultRoomLifecycleManagerTests {
initialState: ARTRealtimeChannelState = .initialized,
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
detachBehavior: MockRoomLifecycleContributorChannel.AttachOrDetachBehavior? = nil,
subscribeToStateBehavior: MockRoomLifecycleContributorChannel.SubscribeToStateBehavior? = nil
) -> MockRoomLifecycleContributor {
.init(
feature: feature,
channel: .init(
initialState: initialState,
attachBehavior: attachBehavior,
detachBehavior: detachBehavior
detachBehavior: detachBehavior,
subscribeToStateBehavior: subscribeToStateBehavior
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
final actor MockRoomLifecycleContributorChannel: RoomLifecycleContributorChannel {
private let attachBehavior: AttachOrDetachBehavior?
private let detachBehavior: AttachOrDetachBehavior?
private let subscribeToStateBehavior: SubscribeToStateBehavior

var state: ARTRealtimeChannelState
var errorReason: ARTErrorInfo?
Expand All @@ -16,11 +17,13 @@ final actor MockRoomLifecycleContributorChannel: RoomLifecycleContributorChannel
init(
initialState: ARTRealtimeChannelState,
attachBehavior: AttachOrDetachBehavior?,
detachBehavior: AttachOrDetachBehavior?
detachBehavior: AttachOrDetachBehavior?,
subscribeToStateBehavior: SubscribeToStateBehavior?
) {
state = initialState
self.attachBehavior = attachBehavior
self.detachBehavior = detachBehavior
self.subscribeToStateBehavior = subscribeToStateBehavior ?? .justAddSubscription
}

enum AttachOrDetachResult {
Expand Down Expand Up @@ -52,6 +55,11 @@ final actor MockRoomLifecycleContributorChannel: RoomLifecycleContributorChannel
}
}

enum SubscribeToStateBehavior {
case justAddSubscription
case addSubscriptionAndEmitStateChange(ARTChannelStateChange)
}

func attach() async throws(ARTErrorInfo) {
attachCallCount += 1

Expand Down Expand Up @@ -100,6 +108,14 @@ final actor MockRoomLifecycleContributorChannel: RoomLifecycleContributorChannel
func subscribeToState() -> Subscription<ARTChannelStateChange> {
let subscription = Subscription<ARTChannelStateChange>(bufferingPolicy: .unbounded)
subscriptions.append(subscription)

switch subscribeToStateBehavior {
case .justAddSubscription:
break
case let .addSubscriptionAndEmitStateChange(stateChange):
emitStateChange(stateChange)
}

return subscription
}

Expand Down

0 comments on commit fd22099

Please sign in to comment.