Skip to content

Commit 2c703b2

Browse files
okada-shogoclaude
andcommitted
serve: test_responses waits for its own sampler thread
NoLeakedSampler counted every "(_loop)" thread in the process, so a sampler still finishing from an earlier test made it fail now and then. It now records the threads alive before serve() and checks only the one its own server started. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 55dd763 commit 2c703b2

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

‎serve/test_responses.py‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,16 +279,19 @@ def test_server_close_ends_the_sampler(self):
279279
import threading
280280
import time
281281
tok = ByteTokenizer()
282-
before = sum(1 for t in threading.enumerate() if t.name.endswith("(_loop)"))
282+
# follow this server's own sampler thread: a count of all of them raced with a sampler an earlier test closed,
283+
# which can still be ending (it sleeps 1 s between samples) and then drops out of the count mid-test
284+
before = {t for t in threading.enumerate() if t.name.endswith("(_loop)")}
283285
httpd = serve(Service(MockEngine(tok, ANSWER, max_context=16384), tok, TEMPLATE), port=0)
284-
self.assertEqual(sum(1 for t in threading.enumerate() if t.name.endswith("(_loop)")), before + 1)
286+
mine = [t for t in threading.enumerate() if t.name.endswith("(_loop)") and t not in before]
287+
self.assertEqual(len(mine), 1)
285288
httpd.shutdown()
286289
httpd.server_close()
287290
for _ in range(50):
288-
if sum(1 for t in threading.enumerate() if t.name.endswith("(_loop)")) == before:
291+
if not mine[0].is_alive():
289292
break
290293
time.sleep(0.1)
291-
self.assertEqual(sum(1 for t in threading.enumerate() if t.name.endswith("(_loop)")), before)
294+
self.assertFalse(mine[0].is_alive())
292295

293296

294297
# ------------------------------------------------------------------------------------------------ over HTTP

0 commit comments

Comments
 (0)