summaryrefslogtreecommitdiff
path: root/tests/unit/serve_test.ts
diff options
context:
space:
mode:
authorMatt Mastracci <matthew@mastracci.com>2024-04-29 09:40:02 -0600
committerGitHub <noreply@github.com>2024-04-29 09:40:02 -0600
commit56fec538e1aa7558dc4a7adea7134394f76251f6 (patch)
treea43d3529f51d5fe9da8e5bc236619c81f3434ff4 /tests/unit/serve_test.ts
parentda52058a945999d486b07700d2834f027a65947c (diff)
fix(ext/http): ensure signal is created iff requested (#23601)
This correctly creates the `AbortSignal` regardless of when we request it. If the signal is requested after the request has completed, the signal is created in the aborted state. Using GC counts, we can see a reduction in object creation: This PR: 440 deno 1.42.4: 1650 deno 1.43.0+b02ffec: 874
Diffstat (limited to 'tests/unit/serve_test.ts')
-rw-r--r--tests/unit/serve_test.ts29
1 files changed, 29 insertions, 0 deletions
diff --git a/tests/unit/serve_test.ts b/tests/unit/serve_test.ts
index 8978c4f7e..74628ace1 100644
--- a/tests/unit/serve_test.ts
+++ b/tests/unit/serve_test.ts
@@ -23,6 +23,7 @@ const {
addTrailers,
serveHttpOnListener,
serveHttpOnConnection,
+ getCachedAbortSignal,
// @ts-expect-error TypeScript (as of 3.7) does not support indexing namespaces by symbol
} = Deno[Deno.internal];
@@ -2838,6 +2839,34 @@ for (const delay of ["delay", "nodelay"]) {
}
}
+// Test for the internal implementation detail of cached request signals. Ensure that the request's
+// signal is aborted if we try to access it after the request has been completed.
+Deno.test(
+ { permissions: { net: true } },
+ async function httpServerSignalCancelled() {
+ let stashedRequest;
+ const { finished, abort } = await makeServer((req) => {
+ // The cache signal is `undefined` because it has not been requested
+ assertEquals(getCachedAbortSignal(req), undefined);
+ stashedRequest = req;
+ return new Response("ok");
+ });
+ await (await fetch(`http://localhost:${servePort}`)).text();
+ abort();
+ await finished;
+
+ // `false` is a semaphore for a signal that should be aborted on creation
+ assertEquals(getCachedAbortSignal(stashedRequest!), false);
+ // Requesting the signal causes it to be materialized
+ assert(stashedRequest!.signal.aborted);
+ // The cached signal is now a full `AbortSignal`
+ assertEquals(
+ getCachedAbortSignal(stashedRequest!).constructor,
+ AbortSignal,
+ );
+ },
+);
+
Deno.test(
{ permissions: { net: true } },
async function httpServerCancelFetch() {