fix(tests): replace asyncio.sleep(1) with event-based wait to fix flaky metadata callback tests#21805
Merged
ishaan-jaff merged 1 commit intomainfrom Feb 21, 2026
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
Greptile SummaryFixes flaky metadata callback tests by replacing
Confidence Score: 5/5
|
| Filename | Overview |
|---|---|
| tests/test_litellm/responses/test_metadata_codex_callback.py | Replaces flaky asyncio.sleep(1) with event-based wait using asyncio.Event in both test functions. Clean, correct fix with no issues found. |
Sequence Diagram
sequenceDiagram
participant Test as Test Function
participant LiteLLM as litellm.acompletion
participant Worker as LoggingWorker
participant CB as MetadataCaptureCallback
Test->>LiteLLM: await acompletion(model, messages, metadata)
LiteLLM->>Worker: enqueue(async_success_handler)
LiteLLM-->>Test: return result
Note over Test: Before: await asyncio.sleep(1) — race condition
Note over Test: After: await asyncio.wait_for(event.wait(), 5s)
Worker->>CB: await async_log_success_event(kwargs, ...)
CB->>CB: captured_kwargs = kwargs
CB->>CB: event.set()
CB-->>Test: event unblocks wait_for
Test->>Test: assert metadata preserved
Last reviewed commit: 61236c0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Relevant issues
Fixes flaky test
test_metadata_passed_via_litellm_metadata_responses_api(CI test ID: litellm_mapped_tests_core/1262471).What changed
The two tests in
test_metadata_codex_callback.pyusedawait asyncio.sleep(1)to wait for the custom callback to fire. The callback is dispatched viaasyncio.create_task()inutils.py, so the fixed 1-second sleep races against the event loop scheduler — causing intermittent failures.Fix: add an
asyncio.EventtoMetadataCaptureCallbackthat gets.set()insideasync_log_success_event. Tests now doasyncio.wait_for(callback.event.wait(), timeout=5.0)— deterministic, no timing dependency.Pre-Submission checklist
make test-unitpasses locallyType
Changes
tests/test_litellm/responses/test_metadata_codex_callback.py: replaceasyncio.sleep(1)withasyncio.Event-based wait in both test functions