Skip to content

Commit

Permalink
test: refactor variable names and descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed Aug 23, 2023
1 parent cc9d49c commit f61d90e
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,10 @@ it("throws NotFoundError if repository does not exist", async (ctx) => {

it("retries after `retry-after` milliseconds if response code is 429", async (ctx) => {
const retryAfter = 200; // ms
const TEST_TOLERANCE = 100; // ms
/**
* The number of milliseconds that time-measuring tests can vary.
*/
const testTolerance = 100;
/**
* The number of times 429 is returned.
*/
Expand All @@ -708,6 +711,8 @@ it("retries after `retry-after` milliseconds if response code is 429", async (ct

let responseTries = 0;

// Override the query endpoint to return a 429 while `responseTries` is
// less than or equal to `retryResponseQty`
ctx.server.use(
msw.rest.get(queryEndpoint, (_req, res, ctx) => {
responseTries++;
Expand All @@ -726,15 +731,15 @@ it("retries after `retry-after` milliseconds if response code is 429", async (ct
}),
);

// Rate limited. Should resolve after around 1000ms.
// Rate limited. Should resolve roughly after retryAfter * retryResponseQty milliseconds.
const t0_0 = performance.now();
const res0 = await client.get();
const t0_1 = performance.now();

expect(res0).toStrictEqual(queryResponse);
expect(t0_1 - t0_0).toBeGreaterThanOrEqual(retryAfter * retryResponseQty);
expect(t0_1 - t0_0).toBeLessThanOrEqual(
retryAfter * retryResponseQty + TEST_TOLERANCE,
retryAfter * retryResponseQty + testTolerance,
);

// Not rate limited. Should resolve nearly immediately.
Expand All @@ -744,5 +749,5 @@ it("retries after `retry-after` milliseconds if response code is 429", async (ct

expect(res1).toStrictEqual(queryResponse);
expect(t1_1 - t1_0).toBeGreaterThanOrEqual(0);
expect(t1_1 - t1_0).toBeLessThanOrEqual(TEST_TOLERANCE);
expect(t1_1 - t1_0).toBeLessThanOrEqual(testTolerance);
});

0 comments on commit f61d90e

Please sign in to comment.