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

RetryPolicy.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
import { expect, use } from 'chai';import { SinonFakeTimers, SinonStub, stub, useFakeTimers } from 'sinon';import { ExponentialBackoff, IterableBackoff, noJitterGenerator } from './backoff/Backoff';import { runInChild } from './common/util.test';import { handleAll, handleType, handleWhenResult, retry } from './Policy';
use(require('sinon-chai'));use(require('chai-as-promised'));
class MyErrorA extends Error { constructor() { super('Error A'); }}class MyErrorB extends Error { constructor() { super('Error B'); }}
describe('RetryPolicy', () => { it('types return data correctly in all cases', async () => { const policy = retry(handleAll, { maxAttempts: 1 }); const multiply = (n: number) => n * 2; multiply(await policy.execute(() => 42)); multiply(await policy.execute(async () => 42)); });
describe('setting backoffs', () => { let s: SinonStub; let clock: SinonFakeTimers; let delays: number[]; beforeEach(() => { clock = useFakeTimers(); delays = []; s = stub().throws(new MyErrorA()); });
afterEach(() => clock.restore());
const makePolicy = (durations: number[]) => { const p = retry(handleAll, { maxAttempts: durations.length, backoff: new IterableBackoff(durations), }); p.onRetry(({ delay }) => { delays.push(delay); clock.tick(delay); }); return p; };
it('sets the retry delay', async () => { await expect(makePolicy([50]).execute(s)).to.eventually.be.rejectedWith(MyErrorA); expect(delays).to.deep.equal([50]); expect(s).to.have.been.calledTwice; });
it('sets the retry sequence', async () => { await expect(makePolicy([10, 20, 20]).execute(s)).to.eventually.be.rejectedWith(MyErrorA); expect(delays).to.deep.equal([10, 20, 20]); expect(s).to.have.callCount(4); }); });
it('retries all errors', async () => { const s = stub().onFirstCall().throws(new MyErrorA()).onSecondCall().returns('ok');
expect(await retry(handleAll, {}).execute(s)).to.equal('ok');
expect(s).to.have.been.calledTwice; });
it('filters error types', async () => { const s = stub().onFirstCall().throws(new MyErrorA()).onSecondCall().throws(new MyErrorB());
await expect( retry(handleType(MyErrorA), { maxAttempts: 5 }).execute(s), ).to.eventually.be.rejectedWith(MyErrorB);
expect(s).to.have.been.calledTwice; });
it('filters returns', async () => { const s = stub().onFirstCall().returns(1).onSecondCall().returns(2);
expect( await retry( handleWhenResult(r => typeof r === 'number' && r < 2), { maxAttempts: 5 }, ).execute(s), ).to.equal(2);
expect(s).to.have.been.calledTwice; });
it('permits specifying exponential backoffs', async () => { const s = stub().returns(1);
expect( await retry( handleWhenResult(r => typeof r === 'number'), { backoff: new ExponentialBackoff({ generator: noJitterGenerator }), maxAttempts: 2 }, ).execute(s), ).to.equal(1);
expect(s).to.have.callCount(3); });
it('bubbles returns when retry attempts exceeded', async () => { const s = stub().returns(1);
expect( await retry( handleWhenResult(r => typeof r === 'number' && r < 2), { maxAttempts: 5 }, ).execute(s), ).to.equal(1);
expect(s).to.have.callCount(6); });
it('bubbles errors when retry attempts exceeded', async () => { const s = stub().throws(new MyErrorB());
await expect(retry(handleAll, { maxAttempts: 5 }).execute(s)).to.eventually.be.rejectedWith( MyErrorB, );
expect(s).to.have.callCount(6); });
it('does not unref by default', async () => { const output = await runInChild(` c.retry(c.handleAll, { maxAttempts: 1 }).execute(() => { console.log('attempt'); throw new Error('oh no!'); }); `);
expect(output).to.contain('oh no!'); });
it('unrefs as requested', async () => { const output = await runInChild(` c.retry(c.handleAll, { maxAttempts: 1 }).dangerouslyUnref().execute(() => { console.log('attempt'); throw new Error('oh no!'); }); `);
expect(output).to.equal('attempt'); });
it('stops retries if cancellation is requested', async () => { const parent = new AbortController(); const err = new Error(); let calls = 0; await expect( retry(handleAll, { maxAttempts: 3 }).execute(({ signal }) => { calls++; expect(signal.aborted).to.be.false; parent.abort(); expect(signal.aborted).to.be.true; throw err; }, parent.signal), ).to.eventually.be.rejectedWith(err); expect(calls).to.equal(1); });
it('fires onGiveUp', async () => { const err = new MyErrorA(); const s = stub().throws(err); const policy = retry(handleType(MyErrorA), { maxAttempts: 5 }); const onGiveUp = stub(); policy.onGiveUp(onGiveUp);
await expect(policy.execute(s)).to.eventually.be.rejectedWith(MyErrorA); expect(onGiveUp).to.have.been.calledWith({ error: err }); });});
cockatiel

Version Info

Tagged at
5 months ago