diff --git a/apps/desktop/src/backend/DesktopLocalEnvironmentAuth.test.ts b/apps/desktop/src/backend/DesktopLocalEnvironmentAuth.test.ts index 348d3a7050f70..7d628a44e856e 100644 --- a/apps/desktop/src/backend/DesktopLocalEnvironmentAuth.test.ts +++ b/apps/desktop/src/backend/DesktopLocalEnvironmentAuth.test.ts @@ -170,17 +170,17 @@ describe("DesktopLocalEnvironmentAuth", () => { }, ]), } as unknown as DesktopBackendPool.DesktopBackendPool["Service"]); - const makeExchange = (failures: number, failure: () => Response) => + const makeExchange = (failures: number, failure: () => Effect.Effect) => Effect.gen(function* () { const requestCount = yield* Ref.make(0); const layerHttpClient = Layer.succeed( HttpClient.HttpClient, HttpClient.make((request) => Ref.updateAndGet(requestCount, (count) => count + 1).pipe( - Effect.map((count) => + Effect.flatMap((count) => count <= failures - ? HttpClientResponse.fromWeb(request, failure()) - : tokenResponse(request), + ? Effect.map(failure(), (response) => HttpClientResponse.fromWeb(request, response)) + : Effect.succeed(tokenResponse(request)), ), ), ), @@ -195,17 +195,45 @@ describe("DesktopLocalEnvironmentAuth", () => { return { auth, requestCount }; }); + it.effect("keeps retrying while the backend is too busy to answer", () => + Effect.gen(function* () { + const { auth, requestCount } = yield* makeExchange(3, () => Effect.never); + + const fiber = yield* auth.getBearerToken.pipe(Effect.forkChild); + yield* TestClock.adjust(Duration.seconds(40)); + const token = yield* Fiber.join(fiber); + + assert.strictEqual(token, "desktop-bearer-token"); + assert.strictEqual(yield* Ref.get(requestCount), 4); + }).pipe(Effect.provide(TestClock.layer())), + ); + + it.effect("gives up after a minute when the backend never answers", () => + Effect.gen(function* () { + const { auth, requestCount } = yield* makeExchange(Infinity, () => Effect.never); + + const fiber = yield* auth.getBearerToken.pipe(Effect.flip, Effect.forkChild); + yield* TestClock.adjust(Duration.seconds(60)); + const error = yield* Fiber.join(fiber); + + assert.strictEqual(error._tag, "DesktopLocalEnvironmentAuthSessionBootstrapError"); + assert.isAbove(yield* Ref.get(requestCount), 1); + }).pipe(Effect.provide(TestClock.layer())), + ); + it.effect("does not retry a rejected bootstrap credential", () => Effect.gen(function* () { const { auth, requestCount } = yield* makeExchange(1, () => - Response.json( - { - _tag: "EnvironmentAuthInvalidError", - code: "auth_invalid", - reason: "invalid_credential", - traceId: "trace-1", - }, - { status: 401 }, + Effect.succeed( + Response.json( + { + _tag: "EnvironmentAuthInvalidError", + code: "auth_invalid", + reason: "invalid_credential", + traceId: "trace-1", + }, + { status: 401 }, + ), ), ); diff --git a/apps/desktop/src/backend/DesktopLocalEnvironmentAuth.ts b/apps/desktop/src/backend/DesktopLocalEnvironmentAuth.ts index 4d95dd075cf3e..61916fae97757 100644 --- a/apps/desktop/src/backend/DesktopLocalEnvironmentAuth.ts +++ b/apps/desktop/src/backend/DesktopLocalEnvironmentAuth.ts @@ -16,7 +16,10 @@ import * as HttpClient from "effect/http/HttpClient"; import * as DesktopBackendPool from "./DesktopBackendPool.ts"; -const BOOTSTRAP_TRANSIENT_RETRY_TIMEOUT = Duration.seconds(15); +// Each token request gives up after 10 s, and a backend that just became ready +// can stall for half a minute on a large state database. The budget has to +// cover several of those attempts, or a slow startup becomes a fatal error. +const BOOTSTRAP_TRANSIENT_RETRY_TIMEOUT = Duration.seconds(60); const BOOTSTRAP_TRANSIENT_RETRY_INTERVAL = Duration.millis(500); const TRANSIENT_BOOTSTRAP_STATUS_CODES = new Set([502, 503, 504]);