Skip to content

Commit b1f499b

Browse files
Anurag-M1herin049
andauthored
test(sdk): fix flaky BatchProcessor shutdown test (#5663) (#5675)
In test_shutdown_allows_1_export_to_finish, processor.shutdown() interrupts the in-progress export via exporter.shutdown(). Depending on thread scheduling, particularly on Windows and PyPy runners, the worker thread can terminate before shutdown() returns, causing the intermediate assertion assert worker_thread.is_alive() is True to fail intermittently. Fix this by: - Using time.monotonic() instead of time.time() for elapsed time. - Removing the unstable intermediate is_alive() is True check. - Waiting deterministically with worker_thread.join(timeout=1) before asserting that the worker thread has terminated. Fixes #5663 Assisted-by: Gemini 3.8 Flash Co-authored-by: Lukas Hering <40302054+herin049@users.noreply.github.com>
1 parent 5321c60 commit b1f499b

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

opentelemetry-sdk/tests/shared_internal/test_batch_processor.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,15 +274,16 @@ def test_shutdown_allows_1_export_to_finish(self, batch_processor_class, telemet
274274
processor._batch_processor.emit(telemetry)
275275
processor._batch_processor.emit(telemetry)
276276
processor._batch_processor.emit(telemetry)
277-
before = time.time()
277+
before = time.monotonic()
278278
processor._batch_processor.shutdown(timeout_millis=3000)
279-
# Shutdown does not kill the thread.
280-
assert processor._batch_processor._worker_thread.is_alive() is True
281279

282-
after = time.time()
280+
after = time.monotonic()
283281
assert after - before < 3.3
284-
# Thread will naturally finish after a little bit.
285-
time.sleep(0.1)
282+
283+
# The exporter shutdown interrupts the in-progress export. Depending
284+
# on thread scheduling, the worker may stop before or shortly after
285+
# shutdown() returns.
286+
processor._batch_processor._worker_thread.join(timeout=1)
286287
assert processor._batch_processor._worker_thread.is_alive() is False
287288
# Expect the second call to be interrupted by shutdown, and the third call to never be made.
288289
assert exporter.sleep_interrupted is True

0 commit comments

Comments
 (0)