Skip to content

Commit

Permalink
test: use resource disposal to ensure servers shutdown even if tests …
Browse files Browse the repository at this point in the history
…fail (#11106)
  • Loading branch information
nektro authored May 17, 2024
1 parent 93abd99 commit ac6eaac
Show file tree
Hide file tree
Showing 33 changed files with 158 additions and 363 deletions.
3 changes: 1 addition & 2 deletions test/bundler/bundler_compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe("bundler", () => {
);
const port = 0;
const server = Bun.serve({
using server = Bun.serve({
port,
async fetch(req) {
return new Response(await renderToReadableStream(<App />), headers);
Expand All @@ -115,7 +115,6 @@ describe("bundler", () => {
const res = await fetch(server.url);
if (res.status !== 200) throw "status error";
console.log(await res.text());
server.stop(true);
`,
},
run: {
Expand Down
5 changes: 1 addition & 4 deletions test/bundler/bundler_npm.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import assert from "assert";
import dedent from "dedent";
import { ESBUILD, itBundled, testForFile } from "./expectBundled";
var { describe, test, expect } = testForFile(import.meta.path);

Expand Down Expand Up @@ -28,7 +26,7 @@ describe("bundler", () => {
);
const port = 42001;
const server = Bun.serve({
using server = Bun.serve({
port,
async fetch(req) {
return new Response(await renderToReadableStream(<App />), headers);
Expand All @@ -37,7 +35,6 @@ describe("bundler", () => {
const res = await fetch("http://localhost:" + port);
if (res.status !== 200) throw "status error";
console.log(await res.text());
server.stop();
`,
},
run: {
Expand Down
3 changes: 1 addition & 2 deletions test/cli/install/bun-install-pathname-trailing-slash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ beforeEach(() => {
test("custom registry doesn't have multiple trailing slashes in pathname", async () => {
const urls: string[] = [];

const server = Bun.serve({
using server = Bun.serve({
port: 0,
async fetch(req) {
urls.push(req.url);
Expand Down Expand Up @@ -48,7 +48,6 @@ registry = "http://${hostname}:${port}/prefixed-route/"
stdin: "ignore",
});

server.stop(true);
expect(urls.length).toBe(1);
expect(urls).toEqual([`http://${hostname}:${port}/prefixed-route/react`]);
});
4 changes: 1 addition & 3 deletions test/cli/install/bun-upgrade.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ beforeEach(async () => {
});

it("two invalid arguments, should display error message and suggest command", async () => {
console.log(run_dir, exe_name);
const { stderr } = spawn({
cmd: [join(run_dir, exe_name), "upgrade", "bun-types", "--dev"],
cwd: run_dir,
Expand Down Expand Up @@ -93,7 +92,7 @@ it("two valid argument, should succeed", async () => {
});

it("zero arguments, should succeed", async () => {
const server = Bun.serve({
using server = Bun.serve({
tls: {
cert: "-----BEGIN CERTIFICATE-----\nMIIDXTCCAkWgAwIBAgIJAKLdQVPy90jjMA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV\nBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX\naWRnaXRzIFB0eSBMdGQwHhcNMTkwMjAzMTQ0OTM1WhcNMjAwMjAzMTQ0OTM1WjBF\nMQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50\nZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB\nCgKCAQEA7i7IIEdICTiSTVx+ma6xHxOtcbd6wGW3nkxlCkJ1UuV8NmY5ovMsGnGD\nhJJtUQ2j5ig5BcJUf3tezqCNW4tKnSOgSISfEAKvpn2BPvaFq3yx2Yjz0ruvcGKp\nDMZBXmB/AAtGyN/UFXzkrcfppmLHJTaBYGG6KnmU43gPkSDy4iw46CJFUOupc51A\nFIz7RsE7mbT1plCM8e75gfqaZSn2k+Wmy+8n1HGyYHhVISRVvPqkS7gVLSVEdTea\nUtKP1Vx/818/HDWk3oIvDVWI9CFH73elNxBkMH5zArSNIBTehdnehyAevjY4RaC/\nkK8rslO3e4EtJ9SnA4swOjCiqAIQEwIDAQABo1AwTjAdBgNVHQ4EFgQUv5rc9Smm\n9c4YnNf3hR49t4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SQarCx0U=\n-----END PRIVATE KEY-----",
Expand Down Expand Up @@ -170,7 +169,6 @@ it("zero arguments, should succeed", async () => {
},
});

server.stop();
closeTempDirHandle();

// Should not contain error message
Expand Down
3 changes: 1 addition & 2 deletions test/js/bun/eventsource/eventsource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
// pathname: string,
// callback: (evtSource: EventSource, done: (err?: unknown) => void) => void,
// ) {
// const server = Bun.serve({
// using server = Bun.serve({
// port: 0,
// fetch(req) {
// if (new URL(req.url).pathname === "/stream") {
Expand All @@ -70,7 +70,6 @@
// });
// } catch (err) {
// evtSource?.close();
// server.stop(true);
// done(err);
// }
// }
Expand Down
13 changes: 4 additions & 9 deletions test/js/bun/http/async-iterator-stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { bunExe, bunEnv } from "harness";

describe("Streaming body via", () => {
test("async generator function", async () => {
const server = Bun.serve({
using server = Bun.serve({
port: 0,

async fetch(req) {
Expand All @@ -25,7 +25,6 @@ describe("Streaming body via", () => {

expect(Buffer.concat(chunks).toString()).toBe("Hello, world!!");
expect(chunks).toHaveLength(2);
server.stop(true);
});

test("async generator function throws an error but continues to send the headers", async () => {
Expand Down Expand Up @@ -58,7 +57,7 @@ describe("Streaming body via", () => {

test("async generator aborted doesn't crash", async () => {
var aborter = new AbortController();
const server = Bun.serve({
using server = Bun.serve({
port: 0,

async fetch(req) {
Expand All @@ -81,13 +80,11 @@ describe("Streaming body via", () => {
} catch (e) {
expect(e).toBeInstanceOf(DOMException);
expect(e.name).toBe("AbortError");
} finally {
server.stop(true);
}
});

test("[Symbol.asyncIterator]", async () => {
const server = Bun.serve({
using server = Bun.serve({
port: 0,

async fetch(req) {
Expand All @@ -114,11 +111,10 @@ describe("Streaming body via", () => {

expect(Buffer.concat(chunks).toString()).toBe("my string goes here\nmy buffer goes here\nend!\n!");
expect(chunks).toHaveLength(2);
server.stop(true);
});

test("[Symbol.asyncIterator] with a custom iterator", async () => {
const server = Bun.serve({
using server = Bun.serve({
port: 0,

async fetch(req) {
Expand Down Expand Up @@ -151,7 +147,6 @@ describe("Streaming body via", () => {
expect(Buffer.concat(chunks).toString()).toBe("Hello, world!");
// TODO:
// expect(chunks).toHaveLength(2);
server.stop(true);
});

test("yield", async () => {
Expand Down
6 changes: 2 additions & 4 deletions test/js/bun/http/bun-serve-headers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { test, expect } from "bun:test";

// https://github.com/oven-sh/bun/issues/9180
test("weird headers", async () => {
const server = Bun.serve({
using server = Bun.serve({
port: 0,
development: false,
fetch(req) {
Expand All @@ -17,7 +17,7 @@ test("weird headers", async () => {
},
});

try {
{
for (let i = 0; i < 255; i++) {
const headers = new Headers();
const name = "X-" + String.fromCharCode(i);
Expand All @@ -32,7 +32,5 @@ test("weird headers", async () => {
});
expect(res.headers.get(name)).toBe("1");
}
} finally {
server.stop(true);
}
});
Loading

0 comments on commit ac6eaac

Please sign in to comment.