Skip to content

test: make stale-while-revalidate cache update test deterministic - #5722

Open
mcollina wants to merge 1 commit into
nodejs:mainfrom
mcollina:fix/flaky-sw-revalidate-cache-update-test
Open

test: make stale-while-revalidate cache update test deterministic#5722
mcollina wants to merge 1 commit into
nodejs:mainfrom
mcollina:fix/flaky-sw-revalidate-cache-update-test

Conversation

@mcollina

Copy link
Copy Markdown
Member

Fixes the flaky stale-while-revalidate updates cache after background revalidation (receiving new data) test in test/interceptors/cache.js, which intermittently fails on slow CI runners (e.g. macos) with AssertionError: Expected values to be strictly equal: 'original-response' vs 'updated-response'.

The test triggered a background revalidation (RFC 5861) and then slept a fixed 500ms before asserting the cache had been updated with the revalidated content. On loaded runners the revalidation round-trip plus the cache write can take longer than that window, so the cache was still stale when asserted.

The fix replaces the fixed sleep with a poll that keeps issuing stale requests until the cache actually returns the updated response (bounded by a 5s deadline). This makes the test deterministic regardless of runner load: repeated stale requests are served from cache (so requestsToOrigin stays 1) and each triggers background revalidation, so revalidationRequests >= 1 confirms revalidation occurred.

Verified by:

  • running the full test/interceptors/*.js suite (323 tests, 0 failures)
  • simulating a slow revalidation (800ms server delay) where the old fixed-sleep version reproduces the exact CI assertion error while the polling version passes

The test relied on a fixed 500ms sleep after triggering background
revalidation before asserting the cache had been updated with new data.
On slow/loaded CI runners the revalidation round-trip plus cache write
can exceed that window, so the assertion intermittently saw the stale
'original-response' instead of 'updated-response'.

Replace the fixed sleep with a poll that waits until the cache actually
returns the revalidated content, keeping the test deterministic
regardless of runner load.
@codecov-commenter

codecov-commenter commented Aug 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.46%. Comparing base (5033aa1) to head (705cf81).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5722      +/-   ##
==========================================
- Coverage   93.47%   93.46%   -0.01%     
==========================================
  Files         110      110              
  Lines       38906    38908       +2     
==========================================
  Hits        36366    36366              
- Misses       2540     2542       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants