-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement room status change upon attach or detach
Based on the simplified requirements described in #19. The decision from 7d6acde to use actors as the mechanism for managing mutable state means that I’ve had to make RoomStatus.{ current, error, onChange(bufferingPolicy:) } async. As mentioned there, if later on we decide this is too weird an API, then we can think of alternatives. I really wanted to avoid making DefaultRoomStatus an actor; I tried to find a way to isolate this state to the DefaultRoom actor (who logically owns this state), by trying to make the DefaultRoomStatus access the DefaultRoom-managed state, but I was not successful and didn’t want to sink much time into it. It means that DefaultRoomStatus has suspension points in order to access its current state, which I am not happy about. But we can revisit later, perhaps armed with more knowledge of concurrency and in less of a rush to get some initial functionality implemented. Resolves #19.
- Loading branch information
1 parent
09e54f6
commit 785c765
Showing
4 changed files
with
102 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
@testable import AblyChat | ||
import XCTest | ||
|
||
class DefaultRoomStatusTests: XCTestCase { | ||
func test_current_startsAsInitialized() async { | ||
let status = DefaultRoomStatus() | ||
let current = await status.current | ||
XCTAssertEqual(current, .initialized) | ||
} | ||
|
||
func test_error_startsAsNil() async { | ||
let status = DefaultRoomStatus() | ||
let error = await status.error | ||
XCTAssertNil(error) | ||
} | ||
|
||
func test_transition() async { | ||
// Given: A RoomStatus | ||
let status = DefaultRoomStatus() | ||
let originalState = await status.current | ||
let newState = RoomLifecycle.attached // arbitrary | ||
|
||
let subscription1 = await status.onChange(bufferingPolicy: .unbounded) | ||
let subscription2 = await status.onChange(bufferingPolicy: .unbounded) | ||
|
||
async let statusChange1 = subscription1.first { $0.current == newState } | ||
async let statusChange2 = subscription2.first { $0.current == newState } | ||
|
||
// When: transition(to:) is called | ||
await status.transition(to: newState) | ||
|
||
// Then: It emits a status change to all subscribers added via onChange(bufferingPolicy:), and updates its `current` property to the new state | ||
guard let statusChange1 = await statusChange1, let statusChange2 = await statusChange2 else { | ||
XCTFail("Expected status changes to be emitted") | ||
return | ||
} | ||
|
||
for statusChange in [statusChange1, statusChange2] { | ||
XCTAssertEqual(statusChange.previous, originalState) | ||
XCTAssertEqual(statusChange.current, newState) | ||
} | ||
|
||
let current = await status.current | ||
XCTAssertEqual(current, .attached) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters