@@ -250,7 +250,6 @@ private final class TestRealtimeTalkAudioCapture: RealtimeTalkAudioCapturing {
250250 private( set) var startCount = 0
251251 private( set) var stopCount = 0
252252 private var onFailure : ( @MainActor ( String ) -> Void ) ?
253- private let started = RealtimeRelayTestSignal < Void > ( )
254253
255254 func start (
256255 targetSampleRate : Double ,
@@ -260,7 +259,6 @@ private final class TestRealtimeTalkAudioCapture: RealtimeTalkAudioCapturing {
260259 self . isStarted = true
261260 self . startCount += 1
262261 self . onFailure = onFailure
263- self . started . send ( ( ) )
264262 }
265263
266264 func stop ( ) {
@@ -272,10 +270,6 @@ private final class TestRealtimeTalkAudioCapture: RealtimeTalkAudioCapturing {
272270 func fail ( _ message : String ) {
273271 self . onFailure ? ( message )
274272 }
275-
276- func waitUntilStarted ( ) async throws {
277- _ = try await self . started . next ( " audio capture to start " )
278- }
279273}
280274
281275private actor RealtimeRelayStartupBarrier {
@@ -1581,9 +1575,16 @@ extension RealtimeTalkRelaySessionTests {
15811575 #expect ( ! audioCapture . isStarted )
15821576 }
15831577
1584- @ Test func `pre-ready event stream end promptly fails startup and closes created session once` ( ) async throws {
1578+ @ Test ( arguments : [ true , false ] )
1579+ func `pre-ready event stream end fails startup and closes created session once` (
1580+ processedBeforeRegistration : Bool ) async throws
1581+ {
15851582 let requests = RealtimeRelayStartupRequestLog ( )
15861583 let eventChannel = AsyncStream < EventFrame > . makeStream ( )
1584+ let issueObserved = AsyncStream . makeStream ( of : Void . self , bufferingPolicy : . bufferingNewest ( 1 ) )
1585+ let startupCompleted = RealtimeRelayTestSignal < Result < Void , any Error > > ( )
1586+ let audioCapture = TestRealtimeTalkAudioCapture ( )
1587+ var endedEventStream = false
15871588 let result = TalkSessionCreateResult (
15881589 sessionid : " talk-session " ,
15891590 mode : AnyCodable ( " realtime " ) ,
@@ -1599,35 +1600,75 @@ extension RealtimeTalkRelaySessionTests {
15991600 return resultData
16001601 }
16011602 return Data ( " { \" ok \" :true} " . utf8 )
1603+ } ,
1604+ isCurrent : { @MainActor ( ) async -> Bool in
1605+ guard audioCapture . isStarted , ! endedEventStream else { return true }
1606+ endedEventStream = true
1607+ eventChannel . continuation . finish ( )
1608+ // Suspending here lets the pump save the issue before waiter registration.
1609+ // Otherwise this MainActor segment registers the waiter before the pump can run.
1610+ if processedBeforeRegistration {
1611+ var iterator = issueObserved . stream . makeAsyncIterator ( )
1612+ _ = await iterator . next ( )
1613+ }
1614+ return true
16021615 } )
1603- let audioCapture = TestRealtimeTalkAudioCapture ( )
16041616 var issues : [ RealtimeTalkRelayIssue ] = [ ]
16051617 let session = RealtimeTalkRelaySession (
16061618 transport : transport ,
16071619 options : . init ( sessionKey : " main " , provider : " openai " , model : nil , voice : nil ) ,
16081620 audioCapture : audioCapture ,
16091621 pcmPlayer : UnusedPCMStreamingAudioPlayer ( ) ,
16101622 onStatus : { _ in } ,
1611- onIssue : { issues . append ( $0 ) } ,
1623+ onIssue : {
1624+ issues . append ( $0 )
1625+ issueObserved . continuation . yield ( ( ) )
1626+ } ,
16121627 onSpeakingChanged : { _ in } )
1613- let start = Task { @MainActor in try await session . start ( ) }
1614- try await audioCapture . waitUntilStarted ( )
1615-
1616- let disconnectedAt = ContinuousClock . now
1617- eventChannel . continuation . finish ( )
1628+ let start = Task { @MainActor in
1629+ do {
1630+ try await session . start ( )
1631+ startupCompleted . send ( . success ( ( ) ) )
1632+ } catch {
1633+ startupCompleted . send ( . failure ( error ) )
1634+ }
1635+ }
1636+ defer {
1637+ issueObserved . continuation . finish ( )
1638+ eventChannel . continuation . finish ( )
1639+ session . stop ( )
1640+ start . cancel ( )
1641+ }
16181642 do {
1619- try await start . value
1620- Issue . record ( " Expected the pre-ready event stream end to throw " )
1643+ let startupResult = try await startupCompleted . next ( " relay startup completion after event stream end " )
1644+ await start . value
1645+ switch startupResult {
1646+ case . success :
1647+ Issue . record ( " Expected the pre-ready event stream end to throw " )
1648+ case let . failure ( error ) :
1649+ let startupError = error as NSError
1650+ #expect ( startupError . domain == " RealtimeTalkRelay " )
1651+ #expect ( startupError . code == 6 )
1652+ #expect ( startupError . localizedDescription == " Realtime connection ended before it became ready. " )
1653+ }
1654+
1655+ #expect ( issues . map ( \. phase ) == [ " connect " ] )
1656+ let recorded = await requests . snapshot ( )
1657+ #expect ( recorded . map ( \. method ) == [ " talk.session.create " , " talk.session.close " ] )
1658+ #expect ( recorded . last ? . params ? [ " sessionId " ] ? . stringValue == " relay-1 " )
1659+ #expect ( audioCapture . startCount == 1 )
1660+ #expect ( ! audioCapture . isStarted )
16211661 } catch {
1622- #expect ( error . localizedDescription == " Realtime connection ended before it became ready. " )
1662+ issueObserved . continuation . finish ( )
1663+ eventChannel . continuation . finish ( )
1664+ session . stop ( )
1665+ start . cancel ( )
1666+ await start . value
1667+ if audioCapture . startCount > 0 {
1668+ try? await requests . waitForRequestCount ( 2 )
1669+ }
1670+ throw error
16231671 }
1624-
1625- #expect ( disconnectedAt . duration ( to : . now ) < . seconds ( 1 ) )
1626- #expect ( issues . map ( \. phase ) == [ " connect " ] )
1627- let recorded = await requests . snapshot ( )
1628- #expect ( recorded . map ( \. method ) == [ " talk.session.create " , " talk.session.close " ] )
1629- #expect ( recorded . last ? . params ? [ " sessionId " ] ? . stringValue == " relay-1 " )
1630- #expect ( ! audioCapture . isStarted )
16311672 }
16321673
16331674 @ Test func `event stream ending during relay creation closes the late relay` ( ) async throws {
0 commit comments