Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: avoid using same port number for different net tests #4147

Merged
merged 24 commits into from
Mar 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
8521877
reorg: [http] move io functions to http/io.ts
keroxp Feb 25, 2020
05ec6bd
Update mod.ts
keroxp Feb 25, 2020
49ba9d6
revert testGroup
keroxp Feb 25, 2020
21045f3
Update cookie_test.ts
keroxp Feb 25, 2020
473d614
misc: reduce unnecessary stout output on tests (Part1)
keroxp Feb 26, 2020
849c594
wip
keroxp Feb 26, 2020
ac60fdd
Merge branch 'reorg-http-io' into port-picker-for-tests
keroxp Feb 26, 2020
f2d1f1b
misc: avoid using same port number for different net tests
keroxp Feb 26, 2020
cf6db83
Update file_server_test.ts
keroxp Feb 26, 2020
1d57f3b
Update server_test.ts
keroxp Feb 26, 2020
0132755
Merge branch 'master' of https://github.com/denoland/deno into port-p…
keroxp Feb 26, 2020
e3f33bb
Merge branch 'master' of https://github.com/denoland/deno into port-p…
keroxp Feb 28, 2020
0b65b4e
Merge branch 'master' of https://github.com/denoland/deno into port-p…
keroxp Mar 4, 2020
217ae4c
Merge branch 'master' of https://github.com/denoland/deno into port-p…
keroxp Mar 5, 2020
7f59c39
Update net_test.ts
keroxp Mar 5, 2020
21e7c98
Merge branch 'master' of https://github.com/denoland/deno into port-p…
keroxp Mar 16, 2020
4769e74
Update net_test.ts
keroxp Mar 16, 2020
b8ff099
Update net_test.ts
keroxp Mar 16, 2020
5352225
Merge branch 'master' of https://github.com/denoland/deno into port-p…
keroxp Mar 18, 2020
209e5fe
apply PR reviews
keroxp Mar 18, 2020
a3286e9
fmt
keroxp Mar 18, 2020
fbbc9e9
Merge branch 'master' of https://github.com/denoland/deno into port-p…
keroxp Mar 20, 2020
a68c25b
fmt
keroxp Mar 20, 2020
c09c931
Merge branch 'master' of https://github.com/denoland/deno into port-p…
keroxp Mar 21, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 40 additions & 25 deletions cli/js/tests/net_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import {
unitTest,
assert,
assertEquals,
createResolvable
createResolvable,
randomPort
} from "./test_util.ts";

unitTest({ perms: { net: true } }, function netTcpListenClose(): void {
const listener = Deno.listen({ hostname: "127.0.0.1", port: 4500 });
const port = randomPort();
const listener = Deno.listen({ hostname: "127.0.0.1", port });
assertEquals(listener.addr.transport, "tcp");
assertEquals(listener.addr.hostname, "127.0.0.1");
assertEquals(listener.addr.port, 4500);
assertEquals(listener.addr.port, port);
listener.close();
});

Expand All @@ -21,14 +23,15 @@ unitTest(
ignore: Deno.build.os === "win"
},
function netUdpListenClose(): void {
const port = randomPort();
const socket = Deno.listen({
hostname: "127.0.0.1",
port: 4500,
port,
transport: "udp"
});
assertEquals(socket.addr.transport, "udp");
assertEquals(socket.addr.hostname, "127.0.0.1");
assertEquals(socket.addr.port, 4500);
assertEquals(socket.addr.port, port);
socket.close();
}
);
Expand All @@ -38,7 +41,8 @@ unitTest(
perms: { net: true }
},
async function netTcpCloseWhileAccept(): Promise<void> {
const listener = Deno.listen({ port: 4501 });
const port = randomPort();
const listener = Deno.listen({ port });
const p = listener.accept();
listener.close();
let err;
Expand All @@ -56,7 +60,8 @@ unitTest(
unitTest(
{ perms: { net: true } },
async function netTcpConcurrentAccept(): Promise<void> {
const listener = Deno.listen({ port: 4502 });
const port = randomPort();
const listener = Deno.listen({ port });
let acceptErrCount = 0;
const checkErr = (e: Error): void => {
if (e.message === "Listener has been closed") {
Expand All @@ -79,19 +84,20 @@ unitTest(
unitTest({ perms: { net: true } }, async function netTcpDialListen(): Promise<
void
> {
const listener = Deno.listen({ port: 4500 });
const port = randomPort();
const listener = Deno.listen({ port });
listener.accept().then(
async (conn): Promise<void> => {
assert(conn.remoteAddr != null);
assertEquals(conn.localAddr.hostname, "127.0.0.1");
assertEquals(conn.localAddr.port, 4500);
assertEquals(conn.localAddr.port, port);
await conn.write(new Uint8Array([1, 2, 3]));
conn.close();
}
);
const conn = await Deno.connect({ hostname: "127.0.0.1", port: 4500 });
const conn = await Deno.connect({ hostname: "127.0.0.1", port });
assertEquals(conn.remoteAddr.hostname, "127.0.0.1");
assertEquals(conn.remoteAddr.port, 4500);
assertEquals(conn.remoteAddr.port, port);
assert(conn.localAddr != null);
const buf = new Uint8Array(1024);
const readResult = await conn.read(buf);
Expand All @@ -113,21 +119,23 @@ unitTest({ perms: { net: true } }, async function netTcpDialListen(): Promise<
unitTest(
{ ignore: Deno.build.os === "win", perms: { net: true } },
async function netUdpSendReceive(): Promise<void> {
const alice = Deno.listen({ port: 4500, transport: "udp" });
assertEquals(alice.addr.port, 4500);
const alicePort = randomPort();
const alice = Deno.listen({ port: alicePort, transport: "udp" });
assertEquals(alice.addr.port, alicePort);
assertEquals(alice.addr.hostname, "0.0.0.0");
assertEquals(alice.addr.transport, "udp");

const bob = Deno.listen({ port: 4501, transport: "udp" });
assertEquals(bob.addr.port, 4501);
const bobPort = randomPort();
const bob = Deno.listen({ port: bobPort, transport: "udp" });
assertEquals(bob.addr.port, bobPort);
assertEquals(bob.addr.hostname, "0.0.0.0");
assertEquals(bob.addr.transport, "udp");

const sent = new Uint8Array([1, 2, 3]);
await alice.send(sent, bob.addr);

const [recvd, remote] = await bob.receive();
assertEquals(remote.port, 4500);
assertEquals(remote.port, alicePort);
assertEquals(recvd.length, 3);
assertEquals(1, recvd[0]);
assertEquals(2, recvd[1]);
Expand All @@ -140,7 +148,8 @@ unitTest(
unitTest(
{ perms: { net: true } },
async function netTcpListenCloseWhileIterating(): Promise<void> {
const listener = Deno.listen({ port: 8000 });
const port = randomPort();
const listener = Deno.listen({ port });
const nextWhileClosing = listener[Symbol.asyncIterator]().next();
listener.close();
assertEquals(await nextWhileClosing, { value: undefined, done: true });
Expand All @@ -153,7 +162,8 @@ unitTest(
unitTest(
{ ignore: Deno.build.os === "win", perms: { net: true } },
async function netUdpListenCloseWhileIterating(): Promise<void> {
const socket = Deno.listen({ port: 8000, transport: "udp" });
const port = randomPort();
const socket = Deno.listen({ port, transport: "udp" });
const nextWhileClosing = socket[Symbol.asyncIterator]().next();
socket.close();
assertEquals(await nextWhileClosing, { value: undefined, done: true });
Expand All @@ -170,7 +180,8 @@ unitTest(
perms: { net: true }
},
async function netListenAsyncIterator(): Promise<void> {
const addr = { hostname: "127.0.0.1", port: 4500 };
const port = randomPort();
const addr = { hostname: "127.0.0.1", port };
const listener = Deno.listen(addr);
const runAsyncIterator = async (): Promise<void> => {
for await (const conn of listener) {
Expand Down Expand Up @@ -205,7 +216,8 @@ unitTest(
perms: { net: true }
},
async function netCloseReadSuccess() {
const addr = { hostname: "127.0.0.1", port: 4500 };
const port = randomPort();
const addr = { hostname: "127.0.0.1", port };
const listener = Deno.listen(addr);
const closeDeferred = createResolvable();
const closeReadDeferred = createResolvable();
Expand Down Expand Up @@ -242,7 +254,8 @@ unitTest(
perms: { net: true }
},
async function netDoubleCloseRead() {
const addr = { hostname: "127.0.0.1", port: 4500 };
const port = randomPort();
const addr = { hostname: "127.0.0.1", port };
const listener = Deno.listen(addr);
const closeDeferred = createResolvable();
listener.accept().then(async conn => {
Expand Down Expand Up @@ -274,7 +287,8 @@ unitTest(
perms: { net: true }
},
async function netCloseWriteSuccess() {
const addr = { hostname: "127.0.0.1", port: 4500 };
const port = randomPort();
const addr = { hostname: "127.0.0.1", port };
const listener = Deno.listen(addr);
const closeDeferred = createResolvable();
listener.accept().then(async conn => {
Expand Down Expand Up @@ -313,7 +327,8 @@ unitTest(
perms: { net: true }
},
async function netDoubleCloseWrite() {
const addr = { hostname: "127.0.0.1", port: 4500 };
const port = randomPort();
const addr = { hostname: "127.0.0.1", port };
const listener = Deno.listen(addr);
const closeDeferred = createResolvable();
listener.accept().then(async conn => {
Expand Down Expand Up @@ -365,8 +380,8 @@ unitTest(

resolvable.resolve();
}

const addr = { hostname: "127.0.0.1", port: 4500 };
const port = randomPort();
const addr = { hostname: "127.0.0.1", port };
const listener = Deno.listen(addr);
iteratorReq(listener);
const conn = await Deno.connect(addr);
Expand Down
18 changes: 18 additions & 0 deletions cli/js/tests/test_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,21 @@ unitTest(
});
}
);
function* portIterator(): IterableIterator<number> {
// use 49152 ~ 55000 for js/cli (rest are for std)
let i = 49152;
while (true) {
yield i;
i++;
if (i > 55000) {
i = 55000;
}
}
}
const it = portIterator();
/** Obtain (maybe) safe port number for net tests */
export function randomPort(): number {
const { value } = it.next();
assert(value != null);
return value;
}
12 changes: 7 additions & 5 deletions cli/js/tests/tls_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import {
assert,
assertEquals,
randomPort,
createResolvable,
unitTest
} from "./test_util.ts";
Expand Down Expand Up @@ -43,7 +44,7 @@ unitTest(
let err;
const options = {
hostname: "localhost",
port: 4500,
port: randomPort(),
certFile: "cli/tests/tls/localhost.crt",
keyFile: "cli/tests/tls/localhost.key"
};
Expand Down Expand Up @@ -72,10 +73,11 @@ unitTest(

unitTest({ perms: { net: true } }, function listenTLSNoReadPerm(): void {
let err;
const port = randomPort();
try {
Deno.listenTLS({
hostname: "localhost",
port: 4500,
port,
certFile: "cli/tests/tls/localhost.crt",
keyFile: "cli/tests/tls/localhost.key"
});
Expand All @@ -94,7 +96,7 @@ unitTest(
let err;
const options = {
hostname: "localhost",
port: 4500,
port: randomPort(),
certFile: "cli/tests/tls/localhost.crt",
keyFile: "cli/tests/tls/localhost.key"
};
Expand Down Expand Up @@ -123,7 +125,7 @@ unitTest(
let err;
const options = {
hostname: "localhost",
port: 4500,
port: randomPort(),
certFile: "cli/tests/tls/localhost.crt",
keyFile: "cli/tests/tls/localhost.key"
};
Expand Down Expand Up @@ -151,7 +153,7 @@ unitTest(
async function dialAndListenTLS(): Promise<void> {
const resolvable = createResolvable();
const hostname = "localhost";
const port = 4500;
const port = randomPort();

const listener = Deno.listenTLS({
hostname,
Expand Down
6 changes: 4 additions & 2 deletions std/examples/chat/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ async function wsHandler(ws: WebSocket): Promise<void> {
}
}

listenAndServe({ port: 8080 }, async req => {
const addr = Deno.args[0] ?? "127.0.0.1:8080";

listenAndServe(addr, async req => {
if (req.method === "GET" && req.url === "/") {
//Serve with hack
const u = new URL("./index.html", import.meta.url);
Expand Down Expand Up @@ -75,4 +77,4 @@ listenAndServe({ port: 8080 }, async req => {
}
}
});
console.log("chat server starting on :8080....");
console.log(`chat server starting on ${addr}....`);
15 changes: 12 additions & 3 deletions std/examples/chat/server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@ import { assert, assertEquals } from "../../testing/asserts.ts";
import { TextProtoReader } from "../../textproto/mod.ts";
import { BufReader } from "../../io/bufio.ts";
import { connectWebSocket, WebSocket } from "../../ws/mod.ts";
import { randomPort } from "../../http/test_util.ts";
import { delay } from "../../util/async.ts";

const port = randomPort();

const { test, build } = Deno;

async function startServer(): Promise<Deno.Process> {
const server = Deno.run({
args: [Deno.execPath(), "--allow-net", "--allow-read", "server.ts"],
args: [
Deno.execPath(),
"--allow-net",
"--allow-read",
"server.ts",
`127.0.0.1:${port}`
],
cwd: "examples/chat",
stdout: "piped"
});
Expand All @@ -35,7 +44,7 @@ test({
async fn() {
const server = await startServer();
try {
const resp = await fetch("http://127.0.0.1:8080/");
const resp = await fetch(`http://127.0.0.1:${port}/`);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/html");
const html = await resp.body.text();
Expand All @@ -55,7 +64,7 @@ test({
const server = await startServer();
let ws: WebSocket | undefined;
try {
ws = await connectWebSocket("http://127.0.0.1:8080/ws");
ws = await connectWebSocket(`http://127.0.0.1:${port}/ws`);
const it = ws.receive();
assertEquals((await it.next()).value, "Connected: [1]");
ws.send("Hello");
Expand Down
2 changes: 1 addition & 1 deletion std/examples/echo_server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
const hostname = "0.0.0.0";
const port = 8080;
const port = +(Deno.args[0] ?? "8080");
const listener = Deno.listen({ hostname, port });
console.log(`Listening on ${hostname}:${port}`);
for await (const conn of listener) {
Expand Down
6 changes: 4 additions & 2 deletions std/examples/tests/curl_test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { serve } from "../../http/server.ts";
import { assertStrictEq } from "../../testing/asserts.ts";
import { randomPort } from "../../http/test_util.ts";

const port = randomPort();
Deno.test({
name: "[examples/curl] send a request to a specified url",
// FIXME(bartlomieju): this test is leaking both resources and ops,
// and causes interference with other tests
ignore: true,
fn: async () => {
const server = serve({ port: 8081 });
const server = serve({ port });
(async (): Promise<void> => {
for await (const req of server) {
req.respond({ body: "Hello world" });
Expand All @@ -21,7 +23,7 @@ Deno.test({
Deno.execPath(),
"--allow-net",
"curl.ts",
"http://localhost:8081"
"http://localhost:" + port
],
cwd: "examples",
stdout: "piped"
Expand Down
8 changes: 5 additions & 3 deletions std/examples/tests/echo_server_test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { assertStrictEq, assertNotEquals } from "../../testing/asserts.ts";
import { BufReader, ReadLineResult } from "../../io/bufio.ts";
import { randomPort } from "../../http/test_util.ts";
const port = randomPort();

Deno.test("[examples/echo_server]", async () => {
const encoder = new TextEncoder();
const decoder = new TextDecoder();
const process = Deno.run({
args: [Deno.execPath(), "--allow-net", "echo_server.ts"],
args: [Deno.execPath(), "--allow-net", "echo_server.ts", `${port}`],
cwd: "examples",
stdout: "piped"
});
Expand All @@ -19,10 +21,10 @@ Deno.test("[examples/echo_server]", async () => {
assertNotEquals(message, Deno.EOF);
assertStrictEq(
decoder.decode((message as ReadLineResult).line).trim(),
"Listening on 0.0.0.0:8080"
"Listening on 0.0.0.0:" + port
);

conn = await Deno.connect({ hostname: "127.0.0.1", port: 8080 });
conn = await Deno.connect({ hostname: "127.0.0.1", port });
const connReader = new BufReader(conn);

await conn.write(encoder.encode("Hello echo_server\n"));
Expand Down
Loading