2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-03-10 11:38:02 -04:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
2020-09-27 06:22:32 -04:00
|
|
|
assertNotEquals,
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows,
|
|
|
|
assertThrowsAsync,
|
2020-03-28 13:03:49 -04:00
|
|
|
createResolvable,
|
2020-09-27 06:22:32 -04:00
|
|
|
unitTest,
|
2020-03-10 11:38:02 -04:00
|
|
|
} from "./test_util.ts";
|
2018-10-03 23:58:29 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { net: true } }, function netTcpListenClose(): void {
|
2020-05-11 14:49:19 -04:00
|
|
|
const listener = Deno.listen({ hostname: "127.0.0.1", port: 3500 });
|
2020-03-23 18:02:51 -04:00
|
|
|
assert(listener.addr.transport === "tcp");
|
2020-01-18 15:49:55 -05:00
|
|
|
assertEquals(listener.addr.hostname, "127.0.0.1");
|
2020-05-11 14:49:19 -04:00
|
|
|
assertEquals(listener.addr.port, 3500);
|
2020-06-08 12:24:51 -04:00
|
|
|
assertNotEquals(listener.rid, 0);
|
2018-10-03 23:58:29 -04:00
|
|
|
listener.close();
|
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{
|
|
|
|
perms: { net: true },
|
|
|
|
// TODO:
|
2020-04-28 12:35:23 -04:00
|
|
|
ignore: Deno.build.os === "windows",
|
2020-03-04 11:31:14 -05:00
|
|
|
},
|
|
|
|
function netUdpListenClose(): void {
|
2020-04-28 15:46:39 -04:00
|
|
|
const socket = Deno.listenDatagram({
|
2020-03-04 11:31:14 -05:00
|
|
|
hostname: "127.0.0.1",
|
2020-05-11 14:49:19 -04:00
|
|
|
port: 3500,
|
2020-03-28 13:03:49 -04:00
|
|
|
transport: "udp",
|
2020-03-04 11:31:14 -05:00
|
|
|
});
|
2020-03-23 18:02:51 -04:00
|
|
|
assert(socket.addr.transport === "udp");
|
2020-03-04 11:31:14 -05:00
|
|
|
assertEquals(socket.addr.hostname, "127.0.0.1");
|
2020-05-11 14:49:19 -04:00
|
|
|
assertEquals(socket.addr.port, 3500);
|
2020-03-04 11:31:14 -05:00
|
|
|
socket.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-01-15 20:36:51 -05:00
|
|
|
|
2020-03-23 18:02:51 -04:00
|
|
|
unitTest(
|
2020-04-28 12:35:23 -04:00
|
|
|
{ ignore: Deno.build.os === "windows", perms: { read: true, write: true } },
|
2020-03-23 18:02:51 -04:00
|
|
|
function netUnixListenClose(): void {
|
|
|
|
const filePath = Deno.makeTempFileSync();
|
|
|
|
const socket = Deno.listen({
|
2020-04-28 12:37:59 -04:00
|
|
|
path: filePath,
|
2020-03-28 13:03:49 -04:00
|
|
|
transport: "unix",
|
2020-03-23 18:02:51 -04:00
|
|
|
});
|
|
|
|
assert(socket.addr.transport === "unix");
|
2020-04-28 12:37:59 -04:00
|
|
|
assertEquals(socket.addr.path, filePath);
|
2020-03-23 18:02:51 -04:00
|
|
|
socket.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-23 18:02:51 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
2020-04-28 12:35:23 -04:00
|
|
|
{ ignore: Deno.build.os === "windows", perms: { read: true, write: true } },
|
2020-03-23 18:02:51 -04:00
|
|
|
function netUnixPacketListenClose(): void {
|
|
|
|
const filePath = Deno.makeTempFileSync();
|
2020-04-28 15:46:39 -04:00
|
|
|
const socket = Deno.listenDatagram({
|
2020-04-28 12:37:59 -04:00
|
|
|
path: filePath,
|
2020-03-28 13:03:49 -04:00
|
|
|
transport: "unixpacket",
|
2020-03-23 18:02:51 -04:00
|
|
|
});
|
|
|
|
assert(socket.addr.transport === "unixpacket");
|
2020-04-28 12:37:59 -04:00
|
|
|
assertEquals(socket.addr.path, filePath);
|
2020-03-23 18:02:51 -04:00
|
|
|
socket.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-23 18:02:51 -04:00
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{
|
2020-03-28 13:03:49 -04:00
|
|
|
perms: { net: true },
|
2020-03-04 11:31:14 -05:00
|
|
|
},
|
|
|
|
async function netTcpCloseWhileAccept(): Promise<void> {
|
2020-03-24 12:24:58 -04:00
|
|
|
const listener = Deno.listen({ port: 4501 });
|
2020-03-04 11:31:14 -05:00
|
|
|
const p = listener.accept();
|
|
|
|
listener.close();
|
2020-06-24 18:57:08 -04:00
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await p;
|
|
|
|
},
|
|
|
|
Deno.errors.BadResource,
|
2020-07-14 15:24:17 -04:00
|
|
|
"Listener has been closed",
|
2020-06-24 18:57:08 -04:00
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
|
|
|
|
2020-03-23 18:02:51 -04:00
|
|
|
unitTest(
|
2020-04-28 12:35:23 -04:00
|
|
|
{ ignore: Deno.build.os === "windows", perms: { read: true, write: true } },
|
2020-03-23 18:02:51 -04:00
|
|
|
async function netUnixCloseWhileAccept(): Promise<void> {
|
|
|
|
const filePath = await Deno.makeTempFile();
|
|
|
|
const listener = Deno.listen({
|
2020-04-28 12:37:59 -04:00
|
|
|
path: filePath,
|
2020-03-28 13:03:49 -04:00
|
|
|
transport: "unix",
|
2020-03-23 18:02:51 -04:00
|
|
|
});
|
|
|
|
const p = listener.accept();
|
|
|
|
listener.close();
|
2020-06-24 18:57:08 -04:00
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await p;
|
|
|
|
},
|
|
|
|
Deno.errors.BadResource,
|
2020-07-14 15:24:17 -04:00
|
|
|
"Listener has been closed",
|
2020-06-24 18:57:08 -04:00
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-23 18:02:51 -04:00
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function netTcpConcurrentAccept(): Promise<void> {
|
2020-03-24 12:24:58 -04:00
|
|
|
const listener = Deno.listen({ port: 4502 });
|
2020-03-04 11:31:14 -05:00
|
|
|
let acceptErrCount = 0;
|
|
|
|
const checkErr = (e: Error): void => {
|
|
|
|
if (e.message === "Listener has been closed") {
|
|
|
|
assertEquals(acceptErrCount, 1);
|
|
|
|
} 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]);
|
|
|
|
listener.close();
|
|
|
|
await Promise.all([p, p1]);
|
|
|
|
assertEquals(acceptErrCount, 1);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-01-15 20:36:51 -05:00
|
|
|
|
2020-03-23 18:02:51 -04:00
|
|
|
// TODO(jsouto): Enable when tokio updates mio to v0.7!
|
|
|
|
unitTest(
|
|
|
|
{ ignore: true, perms: { read: true, write: true } },
|
|
|
|
async function netUnixConcurrentAccept(): Promise<void> {
|
|
|
|
const filePath = await Deno.makeTempFile();
|
2020-04-28 12:37:59 -04:00
|
|
|
const listener = Deno.listen({ transport: "unix", path: filePath });
|
2020-03-23 18:02:51 -04:00
|
|
|
let acceptErrCount = 0;
|
|
|
|
const checkErr = (e: Error): void => {
|
|
|
|
if (e.message === "Listener has been closed") {
|
|
|
|
assertEquals(acceptErrCount, 1);
|
|
|
|
} 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]);
|
|
|
|
listener.close();
|
|
|
|
await [p, p1];
|
|
|
|
assertEquals(acceptErrCount, 1);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-23 18:02:51 -04:00
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { net: true } }, async function netTcpDialListen(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2020-05-11 14:49:19 -04:00
|
|
|
const listener = Deno.listen({ port: 3500 });
|
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-03-23 18:02:51 -04:00
|
|
|
assert(conn.localAddr.transport === "tcp");
|
2020-01-18 15:49:55 -05:00
|
|
|
assertEquals(conn.localAddr.hostname, "127.0.0.1");
|
2020-05-11 14:49:19 -04:00
|
|
|
assertEquals(conn.localAddr.port, 3500);
|
2019-04-21 16:40:10 -04:00
|
|
|
await conn.write(new Uint8Array([1, 2, 3]));
|
|
|
|
conn.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-04-21 16:40:10 -04:00
|
|
|
);
|
2020-03-24 12:24:58 -04:00
|
|
|
|
2020-05-11 14:49:19 -04:00
|
|
|
const conn = await Deno.connect({ hostname: "127.0.0.1", port: 3500 });
|
2020-03-23 18:02:51 -04:00
|
|
|
assert(conn.remoteAddr.transport === "tcp");
|
2020-01-18 15:49:55 -05:00
|
|
|
assertEquals(conn.remoteAddr.hostname, "127.0.0.1");
|
2020-05-11 14:49:19 -04:00
|
|
|
assertEquals(conn.remoteAddr.port, 3500);
|
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);
|
|
|
|
|
2020-04-28 12:40:43 -04:00
|
|
|
assert(readResult !== null);
|
2019-05-01 16:58:09 -04:00
|
|
|
|
|
|
|
const readResult2 = await conn.read(buf);
|
2020-04-28 12:40:43 -04:00
|
|
|
assertEquals(readResult2, null);
|
2019-05-01 16:58:09 -04:00
|
|
|
|
|
|
|
listener.close();
|
|
|
|
conn.close();
|
|
|
|
});
|
|
|
|
|
2020-03-23 18:02:51 -04:00
|
|
|
unitTest(
|
2020-04-28 12:35:23 -04:00
|
|
|
{ ignore: Deno.build.os === "windows", perms: { read: true, write: true } },
|
2020-03-23 18:02:51 -04:00
|
|
|
async function netUnixDialListen(): Promise<void> {
|
|
|
|
const filePath = await Deno.makeTempFile();
|
2020-04-28 12:37:59 -04:00
|
|
|
const listener = Deno.listen({ path: filePath, transport: "unix" });
|
2020-03-23 18:02:51 -04:00
|
|
|
listener.accept().then(
|
|
|
|
async (conn): Promise<void> => {
|
|
|
|
assert(conn.remoteAddr != null);
|
|
|
|
assert(conn.localAddr.transport === "unix");
|
2020-04-28 12:37:59 -04:00
|
|
|
assertEquals(conn.localAddr.path, filePath);
|
2020-03-23 18:02:51 -04:00
|
|
|
await conn.write(new Uint8Array([1, 2, 3]));
|
|
|
|
conn.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-23 18:02:51 -04:00
|
|
|
);
|
2020-04-28 12:37:59 -04:00
|
|
|
const conn = await Deno.connect({ path: filePath, transport: "unix" });
|
2020-03-23 18:02:51 -04:00
|
|
|
assert(conn.remoteAddr.transport === "unix");
|
2020-04-28 12:37:59 -04:00
|
|
|
assertEquals(conn.remoteAddr.path, filePath);
|
2020-03-23 18:02:51 -04:00
|
|
|
assert(conn.remoteAddr != null);
|
|
|
|
const buf = new Uint8Array(1024);
|
|
|
|
const readResult = await conn.read(buf);
|
|
|
|
assertEquals(3, readResult);
|
|
|
|
assertEquals(1, buf[0]);
|
|
|
|
assertEquals(2, buf[1]);
|
|
|
|
assertEquals(3, buf[2]);
|
|
|
|
assert(conn.rid > 0);
|
|
|
|
|
2020-04-28 12:40:43 -04:00
|
|
|
assert(readResult !== null);
|
2020-03-23 18:02:51 -04:00
|
|
|
|
|
|
|
const readResult2 = await conn.read(buf);
|
2020-04-28 12:40:43 -04:00
|
|
|
assertEquals(readResult2, null);
|
2020-03-23 18:02:51 -04:00
|
|
|
|
|
|
|
listener.close();
|
|
|
|
conn.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-23 18:02:51 -04:00
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
2020-04-28 12:35:23 -04:00
|
|
|
{ ignore: Deno.build.os === "windows", perms: { net: true } },
|
2020-03-04 11:31:14 -05:00
|
|
|
async function netUdpSendReceive(): Promise<void> {
|
2020-05-11 14:49:19 -04:00
|
|
|
const alice = Deno.listenDatagram({ port: 3500, transport: "udp" });
|
2020-03-23 18:02:51 -04:00
|
|
|
assert(alice.addr.transport === "udp");
|
2020-05-11 14:49:19 -04:00
|
|
|
assertEquals(alice.addr.port, 3500);
|
2020-03-23 18:02:51 -04:00
|
|
|
assertEquals(alice.addr.hostname, "127.0.0.1");
|
2020-02-21 11:26:54 -05:00
|
|
|
|
2020-04-28 15:46:39 -04:00
|
|
|
const bob = Deno.listenDatagram({ port: 4501, transport: "udp" });
|
2020-03-23 18:02:51 -04:00
|
|
|
assert(bob.addr.transport === "udp");
|
2020-03-24 12:24:58 -04:00
|
|
|
assertEquals(bob.addr.port, 4501);
|
2020-03-23 18:02:51 -04:00
|
|
|
assertEquals(bob.addr.hostname, "127.0.0.1");
|
2020-02-21 11:26:54 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
const sent = new Uint8Array([1, 2, 3]);
|
2020-06-13 10:14:31 -04:00
|
|
|
const byteLength = await alice.send(sent, bob.addr);
|
|
|
|
|
|
|
|
assertEquals(byteLength, 3);
|
2020-02-21 11:26:54 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
const [recvd, remote] = await bob.receive();
|
2020-03-23 18:02:51 -04:00
|
|
|
assert(remote.transport === "udp");
|
2020-05-11 14:49:19 -04:00
|
|
|
assertEquals(remote.port, 3500);
|
2020-03-04 11:31:14 -05:00
|
|
|
assertEquals(recvd.length, 3);
|
|
|
|
assertEquals(1, recvd[0]);
|
|
|
|
assertEquals(2, recvd[1]);
|
|
|
|
assertEquals(3, recvd[2]);
|
|
|
|
alice.close();
|
|
|
|
bob.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-11-09 14:40:22 -05:00
|
|
|
|
2020-06-10 11:26:28 -04:00
|
|
|
unitTest(
|
|
|
|
{ ignore: Deno.build.os === "windows", perms: { net: true } },
|
|
|
|
async function netUdpBorrowMutError(): Promise<void> {
|
|
|
|
const socket = Deno.listenDatagram({
|
|
|
|
port: 4501,
|
|
|
|
transport: "udp",
|
|
|
|
});
|
|
|
|
// Panic happened on second send: BorrowMutError
|
|
|
|
const a = socket.send(new Uint8Array(), socket.addr);
|
|
|
|
const b = socket.send(new Uint8Array(), socket.addr);
|
|
|
|
await Promise.all([a, b]);
|
|
|
|
socket.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-10 11:26:28 -04:00
|
|
|
);
|
|
|
|
|
2020-03-23 18:02:51 -04:00
|
|
|
unitTest(
|
2020-04-28 12:35:23 -04:00
|
|
|
{ ignore: Deno.build.os === "windows", perms: { read: true, write: true } },
|
2020-03-23 18:02:51 -04:00
|
|
|
async function netUnixPacketSendReceive(): Promise<void> {
|
|
|
|
const filePath = await Deno.makeTempFile();
|
2020-04-28 15:46:39 -04:00
|
|
|
const alice = Deno.listenDatagram({
|
|
|
|
path: filePath,
|
|
|
|
transport: "unixpacket",
|
|
|
|
});
|
2020-03-23 18:02:51 -04:00
|
|
|
assert(alice.addr.transport === "unixpacket");
|
2020-04-28 12:37:59 -04:00
|
|
|
assertEquals(alice.addr.path, filePath);
|
2020-03-23 18:02:51 -04:00
|
|
|
|
2020-04-28 15:46:39 -04:00
|
|
|
const bob = Deno.listenDatagram({
|
|
|
|
path: filePath,
|
|
|
|
transport: "unixpacket",
|
|
|
|
});
|
2020-03-23 18:02:51 -04:00
|
|
|
assert(bob.addr.transport === "unixpacket");
|
2020-04-28 12:37:59 -04:00
|
|
|
assertEquals(bob.addr.path, filePath);
|
2020-03-23 18:02:51 -04:00
|
|
|
|
|
|
|
const sent = new Uint8Array([1, 2, 3]);
|
2020-06-15 12:20:45 -04:00
|
|
|
const byteLength = await alice.send(sent, bob.addr);
|
|
|
|
assertEquals(byteLength, 3);
|
2020-03-23 18:02:51 -04:00
|
|
|
|
|
|
|
const [recvd, remote] = await bob.receive();
|
|
|
|
assert(remote.transport === "unixpacket");
|
2020-04-28 12:37:59 -04:00
|
|
|
assertEquals(remote.path, filePath);
|
2020-03-23 18:02:51 -04:00
|
|
|
assertEquals(recvd.length, 3);
|
|
|
|
assertEquals(1, recvd[0]);
|
|
|
|
assertEquals(2, recvd[1]);
|
|
|
|
assertEquals(3, recvd[2]);
|
|
|
|
alice.close();
|
|
|
|
bob.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-23 18:02:51 -04:00
|
|
|
);
|
|
|
|
|
2020-04-28 19:08:02 -04:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function netTcpListenIteratorBreakClosesResource(): Promise<void> {
|
|
|
|
const promise = createResolvable();
|
|
|
|
|
|
|
|
async function iterate(listener: Deno.Listener): Promise<void> {
|
|
|
|
let i = 0;
|
|
|
|
|
|
|
|
for await (const conn of listener) {
|
|
|
|
conn.close();
|
|
|
|
i++;
|
|
|
|
|
|
|
|
if (i > 1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
const addr = { hostname: "127.0.0.1", port: 8888 };
|
|
|
|
const listener = Deno.listen(addr);
|
|
|
|
iterate(listener);
|
|
|
|
|
|
|
|
await new Promise((resolve: () => void, _) => {
|
|
|
|
setTimeout(resolve, 100);
|
|
|
|
});
|
|
|
|
const conn1 = await Deno.connect(addr);
|
|
|
|
conn1.close();
|
|
|
|
const conn2 = await Deno.connect(addr);
|
|
|
|
conn2.close();
|
|
|
|
|
|
|
|
await promise;
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-04-28 19:08:02 -04:00
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
2020-02-21 11:26:54 -05:00
|
|
|
async function netTcpListenCloseWhileIterating(): Promise<void> {
|
2020-03-24 12:24:58 -04:00
|
|
|
const listener = Deno.listen({ port: 8000 });
|
2020-02-21 11:26:54 -05:00
|
|
|
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 });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-02-21 11:26:54 -05:00
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
2020-04-28 12:35:23 -04:00
|
|
|
{ ignore: Deno.build.os === "windows", perms: { net: true } },
|
2020-02-21 11:26:54 -05:00
|
|
|
async function netUdpListenCloseWhileIterating(): Promise<void> {
|
2020-04-28 15:46:39 -04:00
|
|
|
const socket = Deno.listenDatagram({ port: 8000, transport: "udp" });
|
2020-02-21 11:26:54 -05:00
|
|
|
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 });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-02-21 11:26:54 -05:00
|
|
|
);
|
|
|
|
|
2020-03-23 18:02:51 -04:00
|
|
|
unitTest(
|
2020-04-28 12:35:23 -04:00
|
|
|
{ ignore: Deno.build.os === "windows", perms: { read: true, write: true } },
|
2020-03-23 18:02:51 -04:00
|
|
|
async function netUnixListenCloseWhileIterating(): Promise<void> {
|
|
|
|
const filePath = Deno.makeTempFileSync();
|
2020-04-28 12:37:59 -04:00
|
|
|
const socket = Deno.listen({ path: filePath, transport: "unix" });
|
2020-03-23 18:02:51 -04:00
|
|
|
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 });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-23 18:02:51 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
2020-04-28 12:35:23 -04:00
|
|
|
{ ignore: Deno.build.os === "windows", perms: { read: true, write: true } },
|
2020-03-23 18:02:51 -04:00
|
|
|
async function netUnixPacketListenCloseWhileIterating(): Promise<void> {
|
|
|
|
const filePath = Deno.makeTempFileSync();
|
2020-04-28 15:46:39 -04:00
|
|
|
const socket = Deno.listenDatagram({
|
|
|
|
path: filePath,
|
|
|
|
transport: "unixpacket",
|
|
|
|
});
|
2020-03-23 18:02:51 -04:00
|
|
|
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 });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-23 18:02:51 -04:00
|
|
|
);
|
|
|
|
|
2020-03-10 11:38:02 -04:00
|
|
|
unitTest(
|
|
|
|
{
|
|
|
|
// FIXME(bartlomieju)
|
2020-03-19 05:58:12 -04:00
|
|
|
ignore: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
perms: { net: true },
|
2020-03-10 11:38:02 -04:00
|
|
|
},
|
|
|
|
async function netListenAsyncIterator(): Promise<void> {
|
2020-05-11 14:49:19 -04:00
|
|
|
const addr = { hostname: "127.0.0.1", port: 3500 };
|
2020-03-10 11:38:02 -04:00
|
|
|
const listener = Deno.listen(addr);
|
|
|
|
const runAsyncIterator = async (): Promise<void> => {
|
|
|
|
for await (const conn of listener) {
|
|
|
|
await conn.write(new Uint8Array([1, 2, 3]));
|
|
|
|
conn.close();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
runAsyncIterator();
|
|
|
|
const conn = await Deno.connect(addr);
|
|
|
|
const buf = new Uint8Array(1024);
|
|
|
|
const readResult = await conn.read(buf);
|
|
|
|
assertEquals(3, readResult);
|
|
|
|
assertEquals(1, buf[0]);
|
|
|
|
assertEquals(2, buf[1]);
|
|
|
|
assertEquals(3, buf[2]);
|
|
|
|
assert(conn.rid > 0);
|
2018-10-03 23:58:29 -04:00
|
|
|
|
2020-04-28 12:40:43 -04:00
|
|
|
assert(readResult !== null);
|
2018-10-03 23:58:29 -04:00
|
|
|
|
2020-03-10 11:38:02 -04:00
|
|
|
const readResult2 = await conn.read(buf);
|
2020-04-28 12:40:43 -04:00
|
|
|
assertEquals(readResult2, null);
|
2018-10-03 23:58:29 -04:00
|
|
|
|
2020-03-10 11:38:02 -04:00
|
|
|
listener.close();
|
|
|
|
conn.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-10 11:38:02 -04:00
|
|
|
);
|
2018-10-05 12:16:24 -04:00
|
|
|
|
2020-03-10 11:38:02 -04:00
|
|
|
unitTest(
|
|
|
|
{
|
|
|
|
// FIXME(bartlomieju)
|
2020-03-19 05:58:12 -04:00
|
|
|
ignore: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
perms: { net: true },
|
2020-03-10 11:38:02 -04:00
|
|
|
},
|
|
|
|
async function netCloseWriteSuccess() {
|
2020-05-11 14:49:19 -04:00
|
|
|
const addr = { hostname: "127.0.0.1", port: 3500 };
|
2020-03-10 11:38:02 -04:00
|
|
|
const listener = Deno.listen(addr);
|
|
|
|
const closeDeferred = createResolvable();
|
2020-03-28 13:03:49 -04:00
|
|
|
listener.accept().then(async (conn) => {
|
2020-03-10 11:38:02 -04:00
|
|
|
await conn.write(new Uint8Array([1, 2, 3]));
|
|
|
|
await closeDeferred;
|
|
|
|
conn.close();
|
|
|
|
});
|
|
|
|
const conn = await Deno.connect(addr);
|
|
|
|
conn.closeWrite(); // closing write
|
|
|
|
const buf = new Uint8Array(1024);
|
|
|
|
// Check read not impacted
|
|
|
|
const readResult = await conn.read(buf);
|
|
|
|
assertEquals(3, readResult);
|
|
|
|
assertEquals(1, buf[0]);
|
|
|
|
assertEquals(2, buf[1]);
|
|
|
|
assertEquals(3, buf[2]);
|
|
|
|
// Check write should be closed
|
2020-06-24 18:57:08 -04:00
|
|
|
await assertThrowsAsync(async () => {
|
2020-03-10 11:38:02 -04:00
|
|
|
await conn.write(new Uint8Array([1, 2, 3]));
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.BrokenPipe);
|
2020-03-10 11:38:02 -04:00
|
|
|
closeDeferred.resolve();
|
|
|
|
listener.close();
|
2019-04-28 17:15:15 -04:00
|
|
|
conn.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-10 11:38:02 -04:00
|
|
|
);
|
2018-10-05 12:16:24 -04:00
|
|
|
|
2020-03-10 11:38:02 -04:00
|
|
|
unitTest(
|
|
|
|
{
|
|
|
|
// FIXME(bartlomieju)
|
2020-03-19 05:58:12 -04:00
|
|
|
ignore: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
perms: { net: true },
|
2020-03-10 11:38:02 -04:00
|
|
|
},
|
|
|
|
async function netDoubleCloseWrite() {
|
2020-05-11 14:49:19 -04:00
|
|
|
const addr = { hostname: "127.0.0.1", port: 3500 };
|
2020-03-10 11:38:02 -04:00
|
|
|
const listener = Deno.listen(addr);
|
|
|
|
const closeDeferred = createResolvable();
|
2020-03-28 13:03:49 -04:00
|
|
|
listener.accept().then(async (conn) => {
|
2020-03-10 11:38:02 -04:00
|
|
|
await closeDeferred;
|
|
|
|
conn.close();
|
|
|
|
});
|
|
|
|
const conn = await Deno.connect(addr);
|
|
|
|
conn.closeWrite(); // closing write
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2020-03-10 11:38:02 -04:00
|
|
|
// Duplicated close should throw error
|
|
|
|
conn.closeWrite();
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.NotConnected);
|
2020-03-10 11:38:02 -04:00
|
|
|
closeDeferred.resolve();
|
|
|
|
listener.close();
|
2019-04-28 17:15:15 -04:00
|
|
|
conn.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-10 11:38:02 -04:00
|
|
|
);
|
2020-03-11 18:19:24 -04:00
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{
|
2020-03-28 13:03:49 -04:00
|
|
|
perms: { net: true },
|
2020-03-11 18:19:24 -04:00
|
|
|
},
|
|
|
|
async function netHangsOnClose() {
|
|
|
|
let acceptedConn: Deno.Conn;
|
|
|
|
const resolvable = createResolvable();
|
|
|
|
|
|
|
|
async function iteratorReq(listener: Deno.Listener): Promise<void> {
|
|
|
|
const p = new Uint8Array(10);
|
|
|
|
const conn = await listener.accept();
|
|
|
|
acceptedConn = conn;
|
|
|
|
|
|
|
|
try {
|
|
|
|
while (true) {
|
|
|
|
const nread = await conn.read(p);
|
2020-04-28 12:40:43 -04:00
|
|
|
if (nread === null) {
|
2020-03-11 18:19:24 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
await conn.write(new Uint8Array([1, 2, 3]));
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
assert(!!err);
|
|
|
|
assert(err instanceof Deno.errors.BadResource);
|
|
|
|
}
|
|
|
|
|
|
|
|
resolvable.resolve();
|
|
|
|
}
|
2020-03-24 12:24:58 -04:00
|
|
|
|
2020-05-11 14:49:19 -04:00
|
|
|
const addr = { hostname: "127.0.0.1", port: 3500 };
|
2020-03-11 18:19:24 -04:00
|
|
|
const listener = Deno.listen(addr);
|
|
|
|
iteratorReq(listener);
|
|
|
|
const conn = await Deno.connect(addr);
|
|
|
|
await conn.write(new Uint8Array([1, 2, 3, 4]));
|
|
|
|
const buf = new Uint8Array(10);
|
|
|
|
await conn.read(buf);
|
|
|
|
conn!.close();
|
|
|
|
acceptedConn!.close();
|
|
|
|
listener.close();
|
|
|
|
await resolvable;
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-11 18:19:24 -04:00
|
|
|
);
|
2020-09-27 10:44:53 -04:00
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{
|
|
|
|
perms: { net: true },
|
|
|
|
},
|
|
|
|
function netExplicitUndefinedHostname() {
|
|
|
|
const listener = Deno.listen({ hostname: undefined, port: 8080 });
|
|
|
|
assertEquals((listener.addr as Deno.NetAddr).hostname, "0.0.0.0");
|
|
|
|
listener.close();
|
|
|
|
},
|
|
|
|
);
|