From 0143eccb27ef0c9a93bd39636d5f3ecfd08f7eef Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Wed, 22 Feb 2023 21:29:43 -0800 Subject: Fix flaky request.signal implementation --- test/bun.js/fetch.test.js | 68 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 15 deletions(-) (limited to 'test/bun.js/fetch.test.js') diff --git a/test/bun.js/fetch.test.js b/test/bun.js/fetch.test.js index 0ecba0cd8..ec903341f 100644 --- a/test/bun.js/fetch.test.js +++ b/test/bun.js/fetch.test.js @@ -1,21 +1,26 @@ -import { afterAll, beforeAll, describe, expect, it, test } from "bun:test"; +import { afterAll, afterEach, beforeAll, describe, expect, it, test, beforeEach } from "bun:test"; import fs, { chmodSync, unlinkSync } from "fs"; import { mkfifo } from "mkfifo"; import { gc, withoutAggressiveGC } from "./gc"; +const sleep = countdown => { + return Bun.sleep(countdown); +}; + const exampleFixture = fs.readFileSync( import.meta.path.substring(0, import.meta.path.lastIndexOf("/")) + "/fetch.js.txt", "utf8", ); var cachedServer; -function getServer(handler) { - cachedServer ||= Bun.serve(handler); - cachedServer.reload(handler); - return cachedServer; +function getServer({ port, ...rest }) { + return (cachedServer = Bun.serve({ + ...rest, + port: 0, + })); } -afterAll(() => { +afterEach(() => { cachedServer?.stop?.(true); }); @@ -63,9 +68,9 @@ describe("AbortSignalStreamTest", async () => { } catch (ex) { error = ex; } - expect(error instanceof DOMException).toBeTruthy(); expect(error.name).toBe("AbortError"); expect(error.message).toBe("The operation was aborted."); + expect(error instanceof DOMException).toBeTruthy(); } } @@ -77,7 +82,6 @@ describe("AbortSignalStreamTest", async () => { }); describe("AbortSignalDirectStreamTest", () => { - const port = 74322; async function abortOnStage(body, stage) { let error = undefined; var abortController = new AbortController(); @@ -119,9 +123,9 @@ describe("AbortSignalDirectStreamTest", () => { } catch (ex) { error = ex; } - expect(error instanceof DOMException).toBeTruthy(); expect(error.name).toBe("AbortError"); expect(error.message).toBe("The operation was aborted."); + expect(error instanceof DOMException).toBeTruthy(); } } @@ -133,6 +137,39 @@ describe("AbortSignalDirectStreamTest", () => { }); describe("AbortSignal", () => { + var server; + beforeEach(() => { + server = getServer({ + async fetch(request) { + if (request.url.endsWith("/nodelay")) { + return new Response("Hello"); + } + if (request.url.endsWith("/stream")) { + const reader = request.body.getReader(); + const body = new ReadableStream({ + async pull(controller) { + if (!reader) controller.close(); + const { done, value } = await reader.read(); + // When no more data needs to be consumed, close the stream + if (done) { + controller.close(); + return; + } + // Enqueue the next data chunk into our target stream + controller.enqueue(value); + }, + }); + return new Response(body); + } + if (request.method.toUpperCase() === "POST") { + const body = await request.text(); + return new Response(body); + } + await sleep(15); + return new Response("Hello"); + }, + }); + }); it("AbortError", async () => { let name; try { @@ -140,7 +177,7 @@ describe("AbortSignal", () => { const signal = controller.signal; async function manualAbort() { - await Bun.sleep(10); + await sleep(1); controller.abort(); } await Promise.all([ @@ -172,8 +209,9 @@ describe("AbortSignal", () => { try { var controller = new AbortController(); const signal = controller.signal; + async function manualAbort() { - await Bun.sleep(10); + await sleep(10); controller.abort("My Reason"); } await Promise.all([ @@ -197,7 +235,7 @@ describe("AbortSignal", () => { }); async function manualAbort() { - await Bun.sleep(10); + await sleep(10); controller.abort(); } await Promise.all([ @@ -231,9 +269,9 @@ describe("AbortSignal", () => { error = ex; } - expect(error instanceof DOMException).toBeTruthy(); expect(error.name).toBe("AbortError"); expect(error.message).toBe("The operation was aborted."); + expect(error instanceof DOMException).toBeTruthy(); }); it("TimeoutError", async () => { @@ -253,7 +291,7 @@ describe("AbortSignal", () => { const signal = controller.signal; const request = new Request(`http://127.0.0.1:${server.port}`, { signal }); async function manualAbort() { - await Bun.sleep(10); + await sleep(10); controller.abort(); } await Promise.all([fetch(request).then(res => res.text()), manualAbort()]); @@ -443,7 +481,7 @@ describe("fetch", () => { await fetch(url, { body: "buntastic" }); expect(false).toBe(true); } catch (exception) { - expect(exception instanceof TypeError).toBe(true); + expect(exception.name).toBe("TypeError"); } }); }); -- cgit v1.2.3 ption> Unnamed repository; edit this file 'description' to name the repository.
aboutsummaryrefslogtreecommitdiff
path: root/docs/ecosystem/react.md (unfollow)
AgeCommit message (Expand)AuthorFilesLines
2023-07-19fix `make headers`Gravatar Dylan Conway 1-7/+15
2023-07-20Better error for workspace dependency not found (#3678)Gravatar Jarred Sumner 2-21/+85
2023-07-18Fix crash in postMessage that repro'd after ~100,000 messagesGravatar Jarred Sumner 6-21/+57
2023-07-18more progress on fixing gc issueGravatar Jarred Sumner 6-61/+162
2023-07-18add padding bytesGravatar Dylan Conway 1-1/+1
2023-07-18feature(constants) add constants/node:constants module and tests(prisma) use ...Gravatar Ciro Spaciari 16-20/+529
2023-07-18patch checkServerIdentity (#3671)Gravatar Ciro Spaciari 3-3/+9
2023-07-18Update workers.mdGravatar Jarred Sumner 1-2/+2
2023-07-18[jest] execute lifecycle hooks on empty blocks (#3663)Gravatar Alex Lam S.L 2-19/+79
2023-07-18ClarifyGravatar Jarred Sumner 1-0/+2
2023-07-18Fixes #3669Gravatar Jarred Sumner 4-13/+35
2023-07-18zig upgrade (#3667)Gravatar Dylan Conway 154-4894/+4857
2023-07-17Enable postgres prisma testGravatar Jarred Sumner 1-1/+1
2023-07-17Emit writeBarrier in `napi_module_register`Gravatar Jarred Sumner 1-6/+14
2023-07-17Fix potential crash in process.dlopen()Gravatar Jarred Sumner 2-5/+27
2023-07-17Implement `process.{stdout, stderr}.{columns, rows, getWindowSize}`Gravatar Jarred Sumner 4-32/+108
2023-07-17[tls] General compatibility improvements (#3596)Gravatar Ciro Spaciari 23-298/+2907
2023-07-17package json `main` field extension order (#3664)Gravatar Dylan Conway 3-3/+96
2023-07-17[install] handle duplicated workspace declarations gracefully (#3662)Gravatar Alex Lam S.L 2-6/+197
2023-07-17Clean up worker docsGravatar Colin McDonnell 1-65/+69
2023-07-17Tweak test docsGravatar Colin McDonnell 2-4/+3
2023-07-17workaround `readable-stream` compatibility (#3626)Gravatar Alex Lam S.L 3-4/+5
2023-07-17Fix flaky process testGravatar Jarred SUmner 1-2/+2
2023-07-17Fix test with incorrect textGravatar Jarred Sumner 1-3/+3
2023-07-17Fix incorrect nameGravatar Jarred Sumner 2-4/+4
2023-07-17Fix speculative crashes in console.log(formData) and console.log(headers)Gravatar Jarred Sumner 2-30/+24
2023-07-17Fix crash in console.log(urlSearchParams) on a URLSearchParams object with a ...Gravatar Jarred Sumner 2-4/+99
2023-07-17Fix memory leak in `await new Response(latin1String).arrayBuffer()` and `awai...Gravatar Jarred Sumner 16-102/+361
2023-07-1720% faster `deserialize` (#3655)Gravatar Jarred Sumner 2-12/+197
2023-07-16Document `--smol`Gravatar Jarred Sumner 1-70/+59
2023-07-16Add `--smol` to bunfigGravatar Jarred Sumner 1-0/+12
2023-07-16Document serialize/deserializeGravatar Jarred Sumner 1-0/+14