summaryrefslogtreecommitdiff
path: root/cli/tests/unit/net_test.ts
diff options
context:
space:
mode:
authorMatt Mastracci <matthew@mastracci.com>2023-05-22 13:35:59 -0600
committerGitHub <noreply@github.com>2023-05-22 13:35:59 -0600
commit612226de8e2fe3068d981866242bacedfceb9734 (patch)
tree318d644be0770ce1bff9bf1ceff415c0b4705ec2 /cli/tests/unit/net_test.ts
parentffa020f43a2a0d04fade562b2f82cd1a39913780 (diff)
chore(cli): One Rust test per JS and Node unit test file (#19199)
This runs our `js_unit_tests` and `node_unit_tests` in parallel, one rust test per JS unit test file. Some of our JS tests don't like running in parallel due to port requirements, so this also makes those use a specific port-per-file. This does not attempt to make the node-compat tests work.
Diffstat (limited to 'cli/tests/unit/net_test.ts')
-rw-r--r--cli/tests/unit/net_test.ts100
1 files changed, 52 insertions, 48 deletions
diff --git a/cli/tests/unit/net_test.ts b/cli/tests/unit/net_test.ts
index 935a6f846..32250bbd0 100644
--- a/cli/tests/unit/net_test.ts
+++ b/cli/tests/unit/net_test.ts
@@ -12,6 +12,10 @@ import {
} from "./test_util.ts";
import { join } from "../../../test_util/std/path/mod.ts";
+// Since these tests may run in parallel, ensure this port is unique to this file
+const listenPort = 4503;
+const listenPort2 = 4504;
+
let isCI: boolean;
try {
isCI = Deno.env.get("CI") !== undefined;
@@ -20,10 +24,10 @@ try {
}
Deno.test({ permissions: { net: true } }, function netTcpListenClose() {
- const listener = Deno.listen({ hostname: "127.0.0.1", port: 3500 });
+ const listener = Deno.listen({ hostname: "127.0.0.1", port: listenPort });
assert(listener.addr.transport === "tcp");
assertEquals(listener.addr.hostname, "127.0.0.1");
- assertEquals(listener.addr.port, 3500);
+ assertEquals(listener.addr.port, listenPort);
assertNotEquals(listener.rid, 0);
listener.close();
});
@@ -35,12 +39,12 @@ Deno.test(
function netUdpListenClose() {
const socket = Deno.listenDatagram({
hostname: "127.0.0.1",
- port: 3500,
+ port: listenPort,
transport: "udp",
});
assert(socket.addr.transport === "udp");
assertEquals(socket.addr.hostname, "127.0.0.1");
- assertEquals(socket.addr.port, 3500);
+ assertEquals(socket.addr.port, listenPort);
socket.close();
},
);
@@ -127,7 +131,7 @@ Deno.test(
permissions: { net: true },
},
async function netTcpCloseWhileAccept() {
- const listener = Deno.listen({ port: 4501 });
+ const listener = Deno.listen({ port: listenPort });
const p = listener.accept();
listener.close();
// TODO(piscisaureus): the error type should be `Interrupted` here, which
@@ -212,22 +216,22 @@ Deno.test(
);
Deno.test({ permissions: { net: true } }, async function netTcpDialListen() {
- const listener = Deno.listen({ port: 3500 });
+ const listener = Deno.listen({ port: listenPort });
listener.accept().then(
async (conn) => {
assert(conn.remoteAddr != null);
assert(conn.localAddr.transport === "tcp");
assertEquals(conn.localAddr.hostname, "127.0.0.1");
- assertEquals(conn.localAddr.port, 3500);
+ assertEquals(conn.localAddr.port, listenPort);
await conn.write(new Uint8Array([1, 2, 3]));
conn.close();
},
);
- const conn = await Deno.connect({ hostname: "127.0.0.1", port: 3500 });
+ const conn = await Deno.connect({ hostname: "127.0.0.1", port: listenPort });
assert(conn.remoteAddr.transport === "tcp");
assertEquals(conn.remoteAddr.hostname, "127.0.0.1");
- assertEquals(conn.remoteAddr.port, 3500);
+ assertEquals(conn.remoteAddr.port, listenPort);
assert(conn.localAddr != null);
const buf = new Uint8Array(1024);
const readResult = await conn.read(buf);
@@ -247,23 +251,23 @@ Deno.test({ permissions: { net: true } }, async function netTcpDialListen() {
});
Deno.test({ permissions: { net: true } }, async function netTcpSetNoDelay() {
- const listener = Deno.listen({ port: 3500 });
+ const listener = Deno.listen({ port: listenPort });
listener.accept().then(
async (conn) => {
assert(conn.remoteAddr != null);
assert(conn.localAddr.transport === "tcp");
assertEquals(conn.localAddr.hostname, "127.0.0.1");
- assertEquals(conn.localAddr.port, 3500);
+ assertEquals(conn.localAddr.port, listenPort);
await conn.write(new Uint8Array([1, 2, 3]));
conn.close();
},
);
- const conn = await Deno.connect({ hostname: "127.0.0.1", port: 3500 });
+ const conn = await Deno.connect({ hostname: "127.0.0.1", port: listenPort });
conn.setNoDelay(true);
assert(conn.remoteAddr.transport === "tcp");
assertEquals(conn.remoteAddr.hostname, "127.0.0.1");
- assertEquals(conn.remoteAddr.port, 3500);
+ assertEquals(conn.remoteAddr.port, listenPort);
assert(conn.localAddr != null);
const buf = new Uint8Array(1024);
const readResult = await conn.read(buf);
@@ -283,23 +287,23 @@ Deno.test({ permissions: { net: true } }, async function netTcpSetNoDelay() {
});
Deno.test({ permissions: { net: true } }, async function netTcpSetKeepAlive() {
- const listener = Deno.listen({ port: 3500 });
+ const listener = Deno.listen({ port: listenPort });
listener.accept().then(
async (conn) => {
assert(conn.remoteAddr != null);
assert(conn.localAddr.transport === "tcp");
assertEquals(conn.localAddr.hostname, "127.0.0.1");
- assertEquals(conn.localAddr.port, 3500);
+ assertEquals(conn.localAddr.port, listenPort);
await conn.write(new Uint8Array([1, 2, 3]));
conn.close();
},
);
- const conn = await Deno.connect({ hostname: "127.0.0.1", port: 3500 });
+ const conn = await Deno.connect({ hostname: "127.0.0.1", port: listenPort });
conn.setKeepAlive(true);
assert(conn.remoteAddr.transport === "tcp");
assertEquals(conn.remoteAddr.hostname, "127.0.0.1");
- assertEquals(conn.remoteAddr.port, 3500);
+ assertEquals(conn.remoteAddr.port, listenPort);
assert(conn.localAddr != null);
const buf = new Uint8Array(1024);
const readResult = await conn.read(buf);
@@ -360,14 +364,14 @@ Deno.test(
Deno.test(
{ permissions: { net: true } },
async function netUdpSendReceive() {
- const alice = Deno.listenDatagram({ port: 3500, transport: "udp" });
+ const alice = Deno.listenDatagram({ port: listenPort, transport: "udp" });
assert(alice.addr.transport === "udp");
- assertEquals(alice.addr.port, 3500);
+ assertEquals(alice.addr.port, listenPort);
assertEquals(alice.addr.hostname, "127.0.0.1");
- const bob = Deno.listenDatagram({ port: 4501, transport: "udp" });
+ const bob = Deno.listenDatagram({ port: listenPort2, transport: "udp" });
assert(bob.addr.transport === "udp");
- assertEquals(bob.addr.port, 4501);
+ assertEquals(bob.addr.port, listenPort2);
assertEquals(bob.addr.hostname, "127.0.0.1");
const sent = new Uint8Array([1, 2, 3]);
@@ -377,7 +381,7 @@ Deno.test(
const [recvd, remote] = await bob.receive();
assert(remote.transport === "udp");
- assertEquals(remote.port, 3500);
+ assertEquals(remote.port, listenPort);
assertEquals(recvd.length, 3);
assertEquals(1, recvd[0]);
assertEquals(2, recvd[1]);
@@ -393,18 +397,18 @@ Deno.test(
// Must bind sender to an address that can send to the broadcast address on MacOS.
// Macos will give us error 49 when sending the broadcast packet if we omit hostname here.
const alice = Deno.listenDatagram({
- port: 3500,
+ port: listenPort,
transport: "udp",
hostname: "0.0.0.0",
});
const bob = Deno.listenDatagram({
- port: 4501,
+ port: listenPort,
transport: "udp",
hostname: "0.0.0.0",
});
assert(bob.addr.transport === "udp");
- assertEquals(bob.addr.port, 4501);
+ assertEquals(bob.addr.port, listenPort);
assertEquals(bob.addr.hostname, "0.0.0.0");
const broadcastAddr = { ...bob.addr, hostname: "255.255.255.255" };
@@ -415,7 +419,7 @@ Deno.test(
assertEquals(byteLength, 3);
const [recvd, remote] = await bob.receive();
assert(remote.transport === "udp");
- assertEquals(remote.port, 3500);
+ assertEquals(remote.port, listenPort);
assertEquals(recvd.length, 3);
assertEquals(1, recvd[0]);
assertEquals(2, recvd[1]);
@@ -563,9 +567,9 @@ Deno.test(
Deno.test(
{ permissions: { net: true } },
async function netUdpConcurrentSendReceive() {
- const socket = Deno.listenDatagram({ port: 3500, transport: "udp" });
+ const socket = Deno.listenDatagram({ port: listenPort, transport: "udp" });
assert(socket.addr.transport === "udp");
- assertEquals(socket.addr.port, 3500);
+ assertEquals(socket.addr.port, listenPort);
assertEquals(socket.addr.hostname, "127.0.0.1");
const recvPromise = socket.receive();
@@ -588,7 +592,7 @@ Deno.test(
{ permissions: { net: true } },
async function netUdpBorrowMutError() {
const socket = Deno.listenDatagram({
- port: 4501,
+ port: listenPort,
transport: "udp",
});
// Panic happened on second send: BorrowMutError
@@ -761,7 +765,7 @@ Deno.test(
Deno.test(
{ permissions: { net: true } },
async function netListenAsyncIterator() {
- const addr = { hostname: "127.0.0.1", port: 3500 };
+ const addr = { hostname: "127.0.0.1", port: listenPort };
const listener = Deno.listen(addr);
const runAsyncIterator = async () => {
for await (const conn of listener) {
@@ -794,7 +798,7 @@ Deno.test(
permissions: { net: true },
},
async function netCloseWriteSuccess() {
- const addr = { hostname: "127.0.0.1", port: 3500 };
+ const addr = { hostname: "127.0.0.1", port: listenPort };
const listener = Deno.listen(addr);
const closeDeferred = deferred();
listener.accept().then(async (conn) => {
@@ -850,7 +854,7 @@ Deno.test(
}
}
- const addr = { hostname: "127.0.0.1", port: 3500 };
+ const addr = { hostname: "127.0.0.1", port: listenPort };
const listener = Deno.listen(addr);
const listenerPromise = iteratorReq(listener);
const connectionPromise = (async () => {
@@ -898,13 +902,13 @@ Deno.test(
Deno.test({ permissions: { net: true } }, async function whatwgStreams() {
(async () => {
- const listener = Deno.listen({ hostname: "127.0.0.1", port: 3500 });
+ const listener = Deno.listen({ hostname: "127.0.0.1", port: listenPort });
const conn = await listener.accept();
await conn.readable.pipeTo(conn.writable);
listener.close();
})();
- const conn = await Deno.connect({ hostname: "127.0.0.1", port: 3500 });
+ const conn = await Deno.connect({ hostname: "127.0.0.1", port: listenPort });
const reader = conn.readable.getReader();
const writer = conn.writable.getWriter();
const encoder = new TextEncoder();
@@ -957,7 +961,7 @@ Deno.test(
async function netListenUnref() {
const [statusCode, _output] = await execCode(`
async function main() {
- const listener = Deno.listen({ port: 3500 });
+ const listener = Deno.listen({ port: ${listenPort} });
listener.unref();
await listener.accept(); // This doesn't block the program from exiting
}
@@ -972,14 +976,14 @@ Deno.test(
async function netListenUnref() {
const [statusCode, _output] = await execCode(`
async function main() {
- const listener = Deno.listen({ port: 3500 });
+ const listener = Deno.listen({ port: ${listenPort} });
await listener.accept();
listener.unref();
await listener.accept(); // The program exits here
throw new Error(); // The program doesn't reach here
}
main();
- const conn = await Deno.connect({ port: 3500 });
+ const conn = await Deno.connect({ port: ${listenPort} });
conn.close();
`);
assertEquals(statusCode, 0);
@@ -991,7 +995,7 @@ Deno.test(
async function netListenUnrefAndRef() {
const p = execCode2(`
async function main() {
- const listener = Deno.listen({ port: 3500 });
+ const listener = Deno.listen({ port: ${listenPort} });
listener.unref();
listener.ref(); // This restores 'ref' state of listener
console.log("started");
@@ -1001,7 +1005,7 @@ Deno.test(
main();
`);
await p.waitStdoutText("started");
- const conn = await Deno.connect({ port: 3500 });
+ const conn = await Deno.connect({ port: listenPort });
conn.close();
const [statusCode, output] = await p.finished();
assertEquals(statusCode, 0);
@@ -1013,7 +1017,7 @@ Deno.test(
{ permissions: { net: true } },
async function netListenUnrefConcurrentAccept() {
const timer = setTimeout(() => {}, 1000);
- const listener = Deno.listen({ port: 3500 });
+ const listener = Deno.listen({ port: listenPort });
listener.accept().catch(() => {});
listener.unref();
// Unref'd listener still causes Busy error
@@ -1044,12 +1048,12 @@ Deno.test({
Deno.test(
{ permissions: { net: true, read: true, run: true } },
async function netConnUnref() {
- const listener = Deno.listen({ port: 3500 });
+ const listener = Deno.listen({ port: listenPort });
const intervalId = setInterval(() => {}); // This keeps event loop alive.
const program = execCode(`
async function main() {
- const conn = await Deno.connect({ port: 3500 });
+ const conn = await Deno.connect({ port: ${listenPort} });
conn.unref();
await conn.read(new Uint8Array(10)); // The program exits here
throw new Error(); // The program doesn't reach here
@@ -1068,12 +1072,12 @@ Deno.test(
Deno.test(
{ permissions: { net: true, read: true, run: true } },
async function netConnUnrefReadable() {
- const listener = Deno.listen({ port: 3500 });
+ const listener = Deno.listen({ port: listenPort });
const intervalId = setInterval(() => {}); // This keeps event loop alive.
const program = execCode(`
async function main() {
- const conn = await Deno.connect({ port: 3500 });
+ const conn = await Deno.connect({ port: ${listenPort} });
conn.unref();
const reader = conn.readable.getReader();
await reader.read(); // The program exits here
@@ -1093,7 +1097,7 @@ Deno.test(
Deno.test({ permissions: { net: true } }, async function netTcpReuseAddr() {
const listener1 = Deno.listen({
hostname: "127.0.0.1",
- port: 3500,
+ port: listenPort,
});
listener1.accept().then(
(conn) => {
@@ -1101,7 +1105,7 @@ Deno.test({ permissions: { net: true } }, async function netTcpReuseAddr() {
},
);
- const conn1 = await Deno.connect({ hostname: "127.0.0.1", port: 3500 });
+ const conn1 = await Deno.connect({ hostname: "127.0.0.1", port: listenPort });
const buf1 = new Uint8Array(1024);
await conn1.read(buf1);
listener1.close();
@@ -1109,7 +1113,7 @@ Deno.test({ permissions: { net: true } }, async function netTcpReuseAddr() {
const listener2 = Deno.listen({
hostname: "127.0.0.1",
- port: 3500,
+ port: listenPort,
});
listener2.accept().then(
@@ -1118,7 +1122,7 @@ Deno.test({ permissions: { net: true } }, async function netTcpReuseAddr() {
},
);
- const conn2 = await Deno.connect({ hostname: "127.0.0.1", port: 3500 });
+ const conn2 = await Deno.connect({ hostname: "127.0.0.1", port: listenPort });
const buf2 = new Uint8Array(1024);
await conn2.read(buf2);