Integrate live phone and watch remote prototype with guarded motion control
This commit is contained in:
@@ -9,6 +9,7 @@ private final class FakePhoneSession: RelaySessionSending {
|
||||
var activationState: RelayActivationState = .notActivated
|
||||
var isReachable = true
|
||||
var sent: [[String: Any]] = []
|
||||
var contexts: [[String: Any]] = []
|
||||
var reply: (([String: Any]) -> Void)?
|
||||
var error: ((Error) -> Void)?
|
||||
func activate() { activationState = .activated }
|
||||
@@ -19,7 +20,7 @@ private final class FakePhoneSession: RelaySessionSending {
|
||||
reply = replyHandler
|
||||
error = errorHandler
|
||||
}
|
||||
func updateApplicationContext(_ context: [String: Any]) throws {}
|
||||
func updateApplicationContext(_ context: [String: Any]) throws { contexts.append(context) }
|
||||
func answer(_ value: [String: Any]) { reply?(value) }
|
||||
}
|
||||
|
||||
@@ -86,3 +87,70 @@ private actor RecordingPhoneCoordinator: PhoneCommandCoordinating {
|
||||
await Task.yield()
|
||||
#expect(replies == 1)
|
||||
}
|
||||
|
||||
private actor SuspendedCoordinator: PhoneCommandCoordinating {
|
||||
var begins = 0
|
||||
var active = false
|
||||
var waiting: [CheckedContinuation<Result<RemoteSessionGrant, RelayFailure>, Never>] = []
|
||||
func watchBridgeDidActivate(peer: WatchPeerGeneration) { active = true }
|
||||
func watchBridgeDidDeactivate(peer: WatchPeerGeneration) { active = false }
|
||||
func beginWatchSession(_ request: BeginRemoteSessionRequest, peer: WatchPeerGeneration) async -> Result<RemoteSessionGrant, RelayFailure> {
|
||||
begins += 1
|
||||
return await withCheckedContinuation { waiting.append($0) }
|
||||
}
|
||||
func endWatchSession(_ request: EndRemoteSessionRequest, peer: WatchPeerGeneration) -> Result<RemoteSessionEnded, RelayFailure> { .failure(RelayFailure(code: .unavailable)) }
|
||||
func submitWatchCommand(_ envelope: CommandEnvelope, peer: WatchPeerGeneration) -> CommandReceipt { CommandReceipt(id: envelope.id, status: .sentToTransport) }
|
||||
func release() { for continuation in waiting { continuation.resume(returning: .failure(RelayFailure(code: .unavailable))) }; waiting.removeAll() }
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Test func timeoutDoesNotWaitForUncooperativeActorAndBoundsOutstandingCalls() async throws {
|
||||
let session = FakePhoneSession()
|
||||
let coordinator = SuspendedCoordinator()
|
||||
let bridge = PhoneRelayBridge(session: session, dependency: PhoneRelayDependency(coordinator: coordinator))
|
||||
bridge.activate(); bridge.activationDidComplete(error: nil)
|
||||
try await Task.sleep(for: .milliseconds(30))
|
||||
let message = try RelayContainer(kind: .beginSession, value: BeginRemoteSessionRequest()).dictionary()
|
||||
var replies = 0
|
||||
for _ in 0..<20 { bridge.receive(message: message) { _ in replies += 1 } }
|
||||
try await Task.sleep(for: .milliseconds(1_150))
|
||||
#expect(replies == 20)
|
||||
#expect(await coordinator.begins == 16)
|
||||
bridge.receive(message: message) { _ in replies += 1 }
|
||||
#expect(replies == 21)
|
||||
#expect(await coordinator.begins == 16)
|
||||
await coordinator.release()
|
||||
try await Task.sleep(for: .milliseconds(30))
|
||||
#expect(replies == 21)
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Test func deactivationCompletesPendingRepliesBeforeActorReturns() async throws {
|
||||
let session = FakePhoneSession()
|
||||
let coordinator = SuspendedCoordinator()
|
||||
let bridge = PhoneRelayBridge(session: session, dependency: PhoneRelayDependency(coordinator: coordinator))
|
||||
bridge.activate(); bridge.activationDidComplete(error: nil)
|
||||
try await Task.sleep(for: .milliseconds(30))
|
||||
var replies = 0
|
||||
bridge.receive(message: try RelayContainer(kind: .beginSession, value: BeginRemoteSessionRequest()).dictionary()) { _ in replies += 1 }
|
||||
try await Task.sleep(for: .milliseconds(30))
|
||||
bridge.didDeactivate()
|
||||
#expect(replies == 1)
|
||||
await coordinator.release()
|
||||
try await Task.sleep(for: .milliseconds(30))
|
||||
#expect(replies == 1)
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Test func latestContextPublishedBeforeActivationIsFlushed() async throws {
|
||||
let session = FakePhoneSession()
|
||||
let coordinator = RecordingPhoneCoordinator()
|
||||
let bridge = PhoneRelayBridge(session: session, dependency: PhoneRelayDependency(coordinator: coordinator))
|
||||
let context = RelayApplicationContext(selectedTelevisionID: UUID(), displayName: "TV", isConnectionReady: true)
|
||||
bridge.publishContext(context)
|
||||
#expect(session.contexts.isEmpty)
|
||||
bridge.activate(); bridge.activationDidComplete(error: nil)
|
||||
try await Task.sleep(for: .milliseconds(30))
|
||||
#expect(session.contexts.count == 1)
|
||||
#expect(try RelayApplicationContext(dictionary: session.contexts[0]) == context)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user