-
Notifications
You must be signed in to change notification settings - Fork 8
Hold replay batches until Retry-After has passed #55
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
Merged
+100
−7
Merged
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
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,79 @@ | ||
| // Run with: node --test tests/browser/replay-feed.test.js | ||
| // | ||
| // The replay feed's answer to a 429, run rather than read: a fixed one-second | ||
| // retry passes every text check and still asks the server again sixty times | ||
| // inside the window it named. | ||
|
|
||
| import { test } from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
|
|
||
| const replayFeed = await import("../../web/replay-feed.js"); | ||
|
|
||
| function fakeClock() { | ||
| const real = { now: Date.now, setTimeout: globalThis.setTimeout, clearTimeout: globalThis.clearTimeout }; | ||
| let now = 0; | ||
| let nextId = 1; | ||
| const pending = new Map(); | ||
| Date.now = () => now; | ||
| globalThis.setTimeout = (fn, ms) => { | ||
| pending.set(nextId, { at: now + ms, fn }); | ||
| return nextId++; | ||
| }; | ||
| globalThis.clearTimeout = (id) => pending.delete(id); | ||
| return { | ||
| tick(ms) { | ||
| now += ms; | ||
| for (const [id, timer] of [...pending]) { | ||
| if (timer.at > now) continue; | ||
| pending.delete(id); | ||
| timer.fn(); | ||
| } | ||
| }, | ||
| restore() { | ||
| Date.now = real.now; | ||
| globalThis.setTimeout = real.setTimeout; | ||
| globalThis.clearTimeout = real.clearTimeout; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| test("replay-feed waits out Retry-After", async () => { | ||
| const clock = fakeClock(); | ||
| const posts = []; | ||
| const statuses = [429, 204]; | ||
| globalThis.fetch = async (_url, init) => { | ||
| posts.push(JSON.parse(init.body).events.length); | ||
| const status = statuses.shift(); | ||
| return { status, headers: { get: (name) => (name === "Retry-After" ? "30" : null) } }; | ||
| }; | ||
| try { | ||
| replayFeed.initReplay({ | ||
| state: { interviewId: "i1" }, | ||
| nodes: {}, | ||
| recordingEnabled: true, | ||
| consentVersion: "v1", | ||
| replayVersion: 1, | ||
| }); | ||
| replayFeed.recordReplay("lifecycle", { state: "started" }); | ||
| await replayFeed.flushReplay(); | ||
| assert.deepEqual(posts, [1], "the first batch is refused"); | ||
|
|
||
| replayFeed.recordReplay("lifecycle", { state: "still_here" }); | ||
| await replayFeed.flushReplay(); | ||
| assert.deepEqual(posts, [1], "a flush inside the window sends nothing"); | ||
|
|
||
| for (let second = 1; second < 30; second += 1) { | ||
| clock.tick(1000); | ||
| await new Promise(setImmediate); | ||
| } | ||
| assert.deepEqual(posts, [1], "nor does any timer before the window has passed"); | ||
|
|
||
| clock.tick(1000); | ||
| await new Promise(setImmediate); | ||
| assert.deepEqual(posts, [1, 2], "then the kept batch goes, with what queued behind it"); | ||
| } finally { | ||
| replayFeed.closeReplay(); | ||
| clock.restore(); | ||
| delete globalThis.fetch; | ||
| } | ||
| }); | ||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.