Skip to content

Commit

Permalink
tests: test delay for linear, exp strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
jgehrcke committed Aug 16, 2021
1 parent 83f31bb commit 6ba85b6
Showing 1 changed file with 78 additions and 1 deletion.
79 changes: 78 additions & 1 deletion test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,84 @@ describe('retry-axios', () => {
);
});

process.stdout.write(`\n\n ${requesttimes}\n\n`);
it('should have non-zero delay between first and second attempt, linear backoff', async () => {
const requesttimes: bigint[] = [];
const scopes = [
nock(url)
.get('/')
.reply((_, __) => {
requesttimes.push(process.hrtime.bigint());
return [500, 'foo'];
}),
nock(url)
.get('/')
.reply((_, __) => {
requesttimes.push(process.hrtime.bigint());
return [200, 'bar'];
}),
];

interceptorId = rax.attach();
const res = await axios({
url,
raxConfig: {
backoffType: 'linear',
},
});

// Confirm that first retry did yield 200 OK with expected body
assert.strictEqual(res.data, 'bar');
scopes.forEach(s => s.done());

assert.strictEqual(requesttimes.length, 2);
const delayInSeconds = Number(requesttimes[1] - requesttimes[0]) / 10 ** 9;

// The default delay between the first two attempts using the
// linear backoff strategy is 1000 ms. Test with tolerance.
assert.strict(
1.1 > delayInSeconds && delayInSeconds > 1.0,
`unexpected delay: ${delayInSeconds.toFixed(3)} s`
);
});

it('should have non-zero delay between first and second attempt, exp backoff', async () => {
const requesttimes: bigint[] = [];
const scopes = [
nock(url)
.get('/')
.reply((_, __) => {
requesttimes.push(process.hrtime.bigint());
return [500, 'foo'];
}),
nock(url)
.get('/')
.reply((_, __) => {
requesttimes.push(process.hrtime.bigint());
return [200, 'bar'];
}),
];

interceptorId = rax.attach();
const res = await axios({
url,
raxConfig: {
backoffType: 'exponential',
},
});

// Confirm that first retry did yield 200 OK with expected body
assert.strictEqual(res.data, 'bar');
scopes.forEach(s => s.done());

assert.strictEqual(requesttimes.length, 2);
const delayInSeconds = Number(requesttimes[1] - requesttimes[0]) / 10 ** 9;

// The default delay between attempts using the
// exp backoff strategy is 500 ms. Test with tolerance.
assert.strict(
0.55 > delayInSeconds && delayInSeconds > 0.5,
`unexpected delay: ${delayInSeconds.toFixed(3)} s`
);
});

it('should accept a new axios instance', async () => {
Expand Down

0 comments on commit 6ba85b6

Please sign in to comment.