diff --git a/src/hook.ts b/src/hook.ts index c6e16b57d..a2f7c1f68 100644 --- a/src/hook.ts +++ b/src/hook.ts @@ -10,7 +10,7 @@ import { State, } from "./types"; -const SIXTY_SECONDS_IN_MS = 60 * 1000; +const FIVE_SECONDS_IN_MS = 5 * 1000; export async function hook( state: State, @@ -68,12 +68,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)` ); diff --git a/test/index.test.ts b/test/index.test.ts index 6cde8ebe5..c7726322e 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1210,9 +1210,8 @@ test("auth.hook() uses app auth for marketplace URL", async () => { expect(mock.done()).toBe(true); }); -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() @@ -1225,7 +1224,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: { @@ -1280,15 +1279,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; @@ -1303,7 +1297,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 () => {