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

CircuitBreakerPolicy.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
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
import { expect } from 'chai';import { SinonFakeTimers, SinonStub, stub, useFakeTimers } from 'sinon';import { promisify } from 'util';import { ConsecutiveBreaker } from './breaker/Breaker';import { CircuitBreakerPolicy, CircuitState } from './CircuitBreakerPolicy';import { abortedSignal } from './common/abort';import { BrokenCircuitError, TaskCancelledError } from './errors/Errors';import { IsolatedCircuitError } from './errors/IsolatedCircuitError';import { circuitBreaker, handleAll, handleType } from './Policy';
class MyException extends Error {}
const delay = promisify(setTimeout);
describe('CircuitBreakerPolicy', () => { let p: CircuitBreakerPolicy; let clock: SinonFakeTimers; let onBreak: SinonStub; let onReset: SinonStub; let onHalfOpen: SinonStub;
beforeEach(() => { p = circuitBreaker(handleType(MyException), { halfOpenAfter: 1000, breaker: new ConsecutiveBreaker(2), }); clock = useFakeTimers(); onBreak = stub(); onReset = stub(); onHalfOpen = stub(); p.onBreak(onBreak); p.onReset(onReset); p.onHalfOpen(onHalfOpen); });
afterEach(() => { clock.restore(); });
const openBreaker = async () => { const s = stub().throws(new MyException()); await expect(p.execute(s)).to.be.rejectedWith(MyException); await expect(p.execute(s)).to.be.rejectedWith(MyException); };
it('allows calls when open', async () => { expect(await p.execute(() => 42)).to.equal(42); });
it('opens after failing calls', async () => { const s = stub().throws(new MyException());
await expect(p.execute(s)).to.be.rejectedWith(MyException); expect(p.state).to.equal(CircuitState.Closed); expect(onBreak).not.called;
await expect(p.execute(s)).to.be.rejectedWith(MyException); expect(p.state).to.equal(CircuitState.Open); expect(onBreak).called;
await expect(p.execute(s)).to.be.rejectedWith(BrokenCircuitError); expect(p.state).to.equal(CircuitState.Open);
expect((p.lastFailure as any).error).to.be.an.instanceOf(MyException); expect(onBreak).calledOnce; expect(s).calledTwice; });
it('closes if the half open test succeeds', async () => { await openBreaker();
clock.tick(1000);
const result = p.execute(stub().resolves(42)); expect(p.state).to.equal(CircuitState.HalfOpen); expect(onHalfOpen).calledOnce; expect(await result).to.equal(42); expect(p.state).to.equal(CircuitState.Closed); expect(onReset).calledOnce; });
it('dedupes half-open tests', async () => { await openBreaker(); clock.tick(1000);
// Two functinos, a and b. We execute with "a" first, and then make sure // it returns before "b" gets called. let aReturned = false; const a = async () => { await delay(10); aReturned = true; return 1; };
const b = async () => { expect(aReturned).to.be.true; return 2; };
const todo = [ expect(p.execute(a)).to.eventually.equal(1), expect(p.execute(b)).to.eventually.equal(2), ];
clock.tick(10);
await Promise.all(todo); });
it('stops deduped half-open tests if the circuit reopens', async () => { await openBreaker(); clock.tick(1000);
// Two functinos, a and b. We execute with "a" first, and then make sure // it returns before "b" gets called. const a = async () => { await delay(10); throw new MyException(); };
const b = async () => { throw new Error('expected to not be called'); };
const todo = [ expect(p.execute(a)).to.be.rejectedWith(MyException), expect(p.execute(b)).to.be.rejectedWith(BrokenCircuitError), ];
clock.tick(10);
await Promise.all(todo); });
it('re-opens if the half open fails', async () => { await openBreaker();
clock.tick(1000);
const s = stub().throws(new MyException()); await expect(p.execute(s)).to.be.rejectedWith(MyException); expect(p.state).to.equal(CircuitState.Open); });
it('handles isolation correctly', async () => { const handle1 = p.isolate(); expect(onBreak).calledOnceWith({ isolated: true });
const handle2 = p.isolate(); expect(onBreak).calledOnce;
expect(p.state).to.equal(CircuitState.Isolated); await expect(p.execute(() => 42)).to.be.rejectedWith(IsolatedCircuitError);
handle1.dispose(); expect(p.state).to.equal(CircuitState.Isolated); expect(onReset).not.called;
handle2.dispose(); expect(p.state).to.equal(CircuitState.Closed); expect(onReset).calledOnce;
expect(await p.execute(() => 42)).to.equal(42); });
it('links parent cancellation token', async () => { const parent = new AbortController(); await circuitBreaker(handleAll, { halfOpenAfter: 1000, breaker: new ConsecutiveBreaker(3), }).execute(({ signal }) => { expect(signal.aborted).to.be.false; parent.abort(); expect(signal.aborted).to.be.true; }, parent.signal); });
it('aborts function execution if half open test succeeds', async () => { await openBreaker();
clock.tick(1000);
// half open test: p.execute(stub().resolves(42));
// queued timeout: await expect(p.execute(stub(), abortedSignal)).to.be.rejectedWith(TaskCancelledError);
expect(p.state).to.equal(CircuitState.Closed); expect(onReset).calledOnce; });});
cockatiel

Version Info

Tagged at
5 months ago