2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-03-06 20:48:46 -05:00
|
|
|
import { testPerm, assert, assertEquals } from "./test_util.ts";
|
2018-10-03 23:58:29 -04:00
|
|
|
|
2020-02-21 11:26:54 -05:00
|
|
|
testPerm({ net: true }, function netTcpListenClose(): void {
|
2019-09-20 18:32:18 -04:00
|
|
|
const listener = Deno.listen({ hostname: "127.0.0.1", port: 4500 });
|
2020-01-18 15:49:55 -05:00
|
|
|
assertEquals(listener.addr.transport, "tcp");
|
|
|
|
assertEquals(listener.addr.hostname, "127.0.0.1");
|
|
|
|
assertEquals(listener.addr.port, 4500);
|
2018-10-03 23:58:29 -04:00
|
|
|
listener.close();
|
|
|
|
});
|
|
|
|
|
2020-02-21 11:26:54 -05:00
|
|
|
testPerm({ net: true }, function netUdpListenClose(): void {
|
|
|
|
if (Deno.build.os === "win") return; // TODO
|
|
|
|
|
|
|
|
const socket = Deno.listen({
|
|
|
|
hostname: "127.0.0.1",
|
|
|
|
port: 4500,
|
|
|
|
transport: "udp"
|
|
|
|
});
|
|
|
|
assertEquals(socket.addr.transport, "udp");
|
|
|
|
assertEquals(socket.addr.hostname, "127.0.0.1");
|
|
|
|
assertEquals(socket.addr.port, 4500);
|
|
|
|
socket.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
testPerm({ net: true }, async function netTcpCloseWhileAccept(): Promise<void> {
|
2019-09-20 18:32:18 -04:00
|
|
|
const listener = Deno.listen({ port: 4501 });
|
2019-01-15 20:36:51 -05:00
|
|
|
const p = listener.accept();
|
|
|
|
listener.close();
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
await p;
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
assert(!!err);
|
2020-02-21 10:36:13 -05:00
|
|
|
assert(err instanceof Error);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(err.message, "Listener has been closed");
|
2019-01-15 20:36:51 -05:00
|
|
|
});
|
|
|
|
|
2020-02-21 11:26:54 -05:00
|
|
|
testPerm({ net: true }, async function netTcpConcurrentAccept(): Promise<void> {
|
2019-09-20 18:32:18 -04:00
|
|
|
const listener = Deno.listen({ port: 4502 });
|
2019-01-17 18:10:34 -05:00
|
|
|
let acceptErrCount = 0;
|
2020-02-21 10:36:13 -05:00
|
|
|
const checkErr = (e: Error): void => {
|
2019-01-17 18:10:34 -05:00
|
|
|
if (e.message === "Listener has been closed") {
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(acceptErrCount, 1);
|
2019-01-17 18:10:34 -05:00
|
|
|
} else if (e.message === "Another accept task is ongoing") {
|
|
|
|
acceptErrCount++;
|
|
|
|
} else {
|
|
|
|
throw new Error("Unexpected error message");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const p = listener.accept().catch(checkErr);
|
|
|
|
const p1 = listener.accept().catch(checkErr);
|
|
|
|
await Promise.race([p, p1]);
|
2019-01-15 20:36:51 -05:00
|
|
|
listener.close();
|
2020-03-03 12:22:53 -05:00
|
|
|
await Promise.all([p, p1]);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(acceptErrCount, 1);
|
2019-01-15 20:36:51 -05:00
|
|
|
});
|
|
|
|
|
2020-02-21 11:26:54 -05:00
|
|
|
testPerm({ net: true }, async function netTcpDialListen(): Promise<void> {
|
2019-09-20 18:32:18 -04:00
|
|
|
const listener = Deno.listen({ port: 4500 });
|
2019-04-21 16:40:10 -04:00
|
|
|
listener.accept().then(
|
|
|
|
async (conn): Promise<void> => {
|
2019-08-27 11:35:32 -04:00
|
|
|
assert(conn.remoteAddr != null);
|
2020-01-18 15:49:55 -05:00
|
|
|
assertEquals(conn.localAddr.hostname, "127.0.0.1");
|
|
|
|
assertEquals(conn.localAddr.port, 4500);
|
2019-04-21 16:40:10 -04:00
|
|
|
await conn.write(new Uint8Array([1, 2, 3]));
|
|
|
|
conn.close();
|
|
|
|
}
|
|
|
|
);
|
2020-01-18 12:35:12 -05:00
|
|
|
const conn = await Deno.connect({ hostname: "127.0.0.1", port: 4500 });
|
2020-01-18 15:49:55 -05:00
|
|
|
assertEquals(conn.remoteAddr.hostname, "127.0.0.1");
|
|
|
|
assertEquals(conn.remoteAddr.port, 4500);
|
2019-08-27 11:35:32 -04:00
|
|
|
assert(conn.localAddr != null);
|
2018-10-03 23:58:29 -04:00
|
|
|
const buf = new Uint8Array(1024);
|
|
|
|
const readResult = await conn.read(buf);
|
2019-07-06 10:16:03 -04:00
|
|
|
assertEquals(3, readResult);
|
2019-05-01 16:58:09 -04:00
|
|
|
assertEquals(1, buf[0]);
|
|
|
|
assertEquals(2, buf[1]);
|
|
|
|
assertEquals(3, buf[2]);
|
|
|
|
assert(conn.rid > 0);
|
|
|
|
|
2019-07-06 10:16:03 -04:00
|
|
|
assert(readResult !== Deno.EOF);
|
2019-05-01 16:58:09 -04:00
|
|
|
|
|
|
|
const readResult2 = await conn.read(buf);
|
2019-07-06 10:16:03 -04:00
|
|
|
assertEquals(Deno.EOF, readResult2);
|
2019-05-01 16:58:09 -04:00
|
|
|
|
|
|
|
listener.close();
|
|
|
|
conn.close();
|
|
|
|
});
|
|
|
|
|
2020-02-21 11:26:54 -05:00
|
|
|
testPerm({ net: true }, async function netUdpSendReceive(): Promise<void> {
|
|
|
|
if (Deno.build.os === "win") return; // TODO
|
|
|
|
|
|
|
|
const alice = Deno.listen({ port: 4500, transport: "udp" });
|
|
|
|
assertEquals(alice.addr.port, 4500);
|
|
|
|
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);
|
|
|
|
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);
|
2019-11-09 14:40:22 -05:00
|
|
|
|
2020-02-21 11:26:54 -05:00
|
|
|
const [recvd, remote] = await bob.receive();
|
|
|
|
assertEquals(remote.port, 4500);
|
|
|
|
assertEquals(recvd.length, 3);
|
|
|
|
assertEquals(1, recvd[0]);
|
|
|
|
assertEquals(2, recvd[1]);
|
|
|
|
assertEquals(3, recvd[2]);
|
2020-02-29 12:45:47 -05:00
|
|
|
alice.close();
|
|
|
|
bob.close();
|
2019-11-09 14:40:22 -05:00
|
|
|
});
|
|
|
|
|
2020-02-21 11:26:54 -05:00
|
|
|
testPerm(
|
|
|
|
{ net: true },
|
|
|
|
async function netTcpListenCloseWhileIterating(): Promise<void> {
|
|
|
|
const listener = Deno.listen({ port: 8000 });
|
|
|
|
const nextWhileClosing = listener[Symbol.asyncIterator]().next();
|
|
|
|
listener.close();
|
|
|
|
assertEquals(await nextWhileClosing, { value: undefined, done: true });
|
|
|
|
|
|
|
|
const nextAfterClosing = listener[Symbol.asyncIterator]().next();
|
|
|
|
assertEquals(await nextAfterClosing, { value: undefined, done: true });
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
testPerm(
|
|
|
|
{ net: true },
|
|
|
|
async function netUdpListenCloseWhileIterating(): Promise<void> {
|
|
|
|
if (Deno.build.os === "win") return; // TODO
|
|
|
|
|
|
|
|
const socket = Deno.listen({ port: 8000, transport: "udp" });
|
|
|
|
const nextWhileClosing = socket[Symbol.asyncIterator]().next();
|
|
|
|
socket.close();
|
|
|
|
assertEquals(await nextWhileClosing, { value: undefined, done: true });
|
|
|
|
|
|
|
|
const nextAfterClosing = socket[Symbol.asyncIterator]().next();
|
|
|
|
assertEquals(await nextAfterClosing, { value: undefined, done: true });
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-05-02 17:08:02 -04:00
|
|
|
/* TODO(ry) Re-enable this test.
|
2019-05-01 16:58:09 -04:00
|
|
|
testPerm({ net: true }, async function netListenAsyncIterator(): Promise<void> {
|
2019-09-20 18:32:18 -04:00
|
|
|
const listener = Deno.listen(":4500");
|
2019-05-01 16:58:09 -04:00
|
|
|
const runAsyncIterator = async (): Promise<void> => {
|
|
|
|
for await (let conn of listener) {
|
|
|
|
await conn.write(new Uint8Array([1, 2, 3]));
|
|
|
|
conn.close();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
runAsyncIterator();
|
2020-01-18 12:35:12 -05:00
|
|
|
const conn = await Deno.connect("127.0.0.1:4500");
|
2019-05-01 16:58:09 -04:00
|
|
|
const buf = new Uint8Array(1024);
|
|
|
|
const readResult = await conn.read(buf);
|
2019-07-06 10:16:03 -04:00
|
|
|
assertEquals(3, readResult);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(1, buf[0]);
|
|
|
|
assertEquals(2, buf[1]);
|
|
|
|
assertEquals(3, buf[2]);
|
2019-01-13 19:54:30 -05:00
|
|
|
assert(conn.rid > 0);
|
2018-10-03 23:58:29 -04:00
|
|
|
|
2019-07-06 10:16:03 -04:00
|
|
|
assert(readResult !== Deno.EOF);
|
2018-10-03 23:58:29 -04:00
|
|
|
|
|
|
|
const readResult2 = await conn.read(buf);
|
2019-07-06 10:16:03 -04:00
|
|
|
assertEquals(Deno.EOF, readResult2);
|
2018-10-03 23:58:29 -04:00
|
|
|
|
|
|
|
listener.close();
|
|
|
|
conn.close();
|
|
|
|
});
|
2019-05-02 17:08:02 -04:00
|
|
|
*/
|
2018-10-05 12:16:24 -04:00
|
|
|
|
2019-04-28 17:15:15 -04:00
|
|
|
/* TODO Fix broken test.
|
|
|
|
testPerm({ net: true }, async function netCloseReadSuccess() {
|
2018-10-05 12:16:24 -04:00
|
|
|
const addr = "127.0.0.1:4500";
|
2019-09-20 18:32:18 -04:00
|
|
|
const listener = Deno.listen(addr);
|
2018-10-05 12:16:24 -04:00
|
|
|
const closeDeferred = deferred();
|
2018-10-05 20:09:46 -04:00
|
|
|
const closeReadDeferred = deferred();
|
2019-04-28 17:15:15 -04:00
|
|
|
listener.accept().then(async conn => {
|
|
|
|
await closeReadDeferred.promise;
|
|
|
|
await conn.write(new Uint8Array([1, 2, 3]));
|
|
|
|
const buf = new Uint8Array(1024);
|
|
|
|
const readResult = await conn.read(buf);
|
2019-07-06 10:16:03 -04:00
|
|
|
assertEquals(3, readResult);
|
2019-04-28 17:15:15 -04:00
|
|
|
assertEquals(4, buf[0]);
|
|
|
|
assertEquals(5, buf[1]);
|
|
|
|
assertEquals(6, buf[2]);
|
|
|
|
conn.close();
|
|
|
|
closeDeferred.resolve();
|
|
|
|
});
|
2020-01-18 12:35:12 -05:00
|
|
|
const conn = await Deno.connect(addr);
|
2018-10-05 12:16:24 -04:00
|
|
|
conn.closeRead(); // closing read
|
2018-10-05 20:09:46 -04:00
|
|
|
closeReadDeferred.resolve();
|
2018-10-05 12:16:24 -04:00
|
|
|
const buf = new Uint8Array(1024);
|
|
|
|
const readResult = await conn.read(buf);
|
2019-07-06 10:16:03 -04:00
|
|
|
assertEquals(Deno.EOF, readResult); // with immediate EOF
|
2018-10-05 12:16:24 -04:00
|
|
|
// Ensure closeRead does not impact write
|
|
|
|
await conn.write(new Uint8Array([4, 5, 6]));
|
|
|
|
await closeDeferred.promise;
|
|
|
|
listener.close();
|
|
|
|
conn.close();
|
|
|
|
});
|
2019-04-28 17:15:15 -04:00
|
|
|
*/
|
2018-10-05 12:16:24 -04:00
|
|
|
|
2019-04-28 17:15:15 -04:00
|
|
|
/* TODO Fix broken test.
|
|
|
|
testPerm({ net: true }, async function netDoubleCloseRead() {
|
2018-10-05 12:16:24 -04:00
|
|
|
const addr = "127.0.0.1:4500";
|
2019-09-20 18:32:18 -04:00
|
|
|
const listener = Deno.listen(addr);
|
2018-10-05 12:16:24 -04:00
|
|
|
const closeDeferred = deferred();
|
2019-04-28 17:15:15 -04:00
|
|
|
listener.accept().then(async conn => {
|
|
|
|
await conn.write(new Uint8Array([1, 2, 3]));
|
|
|
|
await closeDeferred.promise;
|
|
|
|
conn.close();
|
|
|
|
});
|
2020-01-18 12:35:12 -05:00
|
|
|
const conn = await Deno.connect(addr);
|
2018-10-05 12:16:24 -04:00
|
|
|
conn.closeRead(); // closing read
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
// Duplicated close should throw error
|
|
|
|
conn.closeRead();
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
assert(!!err);
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.NotConnected);
|
2018-10-05 12:16:24 -04:00
|
|
|
closeDeferred.resolve();
|
|
|
|
listener.close();
|
|
|
|
conn.close();
|
|
|
|
});
|
2019-04-28 17:15:15 -04:00
|
|
|
*/
|
2018-10-05 12:16:24 -04:00
|
|
|
|
2019-04-28 17:15:15 -04:00
|
|
|
/* TODO Fix broken test.
|
|
|
|
testPerm({ net: true }, async function netCloseWriteSuccess() {
|
2018-10-05 12:16:24 -04:00
|
|
|
const addr = "127.0.0.1:4500";
|
2019-09-20 18:32:18 -04:00
|
|
|
const listener = Deno.listen(addr);
|
2018-10-05 12:16:24 -04:00
|
|
|
const closeDeferred = deferred();
|
2019-04-28 17:15:15 -04:00
|
|
|
listener.accept().then(async conn => {
|
|
|
|
await conn.write(new Uint8Array([1, 2, 3]));
|
|
|
|
await closeDeferred.promise;
|
|
|
|
conn.close();
|
|
|
|
});
|
2020-01-18 12:35:12 -05:00
|
|
|
const conn = await Deno.connect(addr);
|
2018-10-05 12:16:24 -04:00
|
|
|
conn.closeWrite(); // closing write
|
|
|
|
const buf = new Uint8Array(1024);
|
|
|
|
// Check read not impacted
|
|
|
|
const readResult = await conn.read(buf);
|
2019-07-06 10:16:03 -04:00
|
|
|
assertEquals(3, readResult);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(1, buf[0]);
|
|
|
|
assertEquals(2, buf[1]);
|
|
|
|
assertEquals(3, buf[2]);
|
2018-10-05 12:16:24 -04:00
|
|
|
// Check write should be closed
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
await conn.write(new Uint8Array([1, 2, 3]));
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
assert(!!err);
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.BrokenPipe);
|
2018-10-05 12:16:24 -04:00
|
|
|
closeDeferred.resolve();
|
|
|
|
listener.close();
|
|
|
|
conn.close();
|
|
|
|
});
|
2019-04-28 17:15:15 -04:00
|
|
|
*/
|
2018-10-05 12:16:24 -04:00
|
|
|
|
2019-04-28 17:15:15 -04:00
|
|
|
/* TODO Fix broken test.
|
|
|
|
testPerm({ net: true }, async function netDoubleCloseWrite() {
|
2018-10-05 12:16:24 -04:00
|
|
|
const addr = "127.0.0.1:4500";
|
2019-09-20 18:32:18 -04:00
|
|
|
const listener = Deno.listen(addr);
|
2018-10-05 12:16:24 -04:00
|
|
|
const closeDeferred = deferred();
|
2019-04-28 17:15:15 -04:00
|
|
|
listener.accept().then(async conn => {
|
|
|
|
await closeDeferred.promise;
|
|
|
|
conn.close();
|
|
|
|
});
|
2020-01-18 12:35:12 -05:00
|
|
|
const conn = await Deno.connect(addr);
|
2018-10-05 12:16:24 -04:00
|
|
|
conn.closeWrite(); // closing write
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
// Duplicated close should throw error
|
|
|
|
conn.closeWrite();
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
assert(!!err);
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.NotConnected);
|
2018-10-05 12:16:24 -04:00
|
|
|
closeDeferred.resolve();
|
|
|
|
listener.close();
|
|
|
|
conn.close();
|
|
|
|
});
|
2019-04-28 17:15:15 -04:00
|
|
|
*/
|