diff options
author | 2023-01-03 09:06:45 +0200 | |
---|---|---|
committer | 2023-01-02 23:06:45 -0800 | |
commit | 7edaf736a22d5d5b344932f1a290558cec5eb7fa (patch) | |
tree | 9ae49366cea441add4173ddde572d79c4f55a5f0 /test | |
parent | c770db7d52408bba14fe0d531b7b6bfae7b32cbd (diff) | |
download | bun-7edaf736a22d5d5b344932f1a290558cec5eb7fa.tar.gz bun-7edaf736a22d5d5b344932f1a290558cec5eb7fa.tar.zst bun-7edaf736a22d5d5b344932f1a290558cec5eb7fa.zip |
[socket] handle `connectError` (#1705)
assorted clean-ups & fixes
Diffstat (limited to 'test')
-rw-r--r-- | test/bun.js/socket/node-net.test.ts | 14 | ||||
-rw-r--r-- | test/bun.js/socket/socket.test.ts | 39 | ||||
-rw-r--r-- | test/bun.js/tcp-server.test.ts | 11 |
3 files changed, 54 insertions, 10 deletions
diff --git a/test/bun.js/socket/node-net.test.ts b/test/bun.js/socket/node-net.test.ts index 47c9964cf..d19644bd3 100644 --- a/test/bun.js/socket/node-net.test.ts +++ b/test/bun.js/socket/node-net.test.ts @@ -1,5 +1,5 @@ import { afterAll, beforeAll, beforeEach, describe, expect, it } from "bun:test"; -import { isIP, isIPv4, isIPv6, Socket } from "net"; +import { connect, isIP, isIPv4, isIPv6, Socket } from "net"; it("should support net.isIP()", () => { expect(isIP("::1")).toBe(6); @@ -173,3 +173,15 @@ describe("net.Socket write", () => { afterAll(() => server.stop()); }); + +it("should handle connection error", done => { + var data = {}; + connect(55555, () => { + done(new Error("Should not have connected")); + }).on("error", error => { + expect(error).toBeDefined(); + expect(error.name).toBe("SystemError"); + expect(error.message).toBe("Failed to connect"); + done(); + }); +}); diff --git a/test/bun.js/socket/socket.test.ts b/test/bun.js/socket/socket.test.ts index aff001c75..7336306fa 100644 --- a/test/bun.js/socket/socket.test.ts +++ b/test/bun.js/socket/socket.test.ts @@ -1,6 +1,6 @@ import { expect, it } from "bun:test"; import { bunExe } from "../bunExe"; -import { spawn } from "bun"; +import { connect, spawn } from "bun"; it("should keep process alive only when active", async () => { const { exited, stdout, stderr } = spawn({ @@ -32,3 +32,40 @@ it("should keep process alive only when active", async () => { "[Client] CLOSED", ]); }); + +it("should handle connection error", done => { + var data = {}; + connect({ + data, + hostname: "localhost", + port: 55555, + socket: { + connectError(socket, error) { + expect(socket).toBeDefined(); + expect(socket.data).toBe(data); + expect(error).toBeDefined(); + expect(error.name).toBe("SystemError"); + expect(error.message).toBe("Failed to connect"); + done(); + }, + data() { + done(new Error("Unexpected data()")); + }, + drain() { + done(new Error("Unexpected drain()")); + }, + close() { + done(new Error("Unexpected close()")); + }, + end() { + done(new Error("Unexpected end()")); + }, + error() { + done(new Error("Unexpected error()")); + }, + open() { + done(new Error("Unexpected open()")); + }, + }, + }); +}); diff --git a/test/bun.js/tcp-server.test.ts b/test/bun.js/tcp-server.test.ts index 65dd069a6..3f008a16b 100644 --- a/test/bun.js/tcp-server.test.ts +++ b/test/bun.js/tcp-server.test.ts @@ -96,17 +96,12 @@ it("echo server 1 on 1", async () => { await Promise.all([prom, clientProm, serverProm]); server.stop(); server = serverData = clientData = undefined; - Bun.gc(true); })(); +}); +it("should not leak memory", () => { // Tell the garbage collector for sure that we're done with the sockets - await new Promise((resolve, reject) => { - setTimeout(() => { - Bun.gc(true); - resolve(undefined); - }, 1); - }); - + Bun.gc(true); // assert we don't leak the sockets // we expect 1 because that's the prototype / structure expect(JSC.heapStats().objectTypeCounts.TCPSocket).toBe(1); |