-
Notifications
You must be signed in to change notification settings - Fork 440
DO NOT MERGE: validate refresh timer cleanup test fails without fix #8134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jacekradko
wants to merge
1
commit into
main
Choose a base branch
from
jacek/test-only-refresh-timer-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
integration/tests/session-token-cache/refresh-timer-cleanup.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
|
|
||
| import { appConfigs } from '../../presets'; | ||
| import type { FakeUser } from '../../testUtils'; | ||
| import { createTestUtils, testAgainstRunningApps } from '../../testUtils'; | ||
|
|
||
| /** | ||
| * Tests that the token cache's proactive refresh timer does not accumulate | ||
| * orphaned timers across set() calls. | ||
| * | ||
| * Background: Every API response that piggybacks client data triggers _updateClient, | ||
| * which reconstructs Session objects and calls #hydrateCache → SessionTokenCache.set(). | ||
| * Without proper timer cleanup, each set() call would leave the previous refresh timer | ||
| * running, causing the effective polling rate to accelerate over time. | ||
| */ | ||
| testAgainstRunningApps({ withEnv: [appConfigs.envs.withEmailCodes] })( | ||
| 'Token refresh timer cleanup @generic', | ||
| ({ app }) => { | ||
| test.describe.configure({ mode: 'serial' }); | ||
|
|
||
| let fakeUser: FakeUser; | ||
|
|
||
| test.beforeAll(async () => { | ||
| const u = createTestUtils({ app }); | ||
| fakeUser = u.services.users.createFakeUser(); | ||
| await u.services.users.createBapiUser(fakeUser); | ||
| }); | ||
|
|
||
| test.afterAll(async () => { | ||
| await fakeUser.deleteIfExists(); | ||
| await app.teardown(); | ||
| }); | ||
|
|
||
| test('touch does not cause clustered token refresh requests', async ({ page, context }) => { | ||
| test.setTimeout(120_000); | ||
| const u = createTestUtils({ app, page, context }); | ||
|
|
||
| await u.po.signIn.goTo(); | ||
| await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeUser.email, password: fakeUser.password }); | ||
| await u.po.expect.toBeSignedIn(); | ||
|
|
||
| // Track token fetch requests with timestamps | ||
| const tokenRequests: number[] = []; | ||
| await page.route('**/v1/client/sessions/*/tokens**', async route => { | ||
| tokenRequests.push(Date.now()); | ||
| await route.continue(); | ||
| }); | ||
|
|
||
| // Trigger multiple touch() calls — each causes _updateClient → Session constructor | ||
| // → #hydrateCache → set(), which previously leaked orphaned refresh timers | ||
| for (let i = 0; i < 5; i++) { | ||
| await page.evaluate(async () => { | ||
| await (window as any).Clerk?.session?.touch(); | ||
| }); | ||
| } | ||
|
|
||
| // Wait 50s — enough for one refresh cycle (~43s) but not two | ||
| // eslint-disable-next-line playwright/no-wait-for-timeout | ||
| await page.waitForTimeout(50_000); | ||
|
|
||
| await page.unrouteAll(); | ||
|
|
||
| // With the fix: at most 1-2 refresh requests (one cycle at ~43s) | ||
| // Without the fix: 5+ requests from orphaned timers all firing at different offsets | ||
| expect(tokenRequests.length).toBeLessThanOrEqual(3); | ||
|
|
||
| // If multiple requests occurred, verify they aren't clustered together | ||
| // (clustering = orphaned timers firing near-simultaneously) | ||
| if (tokenRequests.length >= 2) { | ||
| for (let i = 1; i < tokenRequests.length; i++) { | ||
| const gap = tokenRequests[i] - tokenRequests[i - 1]; | ||
| expect(gap).toBeGreaterThan(10_000); | ||
| } | ||
| } | ||
| }); | ||
| }, | ||
| ); | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is unmergeable without the matching token-cache fix.
These assertions require behavior that current
maindoes not have yet: theSessionTokenCache.set()timer cleanup is intentionally omitted in this PR. Merging this file by itself will keep CI red, so the implementation fix needs to land with the test, or this validation test should stay skipped/xfail on this branch.🤖 Prompt for AI Agents