@@ -240,16 +240,17 @@ mod tests {
240240 }
241241
242242 impl Connection {
243- fn new ( open : & Arc < AtomicUsize > , idle : Duration , input : & str ) -> Self {
243+ /// `before` acts on the client before the worker starts reading, so what it sends is
244+ /// already there however late this thread gets scheduled.
245+ fn new ( open : & Arc < AtomicUsize > , idle : Duration , before : impl FnOnce ( & mut TcpStream ) ) -> Self {
244246 let listener = TcpListener :: bind ( ( "127.0.0.1" , 0 ) ) . unwrap ( ) ;
245247 let mut client = TcpStream :: connect ( listener. local_addr ( ) . unwrap ( ) ) . unwrap ( ) ;
246248 client. set_read_timeout ( Some ( Duration :: from_secs ( 3 ) ) ) . unwrap ( ) ;
247249 let ( stream, _) = listener. accept ( ) . unwrap ( ) ;
248250 // Small test messages must not wait for Nagle's algorithm or a delayed ACK.
249251 client. set_nodelay ( true ) . unwrap ( ) ;
250252 stream. set_nodelay ( true ) . unwrap ( ) ;
251- // Queue initial input before starting the worker's short idle timeout.
252- client. write_all ( input. as_bytes ( ) ) . unwrap ( ) ;
253+ before ( & mut client) ;
253254 let ( tx, requests) = channel ( ) ;
254255 let worker = spawn_connection ( stream, tx, egui:: Context :: default ( ) , Arc :: clone ( open) , idle) . unwrap ( ) ;
255256 Self { client : BufReader :: new ( client) , requests, worker : Some ( worker) }
@@ -276,7 +277,7 @@ mod tests {
276277 fn idle_connections_release_their_slots_without_dispatching ( ) {
277278 for input in [ "" , "{\" method\" :\" engine.execute\" " ] {
278279 let open = Arc :: new ( AtomicUsize :: new ( 0 ) ) ;
279- let mut connection = Connection :: new ( & open, Duration :: from_millis ( 100 ) , input) ;
280+ let mut connection = Connection :: new ( & open, Duration :: from_millis ( 100 ) , |c| c . write_all ( input. as_bytes ( ) ) . unwrap ( ) ) ;
280281 assert_eq ! ( open. load( Ordering :: SeqCst ) , 1 ) ;
281282 let mut reply = String :: new ( ) ;
282283 let closed = connection. client . read_to_string ( & mut reply) ;
@@ -289,8 +290,7 @@ mod tests {
289290 assert ! ( reply[ "error" ] . as_str( ) . unwrap( ) . contains( "idle read timed out" ) ) ;
290291
291292 // A subsequent connection is admitted through the same accounting path.
292- let mut next = Connection :: new ( & open, Duration :: from_secs ( 1 ) , "" ) ;
293- next. client . get_mut ( ) . shutdown ( std:: net:: Shutdown :: Write ) . unwrap ( ) ;
293+ let mut next = Connection :: new ( & open, Duration :: from_secs ( 1 ) , |c| c. shutdown ( std:: net:: Shutdown :: Write ) . unwrap ( ) ) ;
294294 let mut eof = String :: new ( ) ;
295295 next. client . read_to_string ( & mut eof) . unwrap ( ) ;
296296 next. join ( ) ;
@@ -302,11 +302,20 @@ mod tests {
302302 #[ test]
303303 fn persistent_requests_and_slow_replies_outlive_the_idle_read_timeout ( ) {
304304 let open = Arc :: new ( AtomicUsize :: new ( 0 ) ) ;
305- let mut connection = Connection :: new ( & open, Duration :: from_millis ( 100 ) , "{\" id\" :1,\" method\" :\" engine.commands\" }\n " ) ;
305+ let request = |id : u64 | format ! ( "{}\n " , json!( { "id" : id, "method" : "engine.commands" } ) ) ;
306+ // Each request reaches the socket before the worker reads it (the next one while the worker
307+ // waits for the reply), so only the reply waits outlast the idle timeout, however slowly
308+ // this thread runs.
309+ let mut connection = Connection :: new ( & open, Duration :: from_millis ( 100 ) , |c| c. write_all ( request ( 1 ) . as_bytes ( ) ) . unwrap ( ) ) ;
306310 for id in [ 1 , 2 ] {
307- let req = connection. requests . recv_timeout ( Duration :: from_secs ( 3 ) ) . unwrap ( ) ;
311+ let req = connection. requests . recv_timeout ( Duration :: from_secs ( 10 ) ) . unwrap ( ) ;
308312 assert_eq ! ( req. method, "engine.commands" ) ;
309313 assert ! ( req. take( ) ) ;
314+ if id == 1 {
315+ connection. client . get_mut ( ) . write_all ( request ( 2 ) . as_bytes ( ) ) . unwrap ( ) ;
316+ } else {
317+ connection. client . get_mut ( ) . shutdown ( std:: net:: Shutdown :: Write ) . unwrap ( ) ;
318+ }
310319 // A dispatched command waits for its reply, not for socket input.
311320 std:: thread:: sleep ( Duration :: from_millis ( 250 ) ) ;
312321 // Prepare the next read before releasing this reply. The client need not be
@@ -340,7 +349,8 @@ mod tests {
340349 let ( req, rrx) = ControlRequest :: new ( "ui.inspect" , json ! ( { } ) ) ;
341350 let t0 = std:: time:: Instant :: now ( ) ;
342351 let r = await_reply ( & rrx, & req. state , || 7 , || { } , Duration :: from_millis ( 50 ) , Duration :: from_secs ( 10 ) ) ;
343- assert ! ( t0. elapsed( ) < Duration :: from_secs( 2 ) ) ;
352+ // the frame window (50 ms), not the 10 s timeout; the margin is for a slow machine
353+ assert ! ( t0. elapsed( ) < Duration :: from_secs( 5 ) , "{:?}" , t0. elapsed( ) ) ;
344354 assert_eq ! ( r[ "ok" ] , false ) ;
345355 assert ! ( r[ "error" ] . as_str( ) . unwrap( ) . contains( "not visible" ) , "{r}" ) ;
346356 assert ! ( !req. take( ) , "the app does not run it once it shows up" ) ;
0 commit comments