Skip to content

Commit b2bafde

Browse files
authored
test: Fix flaky worker-thread source close test (#1108)
`test_generator_source_pulled_in_a_worker_thread_is_closed_once_its_chunk_arrives` failed on Windows in [this run](https://github.com/apify/apify-client-python/actions/runs/37745280813/job/113205183441). The test started the puller thread and returned the 408 right away. If the thread had not entered the generator yet, `close_chunks()` closed the idle generator at once, so the source was closed before the test released it. The source now sets an event just before it blocks, and the fake transport waits for it before responding. The attempt always ends while the worker thread is inside the source. Delaying every thread start by 50 ms reproduced the failure 20/20 before the change and 0/100 after. *✍️ Drafted by Claude Code*
1 parent 2397de6 commit b2bafde

1 file changed

Lines changed: 4 additions & 0 deletions

File tree

‎tests/unit/test_http_clients.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,12 +1542,14 @@ def test_file_source_is_left_open_when_the_transport_stops_pulling_early() -> No
15421542

15431543
def test_generator_source_pulled_in_a_worker_thread_is_closed_once_its_chunk_arrives() -> None:
15441544
"""A source the transport is still pulling when the attempt ends is closed as soon as that chunk is produced."""
1545+
pulling = threading.Event()
15451546
release = threading.Event()
15461547
closed = Mock()
15471548

15481549
def slow_chunks() -> Iterator[bytes]:
15491550
try:
15501551
yield b'first chunk'
1552+
pulling.set()
15511553
release.wait()
15521554
yield b'second chunk'
15531555
finally:
@@ -1563,6 +1565,8 @@ def send_request(self, *, content: bytes | Iterator[bytes] | None, **kwargs: Any
15631565
self.kept = content
15641566
self.puller = threading.Thread(target=next, args=(content, None))
15651567
self.puller.start()
1568+
# The attempt must end while the worker thread is inside the source, or the source closes right away.
1569+
assert pulling.wait(timeout=5)
15661570
return Mock(status_code=408)
15671571

15681572
transport = InFlightTransport()

0 commit comments

Comments
 (0)