Skip to content

Commit

Permalink
Add test for retry backoffLimit
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank van Wijk committed Sep 14, 2022
1 parent b261374 commit 18ef588
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions test/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,3 +440,50 @@ test('throws when retry.statusCodes is not an array', async t => {

await server.close();
});

test('respect maximum backoff', async t => {
const retryCount = 6;
let requestCount = 0;

const server = await createHttpTestServer();
server.get('/', (_request, response) => {
requestCount++;

if (requestCount === retryCount) {
response.end(fixture);
} else {
response.sendStatus(500);
}
});

performance.mark('start');
t.is(await ky(server.url, {
retry: retryCount,
}).text(), fixture);
performance.mark('end');

performance.mark('start-custom');
requestCount = 0;
t.is(await ky(server.url, {
retry: {
limit: retryCount,
backoffLimit: 1000
},
}).text(), fixture);
performance.mark('end-custom');

performance.measure('default', 'start', 'end');
performance.measure('custom', 'start-custom', 'end-custom');

const measurements = performance.getEntriesByType("measure");

const duration = measurements.at(0)?.duration ?? NaN;
const expectedDuration = 300 + 600 + 1200 + 2400 + 4800;
t.truthy(Math.abs(duration - expectedDuration) < 100, `Duration of ${duration} is not close to expected duration ${expectedDuration}`); // Allow for 100ms difference

const customDuration = measurements.at(1)?.duration ?? NaN;
const expectedCustomDuration = 300 + 600 + 1000 + 1000 + 1000;
t.truthy(Math.abs(customDuration - expectedCustomDuration) < 100, `Duration of ${customDuration}ms is not close to expected duration ${expectedCustomDuration}ms`); // Allow for 100ms difference

await server.close();
});

0 comments on commit 18ef588

Please sign in to comment.