Skip to content

Commit

Permalink
chore: update wrapper test
Browse files Browse the repository at this point in the history
  • Loading branch information
jd1378 committed Jul 20, 2024
1 parent 4bd2adb commit f34a207
Showing 1 changed file with 7 additions and 49 deletions.
56 changes: 7 additions & 49 deletions fetch_wrapper_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,8 @@ import {
} from "https://deno.land/[email protected]/testing/asserts.ts";
import { CookieJar } from "./cookie_jar.ts";
import { wrapFetch } from "./fetch_wrapper.ts";
import { delay } from "https://deno.land/[email protected]/async/delay.ts";
import { Cookie } from "./cookie.ts";

function drop(resourceName: string) {
const rt: Deno.ResourceMap = Deno.resources();
for (const rid in rt) {
if (rt[rid] == resourceName) {
try {
Deno.close(Number(rid));
return true;
} catch {
return false;
}
}
}
}

const serverOnePort = 53250;
const serverTwoPort = 53251;
const serverOneHostname = "127.0.0.1";
Expand Down Expand Up @@ -123,44 +108,17 @@ type ListenAndServeOptions = {
abortController: AbortController;
};

async function listenAndServe(options: ListenAndServeOptions) {
const { hostname, port, abortController } = options;

const listener = Deno.listen({ hostname, port });

const handleAbort = () => {
abortController.signal.removeEventListener("abort", handleAbort);
listener.close();
drop("httpConn");
};

abortController.signal.addEventListener("abort", handleAbort);

try {
for await (const conn of listener) {
for await (
const { respondWith, request } of Deno.serveHttp(conn)
) {
respondWith(serverHandler(request));
}
}
} catch {
drop("httpConn");
}
}

function runServer(
options: Omit<ListenAndServeOptions, "abortController">,
) {
const { hostname, port } = options;
const abortController = new AbortController();
listenAndServe({
...options,
abortController,
}).catch((e) => {
abortController.abort();
delay(10);
throw e;
});

Deno.serve(
{ signal: abortController.signal, hostname, port, onListen() {} },
(req) => serverHandler(req),
);

return abortController;
}

Expand Down

0 comments on commit f34a207

Please sign in to comment.