mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 05:01:03 +03:00
test(gui-extensions): wait for worker rpc handlers to start before aborting (#55988)
This commit is contained in:
@@ -33,6 +33,10 @@ Object.assign(globalThis, { ImageBitmap: StandInBitmap })
|
||||
|
||||
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
|
||||
|
||||
// Tells the test a handler is running, so it aborts a running call rather than one the worker has not loaded yet. The
|
||||
// client ignores this message: it carries no call id.
|
||||
const started = (method: string) => self.postMessage({ started: method })
|
||||
|
||||
serveWorker<FixtureMethods>({
|
||||
echo: (input) => {
|
||||
log.ran.push(`echo:${input}`)
|
||||
@@ -47,6 +51,7 @@ serveWorker<FixtureMethods>({
|
||||
},
|
||||
spin: (input) => {
|
||||
log.ran.push("spin")
|
||||
started("spin")
|
||||
|
||||
const until = performance.now() + input
|
||||
|
||||
@@ -61,15 +66,20 @@ serveWorker<FixtureMethods>({
|
||||
hold: (_, signal) => {
|
||||
log.ran.push("hold")
|
||||
|
||||
return new Promise((resolve) =>
|
||||
const done = new Promise<undefined>((resolve) =>
|
||||
signal.addEventListener("abort", () => {
|
||||
log.aborted.push("hold")
|
||||
resolve(undefined)
|
||||
}),
|
||||
)
|
||||
|
||||
started("hold")
|
||||
|
||||
return done
|
||||
},
|
||||
paint: async (input) => {
|
||||
log.ran.push("paint")
|
||||
started("paint")
|
||||
await sleep(input)
|
||||
|
||||
const bitmap = new ImageBitmap()
|
||||
|
||||
@@ -35,9 +35,10 @@ describe("worker rpc", () => {
|
||||
test("an aborted running call aborts its handler's signal", async () => {
|
||||
const client = start()
|
||||
const controller = new AbortController()
|
||||
const running = client.started("hold")
|
||||
const call = client.call("hold", undefined, { signal: controller.signal })
|
||||
|
||||
await Bun.sleep(30)
|
||||
await running
|
||||
controller.abort()
|
||||
await expect(call).rejects.toBeInstanceOf(WorkerClosedError)
|
||||
expect(await client.call("log", undefined)).toEqual({ ran: ["hold"], aborted: ["hold"], closed: 0 })
|
||||
@@ -46,9 +47,10 @@ describe("worker rpc", () => {
|
||||
test("a handler that finishes after its call was aborted closes its bitmaps instead of replying", async () => {
|
||||
const client = start()
|
||||
const controller = new AbortController()
|
||||
const call = client.call("paint", 50, { signal: controller.signal })
|
||||
const running = client.started("paint")
|
||||
const call = client.call("paint", 200, { signal: controller.signal })
|
||||
|
||||
await Bun.sleep(20)
|
||||
await running
|
||||
controller.abort()
|
||||
await expect(call).rejects.toBeInstanceOf(WorkerClosedError)
|
||||
// The stand-in bitmap cannot be posted, so a reply would fail in the worker before it counted a close.
|
||||
@@ -58,9 +60,10 @@ describe("worker rpc", () => {
|
||||
test("a reply that crosses its call's cancel is dropped", async () => {
|
||||
const client = start()
|
||||
const controller = new AbortController()
|
||||
const running = client.started("spin")
|
||||
const call = client.call("spin", 100, { signal: controller.signal })
|
||||
|
||||
await Bun.sleep(30)
|
||||
await running
|
||||
controller.abort()
|
||||
await expect(call).rejects.toBeInstanceOf(WorkerClosedError)
|
||||
// The worker replied before it read the cancel, and the window's next calls still settle with their own replies.
|
||||
@@ -78,12 +81,21 @@ describe("worker rpc", () => {
|
||||
})
|
||||
})
|
||||
|
||||
// Each test starts its own worker, so a fixed delay before an abort can land before the worker has loaded and the cancel
|
||||
// would withdraw a call that never ran. `started` resolves once the fixture reports that the method's handler is running.
|
||||
function start() {
|
||||
const client = createWorkerClient<FixtureMethods>(
|
||||
new Worker(new URL("./worker-rpc.fixture.ts", import.meta.url), { type: "module" }),
|
||||
)
|
||||
const worker = new Worker(new URL("./worker-rpc.fixture.ts", import.meta.url), { type: "module" })
|
||||
const client = createWorkerClient<FixtureMethods>(worker)
|
||||
|
||||
clients.push(client)
|
||||
|
||||
return client
|
||||
return {
|
||||
...client,
|
||||
started: (method: keyof FixtureMethods) =>
|
||||
new Promise<void>((resolve) =>
|
||||
worker.addEventListener("message", (event: MessageEvent<{ readonly started?: string }>) => {
|
||||
if (event.data.started === method) resolve()
|
||||
}),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user