Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into store-time-differ…
Browse files Browse the repository at this point in the history
…ence-for-app-auth
  • Loading branch information
copperwall committed Sep 3, 2020
2 parents eab853a + 22b88b7 commit 17b401c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { RequestError } from "@octokit/request-error";
import { OctokitResponse } from "@octokit/types";

const SIXTY_SECONDS_IN_MS = 60 * 1000;
const FIVE_SECONDS_IN_MS = 5 * 1000;

function isTimeSkewError(error: RequestError) {
return (
Expand Down Expand Up @@ -117,12 +117,12 @@ async function sendRequestWithRetries(
throw error;
}

if (timeSinceTokenCreationInMs >= SIXTY_SECONDS_IN_MS) {
if (timeSinceTokenCreationInMs >= FIVE_SECONDS_IN_MS) {
throw error;
}

++retries;
const awaitTime = retries * retries * 1000;
const awaitTime = retries * 1000;
console.warn(
`[@octokit/auth-app] Retrying after 401 response to account for token replication delay (retry: ${retries}, wait: ${awaitTime}ms)`
);
Expand Down
20 changes: 7 additions & 13 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1403,9 +1403,8 @@ test("auth.hook(): throw 401 error in app auth flow without timing errors", asyn
}
});

test("auth.hook(): handle 401 in first minute (#65)", async () => {
let requestCount = 0;
const ONE_MINUTE_IN_MS = 1000 * 60;
test("auth.hook(): handle 401 in first 5 seconds (#65)", async () => {
const FIVE_SECONDS_IN_MS = 1000 * 5;

const mock = fetchMock
.sandbox()
Expand All @@ -1418,7 +1417,7 @@ test("auth.hook(): handle 401 in first minute (#65)", async () => {
repository_selection: "all",
})
.get("https://api.github.com/repos/octocat/hello-world", (url) => {
if (Date.now() < ONE_MINUTE_IN_MS) {
if (Date.now() < FIVE_SECONDS_IN_MS) {
return {
status: 401,
body: {
Expand Down Expand Up @@ -1473,15 +1472,10 @@ test("auth.hook(): handle 401 in first minute (#65)", async () => {

const promise = requestWithAuth("GET /repos/octocat/hello-world");

let i = 0;

// it takes 6 retries until a total time of more than 60s pass
// it takes 3 retries until a total time of more than 5s pass
await clock.tickAsync(1000);
await clock.tickAsync(4000);
await clock.tickAsync(9000);
await clock.tickAsync(16000);
await clock.tickAsync(25000);
await clock.tickAsync(36000);
await clock.tickAsync(2000);
await clock.tickAsync(3000);

const { data } = await promise;

Expand All @@ -1496,7 +1490,7 @@ test("auth.hook(): handle 401 in first minute (#65)", async () => {
expect(mock.done()).toBe(true);

// @ts-ignore
expect(global.console.warn.mock.calls.length).toEqual(6);
expect(global.console.warn.mock.calls.length).toEqual(3);
});

test("auth.hook(): throws on 500 error without retries", async () => {
Expand Down

0 comments on commit 17b401c

Please sign in to comment.