@@ -92,29 +92,42 @@ def _slow_client(self, port, recv_bufsize=2048):
9292 return sock
9393
9494 def test_slow_client_trips_cap (self ):
95- """Slow consumer: server eventually gets False from send_ndjson."""
96- # Tight cap so the test trips quickly even with kernel buffering.
97- srv = _SlowClientServer (port = self .PORT , max_send_buffer_size = 4096 )
98- srv .start ()
95+ """Slow consumer: server eventually gets False from send_ndjson.
96+
97+ Uses a monkey-patched _flush_send_buffer to simulate a TCP
98+ socket that never drains. Necessary because real kernel
99+ send/recv buffers vary by orders of magnitude across platforms
100+ (Linux auto-tunes tcp_rmem up to 6 MB), making any
101+ network-only version of this test non-deterministic.
102+ """
103+ original_flush = uhttp_server .HttpConnection ._flush_send_buffer
104+ uhttp_server .HttpConnection ._flush_send_buffer = (
105+ lambda self : False )
99106 try :
100- sock = self ._slow_client (srv .port , recv_bufsize = 2048 )
107+ srv = _SlowClientServer (
108+ port = self .PORT , max_send_buffer_size = 4096 )
109+ srv .start ()
101110 try :
102- # Wait up to 3s for the server to hit the cap.
103- deadline = time .time () + 3.0
104- while time .time () < deadline :
105- if srv .last_send_ok is False :
106- break
107- time .sleep (0.05 )
108- self .assertEqual (
109- srv .last_send_ok , False ,
110- f"send_ndjson never returned False after "
111- f"{ srv .send_attempts } attempts" )
112- # Server cleaned up the connection.
113- self .assertEqual (len (srv .stream_clients ), 0 )
111+ sock = self ._slow_client (srv .port , recv_bufsize = 2048 )
112+ try :
113+ # Wait up to 3s for the server to hit the cap.
114+ deadline = time .time () + 3.0
115+ while time .time () < deadline :
116+ if srv .last_send_ok is False :
117+ break
118+ time .sleep (0.05 )
119+ self .assertEqual (
120+ srv .last_send_ok , False ,
121+ f"send_ndjson never returned False after "
122+ f"{ srv .send_attempts } attempts" )
123+ # Server cleaned up the connection.
124+ self .assertEqual (len (srv .stream_clients ), 0 )
125+ finally :
126+ sock .close ()
114127 finally :
115- sock . close ()
128+ srv . stop ()
116129 finally :
117- srv . stop ()
130+ uhttp_server . HttpConnection . _flush_send_buffer = original_flush
118131
119132 def test_fast_client_does_not_trip_cap (self ):
120133 """Healthy reader: send_ndjson keeps returning True."""
0 commit comments