deno.land / x / cockatiel@v3.1.2 / src / BulkheadPolicy.test.ts

BulkheadPolicy.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
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
import { expect } from 'chai';import { promisify } from 'util';import { abortedSignal } from './common/abort';import { defer } from './common/defer';import { BulkheadRejectedError } from './errors/BulkheadRejectedError';import { TaskCancelledError } from './errors/Errors';import { bulkhead } from './Policy';
const delay = promisify(setTimeout);
describe('Bulkhead', () => { let order: string[] = []; let fnIndex = 0; beforeEach(() => { order = []; fnIndex = 0; });
const makeFn = () => { const index = fnIndex++; return async () => { order.push(`${index}: enter`); await delay(index * 2); order.push(`${index}: exit`); return index; }; };
const makeFns = (count: number) => { const out: Array<() => void> = []; for (let i = 0; i < count; i++) { out.push(makeFn()); } return out; };
it('rejects calls after limit is hit', async () => { const b = bulkhead(2); const funcs = makeFns(3); const output = funcs.map(fn => b.execute(fn));
await Promise.all([ expect(output[0]).to.eventually.equal(0), expect(output[1]).to.eventually.equal(1), expect(output[2]).to.be.rejectedWith(BulkheadRejectedError), ]);
expect(order).to.deep.equal(['0: enter', '1: enter', '0: exit', '1: exit']); });
it('queues requests, and rejects after queue limit', async () => { const b = bulkhead(2, 2); const funcs = makeFns(5); const output = funcs.map(fn => b.execute(fn));
await Promise.all([ expect(output[0]).to.eventually.equal(0), expect(output[1]).to.eventually.equal(1), expect(output[2]).to.eventually.equal(2), expect(output[3]).to.eventually.equal(3), expect(output[4]).to.be.rejectedWith(BulkheadRejectedError), ]);
expect(order).to.deep.equal([ '0: enter', '1: enter', '0: exit', '2: enter', '1: exit', '3: enter', '2: exit', '3: exit', ]); });
it('maintains proper state', async () => { const b = bulkhead(2, 2); const defer1 = defer(); const defer2 = defer(); const defer3 = defer(); const defer4 = defer();
expect(b.queueSlots).to.equal(2); expect(b.executionSlots).to.equal(2);
const out1 = b.execute(() => defer1.promise); expect(b.queueSlots).to.equal(2); expect(b.executionSlots).to.equal(1);
const out2 = b.execute(() => defer2.promise); expect(b.queueSlots).to.equal(2); expect(b.executionSlots).to.equal(0);
const out3 = b.execute(() => defer3.promise); expect(b.queueSlots).to.equal(1); expect(b.executionSlots).to.equal(0);
const out4 = b.execute(() => defer4.promise); expect(b.queueSlots).to.equal(0); expect(b.executionSlots).to.equal(0);
defer1.resolve(undefined); await out1; expect(b.executionSlots).to.equal(0); expect(b.queueSlots).to.equal(1);
defer2.resolve(undefined); await out2; expect(b.executionSlots).to.equal(0); expect(b.queueSlots).to.equal(2);
defer3.resolve(undefined); await out3; expect(b.executionSlots).to.equal(1); expect(b.queueSlots).to.equal(2);
defer4.resolve(undefined); await out4; expect(b.executionSlots).to.equal(2); expect(b.queueSlots).to.equal(2); });
it('links parent cancellation token', async () => { const b = bulkhead(1, Infinity); const todo: Array<PromiseLike<void>> = []; for (let i = 0; i < 3; i++) { const parent = new AbortController(); todo.push( b.execute(async ({ signal }) => { await delay(1); expect(signal.aborted).to.be.false; parent.abort(); expect(signal.aborted).to.be.true; }, parent.signal), ); }
// initially cancelled todo.push( expect( b.execute(() => { throw new Error('expected not to call'); }, abortedSignal), ).to.be.rejectedWith(TaskCancelledError), );
// cancelled by the time it gets executed const cancelledCts = new AbortController(); setTimeout(() => cancelledCts.abort(), 2); todo.push( expect( b.execute(() => { throw new Error('expected not to call'); }, cancelledCts.signal), ).to.be.rejectedWith(TaskCancelledError), );
await Promise.all(todo); });});
cockatiel

Version Info

Tagged at
5 months ago