Skip to content

Commit 6ddc22b

Browse files
committed
fix: replace time.sleep with threading.Event in async tests
Address review feedback: use event-based synchronization instead of time.sleep(0.1) in the two async provider tests to avoid flakiness.
1 parent c2eaf24 commit 6ddc22b

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

tests/test_api.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -378,38 +378,58 @@ def test_set_provider_and_wait_with_domain():
378378

379379
def test_set_provider_runs_init_in_background():
380380
"""set_provider() should not raise even if initialize() fails."""
381+
import threading
382+
381383
# Given
382384
provider = MagicMock(spec=FeatureProvider)
383385
provider.initialize.side_effect = Exception("init failed")
386+
error_event = threading.Event()
387+
388+
def handler(_details):
389+
error_event.set()
390+
391+
add_handler(ProviderEvent.PROVIDER_ERROR, handler)
384392

385393
# When - should NOT raise (error is handled in background thread)
386394
set_provider(provider)
387395

388-
# Give the background thread a moment to complete
389-
import time
390-
time.sleep(0.1)
396+
# Wait for the background thread to complete and fire the event
397+
assert error_event.wait(timeout=1), "PROVIDER_ERROR event not received"
391398

392399
# Then - provider should be in ERROR state (not READY)
393400
client = get_client()
394401
assert client.get_provider_status() == ProviderStatus.ERROR
395402

403+
# Cleanup
404+
remove_handler(ProviderEvent.PROVIDER_ERROR, handler)
405+
396406

397407
def test_set_provider_async_eventually_becomes_ready():
398408
"""set_provider() runs init in background, provider transitions to READY."""
409+
import threading
410+
399411
# Given
400412
provider = MagicMock(spec=FeatureProvider)
413+
ready_event = threading.Event()
414+
415+
def handler(_details):
416+
ready_event.set()
417+
418+
add_handler(ProviderEvent.PROVIDER_READY, handler)
401419

402420
# When
403421
set_provider(provider)
404422

405-
# Give the background thread a moment to complete
406-
import time
407-
time.sleep(0.1)
423+
# Wait for the background thread to complete and fire the event
424+
assert ready_event.wait(timeout=1), "PROVIDER_READY event not received"
408425

409426
# Then - provider should be READY
410427
client = get_client()
411428
assert client.get_provider_status() == ProviderStatus.READY
412429

430+
# Cleanup
431+
remove_handler(ProviderEvent.PROVIDER_READY, handler)
432+
413433

414434
def test_provider_status_is_updated_after_provider_emits_event():
415435
# Given

0 commit comments

Comments
 (0)