fix flaky cookieAccess tests in slow CI#4613
Open
watson wants to merge 1 commit into
Open
Conversation
The `document.cookie fallback` variant of cookieAccess.spec was flaky because of a mismatch between mocked time and real time. With `mockClock()` installed, `new Date()` returns the mocked time, so `setCookie()` computes the cookie's expiry as `MOCKED_T_install + expireDelay`. `mockClock()` initializes the mocked time to the current real time, so the absolute value the browser sees is `REAL_T_install + expireDelay`. The browser then evaluates cookie expiration against real wall-clock time. With `expireDelay = 1000` ms, on slow CI runs more than ~1 s of real time could elapse between `mockClock()` and the polling interval's read of `document.cookie`. The browser would consider the cookie expired and `getCookies()` would return []. With `previousCookieValues` captured as [] at construction, the comparison matched, the observable never notified, and `should notify when cookie is changed externally` failed with 0 spy calls. Two complementary changes: - Bump the test cookie expireDelay from 1s to ONE_MINUTE everywhere, so real time can comfortably elapse during a slow test without the cookie disappearing. - Register a generic `deleteCookie(COOKIE_NAME)` cleanup in each `setup()` variant. Three tests (`should pass empty array when cookie does not exist`, `should write the value returned by the callback`, `should notify the observable after writing`) write the cookie via `getAllAndSet` without registering their own cleanup; with the longer expiry those cookies would otherwise leak into the next test and break `should pass empty array when cookie does not exist` under certain random orderings. Registered first so it runs last (LIFO) and is a no-op when a more specific cleanup already deleted the cookie.
Bundles Sizes Evolution
🚀 CPU Performance
🧠 Memory Performance
|
🎉 All green!❄️ No new flaky tests detected 🎯 Code Coverage (details) 🔗 Commit SHA: 203f5c1 | Docs | Datadog PR Page | Give us feedback! |
| { | ||
| title: 'document.cookie fallback', | ||
| setup: () => { | ||
| registerCleanupTask(() => deleteCookie(COOKIE_NAME)) |
Collaborator
There was a problem hiding this comment.
Supposedly we already delete all cookies in the global forEach
If this is not working properly we should probably fix it there, no?
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.

Motivation
The
document.cookie fallbackvariant ofcookieAccess.specis occasionally flaky on CI. The failure mode isshould notify when cookie is changed externallyreporting:The test is unrelated to any specific feature work — it can be reproduced from
mainon slow CI runs.Changes
The root cause is a real-time / mock-time mismatch. When
mockClock()is installed,new Date()returns the mocked time.setCookie()computes the cookie's expiry fromnew Date()+expireDelay, but sincemockClock()initializes the mocked time to the current real time, the absolute expiry the browser sees isREAL_T_install + expireDelay. The browser then expires cookies against real wall-clock time — it doesn't know anything about the mock.With the previous
expireDelay = 1000ms, any CI run slow enough for >1 s of real time to elapse betweenmockClock()and the polling interval readingdocument.cookiewould see the cookie already expired.getCookies()returned[], matchedpreviousCookieValues = []captured at construction, and the observable never notified.Two complementary fixes in
packages/core/src/browser/cookieAccess.spec.ts:expireDelayfrom1000ms toONE_MINUTEat every call site, leaving real time comfortable headroom to elapse during a slow test without the cookie disappearing.deleteCookie(COOKIE_NAME)cleanup in eachsetup()variant. Three tests in the file (should pass empty array when cookie does not exist,should write the value returned by the callback,should notify the observable after writing) write the test cookie viagetAllAndSetwithout registering their own cleanup. Previously the short expiry incidentally limited the blast radius of those leaks; under the longer expiry, the leaked cookies would otherwise persist into the next test and breakshould pass empty array when cookie does not existunder certain Jasmine random orderings. The generic cleanup is registered first so it runs last (LIFO) and is a no-op when a more specific cleanup already deleted the cookie.No production code changes — this is test-only.
Test instructions
To stress the random ordering (including the originally failing seed
00743):Checklist