2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-10-03 23:58:29 -04:00
|
|
|
import { testPerm, assert, assertEqual } from "./test_util.ts";
|
|
|
|
|
|
|
|
testPerm({ net: true }, function netListenClose() {
|
2019-02-12 10:08:56 -05:00
|
|
|
const listener = Deno.listen("tcp", "127.0.0.1:4500");
|
2018-10-03 23:58:29 -04:00
|
|
|
listener.close();
|
|
|
|
});
|
|
|
|
|
2019-01-15 20:36:51 -05:00
|
|
|
testPerm({ net: true }, async function netCloseWhileAccept() {
|
2019-02-12 10:08:56 -05:00
|
|
|
const listener = Deno.listen("tcp", ":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);
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.Other);
|
2019-01-15 20:36:51 -05:00
|
|
|
assertEqual(err.message, "Listener has been closed");
|
|
|
|
});
|
|
|
|
|
|
|
|
testPerm({ net: true }, async function netConcurrentAccept() {
|
2019-02-12 10:08:56 -05:00
|
|
|
const listener = Deno.listen("tcp", ":4502");
|
2019-01-17 18:10:34 -05:00
|
|
|
let acceptErrCount = 0;
|
|
|
|
const checkErr = e => {
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(e.kind, Deno.ErrorKind.Other);
|
2019-01-17 18:10:34 -05:00
|
|
|
if (e.message === "Listener has been closed") {
|
|
|
|
assertEqual(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]);
|
2019-01-15 20:36:51 -05:00
|
|
|
listener.close();
|
2019-01-17 18:10:34 -05:00
|
|
|
await [p, p1];
|
|
|
|
assertEqual(acceptErrCount, 1);
|
2019-01-15 20:36:51 -05:00
|
|
|
});
|
|
|
|
|
2018-10-03 23:58:29 -04:00
|
|
|
testPerm({ net: true }, async function netDialListen() {
|
2019-02-12 10:08:56 -05:00
|
|
|
const listener = Deno.listen("tcp", ":4500");
|
2018-10-03 23:58:29 -04:00
|
|
|
listener.accept().then(async conn => {
|
|
|
|
await conn.write(new Uint8Array([1, 2, 3]));
|
|
|
|
conn.close();
|
|
|
|
});
|
2019-02-12 10:08:56 -05:00
|
|
|
const conn = await Deno.dial("tcp", "127.0.0.1:4500");
|
2018-10-03 23:58:29 -04:00
|
|
|
const buf = new Uint8Array(1024);
|
|
|
|
const readResult = await conn.read(buf);
|
|
|
|
assertEqual(3, readResult.nread);
|
|
|
|
assertEqual(1, buf[0]);
|
|
|
|
assertEqual(2, buf[1]);
|
|
|
|
assertEqual(3, buf[2]);
|
2019-01-13 19:54:30 -05:00
|
|
|
assert(conn.rid > 0);
|
2018-10-03 23:58:29 -04:00
|
|
|
|
|
|
|
// TODO Currently ReadResult does not properly transmit EOF in the same call.
|
|
|
|
// it requires a second call to get the EOF. Either ReadResult to be an
|
|
|
|
// integer in which 0 signifies EOF or the handler should be modified so that
|
|
|
|
// EOF is properly transmitted.
|
|
|
|
assertEqual(false, readResult.eof);
|
|
|
|
|
|
|
|
const readResult2 = await conn.read(buf);
|
|
|
|
assertEqual(true, readResult2.eof);
|
|
|
|
|
|
|
|
listener.close();
|
|
|
|
conn.close();
|
|
|
|
});
|
2018-10-05 12:16:24 -04:00
|
|
|
|
2018-10-06 15:34:00 -04:00
|
|
|
/* TODO Fix broken test.
|
2018-10-05 12:16:24 -04:00
|
|
|
testPerm({ net: true }, async function netCloseReadSuccess() {
|
|
|
|
const addr = "127.0.0.1:4500";
|
2019-02-12 10:08:56 -05:00
|
|
|
const listener = Deno.listen("tcp", addr);
|
2018-10-05 12:16:24 -04:00
|
|
|
const closeDeferred = deferred();
|
2018-10-05 20:09:46 -04:00
|
|
|
const closeReadDeferred = deferred();
|
2018-10-05 12:16:24 -04:00
|
|
|
listener.accept().then(async conn => {
|
2018-10-05 20:09:46 -04:00
|
|
|
await closeReadDeferred.promise;
|
2018-10-05 12:16:24 -04:00
|
|
|
await conn.write(new Uint8Array([1, 2, 3]));
|
|
|
|
const buf = new Uint8Array(1024);
|
|
|
|
const readResult = await conn.read(buf);
|
|
|
|
assertEqual(3, readResult.nread);
|
|
|
|
assertEqual(4, buf[0]);
|
|
|
|
assertEqual(5, buf[1]);
|
|
|
|
assertEqual(6, buf[2]);
|
|
|
|
conn.close();
|
|
|
|
closeDeferred.resolve();
|
|
|
|
});
|
2019-02-12 10:08:56 -05:00
|
|
|
const conn = await Deno.dial("tcp", 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);
|
|
|
|
assertEqual(0, readResult.nread); // No error, read nothing
|
|
|
|
assertEqual(true, readResult.eof); // with immediate EOF
|
|
|
|
// Ensure closeRead does not impact write
|
|
|
|
await conn.write(new Uint8Array([4, 5, 6]));
|
|
|
|
await closeDeferred.promise;
|
|
|
|
listener.close();
|
|
|
|
conn.close();
|
|
|
|
});
|
2018-10-06 15:34:00 -04:00
|
|
|
*/
|
2018-10-05 12:16:24 -04:00
|
|
|
|
2018-10-06 15:34:00 -04:00
|
|
|
/* TODO Fix broken test.
|
2018-10-05 12:16:24 -04:00
|
|
|
testPerm({ net: true }, async function netDoubleCloseRead() {
|
|
|
|
const addr = "127.0.0.1:4500";
|
2019-02-12 10:08:56 -05:00
|
|
|
const listener = Deno.listen("tcp", addr);
|
2018-10-05 12:16:24 -04:00
|
|
|
const closeDeferred = deferred();
|
|
|
|
listener.accept().then(async conn => {
|
|
|
|
await conn.write(new Uint8Array([1, 2, 3]));
|
|
|
|
await closeDeferred.promise;
|
|
|
|
conn.close();
|
|
|
|
});
|
2019-02-12 10:08:56 -05:00
|
|
|
const conn = await Deno.dial("tcp", 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);
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.NotConnected);
|
2018-10-05 12:16:24 -04:00
|
|
|
assertEqual(err.name, "NotConnected");
|
|
|
|
closeDeferred.resolve();
|
|
|
|
listener.close();
|
|
|
|
conn.close();
|
|
|
|
});
|
2018-10-06 15:34:00 -04:00
|
|
|
*/
|
2018-10-05 12:16:24 -04:00
|
|
|
|
2018-10-06 15:34:00 -04:00
|
|
|
/* TODO Fix broken test.
|
2018-10-05 12:16:24 -04:00
|
|
|
testPerm({ net: true }, async function netCloseWriteSuccess() {
|
|
|
|
const addr = "127.0.0.1:4500";
|
2019-02-12 10:08:56 -05:00
|
|
|
const listener = Deno.listen("tcp", addr);
|
2018-10-05 12:16:24 -04:00
|
|
|
const closeDeferred = deferred();
|
|
|
|
listener.accept().then(async conn => {
|
|
|
|
await conn.write(new Uint8Array([1, 2, 3]));
|
|
|
|
await closeDeferred.promise;
|
|
|
|
conn.close();
|
|
|
|
});
|
2019-02-12 10:08:56 -05:00
|
|
|
const conn = await Deno.dial("tcp", 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);
|
|
|
|
assertEqual(3, readResult.nread);
|
|
|
|
assertEqual(1, buf[0]);
|
|
|
|
assertEqual(2, buf[1]);
|
|
|
|
assertEqual(3, buf[2]);
|
|
|
|
// Check write should be closed
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
await conn.write(new Uint8Array([1, 2, 3]));
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
assert(!!err);
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.BrokenPipe);
|
2018-10-05 12:16:24 -04:00
|
|
|
assertEqual(err.name, "BrokenPipe");
|
|
|
|
closeDeferred.resolve();
|
|
|
|
listener.close();
|
|
|
|
conn.close();
|
|
|
|
});
|
2018-10-06 15:34:00 -04:00
|
|
|
*/
|
2018-10-05 12:16:24 -04:00
|
|
|
|
2018-10-06 15:34:00 -04:00
|
|
|
/* TODO Fix broken test.
|
2018-10-05 12:16:24 -04:00
|
|
|
testPerm({ net: true }, async function netDoubleCloseWrite() {
|
|
|
|
const addr = "127.0.0.1:4500";
|
2019-02-12 10:08:56 -05:00
|
|
|
const listener = Deno.listen("tcp", addr);
|
2018-10-05 12:16:24 -04:00
|
|
|
const closeDeferred = deferred();
|
|
|
|
listener.accept().then(async conn => {
|
|
|
|
await closeDeferred.promise;
|
|
|
|
conn.close();
|
|
|
|
});
|
2019-02-12 10:08:56 -05:00
|
|
|
const conn = await Deno.dial("tcp", 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);
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.NotConnected);
|
2018-10-05 12:16:24 -04:00
|
|
|
assertEqual(err.name, "NotConnected");
|
|
|
|
closeDeferred.resolve();
|
|
|
|
listener.close();
|
|
|
|
conn.close();
|
|
|
|
});
|
2018-10-06 15:34:00 -04:00
|
|
|
*/
|