-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[vitest-pool-workers] STOR-5343: Add Durable Object eviction helpers #14398
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a945495
[vitest-pool-workers] Add Durable Object eviction helpers
apeacock1991 2c5d24c
[vitest-pool-workers] Add DO eviction WebSocket option
apeacock1991 7b1044f
Update workerd in miniflare & wrangler
apeacock1991 8c04e9a
Simplify test by removing obsolete example
apeacock1991 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,22 @@ | ||
| --- | ||
| "@cloudflare/vitest-pool-workers": patch | ||
| --- | ||
|
|
||
| Add `evictDurableObject` and `evictAllDurableObjects` test helpers to `cloudflare:test` | ||
|
|
||
| These helpers let you exercise how a Durable Object behaves across evictions in your tests. Eviction is graceful: durable storage is preserved, in-memory state is reset by tearing down the instance, hibernatable WebSockets are hibernated rather than closed, and eviction waits for in-flight requests to drain. | ||
|
|
||
| ```ts | ||
| import { evictDurableObject, evictAllDurableObjects } from "cloudflare:test"; | ||
| import { env } from "cloudflare:workers"; | ||
|
|
||
| const id = env.COUNTER.idFromName("my-counter"); | ||
| const stub = env.COUNTER.get(id); | ||
|
|
||
| // Evict the Durable Object instance pointed to by a specific stub | ||
| await evictDurableObject(stub); | ||
| await evictDurableObject(stub, { webSockets: "close" }); | ||
|
|
||
| // Evict all currently-running Durable Objects in evictable namespaces | ||
| await evictAllDurableObjects(); | ||
| ``` | ||
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
121 changes: 121 additions & 0 deletions
121
fixtures/vitest-pool-workers-examples/durable-objects/test/eviction.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,121 @@ | ||
| import { | ||
| evictAllDurableObjects, | ||
| evictDurableObject, | ||
| runInDurableObject, | ||
| } from "cloudflare:test"; | ||
| import { env } from "cloudflare:workers"; | ||
| import { it } from "vitest"; | ||
| import { Counter } from "../src/"; | ||
|
|
||
| function getResponseWebSocket(response: Response) { | ||
| const socket = response.webSocket; | ||
| if (socket === null || socket === undefined) { | ||
| throw new TypeError("Expected WebSocket response"); | ||
| } | ||
| return socket; | ||
| } | ||
|
|
||
| function waitForMessage(socket: WebSocket) { | ||
| return new Promise<string>((resolve, reject) => { | ||
| const timeout = setTimeout(() => { | ||
| reject(new Error("Timed out waiting for WebSocket message")); | ||
| }, 10_000); | ||
| socket.addEventListener("message", (event) => { | ||
| clearTimeout(timeout); | ||
| resolve( | ||
| typeof event.data === "string" | ||
| ? event.data | ||
| : new TextDecoder().decode(event.data as ArrayBuffer) | ||
| ); | ||
| }); | ||
| socket.addEventListener("error", () => { | ||
| clearTimeout(timeout); | ||
| reject(new Error("WebSocket error while waiting for message")); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| function waitForClose(socket: WebSocket) { | ||
| return new Promise<CloseEvent>((resolve, reject) => { | ||
| const timeout = setTimeout(() => { | ||
| reject(new Error("Timed out waiting for WebSocket close")); | ||
| }, 10_000); | ||
| socket.addEventListener("close", (event) => { | ||
| clearTimeout(timeout); | ||
| resolve(event); | ||
| }); | ||
| socket.addEventListener("error", () => { | ||
| clearTimeout(timeout); | ||
| reject(new Error("WebSocket error while waiting for close")); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| it("resets in-memory state but preserves storage on targeted eviction", async ({ | ||
| expect, | ||
| }) => { | ||
| const id = env.COUNTER.idFromName(`evict-${crypto.randomUUID()}`); | ||
| const stub = env.COUNTER.get(id); | ||
|
|
||
| // Persist `count = 2` through the `fetch()` handler | ||
| expect(await (await stub.fetch("https://example.com/")).text()).toBe("1"); | ||
| expect(await (await stub.fetch("https://example.com/")).text()).toBe("2"); | ||
|
|
||
| // Corrupt in-memory state without persisting it to storage | ||
| await runInDurableObject(stub, (instance: Counter) => { | ||
| instance.count = 999; | ||
| }); | ||
|
|
||
| await evictDurableObject(stub, { webSockets: "hibernate" }); | ||
|
|
||
| // After eviction the instance is torn down: the in-memory `999` is discarded | ||
| // and `count` is reloaded from storage (`2`), so the next increment yields `3` | ||
| expect(await (await stub.fetch("https://example.com/")).text()).toBe("3"); | ||
| }); | ||
|
|
||
| it("resets all running instances with bulk eviction", async ({ expect }) => { | ||
| const id = env.COUNTER.idFromName(`evict-all-${crypto.randomUUID()}`); | ||
| const stub = env.COUNTER.get(id); | ||
|
|
||
| expect(await (await stub.fetch("https://example.com/")).text()).toBe("1"); | ||
| await runInDurableObject(stub, (instance: Counter) => { | ||
| instance.count = 999; | ||
| }); | ||
|
|
||
| await evictAllDurableObjects(); | ||
|
|
||
| expect(await (await stub.fetch("https://example.com/")).text()).toBe("2"); | ||
| }); | ||
|
|
||
| it("hibernates WebSockets across eviction", async ({ expect }) => { | ||
| const id = env.COUNTER.idFromName(`evict-ws-${crypto.randomUUID()}`); | ||
| const stub = env.COUNTER.get(id); | ||
| const response = await stub.fetch("https://example.com/websocket-order", { | ||
| headers: { Upgrade: "websocket" }, | ||
| }); | ||
| const socket = getResponseWebSocket(response); | ||
| socket.accept(); | ||
|
|
||
| await evictDurableObject(stub); | ||
|
|
||
| // The WebSocket should be hibernated rather than closed, so messages still | ||
| // round-trip after eviction (waking the Durable Object back up) | ||
| const messagePromise = waitForMessage(socket); | ||
| socket.send("after-eviction"); | ||
| expect(await messagePromise).toBe("after-eviction"); | ||
| socket.close(1000, "done"); | ||
| }); | ||
|
|
||
| it("closes WebSockets when requested during eviction", async ({ expect }) => { | ||
| const id = env.COUNTER.idFromName(`evict-ws-close-${crypto.randomUUID()}`); | ||
| const stub = env.COUNTER.get(id); | ||
| const response = await stub.fetch("https://example.com/websocket-order", { | ||
| headers: { Upgrade: "websocket" }, | ||
| }); | ||
| const socket = getResponseWebSocket(response); | ||
| socket.accept(); | ||
|
|
||
| const closePromise = waitForClose(socket); | ||
| await evictDurableObject(stub, { webSockets: "close" }); | ||
| expect(await closePromise).toBeDefined(); | ||
| }); |
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.