Skip to content

Commit

Permalink
make manager init async
Browse files Browse the repository at this point in the history
for something upcoming
  • Loading branch information
lawrence-forooghian committed Sep 26, 2024
1 parent 0e99f2e commit 367c7c9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
10 changes: 5 additions & 5 deletions Sources/AblyChat/RoomLifecycleManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ internal actor RoomLifecycleManager<Channel: RoomLifecycleContributorChannel> {
private let clock: SimpleClock
private let contributors: [Contributor]

internal init(contributors: [Contributor], logger: InternalLogger, clock: SimpleClock) {
self.init(
internal init(contributors: [Contributor], logger: InternalLogger, clock: SimpleClock) async {
await self.init(
current: nil,
contributors: contributors,
logger: logger,
Expand All @@ -39,8 +39,8 @@ internal actor RoomLifecycleManager<Channel: RoomLifecycleContributorChannel> {
}

#if DEBUG
internal init(testsOnly_current current: RoomLifecycle? = nil, contributors: [Contributor], logger: InternalLogger, clock: SimpleClock) {
self.init(
internal init(testsOnly_current current: RoomLifecycle? = nil, contributors: [Contributor], logger: InternalLogger, clock: SimpleClock) async {
await self.init(
current: current,
contributors: contributors,
logger: logger,
Expand All @@ -49,7 +49,7 @@ internal actor RoomLifecycleManager<Channel: RoomLifecycleContributorChannel> {
}
#endif

private init(current: RoomLifecycle?, contributors: [Contributor], logger: InternalLogger, clock: SimpleClock) {
private init(current: RoomLifecycle?, contributors: [Contributor], logger: InternalLogger, clock: SimpleClock) async {
self.current = current ?? .initialized
self.contributors = contributors
self.logger = logger
Expand Down
54 changes: 27 additions & 27 deletions Tests/AblyChatTests/RoomLifecycleManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ struct RoomLifecycleManagerTests {
forTestingWhatHappensWhenCurrentlyIn current: RoomLifecycle? = nil,
contributors: [RoomLifecycleManager<MockRoomLifecycleContributorChannel>.Contributor] = [],
clock: SimpleClock = MockSimpleClock()
) -> RoomLifecycleManager<MockRoomLifecycleContributorChannel> {
.init(testsOnly_current: current, contributors: contributors, logger: TestLogger(), clock: clock)
) async -> RoomLifecycleManager<MockRoomLifecycleContributorChannel> {
await .init(testsOnly_current: current, contributors: contributors, logger: TestLogger(), clock: clock)
}

private func createContributor(
Expand All @@ -50,14 +50,14 @@ struct RoomLifecycleManagerTests {
// @spec CHA-RS3
@Test
func current_startsAsInitialized() async {
let manager = createManager()
let manager = await createManager()

#expect(await manager.current == .initialized)
}

@Test
func error_startsAsNil() async {
let manager = createManager()
let manager = await createManager()

#expect(await manager.error == nil)
}
Expand All @@ -69,7 +69,7 @@ struct RoomLifecycleManagerTests {
func attach_whenAlreadyAttached() async throws {
// Given: A RoomLifecycleManager in the ATTACHED state
let contributor = createContributor()
let manager = createManager(forTestingWhatHappensWhenCurrentlyIn: .attached, contributors: [contributor])
let manager = await createManager(forTestingWhatHappensWhenCurrentlyIn: .attached, contributors: [contributor])

// When: `performAttachOperation()` is called on the lifecycle manager
try await manager.performAttachOperation()
Expand All @@ -82,7 +82,7 @@ struct RoomLifecycleManagerTests {
@Test
func attach_whenReleasing() async throws {
// Given: A RoomLifecycleManager in the RELEASING state
let manager = createManager(forTestingWhatHappensWhenCurrentlyIn: .releasing)
let manager = await createManager(forTestingWhatHappensWhenCurrentlyIn: .releasing)

// When: `performAttachOperation()` is called on the lifecycle manager
// Then: It throws a roomIsReleasing error
Expand All @@ -97,7 +97,7 @@ struct RoomLifecycleManagerTests {
@Test
func attach_whenReleased() async throws {
// Given: A RoomLifecycleManager in the RELEASED state
let manager = createManager(forTestingWhatHappensWhenCurrentlyIn: .released)
let manager = await createManager(forTestingWhatHappensWhenCurrentlyIn: .released)

// When: `performAttachOperation()` is called on the lifecycle manager
// Then: It throws a roomIsReleased error
Expand All @@ -114,7 +114,7 @@ struct RoomLifecycleManagerTests {
// Given: A RoomLifecycleManager, with a contributor on whom calling `attach()` will not complete until after the "Then" part of this test (the motivation for this is to suppress the room from transitioning to ATTACHED, so that we can assert its current state as being ATTACHING)
let contributorAttachOperation = SignallableChannelOperation()

let manager = createManager(contributors: [createContributor(attachBehavior: contributorAttachOperation.behavior)])
let manager = await createManager(contributors: [createContributor(attachBehavior: contributorAttachOperation.behavior)])
let statusChangeSubscription = await manager.onChange(bufferingPolicy: .unbounded)
async let statusChange = statusChangeSubscription.first { _ in true }

Expand All @@ -136,7 +136,7 @@ struct RoomLifecycleManagerTests {
func attach_attachesAllContributors_andWhenTheyAllAttachSuccessfully_transitionsToAttached() async throws {
// Given: A RoomLifecycleManager, all of whose contributors’ calls to `attach` succeed
let contributors = (1 ... 3).map { _ in createContributor(attachBehavior: .complete(.success)) }
let manager = createManager(contributors: contributors)
let manager = await createManager(contributors: contributors)

let statusChangeSubscription = await manager.onChange(bufferingPolicy: .unbounded)
async let attachedStatusChange = statusChangeSubscription.first { $0.current == .attached }
Expand Down Expand Up @@ -168,7 +168,7 @@ struct RoomLifecycleManagerTests {
}
}

let manager = createManager(contributors: contributors)
let manager = await createManager(contributors: contributors)

let statusChangeSubscription = await manager.onChange(bufferingPolicy: .unbounded)
async let maybeSuspendedStatusChange = statusChangeSubscription.first { $0.current == .suspended }
Expand Down Expand Up @@ -219,7 +219,7 @@ struct RoomLifecycleManagerTests {
}
}

let manager = createManager(contributors: contributors)
let manager = await createManager(contributors: contributors)

let statusChangeSubscription = await manager.onChange(bufferingPolicy: .unbounded)
async let maybeFailedStatusChange = statusChangeSubscription.first { $0.current == .failed }
Expand Down Expand Up @@ -270,7 +270,7 @@ struct RoomLifecycleManagerTests {
),
]

let manager = createManager(contributors: contributors)
let manager = await createManager(contributors: contributors)

// When: `performAttachOperation()` is called on the lifecycle manager
try? await manager.performAttachOperation()
Expand Down Expand Up @@ -312,7 +312,7 @@ struct RoomLifecycleManagerTests {
),
]

let manager = createManager(contributors: contributors)
let manager = await createManager(contributors: contributors)

// When: `performAttachOperation()` is called on the lifecycle manager
try? await manager.performAttachOperation()
Expand All @@ -328,7 +328,7 @@ struct RoomLifecycleManagerTests {
func detach_whenAlreadyDetached() async throws {
// Given: A RoomLifecycleManager in the DETACHED state
let contributor = createContributor()
let manager = createManager(forTestingWhatHappensWhenCurrentlyIn: .detached, contributors: [contributor])
let manager = await createManager(forTestingWhatHappensWhenCurrentlyIn: .detached, contributors: [contributor])

// When: `performDetachOperation()` is called on the lifecycle manager
try await manager.performDetachOperation()
Expand All @@ -341,7 +341,7 @@ struct RoomLifecycleManagerTests {
@Test
func detach_whenReleasing() async throws {
// Given: A RoomLifecycleManager in the RELEASING state
let manager = createManager(forTestingWhatHappensWhenCurrentlyIn: .releasing)
let manager = await createManager(forTestingWhatHappensWhenCurrentlyIn: .releasing)

// When: `performDetachOperation()` is called on the lifecycle manager
// Then: It throws a roomIsReleasing error
Expand All @@ -356,7 +356,7 @@ struct RoomLifecycleManagerTests {
@Test
func detach_whenReleased() async throws {
// Given: A RoomLifecycleManager in the RELEASED state
let manager = createManager(forTestingWhatHappensWhenCurrentlyIn: .released)
let manager = await createManager(forTestingWhatHappensWhenCurrentlyIn: .released)

// When: `performAttachOperation()` is called on the lifecycle manager
// Then: It throws a roomIsReleased error
Expand All @@ -371,7 +371,7 @@ struct RoomLifecycleManagerTests {
@Test
func detach_whenFailed() async throws {
// Given: A RoomLifecycleManager in the FAILED state
let manager = createManager(forTestingWhatHappensWhenCurrentlyIn: .failed)
let manager = await createManager(forTestingWhatHappensWhenCurrentlyIn: .failed)

// When: `performAttachOperation()` is called on the lifecycle manager
// Then: It throws a roomInFailedState error
Expand All @@ -388,7 +388,7 @@ struct RoomLifecycleManagerTests {
// Given: A RoomLifecycleManager, with a contributor on whom calling `detach()` will not complete until after the "Then" part of this test (the motivation for this is to suppress the room from transitioning to DETACHED, so that we can assert its current state as being DETACHING)
let contributorDetachOperation = SignallableChannelOperation()

let manager = createManager(contributors: [createContributor(detachBehavior: contributorDetachOperation.behavior)])
let manager = await createManager(contributors: [createContributor(detachBehavior: contributorDetachOperation.behavior)])
let statusChangeSubscription = await manager.onChange(bufferingPolicy: .unbounded)
async let statusChange = statusChangeSubscription.first { _ in true }

Expand All @@ -409,7 +409,7 @@ struct RoomLifecycleManagerTests {
func detach_detachesAllContributors_andWhenTheyAllDetachSuccessfully_transitionsToDetached() async throws {
// Given: A RoomLifecycleManager, all of whose contributors’ calls to `detach` succeed
let contributors = (1 ... 3).map { _ in createContributor(detachBehavior: .complete(.success)) }
let manager = createManager(contributors: contributors)
let manager = await createManager(contributors: contributors)

let statusChangeSubscription = await manager.onChange(bufferingPolicy: .unbounded)
async let detachedStatusChange = statusChangeSubscription.first { $0.current == .detached }
Expand Down Expand Up @@ -446,7 +446,7 @@ struct RoomLifecycleManagerTests {
createContributor(feature: .typing, detachBehavior: .success),
]

let manager = createManager(contributors: contributors)
let manager = await createManager(contributors: contributors)

let statusChangeSubscription = await manager.onChange(bufferingPolicy: .unbounded)
async let maybeFailedStatusChange = statusChangeSubscription.first { $0.current == .failed }
Expand Down Expand Up @@ -493,7 +493,7 @@ struct RoomLifecycleManagerTests {
let contributor = createContributor(initialState: .attached, detachBehavior: .fromFunction(detachImpl))
let clock = MockSimpleClock()

let manager = createManager(contributors: [contributor], clock: clock)
let manager = await createManager(contributors: [contributor], clock: clock)

let statusChangeSubscription = await manager.onChange(bufferingPolicy: .unbounded)
async let asyncLetStatusChanges = Array(statusChangeSubscription.prefix(2))
Expand All @@ -517,7 +517,7 @@ struct RoomLifecycleManagerTests {
func release_whenAlreadyReleased() async {
// Given: A RoomLifecycleManager in the RELEASED state
let contributor = createContributor()
let manager = createManager(forTestingWhatHappensWhenCurrentlyIn: .released, contributors: [contributor])
let manager = await createManager(forTestingWhatHappensWhenCurrentlyIn: .released, contributors: [contributor])

// When: `performReleaseOperation()` is called on the lifecycle manager
await manager.performReleaseOperation()
Expand All @@ -531,7 +531,7 @@ struct RoomLifecycleManagerTests {
func release_whenDetached() async throws {
// Given: A RoomLifecycleManager in the DETACHED state
let contributor = createContributor()
let manager = createManager(forTestingWhatHappensWhenCurrentlyIn: .detached, contributors: [contributor])
let manager = await createManager(forTestingWhatHappensWhenCurrentlyIn: .detached, contributors: [contributor])

let statusChangeSubscription = await manager.onChange(bufferingPolicy: .unbounded)
async let statusChange = statusChangeSubscription.first { _ in true }
Expand All @@ -551,7 +551,7 @@ struct RoomLifecycleManagerTests {
// Given: A RoomLifecycleManager, with a contributor on whom calling `detach()` will not complete until after the "Then" part of this test (the motivation for this is to suppress the room from transitioning to RELEASED, so that we can assert its current state as being RELEASING)
let contributorDetachOperation = SignallableChannelOperation()

let manager = createManager(contributors: [createContributor(detachBehavior: contributorDetachOperation.behavior)])
let manager = await createManager(contributors: [createContributor(detachBehavior: contributorDetachOperation.behavior)])
let statusChangeSubscription = await manager.onChange(bufferingPolicy: .unbounded)
async let statusChange = statusChangeSubscription.first { _ in true }

Expand Down Expand Up @@ -580,7 +580,7 @@ struct RoomLifecycleManagerTests {
createContributor(initialState: .detached /* arbitrary non-FAILED */, detachBehavior: .complete(.success)),
]

let manager = createManager(contributors: contributors)
let manager = await createManager(contributors: contributors)

let statusChangeSubscription = await manager.onChange(bufferingPolicy: .unbounded)
async let releasedStatusChange = statusChangeSubscription.first { $0.current == .released }
Expand Down Expand Up @@ -620,7 +620,7 @@ struct RoomLifecycleManagerTests {

let clock = MockSimpleClock()

let manager = createManager(contributors: [contributor], clock: clock)
let manager = await createManager(contributors: [contributor], clock: clock)

// Then: When `performReleaseOperation()` is called on the manager
await manager.performReleaseOperation()
Expand All @@ -640,7 +640,7 @@ struct RoomLifecycleManagerTests {

let clock = MockSimpleClock()

let manager = createManager(contributors: [contributor], clock: clock)
let manager = await createManager(contributors: [contributor], clock: clock)

let statusChangeSubscription = await manager.onChange(bufferingPolicy: .unbounded)
async let releasedStatusChange = statusChangeSubscription.first { $0.current == .released }
Expand Down

0 comments on commit 367c7c9

Please sign in to comment.