diff --git a/application.test.ts b/application.test.ts index 16ed7dd..bcf3865 100644 --- a/application.test.ts +++ b/application.test.ts @@ -1105,10 +1105,8 @@ Deno.test({ }); Deno.test({ - name: "Application.listen() - no options", + name: "Application.listen() - no options - aborted before onListen", // ignore: isNode(), - ignore: true, // there is a challenge with serve and the abort controller that - // needs to be isolated async fn() { const controller = new AbortController(); const app = new Application(); @@ -1118,6 +1116,22 @@ Deno.test({ const { signal } = controller; const p = app.listen({ signal }); controller.abort(); + assertRejects(async () => await p, "aborted prematurely before 'listen' event"); + teardown(); + }, +}); + +Deno.test({ + name: "Application.listen() - no options - aborted after onListen", + async fn() { + const controller = new AbortController(); + const app = new Application(); + app.use((ctx) => { + ctx.response.body = "hello world"; + }); + const { signal } = controller; + app.addEventListener("listen", () => controller.abort()) + const p = app.listen({ signal }); await p; teardown(); }, diff --git a/http_server_native.ts b/http_server_native.ts index 3498406..2e49f6e 100644 --- a/http_server_native.ts +++ b/http_server_native.ts @@ -79,6 +79,10 @@ export class Server> const { signal } = this.#options; const { onListen, ...options } = this.#options; const { promise, resolve } = createPromiseWithResolvers(); + if (signal?.aborted) { + // if user somehow aborted before `listen` is invoked, we throw + return Promise.reject(new Error("aborted prematurely before 'listen' event")); + } this.#stream = new ReadableStream({ start: (controller) => { this.#httpServer = serve?.({ @@ -96,6 +100,9 @@ export class Server> signal, ...options, }); + // closinng stream, so that the Application listen promise can resolve itself + // https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultController/close + signal?.addEventListener("abort", () => controller.close(), { once: true }); }, });