-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall-settled.test.ts
More file actions
344 lines (287 loc) Β· 8.29 KB
/
all-settled.test.ts
File metadata and controls
344 lines (287 loc) Β· 8.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import { describe, expect, it } from "bun:test"
import { CancellationError, Panic } from "../errors"
import * as try$ from "../index"
import { expectPanic, sleep } from "./test-utils"
describe("allSettled", () => {
it("returns empty object when task map is empty", async () => {
const result = await try$.allSettled({})
expect(result).toEqual({})
})
it("returns mixed fulfilled and rejected task results", async () => {
const boom = new Error("boom")
const result = await try$.allSettled({
a() {
return 1
},
b() {
throw boom
},
})
expect(result.a).toEqual({ status: "fulfilled", value: 1 })
expect(result.b).toEqual({ reason: boom, status: "rejected" })
})
it("does not reject outer promise when tasks fail", async () => {
const result = await try$.allSettled({
a() {
throw new Error("a failed")
},
b() {
throw new Error("b failed")
},
})
expect(result.a.status).toBe("rejected")
expect(result.b.status).toBe("rejected")
})
it("allows dependent tasks to handle failed dependencies", async () => {
const result = await try$.allSettled({
a() {
throw new Error("a failed")
},
async b() {
try {
return await this.$result.a
} catch {
return "fallback"
}
},
})
expect(result.a.status).toBe("rejected")
expect(result.b).toEqual({ status: "fulfilled", value: "fallback" })
})
it("resolves dependent tasks when referenced task succeeds", async () => {
const result = await try$.allSettled({
a() {
return 10
},
async b() {
const a = await this.$result.a
return a + 5
},
})
expect(result.a).toEqual({ status: "fulfilled", value: 10 })
expect(result.b).toEqual({ status: "fulfilled", value: 15 })
})
it("passes a non-aborted task signal when no external signal is configured", async () => {
let taskSignalAborted: boolean | undefined
const result = await try$.allSettled({
a() {
taskSignalAborted = this.$signal.aborted
return 1
},
})
expect(result.a).toEqual({ status: "fulfilled", value: 1 })
expect(taskSignalAborted).toBe(false)
})
it("rejects dependent task when referenced task fails", async () => {
const error = new Error("a failed")
const result = await try$.allSettled({
a() {
throw error
},
async b() {
const a = await this.$result.a
return a
},
})
expect(result.a).toEqual({ reason: error, status: "rejected" })
expect(result.b.status).toBe("rejected")
})
it("marks self-referential task as rejected", async () => {
const result = await try$.allSettled({
async a() {
return await (this.$result as Record<string, Promise<unknown>>).a
},
b() {
return 1
},
})
expect(result.a.status).toBe("rejected")
expect(result.b).toEqual({ status: "fulfilled", value: 1 })
})
it("rejects when accessing an unknown task result", async () => {
const result = await try$.allSettled({
async a() {
return await (this.$result as Record<string, Promise<unknown>>).doesNotExist
},
})
expect(result.a.status).toBe("rejected")
expect((result.a as { reason: unknown }).reason).toBeInstanceOf(Panic)
expectPanic((result.a as { reason: unknown }).reason, "TASK_UNKNOWN_REFERENCE")
})
it("rejects invalid handlers in the settled result", async () => {
const result = await try$.allSettled({
a: 123,
} as unknown as {
a(): number
})
expect(result.a.status).toBe("rejected")
expect((result.a as { reason: unknown }).reason).toBeInstanceOf(Panic)
expectPanic((result.a as { reason: unknown }).reason, "TASK_INVALID_HANDLER")
})
it("applies nested run() policies inside allSettled tasks", async () => {
let attempts = 0
const result = await try$.allSettled({
async a() {
return await try$.retry(2).run(() => {
attempts += 1
if (attempts === 1) {
throw new Error("boom")
}
return 1
})
},
async b() {
const value = await try$.timeout(5).run(async () => {
await sleep(20)
return 2
})
if (value instanceof Error) {
throw value
}
return value
},
})
expect(result.a).toEqual({ status: "fulfilled", value: 1 })
expect(result.b.status).toBe("rejected")
expect(attempts).toBe(2)
})
it("does not abort sibling signals when one task fails", async () => {
let signalAbortedInB = false
const result = await try$.allSettled({
a() {
throw new Error("a failed")
},
async b() {
await sleep(20)
signalAbortedInB = this.$signal.aborted
return "b done"
},
})
expect(signalAbortedInB).toBe(false)
expect(result.b).toEqual({ status: "fulfilled", value: "b done" })
})
it("keeps sibling task signals usable after another task fails", async () => {
const signalStates: boolean[] = []
const result = await try$.allSettled({
a() {
throw new Error("a failed")
},
async b() {
signalStates.push(this.$signal.aborted)
await sleep(10)
signalStates.push(this.$signal.aborted)
return "b done"
},
})
expect(signalStates).toEqual([false, false])
expect(result.b).toEqual({ status: "fulfilled", value: "b done" })
})
it("applies wrap middleware around allSettled execution", async () => {
let wrapCalls = 0
const result = await try$
.wrap((ctx, next) => {
wrapCalls += 1
expect(ctx.retry.attempt).toBe(1)
return next()
})
.allSettled({
fail() {
throw new Error("boom")
},
ok() {
return 1
},
})
expect(result.ok).toEqual({ status: "fulfilled", value: 1 })
expect(result.fail.status).toBe("rejected")
expect(wrapCalls).toBe(1)
})
it("runs wrap promise cleanup when allSettled() starts with an already-aborted signal", async () => {
const controller = new AbortController()
let cleaned = false
controller.abort(new Error("stop"))
try {
await try$
.wrap((_, next) =>
Promise.resolve(next()).finally(() => {
cleaned = true
})
)
.signal(controller.signal)
.allSettled({
a() {
return 1
},
})
expect.unreachable("should have thrown")
} catch (error) {
expect(error).toBeInstanceOf(CancellationError)
}
expect(cleaned).toBe(true)
})
it("honors cancellation signal from builder options", async () => {
const controller = new AbortController()
const pending = try$.signal(controller.signal).allSettled({
async a() {
await sleep(20)
if (this.$signal.aborted) {
throw this.$signal.reason
}
return 1
},
async b() {
await sleep(25)
if (this.$signal.aborted) {
throw this.$signal.reason
}
return 2
},
})
setTimeout(() => {
controller.abort(new Error("stop"))
}, 5)
try {
await pending
expect.unreachable("should have thrown")
} catch (error) {
expect(error).toBeInstanceOf(CancellationError)
}
})
it("runs disposer cleanup after all tasks settle", async () => {
let cleaned = false
await try$.allSettled({
a() {
this.$disposer.defer(() => {
cleaned = true
})
return 1
},
b() {
throw new Error("boom")
},
})
expect(cleaned).toBe(true)
})
it("runs disposer cleanup for both fulfilled and rejected tasks without external signals", async () => {
let cleanedA = false
let cleanedB = false
const result = await try$.allSettled({
a() {
this.$disposer.defer(() => {
cleanedA = true
})
return 1
},
b() {
this.$disposer.defer(() => {
cleanedB = true
})
throw new Error("boom")
},
})
expect(result.a).toEqual({ status: "fulfilled", value: 1 })
expect(result.b.status).toBe("rejected")
expect(cleanedA).toBe(true)
expect(cleanedB).toBe(true)
})
})