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

Revert "avoid using same port number for test (#4147)" #4478

Merged
merged 3 commits into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
66 changes: 26 additions & 40 deletions cli/js/tests/net_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ import {
unitTest,
assert,
assertEquals,
createResolvable,
randomPort
createResolvable
} from "./test_util.ts";

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

Expand All @@ -23,15 +21,14 @@ unitTest(
ignore: Deno.build.os === "win"
},
function netUdpListenClose(): void {
const port = randomPort();
const socket = Deno.listen({
hostname: "127.0.0.1",
port,
port: 4500,
transport: "udp"
});
assert(socket.addr.transport === "udp");
assertEquals(socket.addr.hostname, "127.0.0.1");
assertEquals(socket.addr.port, port);
assertEquals(socket.addr.port, 4500);
socket.close();
}
);
Expand Down Expand Up @@ -69,8 +66,7 @@ unitTest(
perms: { net: true }
},
async function netTcpCloseWhileAccept(): Promise<void> {
const port = randomPort();
const listener = Deno.listen({ port });
const listener = Deno.listen({ port: 4501 });
const p = listener.accept();
listener.close();
let err;
Expand Down Expand Up @@ -110,8 +106,7 @@ unitTest(
unitTest(
{ perms: { net: true } },
async function netTcpConcurrentAccept(): Promise<void> {
const port = randomPort();
const listener = Deno.listen({ port });
const listener = Deno.listen({ port: 4502 });
let acceptErrCount = 0;
const checkErr = (e: Error): void => {
if (e.message === "Listener has been closed") {
Expand Down Expand Up @@ -159,22 +154,22 @@ unitTest(
unitTest({ perms: { net: true } }, async function netTcpDialListen(): Promise<
void
> {
const port = randomPort();
const listener = Deno.listen({ port });
const listener = Deno.listen({ port: 4500 });
listener.accept().then(
async (conn): Promise<void> => {
assert(conn.remoteAddr != null);
assert(conn.localAddr.transport === "tcp");
assertEquals(conn.localAddr.hostname, "127.0.0.1");
assertEquals(conn.localAddr.port, port);
assertEquals(conn.localAddr.port, 4500);
await conn.write(new Uint8Array([1, 2, 3]));
conn.close();
}
);
const conn = await Deno.connect({ hostname: "127.0.0.1", port });

const conn = await Deno.connect({ hostname: "127.0.0.1", port: 4500 });
assert(conn.remoteAddr.transport === "tcp");
assertEquals(conn.remoteAddr.hostname, "127.0.0.1");
assertEquals(conn.remoteAddr.port, port);
assertEquals(conn.remoteAddr.port, 4500);
assert(conn.localAddr != null);
const buf = new Uint8Array(1024);
const readResult = await conn.read(buf);
Expand Down Expand Up @@ -232,24 +227,22 @@ unitTest(
unitTest(
{ ignore: Deno.build.os === "win", perms: { net: true } },
async function netUdpSendReceive(): Promise<void> {
const alicePort = randomPort();
const alice = Deno.listen({ port: alicePort, transport: "udp" });
const alice = Deno.listen({ port: 4500, transport: "udp" });
assert(alice.addr.transport === "udp");
assertEquals(alice.addr.port, alicePort);
assertEquals(alice.addr.port, 4500);
assertEquals(alice.addr.hostname, "127.0.0.1");

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

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

const [recvd, remote] = await bob.receive();
assert(remote.transport === "udp");
assertEquals(remote.port, alicePort);
assertEquals(remote.port, 4500);
assertEquals(recvd.length, 3);
assertEquals(1, recvd[0]);
assertEquals(2, recvd[1]);
Expand Down Expand Up @@ -289,8 +282,7 @@ unitTest(
unitTest(
{ perms: { net: true } },
async function netTcpListenCloseWhileIterating(): Promise<void> {
const port = randomPort();
const listener = Deno.listen({ port });
const listener = Deno.listen({ port: 8000 });
const nextWhileClosing = listener[Symbol.asyncIterator]().next();
listener.close();
assertEquals(await nextWhileClosing, { value: undefined, done: true });
Expand All @@ -303,8 +295,7 @@ unitTest(
unitTest(
{ ignore: Deno.build.os === "win", perms: { net: true } },
async function netUdpListenCloseWhileIterating(): Promise<void> {
const port = randomPort();
const socket = Deno.listen({ port, transport: "udp" });
const socket = Deno.listen({ port: 8000, transport: "udp" });
const nextWhileClosing = socket[Symbol.asyncIterator]().next();
socket.close();
assertEquals(await nextWhileClosing, { value: undefined, done: true });
Expand Down Expand Up @@ -349,8 +340,7 @@ unitTest(
perms: { net: true }
},
async function netListenAsyncIterator(): Promise<void> {
const port = randomPort();
const addr = { hostname: "127.0.0.1", port };
const addr = { hostname: "127.0.0.1", port: 4500 };
const listener = Deno.listen(addr);
const runAsyncIterator = async (): Promise<void> => {
for await (const conn of listener) {
Expand Down Expand Up @@ -385,8 +375,7 @@ unitTest(
perms: { net: true }
},
async function netCloseReadSuccess() {
const port = randomPort();
const addr = { hostname: "127.0.0.1", port };
const addr = { hostname: "127.0.0.1", port: 4500 };
const listener = Deno.listen(addr);
const closeDeferred = createResolvable();
const closeReadDeferred = createResolvable();
Expand Down Expand Up @@ -423,8 +412,7 @@ unitTest(
perms: { net: true }
},
async function netDoubleCloseRead() {
const port = randomPort();
const addr = { hostname: "127.0.0.1", port };
const addr = { hostname: "127.0.0.1", port: 4500 };
const listener = Deno.listen(addr);
const closeDeferred = createResolvable();
listener.accept().then(async conn => {
Expand Down Expand Up @@ -456,8 +444,7 @@ unitTest(
perms: { net: true }
},
async function netCloseWriteSuccess() {
const port = randomPort();
const addr = { hostname: "127.0.0.1", port };
const addr = { hostname: "127.0.0.1", port: 4500 };
const listener = Deno.listen(addr);
const closeDeferred = createResolvable();
listener.accept().then(async conn => {
Expand Down Expand Up @@ -496,8 +483,7 @@ unitTest(
perms: { net: true }
},
async function netDoubleCloseWrite() {
const port = randomPort();
const addr = { hostname: "127.0.0.1", port };
const addr = { hostname: "127.0.0.1", port: 4500 };
const listener = Deno.listen(addr);
const closeDeferred = createResolvable();
listener.accept().then(async conn => {
Expand Down Expand Up @@ -549,8 +535,8 @@ unitTest(

resolvable.resolve();
}
const port = randomPort();
const addr = { hostname: "127.0.0.1", port };

const addr = { hostname: "127.0.0.1", port: 4500 };
const listener = Deno.listen(addr);
iteratorReq(listener);
const conn = await Deno.connect(addr);
Expand Down
18 changes: 0 additions & 18 deletions cli/js/tests/test_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,21 +359,3 @@ 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: 5 additions & 7 deletions cli/js/tests/tls_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import {
assert,
assertEquals,
randomPort,
createResolvable,
unitTest
} from "./test_util.ts";
Expand Down Expand Up @@ -44,7 +43,7 @@ unitTest(
let err;
const options = {
hostname: "localhost",
port: randomPort(),
port: 4500,
certFile: "cli/tests/tls/localhost.crt",
keyFile: "cli/tests/tls/localhost.key"
};
Expand Down Expand Up @@ -73,11 +72,10 @@ unitTest(

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

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

const addr = Deno.args[0] ?? "127.0.0.1:8080";

listenAndServe(addr, async req => {
listenAndServe({ port: 8080 }, async req => {
if (req.method === "GET" && req.url === "/") {
//Serve with hack
const u = new URL("./index.html", import.meta.url);
Expand Down Expand Up @@ -77,4 +75,4 @@ listenAndServe(addr, async req => {
}
}
});
console.log(`chat server starting on ${addr}....`);
console.log("chat server starting on :8080....");
15 changes: 3 additions & 12 deletions std/examples/chat/server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,13 @@ 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({
cmd: [
Deno.execPath(),
"--allow-net",
"--allow-read",
"server.ts",
`127.0.0.1:${port}`
],
cmd: [Deno.execPath(), "--allow-net", "--allow-read", "server.ts"],
cwd: "examples/chat",
stdout: "piped"
});
Expand All @@ -44,7 +35,7 @@ test({
async fn() {
const server = await startServer();
try {
const resp = await fetch(`http://127.0.0.1:${port}/`);
const resp = await fetch("http://127.0.0.1:8080/");
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/html");
const html = await resp.body.text();
Expand All @@ -64,7 +55,7 @@ test({
const server = await startServer();
let ws: WebSocket | undefined;
try {
ws = await connectWebSocket(`http://127.0.0.1:${port}/ws`);
ws = await connectWebSocket("http://127.0.0.1:8080/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 = +(Deno.args[0] ?? "8080");
const port = 8080;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bartlomieju Would you limit port range around 4500?

const listener = Deno.listen({ hostname, port });
console.log(`Listening on ${hostname}:${port}`);
for await (const conn of listener) {
Expand Down
11 changes: 2 additions & 9 deletions std/examples/tests/curl_test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// 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",
fn: async () => {
const server = serve({ port });
const server = serve({ port: 8081 });
const serverPromise = (async (): Promise<void> => {
for await (const req of server) {
req.respond({ body: "Hello world" });
Expand All @@ -16,12 +14,7 @@ Deno.test({

const decoder = new TextDecoder();
const process = Deno.run({
cmd: [
Deno.execPath(),
"--allow-net",
"curl.ts",
"http://localhost:" + port
],
cmd: [Deno.execPath(), "--allow-net", "curl.ts", "http://localhost:8081"],
cwd: "examples",
stdout: "piped"
});
Expand Down
Loading