|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { test } from 'vitest'; |
| 3 | +import { createSnapshotSourceDeadline, waitForSnapshotSourceDelay } from './deadline.ts'; |
| 4 | +import { SnapshotSourceError } from './errors.ts'; |
| 5 | + |
| 6 | +const WAIT_CODE = 'bridge-preparation-pending'; |
| 7 | + |
| 8 | +test('a stopped delay returns without spending the rest of the deadline', async () => { |
| 9 | + const stop = new AbortController(); |
| 10 | + const deadline = createSnapshotSourceDeadline(60_000, undefined); |
| 11 | + const waiting = waitForSnapshotSourceDelay(deadline, 60_000, WAIT_CODE, stop.signal); |
| 12 | + |
| 13 | + const startedAt = Date.now(); |
| 14 | + stop.abort(); |
| 15 | + await waiting; |
| 16 | + assert.ok(Date.now() - startedAt < 5_000, 'a stopped wait does not sleep out its budget'); |
| 17 | +}); |
| 18 | + |
| 19 | +test('a delay started with its stop already aborted does not sleep', async () => { |
| 20 | + const stop = new AbortController(); |
| 21 | + stop.abort(); |
| 22 | + const deadline = createSnapshotSourceDeadline(60_000, undefined); |
| 23 | + |
| 24 | + const startedAt = Date.now(); |
| 25 | + await waitForSnapshotSourceDelay(deadline, 60_000, WAIT_CODE, stop.signal); |
| 26 | + assert.ok(Date.now() - startedAt < 5_000); |
| 27 | +}); |
| 28 | + |
| 29 | +test('an aborted caller signal stays typed cancellation next to a stop', async () => { |
| 30 | + const caller = new AbortController(); |
| 31 | + const deadline = createSnapshotSourceDeadline(60_000, caller.signal); |
| 32 | + const waiting = waitForSnapshotSourceDelay( |
| 33 | + deadline, |
| 34 | + 60_000, |
| 35 | + WAIT_CODE, |
| 36 | + new AbortController().signal, |
| 37 | + ); |
| 38 | + |
| 39 | + caller.abort(); |
| 40 | + await assert.rejects(waiting, (error: unknown) => { |
| 41 | + assert.ok(error instanceof SnapshotSourceError); |
| 42 | + assert.equal(error.failureKind, 'cancelled'); |
| 43 | + assert.equal(error.failureCode, 'abort-signal'); |
| 44 | + return true; |
| 45 | + }); |
| 46 | +}); |
0 commit comments