-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFinalization.test.ts
More file actions
463 lines (376 loc) · 15.8 KB
/
Copy pathFinalization.test.ts
File metadata and controls
463 lines (376 loc) · 15.8 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import * as assert from 'node:assert/strict'
import { describe, it } from 'node:test'
import { abort, Abort, orReturn } from './Abort.js'
import { Fail, fail, returnFail } from './Fail.js'
import { fx, ok, run, type Fx } from './Fx.js'
import { andFinally, andFinallyExit, managed, using, usingExit, usingManaged, type Finally, type Managed } from './Finalization.js'
import type { Interrupt } from './Interrupt.js'
import { returnFrom } from './ReturnFrom.js'
import { scope, type Exit } from './Scope.js'
import { collectFrom, YieldFrom, yieldFrom, yieldScope } from './YieldFrom.js'
describe('Finalization', () => {
const TestScope = 'test/Finalization' as const
const CleanupEvents = yieldScope<'cleanup'>()('test/Finalization/cleanup')
it('preserves finalizer effects in constructor types', () => {
const releaseFailure = new Error('release failed')
const finalizer = andFinally(TestScope, yieldFrom(CleanupEvents, 'cleanup'))
const exitFinalizer = andFinallyExit(TestScope, () => fail(releaseFailure))
const _: typeof finalizer extends Fx<Finally<typeof TestScope, YieldFrom<typeof CleanupEvents>>, void> ? true : false = true
const __: typeof exitFinalizer extends Fx<Finally<typeof TestScope, Fail<Error>>, void> ? true : false = true
assert.equal(typeof _, 'boolean')
assert.equal(typeof __, 'boolean')
})
it('preserves finalizer effects in resource helper types', () => {
const releaseFailure = new Error('release failed')
const resource = using(TestScope, ok('resource'), () => yieldFrom(CleanupEvents, 'cleanup'))
const exitResource = usingExit(TestScope, ok('resource'), () => fail(releaseFailure))
const managedResource = usingManaged(TestScope, ok(managed(
'resource',
() => yieldFrom(CleanupEvents, 'cleanup')
)))
const _: typeof resource extends Fx<Finally<typeof TestScope, YieldFrom<typeof CleanupEvents>> | Interrupt, 'resource'> ? true : false = true
const __: typeof exitResource extends Fx<Finally<typeof TestScope, Fail<Error>> | Interrupt, 'resource'> ? true : false = true
const ___: typeof managedResource extends Fx<Finally<typeof TestScope, YieldFrom<typeof CleanupEvents>> | Interrupt, 'resource'> ? true : false = true
assert.equal(typeof _, 'boolean')
assert.equal(typeof __, 'boolean')
assert.equal(typeof ___, 'boolean')
})
it('exposes non-failure finalizer effects after scope', () => {
const scoped = fx(function* () {
yield* andFinally(TestScope, yieldFrom(CleanupEvents, 'cleanup'))
return 'done'
}).pipe(scope(TestScope))
const _: typeof scoped extends Fx<YieldFrom<typeof CleanupEvents> | Fail<AggregateError>, 'done'> ? true : false = true
const result = run(scoped.pipe(
collectFrom(CleanupEvents),
returnFail
))
assert.equal(typeof _, 'boolean')
assert.deepEqual(result, ['done', ['cleanup']])
})
it('exposes cleanup failures as AggregateError after scope', () => {
const releaseFailure = new Error('release failed')
const scoped = fx(function* () {
yield* andFinally(TestScope, fail(releaseFailure))
return 'done'
}).pipe(scope(TestScope))
const _: typeof scoped extends Fx<Fail<AggregateError>, 'done'> ? true : false = true
const result = run(scoped.pipe(returnFail))
assert.equal(typeof _, 'boolean')
assert.ok(Fail.is(result))
assert.ok(result.arg instanceof AggregateError)
assert.deepEqual(result.arg.errors, [releaseFailure])
})
it('leaves finalizers from a different scope visible after scope', () => {
const OtherScope = 'test/Finalization/other' as const
const scoped = fx(function* () {
yield* andFinally(OtherScope, yieldFrom(CleanupEvents, 'cleanup'))
return 'done'
}).pipe(scope(TestScope))
const _: typeof scoped extends Fx<Finally<typeof OtherScope, YieldFrom<typeof CleanupEvents>>, 'done'> ? true : false = true
const next = scoped[Symbol.iterator]().next()
assert.equal(typeof _, 'boolean')
assert.equal(next.done, false)
assert.equal((next.value as { readonly scope?: unknown }).scope, OtherScope)
})
it('releases finalizers in reverse registration order after success', () => {
const released = [] as string[]
const result = run(fx(function* () {
yield* andFinally(TestScope, record(released, 'A'))
yield* andFinally(TestScope, record(released, 'B'))
yield* andFinally(TestScope, record(released, 'C'))
return 'done'
}).pipe(scope(TestScope), returnFail))
assert.equal(result, 'done')
assert.deepEqual(released, ['C', 'B', 'A'])
})
it('releases finalizers in reverse registration order after failure', () => {
const released = [] as string[]
const programFailure = new Error('program failed')
const result = run(fx(function* () {
yield* andFinally(TestScope, record(released, 'A'))
yield* andFinally(TestScope, record(released, 'B'))
yield* andFinally(TestScope, record(released, 'C'))
yield* fail(programFailure)
}).pipe(scope(TestScope), returnFail))
assert.ok(Fail.is(result))
assert.equal(result.arg, programFailure)
assert.deepEqual(released, ['C', 'B', 'A'])
})
it('aggregates cleanup failures in release order', () => {
const released = [] as string[]
const aFailure = new Error('A release failed')
const cFailure = new Error('C release failed')
const result = run(fx(function* () {
yield* andFinally(TestScope, record(released, 'A', aFailure))
yield* andFinally(TestScope, record(released, 'B'))
yield* andFinally(TestScope, record(released, 'C', cFailure))
return 'done'
}).pipe(scope(TestScope), returnFail))
assert.ok(Fail.is(result))
assert.ok(result.arg instanceof AggregateError)
assert.deepEqual(result.arg.errors, [cFailure, aFailure])
assert.deepEqual(released, ['C', 'B', 'A'])
})
it('keeps program failure first before cleanup failures in release order', () => {
const released = [] as string[]
const programFailure = new Error('program failed')
const aFailure = new Error('A release failed')
const cFailure = new Error('C release failed')
const result = run(fx(function* () {
yield* andFinally(TestScope, record(released, 'A', aFailure))
yield* andFinally(TestScope, record(released, 'B'))
yield* andFinally(TestScope, record(released, 'C', cFailure))
yield* fail(programFailure)
}).pipe(scope(TestScope), returnFail))
assert.ok(Fail.is(result))
assert.ok(result.arg instanceof AggregateError)
assert.deepEqual(result.arg.errors, [programFailure, cFailure, aFailure])
assert.deepEqual(released, ['C', 'B', 'A'])
})
it('provides a success exit to exit-aware finalizers', () => {
const exits = [] as Exit[]
const result = run(fx(function* () {
yield* andFinallyExit(TestScope, exit => fx(function* () {
exits.push(exit)
}))
return 'done'
}).pipe(scope(TestScope), returnFail))
assert.equal(result, 'done')
assert.deepEqual(exits, [{ type: 'success', value: 'done' }])
})
it('provides a failure exit with the original Fail to exit-aware finalizers', () => {
const exits = [] as Exit[]
const programFailure = new Error('program failed')
const result = run(fx(function* () {
yield* andFinallyExit(TestScope, exit => fx(function* () {
exits.push(exit)
}))
yield* fail(programFailure)
}).pipe(scope(TestScope), returnFail))
assert.ok(Fail.is(result))
assert.equal(result.arg, programFailure)
assert.equal(exits.length, 1)
const [exit] = exits
assert.equal(exit.type, 'failure')
if (exit.type === 'failure') assert.equal(exit.failure.arg, programFailure)
})
it('runs multiple exit-aware finalizers in reverse registration order with the same exit', () => {
const released = [] as string[]
const exits = [] as Exit[]
const result = run(fx(function* () {
yield* andFinallyExit(TestScope, exit => fx(function* () {
released.push('A')
exits.push(exit)
}))
yield* andFinallyExit(TestScope, exit => fx(function* () {
released.push('B')
exits.push(exit)
}))
return 'done'
}).pipe(scope(TestScope), returnFail))
assert.equal(result, 'done')
assert.deepEqual(released, ['B', 'A'])
assert.equal(exits.length, 2)
assert.equal(exits[0], exits[1])
assert.deepEqual(exits[0], { type: 'success', value: 'done' })
})
it('using returns the initialized value and registers cleanup', () => {
const released = [] as string[]
const result = run(fx(function* () {
return yield* using(TestScope, ok('resource'),
resource => record(released, resource)
)
}).pipe(scope(TestScope), returnFail))
assert.equal(result, 'resource')
assert.deepEqual(released, ['resource'])
})
it('using does not register cleanup when initialization fails', () => {
const released = [] as string[]
const initFailure = new Error('init failed')
const result = run(fx(function* () {
return yield* using(TestScope, fail(initFailure),
resource => record(released, String(resource))
)
}).pipe(scope(TestScope), returnFail))
assert.ok(Fail.is(result))
assert.equal(result.arg, initFailure)
assert.deepEqual(released, [])
})
it('using runs cleanup when later work fails', () => {
const released = [] as string[]
const programFailure = new Error('program failed')
const result = run(fx(function* () {
yield* using(TestScope,
ok('resource'),
resource => record(released, resource)
)
yield* fail(programFailure)
}).pipe(scope(TestScope), returnFail))
assert.ok(Fail.is(result))
assert.equal(result.arg, programFailure)
assert.deepEqual(released, ['resource'])
})
it('usingExit provides the resource and success exit', () => {
const released = [] as string[]
const exits = [] as Exit[]
const result = run(fx(function* () {
return yield* usingExit(TestScope, ok('resource'),
(resource, exit) => fx(function* () {
released.push(resource)
exits.push(exit)
})
)
}).pipe(scope(TestScope), returnFail))
assert.equal(result, 'resource')
assert.deepEqual(released, ['resource'])
assert.deepEqual(exits, [{ type: 'success', value: 'resource' }])
})
it('usingExit provides the resource and failure exit', () => {
const released = [] as string[]
const exits = [] as Exit[]
const programFailure = new Error('program failed')
const result = run(fx(function* () {
yield* usingExit(TestScope,
ok('resource'),
(resource, exit) => fx(function* () {
released.push(resource)
exits.push(exit)
})
)
yield* fail(programFailure)
}).pipe(scope(TestScope), returnFail))
assert.ok(Fail.is(result))
assert.equal(result.arg, programFailure)
assert.deepEqual(released, ['resource'])
assert.equal(exits.length, 1)
const [exit] = exits
assert.equal(exit.type, 'failure')
if (exit.type === 'failure') assert.equal(exit.failure.arg, programFailure)
})
it('usingExit release failure participates in cleanup aggregation', () => {
const programFailure = new Error('program failed')
const releaseFailure = new Error('release failed')
const result = run(fx(function* () {
yield* usingExit(TestScope,
ok('resource'),
() => fail(releaseFailure)
)
yield* fail(programFailure)
}).pipe(scope(TestScope), returnFail))
assert.ok(Fail.is(result))
assert.ok(result.arg instanceof AggregateError)
assert.deepEqual(result.arg.errors, [programFailure, releaseFailure])
})
it('managed constructs a managed value with an inferred exit finalizer', () => {
const exits = [] as string[]
const m = managed('resource', exit => fx(function* () {
exits.push(exit.type)
}))
assert.equal(m.value, 'resource')
run(m.finalizer({ type: 'success', value: undefined }))
assert.deepEqual(exits, ['success'])
})
it('usingManaged returns the managed value and registers its finalizer', () => {
const released = [] as string[]
const result = run(fx(function* () {
return yield* usingManaged(TestScope, ok(managed(
'resource',
() => record(released, 'resource')
)))
}).pipe(scope(TestScope), returnFail))
assert.equal(result, 'resource')
assert.deepEqual(released, ['resource'])
})
it('usingManaged does not register cleanup when initialization fails', () => {
const released = [] as string[]
const initFailure = new Error('init failed')
const result = run(fx(function* () {
return yield* usingManaged(TestScope, fail(initFailure) as Fx<Fail<Error>, Managed<string>>)
}).pipe(scope(TestScope), returnFail))
assert.ok(Fail.is(result))
assert.equal(result.arg, initFailure)
assert.deepEqual(released, [])
})
it('usingManaged finalizer receives the scope exit', () => {
const exits = [] as Exit[]
const programFailure = new Error('program failed')
const result = run(fx(function* () {
yield* usingManaged(TestScope, ok(managed(
'resource',
exit => fx(function* () {
exits.push(exit)
})
)))
yield* fail(programFailure)
}).pipe(scope(TestScope), returnFail))
assert.ok(Fail.is(result))
assert.equal(result.arg, programFailure)
assert.equal(exits.length, 1)
const [exit] = exits
assert.equal(exit.type, 'failure')
if (exit.type === 'failure') assert.equal(exit.failure.arg, programFailure)
})
it('runs scoped finalizers after returnFrom', () => {
const released = [] as string[]
const result = run(fx(function* () {
yield* andFinally(TestScope, record(released, 'A'))
yield* andFinally(TestScope, record(released, 'B'))
yield* returnFrom(TestScope, 'returned')
}).pipe(scope(TestScope), returnFail))
assert.equal(result, 'returned')
assert.deepEqual(released, ['B', 'A'])
})
it('provides returnFrom exit to exit-aware finalizers', () => {
const exits = [] as Exit[]
const result = run(fx(function* () {
yield* andFinallyExit(TestScope, exit => fx(function* () {
exits.push(exit)
}))
yield* returnFrom(TestScope, 'returned')
}).pipe(scope(TestScope), returnFail))
assert.equal(result, 'returned')
assert.deepEqual(exits, [{
type: 'returnFrom',
scope: TestScope,
value: 'returned'
}])
})
it('runs scoped finalizers after handled abort', () => {
const released = [] as string[]
const result = run(fx(function* () {
yield* andFinally(TestScope, record(released, 'A'))
yield* andFinally(TestScope, record(released, 'B'))
yield* abort(TestScope)
}).pipe(scope(TestScope), orReturn(TestScope, 'aborted'), returnFail))
assert.equal(result, 'aborted')
assert.deepEqual(released, ['B', 'A'])
})
it('provides abort exit to exit-aware finalizers before re-emitting unhandled abort', () => {
const exits = [] as Exit[]
const f = fx(function* () {
yield* andFinallyExit(TestScope, exit => fx(function* () {
exits.push(exit)
}))
yield* abort(TestScope)
}).pipe(scope(TestScope))
const next = f[Symbol.iterator]().next()
assert.equal(Abort.is(next.value), true)
assert.deepEqual(exits, [{ type: 'abort', scope: TestScope }])
})
it('does not run finalizers from a different scope', () => {
const OtherScope = 'test/Finalization/other' as const
const released = [] as string[]
const f = fx(function* () {
yield* andFinally(OtherScope, record(released, 'other'))
return 'done'
}).pipe(scope(TestScope))
const next = f[Symbol.iterator]().next()
assert.equal(next.done, false)
assert.deepEqual(released, [])
})
})
const record = (released: string[], label: string, failure?: unknown) => fx(function* () {
released.push(label)
if (failure !== undefined) yield* fail(failure)
})