From cba991da07d86f4e8ab61b827df07fd674b2d80e Mon Sep 17 00:00:00 2001 From: Lawrence Forooghian Date: Tue, 1 Oct 2024 11:17:03 -0300 Subject: [PATCH] Change room lifecycle contributor into a protocol MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- Sources/AblyChat/RoomLifecycleManager.swift | 15 ++++++++------- .../Mocks/MockRoomLifecycleContributor.swift | 11 +++++++++++ .../AblyChatTests/RoomLifecycleManagerTests.swift | 6 +++--- 3 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 Tests/AblyChatTests/Mocks/MockRoomLifecycleContributor.swift diff --git a/Sources/AblyChat/RoomLifecycleManager.swift b/Sources/AblyChat/RoomLifecycleManager.swift index 845de196..84c656e3 100644 --- a/Sources/AblyChat/RoomLifecycleManager.swift +++ b/Sources/AblyChat/RoomLifecycleManager.swift @@ -13,15 +13,16 @@ internal protocol RoomLifecycleContributorChannel: Sendable { var errorReason: ARTErrorInfo? { get async } } -internal actor RoomLifecycleManager { - /// 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 { internal private(set) var current: RoomLifecycle internal private(set) var error: ARTErrorInfo? diff --git a/Tests/AblyChatTests/Mocks/MockRoomLifecycleContributor.swift b/Tests/AblyChatTests/Mocks/MockRoomLifecycleContributor.swift new file mode 100644 index 00000000..d1a0d2af --- /dev/null +++ b/Tests/AblyChatTests/Mocks/MockRoomLifecycleContributor.swift @@ -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 + } +} diff --git a/Tests/AblyChatTests/RoomLifecycleManagerTests.swift b/Tests/AblyChatTests/RoomLifecycleManagerTests.swift index e69f7118..e2c477b8 100644 --- a/Tests/AblyChatTests/RoomLifecycleManagerTests.swift +++ b/Tests/AblyChatTests/RoomLifecycleManagerTests.swift @@ -29,9 +29,9 @@ struct RoomLifecycleManagerTests { private func createManager( forTestingWhatHappensWhenCurrentlyIn current: RoomLifecycle? = nil, - contributors: [RoomLifecycleManager.Contributor] = [], + contributors: [MockRoomLifecycleContributor] = [], clock: SimpleClock = MockSimpleClock() - ) async -> RoomLifecycleManager { + ) async -> RoomLifecycleManager { await .init( testsOnly_current: current, contributors: contributors, @@ -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.Contributor { + ) -> MockRoomLifecycleContributor { .init( feature: feature, channel: .init(