|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { coalesceAsync } from './coalesceAsync' |
| 3 | + |
| 4 | +/** |
| 5 | + * Let pending microtasks run. A rerun is scheduled off the promise of the run |
| 6 | + * that precedes it, so it starts a few hops after that run settles. |
| 7 | + */ |
| 8 | +const flush = () => new Promise<void>(resolve => setTimeout(resolve, 0)) |
| 9 | + |
| 10 | +/** A task that only settles when the test releases it. */ |
| 11 | +function deferredTask() { |
| 12 | + let release: (() => void) | undefined |
| 13 | + let rejectWith: ((error: Error) => void) | undefined |
| 14 | + let calls = 0 |
| 15 | + |
| 16 | + const run = () => { |
| 17 | + calls += 1 |
| 18 | + return new Promise<void>((resolve, reject) => { |
| 19 | + release = resolve |
| 20 | + rejectWith = reject |
| 21 | + }) |
| 22 | + } |
| 23 | + |
| 24 | + return { |
| 25 | + run, |
| 26 | + get calls() { |
| 27 | + return calls |
| 28 | + }, |
| 29 | + release: () => release?.(), |
| 30 | + reject: (error: Error) => rejectWith?.(error), |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +describe('coalesceAsync', () => { |
| 35 | + it('runs the task immediately when idle', async () => { |
| 36 | + const task = deferredTask() |
| 37 | + const run = coalesceAsync(task.run) |
| 38 | + |
| 39 | + const first = run() |
| 40 | + expect(task.calls).toBe(1) |
| 41 | + |
| 42 | + task.release() |
| 43 | + await first |
| 44 | + }) |
| 45 | + |
| 46 | + // A burst of triggers used to start a restart per event, which left clients |
| 47 | + // disposing and starting concurrently. See #5642. |
| 48 | + it('collapses every call made while running into a single rerun', async () => { |
| 49 | + const task = deferredTask() |
| 50 | + const run = coalesceAsync(task.run) |
| 51 | + |
| 52 | + const first = run() |
| 53 | + expect(task.calls).toBe(1) |
| 54 | + |
| 55 | + const queued = [run(), run(), run(), run()] |
| 56 | + expect(task.calls).toBe(1) |
| 57 | + |
| 58 | + task.release() |
| 59 | + await first |
| 60 | + await flush() |
| 61 | + |
| 62 | + // Exactly one rerun is scheduled, no matter how many calls arrived. |
| 63 | + expect(task.calls).toBe(2) |
| 64 | + |
| 65 | + task.release() |
| 66 | + await Promise.all(queued) |
| 67 | + await flush() |
| 68 | + expect(task.calls).toBe(2) |
| 69 | + }) |
| 70 | + |
| 71 | + it('runs again for calls made after the previous run finished', async () => { |
| 72 | + const task = deferredTask() |
| 73 | + const run = coalesceAsync(task.run) |
| 74 | + |
| 75 | + const first = run() |
| 76 | + task.release() |
| 77 | + await first |
| 78 | + expect(task.calls).toBe(1) |
| 79 | + |
| 80 | + const second = run() |
| 81 | + expect(task.calls).toBe(2) |
| 82 | + task.release() |
| 83 | + await second |
| 84 | + }) |
| 85 | + |
| 86 | + it('resolves the callers that were coalesced together', async () => { |
| 87 | + const task = deferredTask() |
| 88 | + const run = coalesceAsync(task.run) |
| 89 | + |
| 90 | + const first = run() |
| 91 | + const queued = [run(), run()] |
| 92 | + |
| 93 | + task.release() |
| 94 | + await first |
| 95 | + // The rerun has to be in flight before it can be released. |
| 96 | + await flush() |
| 97 | + task.release() |
| 98 | + |
| 99 | + await expect(Promise.all(queued)).resolves.toEqual([undefined, undefined]) |
| 100 | + }) |
| 101 | + |
| 102 | + it('surfaces a failure without wedging later calls', async () => { |
| 103 | + const task = deferredTask() |
| 104 | + const run = coalesceAsync(task.run) |
| 105 | + |
| 106 | + const first = run() |
| 107 | + task.reject(new Error('restart failed')) |
| 108 | + await expect(first).rejects.toThrow('restart failed') |
| 109 | + |
| 110 | + const second = run() |
| 111 | + expect(task.calls).toBe(2) |
| 112 | + task.release() |
| 113 | + await second |
| 114 | + }) |
| 115 | +}) |
0 commit comments