Skip to content

Commit

Permalink
Speed up a couple tests (#11369)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner authored May 26, 2024
1 parent 4539d16 commit fdbccef
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 82 deletions.
10 changes: 5 additions & 5 deletions test/js/bun/http/serve-body-leak.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ async function calculateMemoryLeak(fn: () => Promise<void>) {
}

for (const test_info of [
["#10265 should not leak memory when ignoring the body", callIgnore, false, 8],
["should not leak memory when buffering the body", callBuffering, false, 24],
["should not leak memory when streaming the body", callStreaming, false, 8],
["should not leak memory when streaming the body incompletely", callIncompleteStreaming, false, 16],
["should not leak memory when streaming the body and echoing it back", callStreamingEcho, false, 16],
["#10265 should not leak memory when ignoring the body", callIgnore, false, 16],
["should not leak memory when buffering the body", callBuffering, false, 48],
["should not leak memory when streaming the body", callStreaming, false, 16],
["should not leak memory when streaming the body incompletely", callIncompleteStreaming, false, 32],
["should not leak memory when streaming the body and echoing it back", callStreamingEcho, false, 32],
] as const) {
const [testName, fn, skip, maxMemoryGrowth] = test_info;
it.todoIf(skip)(
Expand Down
24 changes: 15 additions & 9 deletions test/js/bun/http/serve-listen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { file, serve } from "bun";
import type { NetworkInterfaceInfo } from "node:os";
import { networkInterfaces } from "node:os";
import { join } from "node:path";
import { tmpdirSync } from "harness";
import { isWindows, tmpdirSync } from "harness";

const networks = Object.values(networkInterfaces()).flat() as NetworkInterfaceInfo[];
const hasIPv4 = networks.some(({ family }) => family === "IPv4");
const hasIPv6 = networks.some(({ family }) => family === "IPv6");

const unix = join(tmpdirSync(), "unix.sock");
const unix = join(tmpdirSync(), "unix.sock").replaceAll("\\", "/");
const tls = {
cert: file(new URL("./fixtures/cert.pem", import.meta.url)),
key: file(new URL("./fixtures/cert.key", import.meta.url)),
Expand Down Expand Up @@ -109,14 +109,20 @@ describe.each([
},
},
{
if: process.platform !== "win32",
options: {
unix,
},
url: {
protocol: "unix:",
pathname: unix,
},
unix: unix,
},
url: isWindows
? {
protocol: "unix:",
pathname: unix.substring(unix.indexOf(":") + 1),
hostname: unix.substring(0, unix.indexOf(":")),
port: "",
}
: {
protocol: "unix:",
pathname: unix,
},
},
])("Bun.serve()", ({ if: enabled = true, options, hostname, url }) => {
const title = Bun.inspect(options).replaceAll("\n", " ");
Expand Down
25 changes: 15 additions & 10 deletions test/js/bun/udp/dgram.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createSocket } from "dgram";
import { describe, test, expect, it } from "bun:test";

import { nodeDataCases } from "./testdata";
import { disableAggressiveGCScope } from "harness";

describe("createSocket()", () => {
test("connect", done => {
Expand Down Expand Up @@ -39,8 +40,8 @@ describe("createSocket()", () => {
});

socket.on("error", err => {
socket.close(done);
expect(err).toBeNull();
socket.close(done);
});

socket.bind(0, "127.0.0.1");
Expand All @@ -62,21 +63,25 @@ describe("createSocket()", () => {
});

socket.on("error", err => {
socket.close(done);
expect(err).toBeNull();
socket.close(done);
});

socket.bind(0, localhost);
});

const validateRecv = (server, data, rinfo, bytes) => {
expect(data).toHaveLength(bytes.length);
expect(data).toStrictEqual(Buffer.from(bytes));
expect(rinfo.port).toBeInteger();
expect(rinfo.port).toBeWithin(1, 65535 + 1);
expect(rinfo.address).toBeString();
expect(rinfo.address).not.toBeEmpty();
expect(rinfo.port).not.toBe(server.address().port);
using _ = disableAggressiveGCScope();
try {
expect(rinfo.port).toBeInteger();
expect(rinfo.port).toBeWithin(1, 65535 + 1);
expect(rinfo.address).toBeString();
expect(rinfo.address).not.toBeEmpty();
expect(rinfo.port).not.toBe(server.address().port);
} catch (e) {
console.error(e);
throw e;
}
};

for (const { label, data, bytes } of nodeDataCases) {
Expand Down Expand Up @@ -163,7 +168,7 @@ describe("createSocket()", () => {
expect(err).toBeNull();
});
server.on("message", (data, rinfo) => {
validateRecv(server, data, rinfo, [bytes, bytes, bytes].flat());
validateRecv(server, data, rinfo, Buffer.from([...bytes, ...bytes, ...bytes].flat()));

server.close();
client.close();
Expand Down
24 changes: 14 additions & 10 deletions test/js/node/http/node-http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { unlinkSync } from "node:fs";
import { PassThrough } from "node:stream";
const { describe, expect, it, beforeAll, afterAll, createDoneDotAll, mock } = createTest(import.meta.path);
import { bunExe } from "bun:harness";
import { bunEnv, tmpdirSync } from "harness";
import { bunEnv, disableAggressiveGCScope, tmpdirSync } from "harness";
import * as stream from "node:stream";
import * as zlib from "node:zlib";

Expand Down Expand Up @@ -1207,7 +1207,7 @@ describe("server.address should be valid IP", () => {
test("ServerResponse instanceof OutgoingMessage", () => {
expect(new ServerResponse({}) instanceof OutgoingMessage).toBe(true);
});
test("ServerResponse assign assignSocket", done => {
test("ServerResponse assign assignSocket", async done => {
const createDone = createDoneDotAll(done);
const doneRequest = createDone();
const waitSocket = createDone();
Expand All @@ -1223,13 +1223,13 @@ describe("server.address should be valid IP", () => {
doneRequest();
});
res.assignSocket(socket);
setImmediate(() => {
expect(res.socket).toBe(socket);
expect(socket._httpMessage).toBe(res);
expect(() => res.assignSocket(socket)).toThrow("ServerResponse has an already assigned socket");
socket.emit("close");
doneSocket();
});
await Bun.sleep(10);

expect(res.socket).toBe(socket);
expect(socket._httpMessage).toBe(res);
expect(() => res.assignSocket(socket)).toThrow("ServerResponse has an already assigned socket");
socket.emit("close");
doneSocket();
} catch (err) {
doneRequest(err);
}
Expand Down Expand Up @@ -1785,7 +1785,7 @@ it("#10177 response.write with non-ascii latin1 should not cause duplicated char

for (const char of chars) {
for (let size = start_size; size <= end_size; size += increment_step) {
expected = char + "-".repeat(size) + "x";
expected = char + Buffer.alloc(size, "-").toString("utf8") + "x";

try {
const url = `http://${hostname}:${port}`;
Expand All @@ -1798,13 +1798,17 @@ it("#10177 response.write with non-ascii latin1 should not cause duplicated char
all.push(...(await Promise.all(batch)));
}

using _ = disableAggressiveGCScope();
for (const result of all) {
expect(result).toBe(expected);
}
} catch (err) {
return finish(err);
}
}

// still always run GC at the end here.
Bun.gc(true);
}
finish();
});
Expand Down
104 changes: 56 additions & 48 deletions test/js/web/fetch/fetch.tls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ it("can handle multiple requests with non native checkServerIdentity", async ()
return tls.checkServerIdentity(hostname, cert);
},
},
}).then((res: Response) => res.text());
expect(result?.length).toBeGreaterThan(0);
}).then((res: Response) => res.blob());
expect(result?.size).toBeGreaterThan(0);
expect(called).toBe(true);
}
const promises = [];
Expand All @@ -48,35 +48,36 @@ it("can handle multiple requests with non native checkServerIdentity", async ()
});

it("fetch with valid tls should not throw", async () => {
const urls = ["https://bun.sh", "https://www.example.com"];
for (const url of urls) {
const result = await fetch(url, { keepalive: false }).then((res: Response) => res.text());
expect(result?.length).toBeGreaterThan(0);
}
const promises = [`https://bun.sh`, `https://www.example.com`].map(async url => {
const result = await fetch(url, { keepalive: false }).then((res: Response) => res.blob());
expect(result?.size).toBeGreaterThan(0);
});

await Promise.all(promises);
});

it("fetch with valid tls and non-native checkServerIdentity should work", async () => {
const urls = [`https://bun.sh`, `https://www.example.com`];
let count = 0;
for (const url of urls) {
const promises = [`https://bun.sh`, `https://www.example.com`].map(async url => {
await fetch(url, {
keepalive: false,
tls: {
checkServerIdentity(hostname: string, cert: tls.PeerCertificate) {
count++;
expect(["bun.sh", "www.example.com"]).toContain(hostname);
expect(url).toContain(hostname);
return tls.checkServerIdentity(hostname, cert);
},
},
}).then((res: Response) => res.text());
}
}).then((res: Response) => res.blob());
});
await Promise.all(promises);
expect(count).toBe(2);
});

it("fetch with rejectUnauthorized: false should not call checkServerIdentity", async () => {
let count = 0;

await fetch("https://bun.sh", {
await fetch("https://example.com", {
keepalive: false,
tls: {
rejectUnauthorized: false,
Expand All @@ -85,35 +86,38 @@ it("fetch with rejectUnauthorized: false should not call checkServerIdentity", a
return tls.checkServerIdentity(hostname, cert);
},
},
}).then((res: Response) => res.text());
}).then((res: Response) => res.blob());
expect(count).toBe(0);
});

it("fetch with self-sign tls should throw", async () => {
await createServer(CERT_LOCALHOST_IP, async port => {
const urls = [`https://localhost:${port}`, `https://127.0.0.1:${port}`];
for (const url of urls) {
try {
await fetch(url).then((res: Response) => res.text());
expect.unreachable();
} catch (e: any) {
expect(e.code).toBe("DEPTH_ZERO_SELF_SIGNED_CERT");
}
}
await Promise.all(
urls.map(async url => {
try {
await fetch(url).then((res: Response) => res.blob());
expect.unreachable();
} catch (e: any) {
expect(e.code).toBe("DEPTH_ZERO_SELF_SIGNED_CERT");
}
}),
);
});
});

it("fetch with invalid tls should throw", async () => {
await createServer(CERT_EXPIRED, async port => {
const urls = [`https://localhost:${port}`, `https://127.0.0.1:${port}`];
for (const url of urls) {
try {
await fetch(url).then((res: Response) => res.text());
expect.unreachable();
} catch (e: any) {
expect(e.code).toBe("CERT_HAS_EXPIRED");
}
}
await Promise.all(
[`https://localhost:${port}`, `https://127.0.0.1:${port}`].map(async url => {
try {
await fetch(url).then((res: Response) => res.blob());
expect.unreachable();
} catch (e: any) {
expect(e.code).toBe("CERT_HAS_EXPIRED");
}
}),
);
});
});

Expand All @@ -126,7 +130,7 @@ it("fetch with checkServerIdentity failing should throw", async () => {
return new Error("CustomError");
},
},
}).then((res: Response) => res.text());
}).then((res: Response) => res.blob());

expect.unreachable();
} catch (e: any) {
Expand All @@ -137,28 +141,32 @@ it("fetch with checkServerIdentity failing should throw", async () => {
it("fetch with self-sign certificate tls + rejectUnauthorized: false should not throw", async () => {
await createServer(CERT_LOCALHOST_IP, async port => {
const urls = [`https://localhost:${port}`, `https://127.0.0.1:${port}`];
for (const url of urls) {
try {
const result = await fetch(url, { tls: { rejectUnauthorized: false } }).then((res: Response) => res.text());
expect(result).toBe("Hello World");
} catch {
expect.unreachable();
}
}
await Promise.all(
urls.map(async url => {
try {
const result = await fetch(url, { tls: { rejectUnauthorized: false } }).then((res: Response) => res.text());
expect(result).toBe("Hello World");
} catch {
expect.unreachable();
}
}),
);
});
});

it("fetch with invalid tls + rejectUnauthorized: false should not throw", async () => {
await createServer(CERT_EXPIRED, async port => {
const urls = [`https://localhost:${port}`, `https://127.0.0.1:${port}`];
for (const url of urls) {
try {
const result = await fetch(url, { tls: { rejectUnauthorized: false } }).then((res: Response) => res.text());
expect(result).toBe("Hello World");
} catch {
expect.unreachable();
}
}
await Promise.all(
urls.map(async url => {
try {
const result = await fetch(url, { tls: { rejectUnauthorized: false } }).then((res: Response) => res.text());
expect(result).toBe("Hello World");
} catch {
expect.unreachable();
}
}),
);
});
});

Expand Down

0 comments on commit fdbccef

Please sign in to comment.