|
2 | 2 | * @vitest-environment node |
3 | 3 | */ |
4 | 4 | import { describe, expect, it, vi } from 'vitest' |
5 | | -import { createSingleFlight, runDetached } from '@/lib/core/utils/background' |
| 5 | +import { runDetached } from '@/lib/core/utils/background' |
6 | 6 |
|
7 | 7 | const flushMicrotasks = () => new Promise((resolve) => setTimeout(resolve, 0)) |
8 | 8 |
|
@@ -33,88 +33,3 @@ describe('runDetached', () => { |
33 | 33 | await flushMicrotasks() |
34 | 34 | }) |
35 | 35 | }) |
36 | | - |
37 | | -describe('createSingleFlight', () => { |
38 | | - it('starts work and reports active while in flight', async () => { |
39 | | - const guard = createSingleFlight({ staleAfterMs: 60_000 }) |
40 | | - let release: () => void = () => {} |
41 | | - const gate = new Promise<void>((resolve) => { |
42 | | - release = resolve |
43 | | - }) |
44 | | - |
45 | | - const started = guard.run('task', () => gate) |
46 | | - expect(started).toBe(true) |
47 | | - expect(guard.isActive()).toBe(true) |
48 | | - |
49 | | - release() |
50 | | - await flushMicrotasks() |
51 | | - expect(guard.isActive()).toBe(false) |
52 | | - }) |
53 | | - |
54 | | - it('refuses a second run while one is already in flight', async () => { |
55 | | - const guard = createSingleFlight({ staleAfterMs: 60_000 }) |
56 | | - let release: () => void = () => {} |
57 | | - const gate = new Promise<void>((resolve) => { |
58 | | - release = resolve |
59 | | - }) |
60 | | - |
61 | | - expect(guard.run('task', () => gate)).toBe(true) |
62 | | - expect(guard.run('task', () => Promise.resolve())).toBe(false) |
63 | | - |
64 | | - release() |
65 | | - await flushMicrotasks() |
66 | | - expect(guard.run('task', () => Promise.resolve())).toBe(true) |
67 | | - }) |
68 | | - |
69 | | - it('clears the active flag even when work rejects', async () => { |
70 | | - const guard = createSingleFlight({ staleAfterMs: 60_000 }) |
71 | | - |
72 | | - expect(guard.run('task', () => Promise.reject(new Error('boom')))).toBe(true) |
73 | | - await flushMicrotasks() |
74 | | - expect(guard.isActive()).toBe(false) |
75 | | - }) |
76 | | - |
77 | | - it('takes over a stale run whose work never settles', async () => { |
78 | | - const guard = createSingleFlight({ staleAfterMs: 10 }) |
79 | | - |
80 | | - // A run whose promise never settles — its `finally` never fires. |
81 | | - expect(guard.run('task', () => new Promise<void>(() => {}))).toBe(true) |
82 | | - expect(guard.run('task', () => Promise.resolve())).toBe(false) |
83 | | - |
84 | | - await new Promise((resolve) => setTimeout(resolve, 20)) |
85 | | - |
86 | | - const second = vi.fn().mockResolvedValue(undefined) |
87 | | - expect(guard.run('task', second)).toBe(true) |
88 | | - await flushMicrotasks() |
89 | | - expect(second).toHaveBeenCalledTimes(1) |
90 | | - expect(guard.isActive()).toBe(false) |
91 | | - }) |
92 | | - |
93 | | - it('does not let a late stale run clear a newer run slot', async () => { |
94 | | - const guard = createSingleFlight({ staleAfterMs: 10 }) |
95 | | - |
96 | | - let releaseStale: () => void = () => {} |
97 | | - const stale = new Promise<void>((resolve) => { |
98 | | - releaseStale = resolve |
99 | | - }) |
100 | | - expect(guard.run('task', () => stale)).toBe(true) |
101 | | - |
102 | | - await new Promise((resolve) => setTimeout(resolve, 20)) |
103 | | - |
104 | | - // New run takes over the stale slot. |
105 | | - let releaseFresh: () => void = () => {} |
106 | | - const fresh = new Promise<void>((resolve) => { |
107 | | - releaseFresh = resolve |
108 | | - }) |
109 | | - expect(guard.run('task', () => fresh)).toBe(true) |
110 | | - |
111 | | - // The original stale run settling late must not release the newer slot. |
112 | | - releaseStale() |
113 | | - await flushMicrotasks() |
114 | | - expect(guard.isActive()).toBe(true) |
115 | | - |
116 | | - releaseFresh() |
117 | | - await flushMicrotasks() |
118 | | - expect(guard.isActive()).toBe(false) |
119 | | - }) |
120 | | -}) |
0 commit comments