|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +import { describe, expect, it } from 'vitest' |
| 4 | +import { coalesceAsync } from './coalesceAsync' |
| 5 | + |
| 6 | +/** |
| 7 | + * Let pending microtasks run. A rerun is scheduled off the promise of the run |
| 8 | + * that precedes it, so it starts a few hops after that run settles. |
| 9 | + */ |
| 10 | +const flush = () => new Promise<void>(resolve => setTimeout(resolve, 0)) |
| 11 | + |
| 12 | +/** A task that only settles when the test releases it. */ |
| 13 | +function deferredTask() { |
| 14 | + let release: (() => void) | undefined |
| 15 | + let rejectWith: ((error: Error) => void) | undefined |
| 16 | + let calls = 0 |
| 17 | + |
| 18 | + const run = () => { |
| 19 | + calls += 1 |
| 20 | + return new Promise<void>((resolve, reject) => { |
| 21 | + release = resolve |
| 22 | + rejectWith = reject |
| 23 | + }) |
| 24 | + } |
| 25 | + |
| 26 | + return { |
| 27 | + run, |
| 28 | + get calls() { |
| 29 | + return calls |
| 30 | + }, |
| 31 | + release: () => release?.(), |
| 32 | + reject: (error: Error) => rejectWith?.(error), |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +describe('coalesceAsync', () => { |
| 37 | + it('runs the task immediately when idle', async () => { |
| 38 | + const task = deferredTask() |
| 39 | + const run = coalesceAsync(task.run) |
| 40 | + |
| 41 | + const first = run() |
| 42 | + expect(task.calls).toBe(1) |
| 43 | + |
| 44 | + task.release() |
| 45 | + await first |
| 46 | + }) |
| 47 | + |
| 48 | + // A burst of triggers used to start a restart per event, which left clients |
| 49 | + // disposing and starting concurrently. See #5642. |
| 50 | + it('collapses every call made while running into a single rerun', async () => { |
| 51 | + const task = deferredTask() |
| 52 | + const run = coalesceAsync(task.run) |
| 53 | + |
| 54 | + const first = run() |
| 55 | + expect(task.calls).toBe(1) |
| 56 | + |
| 57 | + const queued = [run(), run(), run(), run()] |
| 58 | + expect(task.calls).toBe(1) |
| 59 | + |
| 60 | + task.release() |
| 61 | + await first |
| 62 | + await flush() |
| 63 | + |
| 64 | + // Exactly one rerun is scheduled, no matter how many calls arrived. |
| 65 | + expect(task.calls).toBe(2) |
| 66 | + |
| 67 | + task.release() |
| 68 | + await Promise.all(queued) |
| 69 | + await flush() |
| 70 | + expect(task.calls).toBe(2) |
| 71 | + }) |
| 72 | + |
| 73 | + it('runs again for calls made after the previous run finished', async () => { |
| 74 | + const task = deferredTask() |
| 75 | + const run = coalesceAsync(task.run) |
| 76 | + |
| 77 | + const first = run() |
| 78 | + task.release() |
| 79 | + await first |
| 80 | + expect(task.calls).toBe(1) |
| 81 | + |
| 82 | + const second = run() |
| 83 | + expect(task.calls).toBe(2) |
| 84 | + task.release() |
| 85 | + await second |
| 86 | + }) |
| 87 | + |
| 88 | + it('resolves the callers that were coalesced together', async () => { |
| 89 | + const task = deferredTask() |
| 90 | + const run = coalesceAsync(task.run) |
| 91 | + |
| 92 | + const first = run() |
| 93 | + const queued = [run(), run()] |
| 94 | + |
| 95 | + task.release() |
| 96 | + await first |
| 97 | + // The rerun has to be in flight before it can be released. |
| 98 | + await flush() |
| 99 | + task.release() |
| 100 | + |
| 101 | + await expect(Promise.all(queued)).resolves.toEqual([undefined, undefined]) |
| 102 | + }) |
| 103 | + |
| 104 | + it('surfaces a failure without wedging later calls', async () => { |
| 105 | + const task = deferredTask() |
| 106 | + const run = coalesceAsync(task.run) |
| 107 | + |
| 108 | + const first = run() |
| 109 | + task.reject(new Error('restart failed')) |
| 110 | + await expect(first).rejects.toThrow('restart failed') |
| 111 | + |
| 112 | + const second = run() |
| 113 | + expect(task.calls).toBe(2) |
| 114 | + task.release() |
| 115 | + await second |
| 116 | + }) |
| 117 | + |
| 118 | + // A failed restart is reported to the caller, which handles it after the run |
| 119 | + // has finished. The not_signed_in handler signs the user in and restarts the |
| 120 | + // client again, so that handler must not run inside the task: the restart it |
| 121 | + // triggers would wait on the run that is still waiting on the handler, and |
| 122 | + // neither would ever settle. See #5920. |
| 123 | + it('lets a failure handler trigger another run without deadlocking', async () => { |
| 124 | + let failNextRun = true |
| 125 | + let reportedError: string | undefined |
| 126 | + let runs = 0 |
| 127 | + |
| 128 | + const runRestart = async (): Promise<void> => { |
| 129 | + runs += 1 |
| 130 | + // The real run awaits the client restart before it knows the outcome. |
| 131 | + // Without that await the re-entrant call below lands in the synchronous |
| 132 | + // prefix of the run, before it has been recorded as in flight, and the |
| 133 | + // deadlock this covers cannot happen. |
| 134 | + const failed = await Promise.resolve(failNextRun) |
| 135 | + if (failed) { |
| 136 | + failNextRun = false |
| 137 | + reportedError = 'not_signed_in' |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + const runRestartSerialized = coalesceAsync(runRestart) |
| 142 | + |
| 143 | + const restart = async (): Promise<void> => { |
| 144 | + await runRestartSerialized() |
| 145 | + |
| 146 | + const error = reportedError |
| 147 | + reportedError = undefined |
| 148 | + if (error === 'not_signed_in') { |
| 149 | + // Stands in for the sign-in flow, which restarts once signed in. |
| 150 | + await restart() |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + await restart() |
| 155 | + |
| 156 | + expect(runs).toBe(2) |
| 157 | + expect(reportedError).toBeUndefined() |
| 158 | + }, 2_000) |
| 159 | +}) |
0 commit comments