@@ -276,38 +276,39 @@ struct LiveSessionTests {
276276 await session. close ( )
277277 }
278278
279- // Getting a limited use token adds too much of an overhead; we can't interrupt the model in time
280279 @Test (
281280 arguments: arguments. filter { !$0. 0 . useLimitedUseAppCheckTokens }
282281 )
282+ // Getting a limited use token adds too much of an overhead; we can't interrupt the model in time
283283 func realtime_interruption( _ config: InstanceConfig , modelName: String ) async throws {
284284 let model = FirebaseAI . componentInstance ( config) . liveModel (
285285 modelName: modelName,
286286 generationConfig: audioConfig
287287 )
288288
289- let session = try await model. connect ( )
290-
291289 guard let audioFile = NSDataAsset ( name: " hello " ) else {
292290 Issue . record ( " Missing audio file 'hello.wav' in Assets " )
293291 return
294292 }
295- await session. sendAudioRealtime ( audioFile. data)
296- await session. sendAudioRealtime ( Data ( repeating: 0 , count: audioFile. data. count) )
297293
298- // wait a second to allow the model to start generating (and cuase a proper interruption)
299- try await Task . sleep ( nanoseconds : oneSecondInNanoseconds )
300- await session. sendAudioRealtime ( audioFile. data)
301- await session. sendAudioRealtime ( Data ( repeating: 0 , count: audioFile. data. count) )
294+ try await retry ( times : 3 , delayInSeconds : 2.0 ) {
295+ let session = try await model . connect ( )
296+ await session. sendAudioRealtime ( audioFile. data)
297+ await session. sendAudioRealtime ( Data ( repeating: 0 , count: audioFile. data. count) )
302298
303- for try await content in session . responsesOf ( LiveServerContent . self ) {
304- if content . wasInterrupted {
305- break
306- }
299+ // wait a second to allow the model to start generating (and cuase a proper interruption)
300+ try await Task . sleep ( nanoseconds : oneSecondInNanoseconds )
301+ await session . sendAudioRealtime ( audioFile . data )
302+ await session . sendAudioRealtime ( Data ( repeating : 0 , count : audioFile . data . count ) )
307303
308- if content. isTurnComplete {
309- Issue . record ( " The model never sent an interrupted message. " )
310- return
304+ for try await content in session. responsesOf ( LiveServerContent . self) {
305+ if content. wasInterrupted {
306+ break
307+ }
308+
309+ if content. isTurnComplete {
310+ throw NoInterruptionError ( )
311+ }
311312 }
312313 }
313314 }
@@ -472,6 +473,11 @@ private extension LiveSession {
472473 }
473474}
474475
476+ private struct NoInterruptionError : Error ,
477+ CustomStringConvertible {
478+ var description : String { " The model never sent an interrupted message. " }
479+ }
480+
475481private extension ModelContent {
476482 /// A collection of text from all parts.
477483 ///
0 commit comments