Skip to content

Commit

Permalink
Change room lifecycle contributor into a protocol
Browse files Browse the repository at this point in the history
For an upcoming feature, I’m going to want to add methods to the
contributor itself, and hence want to be able to mock it.
  • Loading branch information
lawrence-forooghian committed Oct 8, 2024
1 parent e3342c5 commit cba991d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
15 changes: 8 additions & 7 deletions Sources/AblyChat/RoomLifecycleManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ internal protocol RoomLifecycleContributorChannel: Sendable {
var errorReason: ARTErrorInfo? { get async }
}

internal actor RoomLifecycleManager<Channel: RoomLifecycleContributorChannel> {
/// A realtime channel that contributes to the room lifecycle.
internal struct Contributor {
/// The room feature that this contributor corresponds to. Used only for choosing which error to throw when a contributor operation fails.
internal var feature: RoomFeature
/// A realtime channel that contributes to the room lifecycle.
internal protocol RoomLifecycleContributor: Sendable {
associatedtype Channel: RoomLifecycleContributorChannel

internal var channel: Channel
}
/// The room feature that this contributor corresponds to. Used only for choosing which error to throw when a contributor operation fails.
var feature: RoomFeature { get }
var channel: Channel { get }
}

internal actor RoomLifecycleManager<Contributor: RoomLifecycleContributor> {
internal private(set) var current: RoomLifecycle
internal private(set) var error: ARTErrorInfo?

Expand Down
11 changes: 11 additions & 0 deletions Tests/AblyChatTests/Mocks/MockRoomLifecycleContributor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@testable import AblyChat

actor MockRoomLifecycleContributor: RoomLifecycleContributor {
nonisolated let feature: RoomFeature
nonisolated let channel: MockRoomLifecycleContributorChannel

init(feature: RoomFeature, channel: MockRoomLifecycleContributorChannel) {
self.feature = feature
self.channel = channel
}
}
6 changes: 3 additions & 3 deletions Tests/AblyChatTests/RoomLifecycleManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ struct RoomLifecycleManagerTests {

private func createManager(
forTestingWhatHappensWhenCurrentlyIn current: RoomLifecycle? = nil,
contributors: [RoomLifecycleManager<MockRoomLifecycleContributorChannel>.Contributor] = [],
contributors: [MockRoomLifecycleContributor] = [],
clock: SimpleClock = MockSimpleClock()
) async -> RoomLifecycleManager<MockRoomLifecycleContributorChannel> {
) async -> RoomLifecycleManager<MockRoomLifecycleContributor> {
await .init(
testsOnly_current: current,
contributors: contributors,
Expand All @@ -45,7 +45,7 @@ struct RoomLifecycleManagerTests {
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
) -> RoomLifecycleManager<MockRoomLifecycleContributorChannel>.Contributor {
) -> MockRoomLifecycleContributor {
.init(
feature: feature,
channel: .init(
Expand Down

0 comments on commit cba991d

Please sign in to comment.