@@ -12,6 +12,7 @@ use futures::SinkExt;
1212use futures:: StreamExt ;
1313use serde_json:: Value ;
1414use tokio:: net:: TcpListener ;
15+ use tokio:: sync:: Notify ;
1516use tokio:: sync:: oneshot;
1617use tokio_tungstenite:: accept_hdr_async_with_config;
1718use tokio_tungstenite:: tungstenite:: Message ;
@@ -335,6 +336,7 @@ pub struct WebSocketTestServer {
335336 uri : String ,
336337 connections : Arc < Mutex < Vec < Vec < WebSocketRequest > > > > ,
337338 handshakes : Arc < Mutex < Vec < WebSocketHandshake > > > ,
339+ request_log_updated : Arc < Notify > ,
338340 shutdown : oneshot:: Sender < ( ) > ,
339341 task : tokio:: task:: JoinHandle < ( ) > ,
340342}
@@ -356,6 +358,26 @@ impl WebSocketTestServer {
356358 connections. first ( ) . cloned ( ) . unwrap_or_default ( )
357359 }
358360
361+ pub async fn wait_for_request (
362+ & self ,
363+ connection_index : usize ,
364+ request_index : usize ,
365+ ) -> WebSocketRequest {
366+ loop {
367+ if let Some ( request) = self
368+ . connections
369+ . lock ( )
370+ . unwrap ( )
371+ . get ( connection_index)
372+ . and_then ( |connection| connection. get ( request_index) )
373+ . cloned ( )
374+ {
375+ return request;
376+ }
377+ self . request_log_updated . notified ( ) . await ;
378+ }
379+ }
380+
359381 pub fn handshakes ( & self ) -> Vec < WebSocketHandshake > {
360382 self . handshakes . lock ( ) . unwrap ( ) . clone ( )
361383 }
@@ -1069,15 +1091,18 @@ pub async fn start_websocket_server(connections: Vec<Vec<Vec<Value>>>) -> WebSoc
10691091pub async fn start_websocket_server_with_headers (
10701092 connections : Vec < WebSocketConnectionConfig > ,
10711093) -> WebSocketTestServer {
1094+ let start = std:: time:: Instant :: now ( ) ;
10721095 let listener = TcpListener :: bind ( "127.0.0.1:0" )
10731096 . await
10741097 . expect ( "bind websocket server" ) ;
10751098 let addr = listener. local_addr ( ) . expect ( "websocket server address" ) ;
10761099 let uri = format ! ( "ws://{addr}" ) ;
10771100 let connections_log = Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ;
10781101 let handshakes_log = Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ;
1102+ let request_log_updated = Arc :: new ( Notify :: new ( ) ) ;
10791103 let requests = Arc :: clone ( & connections_log) ;
10801104 let handshakes = Arc :: clone ( & handshakes_log) ;
1105+ let request_log = Arc :: clone ( & request_log_updated) ;
10811106 let connections = Arc :: new ( Mutex :: new ( VecDeque :: from ( connections) ) ) ;
10821107 let ( shutdown_tx, mut shutdown_rx) = oneshot:: channel ( ) ;
10831108
@@ -1159,9 +1184,51 @@ pub async fn start_websocket_server_with_headers(
11591184 let mut log = requests. lock ( ) . unwrap ( ) ;
11601185 if let Some ( connection_log) = log. get_mut ( connection_index) {
11611186 connection_log. push ( WebSocketRequest { body } ) ;
1187+ let request_index = connection_log. len ( ) - 1 ;
1188+ let request = & connection_log[ request_index] ;
1189+ let request_body = request. body_json ( ) ;
1190+ eprintln ! (
1191+ "[ws test server +{}ms] connection={} received request={} type={:?} role={:?} text={:?} data={:?}" ,
1192+ start. elapsed( ) . as_millis( ) ,
1193+ connection_index,
1194+ request_index,
1195+ request_body. get( "type" ) . and_then( Value :: as_str) ,
1196+ request_body
1197+ . get( "item" )
1198+ . and_then( |item| item. get( "role" ) )
1199+ . and_then( Value :: as_str) ,
1200+ request_body
1201+ . get( "item" )
1202+ . and_then( |item| item. get( "content" ) )
1203+ . and_then( Value :: as_array)
1204+ . and_then( |content| content. first( ) )
1205+ . and_then( |content| content. get( "text" ) )
1206+ . and_then( Value :: as_str) ,
1207+ request_body
1208+ . get( "item" )
1209+ . and_then( |item| item. get( "content" ) )
1210+ . and_then( Value :: as_array)
1211+ . and_then( |content| content. first( ) )
1212+ . and_then( |content| content. get( "data" ) )
1213+ . and_then( Value :: as_str) ,
1214+ ) ;
11621215 }
1216+ request_log. notify_waiters ( ) ;
11631217 }
11641218
1219+ eprintln ! (
1220+ "[ws test server +{}ms] connection={} sending batch_size={} event_types={:?} audio_data={:?}" ,
1221+ start. elapsed( ) . as_millis( ) ,
1222+ connection_index,
1223+ request_events. len( ) ,
1224+ request_events
1225+ . iter( )
1226+ . map( |event| event. get( "type" ) . and_then( Value :: as_str) )
1227+ . collect:: <Vec <_>>( ) ,
1228+ request_events
1229+ . iter( )
1230+ . find_map( |event| event. get( "delta" ) . and_then( Value :: as_str) ) ,
1231+ ) ;
11651232 for event in & request_events {
11661233 let Ok ( payload) = serde_json:: to_string ( event) else {
11671234 continue ;
@@ -1184,6 +1251,7 @@ pub async fn start_websocket_server_with_headers(
11841251 uri,
11851252 connections : connections_log,
11861253 handshakes : handshakes_log,
1254+ request_log_updated,
11871255 shutdown : shutdown_tx,
11881256 task,
11891257 }
0 commit comments