test(timed_cache): de-flake test_expiry_removal (#1408) - #1409
Open
yashksaini-coder wants to merge 1 commit into
Open
test(timed_cache): de-flake test_expiry_removal (#1408)#1409yashksaini-coder wants to merge 1 commit into
yashksaini-coder wants to merge 1 commit into
Conversation
The test added an entry then waited a fixed trio.sleep(2.1) before
asserting the background sweeper had removed it. The sweeper runs in a
separate thread on a ~1s cadence (`_stop_event.wait(sweep_interval)`) and
the TTL is integer-second, so removal timing is coarse; on a slow CI
runner (seen on windows 3.11) the sweep tick landed after 2.1s and the
entry was still present -> 'assert MSG_1 not in cache.cache' failed.
Replace the fixed sleep + immediate assert with a bounded poll until the
entry is swept (the idiom test_simple_last_seen_cache already uses):
with trio.fail_after(15):
while MSG_1 in cache.cache:
await trio.sleep(0.1)
Deterministic and non-hanging (15s timeout if the sweeper is broken).
Closes libp2p#1408.
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.
Summary
Closes #1408.
test_expiry_removalfails intermittently on CI (seen onwindows (3.11, core); green on all Linux runners and locally):Root cause
BaseTimedCachesweeps in a background thread (while not self._stop_event.wait(self.sweep_interval)) with an integer-second TTL. Removal therefore happens on a coarse ~1s cadence in a separate thread. The fixedtrio.sleep(2.1)assumes the sweep tick lands within 2.1s; on a slow Windows runner the thread wakeup is delayed and the entry is still present when the assertion runs. A timing race, not a logic bug.Fix
Poll until the sweeper removes the entry — the idiom
test_simple_last_seen_cachealready uses in this same file:Deterministic (exits as soon as the entry is swept) and non-hanging (fails after 15s if the sweeper is genuinely broken).
Scope
Limited to the test that actually failed in CI. Sibling tests in the file (
test_simple_first_seen_cache,test_timed_cache_expiry,test_timed_cache_stress_test,test_readding_after_expiry) share the same fixed-sleep pattern and are latent flakes on the same root cause — notable as a follow-up, out of scope here.Test plan
test_expiry_removalrun 5× locally → stable passtests/core/tools/timed_cache/→ 8 passedruff check/ruff format --checkcleannewsfragments/1408.internal.rstadded