Open History to see the last commit
1import { expect, it } from 'vitest'
2import { harnessScope } from './contracts/harness-scope'
3
4it.each([false, true])(
5 `owns all returned harnesses async=%s`,
6 async (asyncCleanup) => {
7 const calls: Array<number> = []
8 const scope = harnessScope((id: number) => ({
9 id,
10 cleanup() {
11 const record = () => {
12 calls.push(this.id)
13 }
14 return asyncCleanup ? Promise.resolve().then(record) : record()
15 },
16 }))
17 expect(scope.create(1).id).toBe(1)
18 expect(scope.create(2).id).toBe(2)
19 await scope.cleanup()
20 expect(calls).toEqual([2, 1])
21 await scope.cleanup()
22 expect(calls).toEqual([2, 1])
23 scope.create(3)
24 await scope.cleanup()
25 expect(calls).toEqual([2, 1, 3])
26 },
27)
28
29it.each(
30 [false, true].flatMap((asyncCleanup) =>
31 [undefined, `failure`, new Error(`failure`)].map((error) => ({
32 asyncCleanup,
33 error,
34 })),
35 ),
36)(
37 `drains healthy peers and preserves identical failures async=$asyncCleanup error=$error`,
38 async ({ asyncCleanup, error }) => {
39 const calls: Array<number> = []
40 const scope = harnessScope((id: number) => ({
41 cleanup: () => {
42 calls.push(id)
43 if (id === 1) return
44 if (asyncCleanup) return Promise.reject(error)
45 throw error
46 },
47 }))
48 for (const id of [1, 2, 3]) scope.create(id)
49 const result = await scope.cleanup().then(
50 () => ({ ok: true as const }),
51 (caught: unknown) => ({ ok: false as const, caught }),
52 )
53 expect(result.ok).toBe(false)
54 if (result.ok) throw new Error(`Expected cleanup failure`)
55 expect(result.caught).toBeInstanceOf(AggregateError)
56 const aggregate = result.caught as AggregateError
57 expect(aggregate.errors).toEqual([error, error])
58 expect(aggregate.cause).toBe(error)
59 expect(calls).toEqual([3, 2, 1])
60 await scope.cleanup()
61 expect(calls).toEqual([3, 2, 1])
62 },
63)
64
65it(`waits for an actual cleanup gate before finishing or disposing earlier peers`, async () => {
66 let release!: () => void
67 let enter!: () => void
68 const hold = new Promise<void>((resolve) => {
69 release = resolve
70 })
71 const entered = new Promise<void>((resolve) => {
72 enter = resolve
73 })
74 const calls: Array<number> = []
75 const scope = harnessScope((id: number) => ({
76 cleanup: async () => {
77 calls.push(id)
78 if (id === 2) {
79 enter()
80 await hold
81 }
82 },
83 }))
84 scope.create(1)
85 scope.create(2)
86 let finished = false
87 const drained = scope.cleanup().then(() => {
88 finished = true
89 })
90 void drained.catch(() => undefined)
91 try {
92 await entered
93 expect(finished).toBe(false)
94 expect(calls).toEqual([2])
95 } finally {
96 release()
97 await drained
98 }
99 expect(finished).toBe(true)
100 expect(calls).toEqual([2, 1])
101})
102
103it(`keeps a single raw cleanup error and scope ownership independent`, async () => {
104 const error = new Error(`one`)
105 const scope = harnessScope(() => ({
106 cleanup: () => {
107 throw error
108 },
109 }))
110 let peerCleanups = 0
111 const peer = harnessScope(() => ({
112 cleanup: () => {
113 peerCleanups++
114 },
115 }))
116 scope.create()
117 peer.create()
118 await expect(scope.cleanup()).rejects.toBe(error)
119 expect(peerCleanups).toBe(0)
120 await peer.cleanup()
121 expect(peerCleanups).toBe(1)
122})