diff --git a/opentelemetry-sdk/tests/shared_internal/test_batch_processor.py b/opentelemetry-sdk/tests/shared_internal/test_batch_processor.py index 3fa263d1153..49186404350 100644 --- a/opentelemetry-sdk/tests/shared_internal/test_batch_processor.py +++ b/opentelemetry-sdk/tests/shared_internal/test_batch_processor.py @@ -274,15 +274,16 @@ def test_shutdown_allows_1_export_to_finish(self, batch_processor_class, telemet processor._batch_processor.emit(telemetry) processor._batch_processor.emit(telemetry) processor._batch_processor.emit(telemetry) - before = time.time() + before = time.monotonic() processor._batch_processor.shutdown(timeout_millis=3000) - # Shutdown does not kill the thread. - assert processor._batch_processor._worker_thread.is_alive() is True - after = time.time() + after = time.monotonic() assert after - before < 3.3 - # Thread will naturally finish after a little bit. - time.sleep(0.1) + + # The exporter shutdown interrupts the in-progress export. Depending + # on thread scheduling, the worker may stop before or shortly after + # shutdown() returns. + processor._batch_processor._worker_thread.join(timeout=1) assert processor._batch_processor._worker_thread.is_alive() is False # Expect the second call to be interrupted by shutdown, and the third call to never be made. assert exporter.sleep_interrupted is True