-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
73 lines (57 loc) · 1.82 KB
/
test.ts
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
import * as context from ".";
import assert from "node:assert/strict";
// should abort children
{
const { abort, ctx } = context.withAbort(context.TODO);
const { ctx: ctx2 } = context.withAbort(ctx);
abort();
assert.ok(ctx.signal?.aborted);
assert.ok(ctx2.signal?.aborted);
}
// should not abort parent
{
const { ctx } = context.withAbort(context.background);
const { ctx: ctx2, abort } = context.withAbort(ctx);
abort();
assert.equal(ctx.signal?.aborted, false);
assert.ok(ctx2.signal?.aborted);
}
// values should be inherited
{
const key = Symbol("test key");
const otherKey = Symbol("other");
const { ctx } = context.withAbort(context.background);
const ctx2 = context.withValues(ctx, { [key]: "value" });
const { ctx: ctx3, abort } = context.withAbort(ctx2);
const ctx4 = context.withValues(ctx3, { [otherKey]: "otherValue" });
const ctx5 = context.withValues(ctx4, { [key]: "value2" });
abort();
assert.equal(getValue(ctx, key), undefined);
assert.equal(getValue(ctx2, key), "value");
assert.equal(getValue(ctx3, key), "value");
assert.equal(getValue(ctx4, key), "value");
assert.equal(getValue(ctx5, key), "value2");
assert.equal(getValue(ctx3, otherKey), undefined);
assert.equal(getValue(ctx5, otherKey), "otherValue");
}
// timeout should abort
{
const { ctx } = context.withTimeout(context.background, 100);
assert.equal(ctx.signal?.aborted, false);
setTimeout(() => {
assert.ok(ctx.signal?.aborted);
}, 110);
}
// deadline should abort
{
const { ctx } = context.withDeadline(context.background, Date.now() + 100);
assert.equal(ctx.signal?.aborted, false);
setTimeout(() => {
assert.ok(ctx.signal?.aborted);
}, 110);
}
function getValue(ctx: context.Context, key: symbol): unknown {
if (key in ctx) {
return (ctx as Record<typeof key, unknown>)[key];
}
}