|
| 1 | +/** |
| 2 | + * Unit tests for gather AbortSignal cancellation. |
| 3 | + * |
| 4 | + * These tests exercise the abort-promise logic used inside callSourceGather |
| 5 | + * without requiring a live Denops instance. |
| 6 | + */ |
| 7 | + |
| 8 | +import { assertEquals } from "@std/assert/equals"; |
| 9 | +import { isDdcCallbackCancelError } from "../callback.ts"; |
| 10 | +import { createAbortPromise } from "../ext.ts"; |
| 11 | + |
| 12 | +// --------------------------------------------------------------------------- |
| 13 | +// Test: an already-aborted signal causes immediate rejection with the right |
| 14 | +// error name so that isDdcCallbackCancelError recognises it. |
| 15 | +// --------------------------------------------------------------------------- |
| 16 | +Deno.test("gather cancel: already-aborted signal rejects with DdcCallbackCancelError", async () => { |
| 17 | + const controller = new AbortController(); |
| 18 | + controller.abort(); |
| 19 | + |
| 20 | + let caught: unknown; |
| 21 | + try { |
| 22 | + await createAbortPromise(controller.signal); |
| 23 | + } catch (e) { |
| 24 | + caught = e; |
| 25 | + } |
| 26 | + |
| 27 | + assertEquals(caught instanceof Error, true, "should throw an Error"); |
| 28 | + assertEquals( |
| 29 | + isDdcCallbackCancelError(caught), |
| 30 | + true, |
| 31 | + "error must satisfy isDdcCallbackCancelError", |
| 32 | + ); |
| 33 | +}); |
| 34 | + |
| 35 | +// --------------------------------------------------------------------------- |
| 36 | +// Test: aborting a signal after Promise.race starts cancels a never-resolving |
| 37 | +// gather and the result is [] (simulating callSourceGather error handling). |
| 38 | +// --------------------------------------------------------------------------- |
| 39 | +Deno.test("gather cancel: aborting mid-flight cancels the gather via Promise.race", async () => { |
| 40 | + const controller = new AbortController(); |
| 41 | + |
| 42 | + // Simulate a slow gather that never completes on its own. |
| 43 | + const slowGather = new Promise<string[]>(() => { |
| 44 | + // intentionally never resolves |
| 45 | + }); |
| 46 | + |
| 47 | + const racePromise = Promise.race([ |
| 48 | + slowGather, |
| 49 | + createAbortPromise(controller.signal), |
| 50 | + ]); |
| 51 | + |
| 52 | + // Abort after a microtask to let the race settle. |
| 53 | + controller.abort(); |
| 54 | + |
| 55 | + let caught: unknown; |
| 56 | + try { |
| 57 | + await racePromise; |
| 58 | + } catch (e) { |
| 59 | + caught = e; |
| 60 | + } |
| 61 | + |
| 62 | + assertEquals(caught instanceof Error, true, "should throw an Error"); |
| 63 | + assertEquals( |
| 64 | + isDdcCallbackCancelError(caught), |
| 65 | + true, |
| 66 | + "error must satisfy isDdcCallbackCancelError", |
| 67 | + ); |
| 68 | +}); |
| 69 | + |
| 70 | +// --------------------------------------------------------------------------- |
| 71 | +// Test: without a signal the gather completes normally (legacy path). |
| 72 | +// --------------------------------------------------------------------------- |
| 73 | +Deno.test("gather cancel: no signal – gather resolves normally", async () => { |
| 74 | + // When callSourceGather receives no signal it just awaits the gather promise. |
| 75 | + const fastGather = Promise.resolve(["item1", "item2"]); |
| 76 | + |
| 77 | + // No signal → just await directly (the `if (!signal) return await gather` |
| 78 | + // path). Simulate that here. |
| 79 | + const result = await fastGather; |
| 80 | + |
| 81 | + assertEquals(result, ["item1", "item2"]); |
| 82 | +}); |
| 83 | + |
| 84 | +// --------------------------------------------------------------------------- |
| 85 | +// Test: a signal that is never aborted does not interfere with normal |
| 86 | +// completion. |
| 87 | +// --------------------------------------------------------------------------- |
| 88 | +Deno.test("gather cancel: non-aborted signal – gather resolves normally", async () => { |
| 89 | + const controller = new AbortController(); |
| 90 | + |
| 91 | + const fastGather = Promise.resolve(["item1"]); |
| 92 | + const abortPromise = createAbortPromise(controller.signal); |
| 93 | + |
| 94 | + const result = await Promise.race([fastGather, abortPromise]); |
| 95 | + |
| 96 | + assertEquals(result, ["item1"]); |
| 97 | +}); |
0 commit comments