test: ensure assertions are reached on more tests

PR-URL: https://github.com/nodejs/node/pull/60761
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
Antoine du Hamel committed 2025-11-24 23:47:52 +01:00
1 parent 411ce7ed2e
commit 4a4fdcf390
49 files changed
+276 -285

No files matched your search

+1 -1
View File
@@ -195,7 +195,7 @@ export default [
`test/parallel/test-{${
// 0x61 is code for 'a', this generates a string enumerating latin letters: 'a*,b*,…'
Array.from({ length: 13 }, (_, i) => String.fromCharCode(0x61 + i, 42)).join(',')
},${
},n*,${
// 0x61 is code for 'a', this generates a string enumerating latin letters: 'z*,y*,…'
Array.from({ length: 8 }, (_, i) => String.fromCharCode(0x61 + 25 - i, 42)).join(',')
}}.{js,mjs,cjs}`,
+2 -2
View File
@@ -36,10 +36,10 @@ const { getNavigatorPlatform } = require('internal/navigator');
const { execFile } = require('child_process');
const is = {
number: (value, key) => {
number: common.mustCallAtLeast((value, key) => {
assert(!Number.isNaN(value), `${key} should not be NaN`);
assert.strictEqual(typeof value, 'number');
},
}),
};
is.number(+navigator.hardwareConcurrency, 'hardwareConcurrency');
@@ -10,12 +10,12 @@ const { createConnection, createServer } = require('net');
// Test that IPV4 is NOT reached if IPV6 is not reachable and the option has been disabled via command line
{
const ipv4Server = createServer((socket) => {
const ipv4Server = createServer(common.mustCallAtLeast((socket) => {
socket.on('data', common.mustCall(() => {
socket.write('response-ipv4');
socket.end();
}));
});
}, 0));
ipv4Server.listen(0, '127.0.0.1', common.mustCall(() => {
const port = ipv4Server.address().port;
@@ -10,12 +10,12 @@ const autoSelectFamilyAttemptTimeout = common.defaultAutoSelectFamilyAttemptTime
// Test that IPV4 is reached by default if IPV6 is not reachable and the default is enabled
{
const ipv4Server = createServer((socket) => {
const ipv4Server = createServer(common.mustCall((socket) => {
socket.on('data', common.mustCall(() => {
socket.write('response-ipv4');
socket.end();
}));
});
}));
ipv4Server.listen(0, '127.0.0.1', common.mustCall(() => {
setDefaultAutoSelectFamily(true);
@@ -45,12 +45,12 @@ const autoSelectFamilyAttemptTimeout = common.defaultAutoSelectFamilyAttemptTime
// Test that IPV4 is not reached by default if IPV6 is not reachable and the default is disabled
{
const ipv4Server = createServer((socket) => {
const ipv4Server = createServer(common.mustCallAtLeast((socket) => {
socket.on('data', common.mustCall(() => {
socket.write('response-ipv4');
socket.end();
}));
});
}, 0));
ipv4Server.listen(0, '127.0.0.1', common.mustCall(() => {
setDefaultAutoSelectFamily(false);
@@ -8,12 +8,12 @@ const { createConnection, createServer } = require('net');
// Test that happy eyeballs algorithm is properly implemented when a A record is returned first.
if (common.hasIPv6) {
const ipv4Server = createServer((socket) => {
const ipv4Server = createServer(common.mustCall((socket) => {
socket.on('data', common.mustCall(() => {
socket.write('response-ipv4');
socket.end();
}));
});
}));
const ipv6Server = createServer((socket) => {
socket.on('data', common.mustNotCall(() => {
+8 -8
View File
@@ -16,12 +16,12 @@ const autoSelectFamilyAttemptTimeout = common.defaultAutoSelectFamilyAttemptTime
// Test that IPV4 is reached if IPV6 is not reachable
{
const ipv4Server = createServer((socket) => {
const ipv4Server = createServer(common.mustCall((socket) => {
socket.on('data', common.mustCall(() => {
socket.write('response-ipv4');
socket.end();
}));
});
}));
ipv4Server.listen(0, '127.0.0.1', common.mustCall(() => {
const port = ipv4Server.address().port;
@@ -56,12 +56,12 @@ const autoSelectFamilyAttemptTimeout = common.defaultAutoSelectFamilyAttemptTime
// Test that only the last successful connection is established.
{
const ipv4Server = createServer((socket) => {
const ipv4Server = createServer(common.mustCall((socket) => {
socket.on('data', common.mustCall(() => {
socket.write('response-ipv4');
socket.end();
}));
});
}));
ipv4Server.listen(0, '127.0.0.1', common.mustCall(() => {
const port = ipv4Server.address().port;
@@ -116,12 +116,12 @@ if (common.hasIPv6) {
}));
});
const ipv6Server = createServer((socket) => {
const ipv6Server = createServer(common.mustCall((socket) => {
socket.on('data', common.mustCall(() => {
socket.write('response-ipv6');
socket.end();
}));
});
}));
ipv4Server.listen(0, '127.0.0.1', common.mustCall(() => {
const port = ipv4Server.address().port;
@@ -184,12 +184,12 @@ if (common.hasIPv6) {
// Test that the option can be disabled
{
const ipv4Server = createServer((socket) => {
const ipv4Server = createServer(common.mustCallAtLeast((socket) => {
socket.on('data', common.mustCall(() => {
socket.write('response-ipv4');
socket.end();
}));
});
}, 0));
ipv4Server.listen(0, '127.0.0.1', common.mustCall(() => {
const port = ipv4Server.address().port;
@@ -35,10 +35,10 @@ if (process.argv[2] === 'child') {
}
});
conn.on('error', (err) => {
conn.on('error', common.mustCallAtLeast((err) => {
// Error emitted on Windows.
assert.strictEqual(err.code, 'ECONNRESET');
});
}, 0));
conn.on('connect', common.mustCall(() => {
cp.kill('SIGKILL');
@@ -18,14 +18,14 @@ server.listen(0, common.mustCall(async () => {
});
});
const assertAbort = async (socket, testName) => {
const assertAbort = common.mustCallAtLeast(async (socket, testName) => {
try {
await once(socket, 'close');
assert.fail(`close ${testName} should have thrown`);
} catch (err) {
assert.strictEqual(err.name, 'AbortError');
}
};
});
async function postAbort() {
const ac = new AbortController();
+4 -6
View File
@@ -8,11 +8,6 @@ const truthyValues = [true, 1, 'true', {}, []];
const delays = [[123, 0], [456123, 456], [-123000, 0], [undefined, 0]];
const falseyValues = [false, 0, ''];
const genSetKeepAlive = (desiredEnable, desiredDelay) => (enable, delay) => {
assert.strictEqual(enable, desiredEnable);
assert.strictEqual(delay, desiredDelay);
};
for (const value of truthyValues) {
for (const delay of delays) {
const server = net.createServer();
@@ -26,7 +21,10 @@ for (const value of truthyValues) {
);
client._handle.setKeepAlive = common.mustCall(
genSetKeepAlive(true, delay[1])
(enable, actualDelay) => {
assert.strictEqual(enable, true);
assert.strictEqual(actualDelay, delay[1]);
},
);
client.on('end', common.mustCall(function() {
+2 -2
View File
@@ -50,9 +50,9 @@ const gcListener = { ongc() { collected = true; } };
function done(sock) {
globalThis.gc();
setImmediate(() => {
setImmediate(common.mustCall(() => {
assert.strictEqual(collected, true);
sock.end();
server.close();
});
}));
}
+3 -4
View File
@@ -6,9 +6,6 @@ const net = require('net');
const truthyValues = [true, 1, 'true', {}, []];
const falseyValues = [false, 0, ''];
const genSetNoDelay = (desiredArg) => (enable) => {
assert.strictEqual(enable, desiredArg);
};
for (const value of truthyValues) {
const server = net.createServer();
@@ -21,7 +18,9 @@ for (const value of truthyValues) {
common.mustCall(() => client.end())
);
client._handle.setNoDelay = common.mustCall(genSetNoDelay(true));
client._handle.setNoDelay = common.mustCall((enable) => {
assert.strictEqual(enable, true);
});
client.on('end', common.mustCall(function() {
server.close();
@@ -66,7 +66,7 @@ const net = require('net');
console.log(`'connect' emitted on Client ${index}`);
client.resume();
client.on('end', common.mustCall(function clientOnEnd() {
setTimeout(function closeServer() {
setTimeout(common.mustCall(() => {
// When allowHalfOpen is true, client must still be writable
// after the server closes the connections, but not readable
console.log(`client ${index} received FIN`);
@@ -77,7 +77,7 @@ const net = require('net');
clientSentFIN++;
console.log(
`client ${index} sent FIN, ${clientSentFIN} have been sent`);
}, 50);
}), 50);
}));
client.on('close', common.mustCall(function clientOnClose() {
clientReceivedFIN++;
+6 -7
View File
@@ -30,7 +30,6 @@ function testClients(getSocketOpt, getConnectOpt, getConnectCb) {
}
const CLIENT_VARIANTS = 6; // Same length as array above
const forAllClients = (cb) => common.mustCall(cb, CLIENT_VARIANTS);
// Test Pipe fd is wrapped correctly
{
@@ -42,7 +41,7 @@ const forAllClients = (cb) => common.mustCall(cb, CLIENT_VARIANTS);
let socketCounter = 0;
const handleMap = new Map();
const server = net.createServer()
.on('connection', forAllClients(function serverOnConnection(socket) {
.on('connection', common.mustCallAtLeast(function serverOnConnection(socket) {
let clientFd;
socket.on('data', common.mustCall(function(data) {
clientFd = data.toString();
@@ -59,7 +58,7 @@ const forAllClients = (cb) => common.mustCall(cb, CLIENT_VARIANTS);
}, 10);
}
}, 1));
}))
}), CLIENT_VARIANTS)
.on('close', function() {
setTimeout(() => {
for (const pair of handleMap) {
@@ -73,7 +72,7 @@ const forAllClients = (cb) => common.mustCall(cb, CLIENT_VARIANTS);
assert.fail(`[Pipe server]${err}`);
})
.listen({ path: serverPath }, common.mustCall(function serverOnListen() {
const getSocketOpt = (index) => {
const getSocketOpt = common.mustCallAtLeast((index) => {
const handle = new Pipe(PipeConstants.SOCKET);
const err = handle.bind(`${prefix}-client-${socketCounter++}`);
assert(err >= 0, String(err));
@@ -81,11 +80,11 @@ const forAllClients = (cb) => common.mustCall(cb, CLIENT_VARIANTS);
handleMap.set(index, handle);
console.error(`[Pipe]Bound handle with Pipe ${handle.fd}`);
return { fd: handle.fd, readable: true, writable: true };
};
});
const getConnectOpt = () => ({
path: serverPath
});
const getConnectCb = (index) => common.mustCall(function clientOnConnect() {
const getConnectCb = common.mustCallAtLeast((index) => common.mustCall(function clientOnConnect() {
// Test if it's wrapping an existing fd
assert(handleMap.has(index));
const oldHandle = handleMap.get(index);
@@ -96,7 +95,7 @@ const forAllClients = (cb) => common.mustCall(cb, CLIENT_VARIANTS);
console.error(err);
assert.fail(`[Pipe Client]${err}`);
});
});
}));
testClients(getSocketOpt, getConnectOpt, getConnectCb);
}));
@@ -32,11 +32,11 @@ const net = require('net');
const hostAddrIPv6 = '::1';
const HOSTNAME = 'dummy';
const server = net.createServer({ allowHalfOpen: true }, (socket) => {
const server = net.createServer({ allowHalfOpen: true }, common.mustCall((socket) => {
socket.resume();
socket.on('end', common.mustCall());
socket.end();
});
}));
function tryConnect() {
const connectOpt = {
@@ -51,17 +51,17 @@ function tryConnect() {
})
};
// No `mustCall`, since test could skip, and it's the only path to `close`.
const client = net.connect(connectOpt, () => {
const client = net.connect(connectOpt, common.mustCall(() => {
client.resume();
client.on('end', () => {
client.on('end', common.mustCall(() => {
// Wait for next uv tick and make sure the socket stream is writable.
setTimeout(function() {
setTimeout(common.mustCall(() => {
assert(client.writable);
client.end();
}, 10);
});
}), 10);
}));
client.on('close', () => server.close());
});
}));
}
server.listen(0, hostAddrIPv6, tryConnect);
@@ -20,7 +20,7 @@ const CLIENT_VARIANTS = 12;
socket.end('ok');
}, CLIENT_VARIANTS))
.listen(serverPath, common.mustCall(function() {
const getConnectCb = () => common.mustCall(function() {
const getConnectCb = common.mustCallAtLeast(() => common.mustCall(function() {
this.end();
this.on('close', common.mustCall(function() {
counter++;
@@ -28,7 +28,7 @@ const CLIENT_VARIANTS = 12;
server.close();
}
}));
});
}));
// CLIENT_VARIANTS depends on the following code
net.connect(serverPath, getConnectCb()).resume();
@@ -171,7 +171,7 @@ function syncFailToConnect(port, assertErr, optOnly) {
}
function canConnect(port) {
const noop = () => common.mustCall();
const noop = common.mustCallAtLeast(() => common.mustCall());
const family = 4;
// connect(port, cb) and connect(port)
@@ -202,10 +202,10 @@ function canConnect(port) {
}
function asyncFailToConnect(port) {
const onError = () => common.mustCall((err) => {
const onError = common.mustCallAtLeast(() => common.mustCall((err) => {
const regexp = /^Error: connect E\w+.+$/;
assert.match(String(err), regexp);
});
}));
const dont = () => common.mustNotCall();
const family = 4;
+2 -2
View File
@@ -27,9 +27,9 @@ const net = require('net');
const server1 = net.createServer(common.mustNotCall());
const server2 = net.createServer(common.mustNotCall());
server1.listen(0, common.mustCall(function() {
server2.on('error', function(error) {
server2.on('error', common.mustCall((error) => {
assert.strictEqual(error.message.includes('EADDRINUSE'), true);
server1.close();
});
}));
server2.listen(this.address().port);
}));
+2 -2
View File
@@ -26,7 +26,7 @@ const net = require('net');
let serverConnection;
let clientConnection;
const echoServer = net.createServer(function(connection) {
const echoServer = net.createServer(common.mustCall((connection) => {
serverConnection = connection;
setTimeout(common.mustCall(function() {
// Make sure both connections are still open
@@ -43,7 +43,7 @@ const echoServer = net.createServer(function(connection) {
connection.on('end', function() {
connection.end();
});
});
}));
echoServer.listen(0);
echoServer.on('listening', function() {
@@ -1,6 +1,6 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const cluster = require('cluster');
const net = require('net');
@@ -8,19 +8,19 @@ const net = require('net');
if (cluster.isPrimary) {
const worker1 = cluster.fork();
worker1.on('message', function(port1) {
worker1.on('message', common.mustCall((port1) => {
assert.strictEqual(port1, port1 | 0,
`first worker could not listen on port ${port1}`);
const worker2 = cluster.fork();
worker2.on('message', function(port2) {
worker2.on('message', common.mustCall((port2) => {
assert.strictEqual(port2, port2 | 0,
`second worker could not listen on port ${port2}`);
assert.notStrictEqual(port1, port2);
worker1.kill();
worker2.kill();
});
});
}));
}));
} else {
const server = net.createServer(() => {});
@@ -10,13 +10,13 @@ const net = require('net');
const invalidPort = -1 >>> 0;
net.Server().listen(0, function() {
net.Server().listen(0, common.mustCall(function() {
const address = this.address();
const key = `${address.family.slice(-1)}:${address.address}:0`;
assert.strictEqual(this._connectionKey, key);
this.close();
});
}));
// The first argument is a configuration object
assert.throws(() => {
+3 -3
View File
@@ -20,16 +20,16 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const net = require('net');
const connect = (opts, code, type) => {
const connect = common.mustCall((opts, code, type) => {
assert.throws(
() => net.connect(opts),
{ code, name: type.name }
);
};
}, 2);
connect({
host: 'localhost',
+24 -24
View File
@@ -9,7 +9,7 @@ const message = Buffer.from('hello world');
net.createServer(common.mustCall(function(socket) {
this.close();
socket.end(message);
})).listen(0, function() {
})).listen(0, common.mustCall(function() {
let received = 0;
const buffers = [];
const sockBuf = Buffer.alloc(8);
@@ -17,23 +17,23 @@ net.createServer(common.mustCall(function(socket) {
port: this.address().port,
onread: {
buffer: sockBuf,
callback: function(nread, buf) {
callback: common.mustCallAtLeast(function(nread, buf) {
assert.strictEqual(buf, sockBuf);
received += nread;
buffers.push(Buffer.from(buf.slice(0, nread)));
}
})
}
}).on('data', common.mustNotCall()).on('end', common.mustCall(() => {
assert.strictEqual(received, message.length);
assert.deepStrictEqual(Buffer.concat(buffers), message);
}));
});
}));
// Test Uint8Array support
net.createServer(common.mustCall(function(socket) {
this.close();
socket.end(message);
})).listen(0, function() {
})).listen(0, common.mustCall(function() {
let received = 0;
let incoming = new Uint8Array(0);
const sockBuf = new Uint8Array(8);
@@ -41,26 +41,26 @@ net.createServer(common.mustCall(function(socket) {
port: this.address().port,
onread: {
buffer: sockBuf,
callback: function(nread, buf) {
callback: common.mustCallAtLeast(function(nread, buf) {
assert.strictEqual(buf, sockBuf);
received += nread;
const newIncoming = new Uint8Array(incoming.length + nread);
newIncoming.set(incoming);
newIncoming.set(buf.slice(0, nread), incoming.length);
incoming = newIncoming;
}
})
}
}).on('data', common.mustNotCall()).on('end', common.mustCall(() => {
assert.strictEqual(received, message.length);
assert.deepStrictEqual(incoming, new Uint8Array(message));
}));
});
}));
// Test Buffer callback usage
net.createServer(common.mustCall(function(socket) {
this.close();
socket.end(message);
})).listen(0, function() {
})).listen(0, common.mustCall(function() {
let received = 0;
const incoming = [];
const bufPool = [ Buffer.alloc(2), Buffer.alloc(2), Buffer.alloc(2) ];
@@ -74,24 +74,24 @@ net.createServer(common.mustCall(function(socket) {
bufPoolIdx = (bufPoolIdx + 1) % bufPool.length;
return bufPool[bufPoolIdx];
},
callback: function(nread, buf) {
callback: common.mustCallAtLeast(function(nread, buf) {
assert.strictEqual(buf, bufPool[bufPoolIdx]);
received += nread;
incoming.push(Buffer.from(buf.slice(0, nread)));
}
})
}
}).on('data', common.mustNotCall()).on('end', common.mustCall(() => {
assert.strictEqual(received, message.length);
assert.deepStrictEqual(Buffer.concat(incoming), message);
assert.strictEqual(bufPoolUsage, 7);
}));
});
}));
// Test Uint8Array callback support
net.createServer(common.mustCall(function(socket) {
this.close();
socket.end(message);
})).listen(0, function() {
})).listen(0, common.mustCall(function() {
let received = 0;
let incoming = new Uint8Array(0);
const bufPool = [ new Uint8Array(2), new Uint8Array(2), new Uint8Array(2) ];
@@ -105,27 +105,27 @@ net.createServer(common.mustCall(function(socket) {
bufPoolIdx = (bufPoolIdx + 1) % bufPool.length;
return bufPool[bufPoolIdx];
},
callback: function(nread, buf) {
callback: common.mustCallAtLeast(function(nread, buf) {
assert.strictEqual(buf, bufPool[bufPoolIdx]);
received += nread;
const newIncoming = new Uint8Array(incoming.length + nread);
newIncoming.set(incoming);
newIncoming.set(buf.slice(0, nread), incoming.length);
incoming = newIncoming;
}
})
}
}).on('data', common.mustNotCall()).on('end', common.mustCall(() => {
assert.strictEqual(received, message.length);
assert.deepStrictEqual(incoming, new Uint8Array(message));
assert.strictEqual(bufPoolUsage, 7);
}));
});
}));
// Test explicit socket pause
net.createServer(common.mustCall(function(socket) {
this.close();
socket.end(message);
})).listen(0, function() {
})).listen(0, common.mustCall(function() {
let received = 0;
const buffers = [];
const sockBuf = Buffer.alloc(8);
@@ -134,7 +134,7 @@ net.createServer(common.mustCall(function(socket) {
port: this.address().port,
onread: {
buffer: sockBuf,
callback: function(nread, buf) {
callback: common.mustCallAtLeast(function(nread, buf) {
assert.strictEqual(paused, false);
assert.strictEqual(buf, sockBuf);
received += nread;
@@ -145,19 +145,19 @@ net.createServer(common.mustCall(function(socket) {
paused = false;
this.resume();
}, 100);
}
})
}
}).on('data', common.mustNotCall()).on('end', common.mustCall(() => {
assert.strictEqual(received, message.length);
assert.deepStrictEqual(Buffer.concat(buffers), message);
}));
});
}));
// Test implicit socket pause
net.createServer(common.mustCall(function(socket) {
this.close();
socket.end(message);
})).listen(0, function() {
})).listen(0, common.mustCall(function() {
let received = 0;
const buffers = [];
const sockBuf = Buffer.alloc(8);
@@ -166,7 +166,7 @@ net.createServer(common.mustCall(function(socket) {
port: this.address().port,
onread: {
buffer: sockBuf,
callback: function(nread, buf) {
callback: common.mustCallAtLeast(function(nread, buf) {
assert.strictEqual(paused, false);
assert.strictEqual(buf, sockBuf);
received += nread;
@@ -177,10 +177,10 @@ net.createServer(common.mustCall(function(socket) {
this.resume();
}, 100);
return false;
}
})
}
}).on('data', common.mustNotCall()).on('end', common.mustCall(() => {
assert.strictEqual(received, message.length);
assert.deepStrictEqual(Buffer.concat(buffers), message);
}));
});
}));
@@ -1,29 +1,29 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const net = require('net');
let serverConnection;
let clientConnection;
const echoServer = net.createServer(function(connection) {
const echoServer = net.createServer(common.mustCall((connection) => {
serverConnection = connection;
setTimeout(function() {
setTimeout(common.mustCall(() => {
// Make sure both connections are still open
assert.strictEqual(serverConnection.readyState, 'open');
assert.strictEqual(clientConnection.readyState, 'open');
serverConnection.end();
clientConnection.end();
echoServer.close();
}, 600);
}), 600);
connection.setTimeout(0);
assert.strictEqual(typeof connection.setKeepAlive, 'function');
connection.on('end', function() {
connection.end();
});
});
}));
echoServer.listen(0);
echoServer.on('listening', function() {
echoServer.on('listening', common.mustCall(function() {
clientConnection = new net.Socket();
// Send a keepalive packet after 1000 ms
// and make sure it persists
@@ -31,4 +31,4 @@ echoServer.on('listening', function() {
assert.ok(s instanceof net.Socket);
clientConnection.connect(this.address().port);
clientConnection.setTimeout(0);
});
}));
+5 -5
View File
@@ -1,6 +1,6 @@
// Flags: --expose-internals
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const net = require('net');
const { internalBinding } = require('internal/test/binding');
@@ -21,7 +21,7 @@ TCPWrap.prototype.setNoDelay = function(enable) {
callCount++;
};
echoServer.on('listening', function() {
echoServer.on('listening', common.mustCall(function() {
const sock1 = new Socket();
// setNoDelay before the handle is created
// there is probably a better way to test this
@@ -29,8 +29,8 @@ echoServer.on('listening', function() {
const s = sock1.setNoDelay();
assert.ok(s instanceof net.Socket);
sock1.connect(this.address().port);
sock1.on('end', function() {
sock1.on('end', common.mustCall(() => {
assert.strictEqual(callCount, 1);
echoServer.close();
});
});
}));
}));
@@ -1,6 +1,6 @@
// Flags: --expose-internals
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const net = require('net');
const { internalBinding } = require('internal/test/binding');
@@ -15,27 +15,27 @@ const unref = TCPWrap.prototype.unref;
let refCount = 0;
TCPWrap.prototype.ref = function() {
TCPWrap.prototype.ref = common.mustCallAtLeast(function() {
ref.call(this);
refCount++;
assert.strictEqual(refCount, 0);
};
});
TCPWrap.prototype.unref = function() {
TCPWrap.prototype.unref = common.mustCallAtLeast(function() {
unref.call(this);
refCount--;
assert.strictEqual(refCount, -1);
};
});
echoServer.listen(0);
echoServer.on('listening', function() {
echoServer.on('listening', common.mustCall(function() {
const sock = new net.Socket();
sock.unref();
sock.ref();
sock.connect(this.address().port);
sock.on('end', () => {
sock.on('end', common.mustCall(() => {
assert.strictEqual(refCount, 0);
echoServer.close();
});
});
}));
}));
@@ -19,18 +19,18 @@ const pipePath = `${tmpdir.path}/${'x'.repeat(10000)}.sock`;
const server = net.createServer()
.listen(pipePath)
// It may work on some operating systems
.on('listening', () => {
.on('listening', common.mustCallAtLeast(() => {
// The socket file must exist
assert.ok(fs.existsSync(pipePath));
const socket = net.connect(pipePath, common.mustCall(() => {
socket.destroy();
server.close();
}));
})
.on('error', (error) => {
}, 0))
.on('error', common.mustCall((error) => {
assert.ok(error.code === 'EINVAL', error.message);
net.connect(pipePath)
.on('error', common.mustCall((error) => {
assert.ok(error.code === 'EINVAL', error.message);
}));
});
}));
+11 -11
View File
@@ -20,7 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const net = require('net');
@@ -30,7 +30,7 @@ let client_recv_count = 0;
let client_end_count = 0;
let disconnect_count = 0;
const server = net.createServer(function(socket) {
const server = net.createServer(common.mustCallAtLeast((socket) => {
console.error('SERVER: got socket connection');
socket.resume();
@@ -42,13 +42,13 @@ const server = net.createServer(function(socket) {
socket.end();
});
socket.on('close', (had_error) => {
socket.on('close', common.mustCall((had_error) => {
console.log(`SERVER had_error: ${JSON.stringify(had_error)}`);
assert.strictEqual(had_error, false);
});
});
}));
}));
server.listen(0, function() {
server.listen(0, common.mustCall(function() {
console.log('SERVER listening');
const client = net.createConnection(this.address().port);
@@ -58,28 +58,28 @@ server.listen(0, function() {
console.error('CLIENT connected', client._writableState);
});
client.on('data', function(chunk) {
client.on('data', common.mustCallAtLeast((chunk) => {
client_recv_count += 1;
console.log(`client_recv_count ${client_recv_count}`);
assert.strictEqual(chunk, 'hello\r\n');
console.error('CLIENT: calling end', client._writableState);
client.end();
});
}));
client.on('end', () => {
console.error('CLIENT end');
client_end_count++;
});
client.on('close', (had_error) => {
client.on('close', common.mustCallAtLeast((had_error) => {
console.log('CLIENT disconnect');
assert.strictEqual(had_error, false);
if (disconnect_count++ < N)
client.connect(server.address().port); // reconnect
else
server.close();
});
});
}));
}));
process.on('exit', () => {
assert.strictEqual(disconnect_count, N + 1);
+12 -12
View File
@@ -43,14 +43,14 @@ const server = net.createServer(common.mustCall(function(socket) {
socket.on('end', function() {
if (++conns_closed === 2) server.close();
});
socket.on('close', function() {
socket.on('close', common.mustCall(() => {
assert.ok(remoteAddrCandidates.includes(socket.remoteAddress));
assert.ok(remoteFamilyCandidates.includes(socket.remoteFamily));
});
}));
socket.resume();
}, 2));
server.listen(0, function() {
server.listen(0, common.mustCall(function() {
const client = net.createConnection(this.address().port, '127.0.0.1');
const client2 = net.createConnection(this.address().port);
@@ -61,24 +61,24 @@ server.listen(0, function() {
assert.strictEqual(client2.remoteFamily, undefined);
assert.strictEqual(client2.remotePort, undefined);
client.on('connect', function() {
client.on('connect', common.mustCall(() => {
assert.ok(remoteAddrCandidates.includes(client.remoteAddress));
assert.ok(remoteFamilyCandidates.includes(client.remoteFamily));
assert.strictEqual(client.remotePort, server.address().port);
client.end();
});
client.on('close', function() {
}));
client.on('close', common.mustCall(() => {
assert.ok(remoteAddrCandidates.includes(client.remoteAddress));
assert.ok(remoteFamilyCandidates.includes(client.remoteFamily));
});
client2.on('connect', function() {
}));
client2.on('connect', common.mustCall(() => {
assert.ok(remoteAddrCandidates.includes(client2.remoteAddress));
assert.ok(remoteFamilyCandidates.includes(client2.remoteFamily));
assert.strictEqual(client2.remotePort, server.address().port);
client2.end();
});
client2.on('close', function() {
}));
client2.on('close', common.mustCall(() => {
assert.ok(remoteAddrCandidates.includes(client2.remoteAddress));
assert.ok(remoteFamilyCandidates.includes(client2.remoteFamily));
});
});
}));
}));
+4 -4
View File
@@ -2,18 +2,18 @@
const common = require('../common');
const net = require('net');
const { strictEqual } = require('assert');
const assert = require('assert');
const server = net.createServer();
server.listen(common.mustCall(function() {
const socket = net.connect({ port: server.address().port });
strictEqual(socket.connecting, true);
strictEqual(socket.remoteAddress, undefined);
assert.strictEqual(socket.connecting, true);
assert.strictEqual(socket.remoteAddress, undefined);
socket.on('connect', common.mustCall(function() {
strictEqual(socket.remoteAddress !== undefined, true);
assert.strictEqual(socket.remoteAddress !== undefined, true);
socket.end();
}));
+2 -2
View File
@@ -26,7 +26,7 @@ const net = require('net');
const sockets = [];
const server = net.createServer(function(c) {
const server = net.createServer(common.mustCallAtLeast((c) => {
c.on('close', common.mustCall());
sockets.push(c);
@@ -35,7 +35,7 @@ const server = net.createServer(function(c) {
assert.strictEqual(server.close(), server);
sockets.forEach((c) => c.destroy());
}
});
}));
server.on('close', common.mustCall());
+2 -2
View File
@@ -73,7 +73,7 @@ function randomPipePath() {
// Test should emit "error" events when listening fails.
{
const handlePath = randomPipePath();
const server1 = net.createServer().listen({ path: handlePath }, () => {
const server1 = net.createServer().listen({ path: handlePath }, common.mustCall(() => {
// As the handlePath is in use, binding to the same address again should
// make the server emit an 'EADDRINUSE' error.
const server2 = net.createServer()
@@ -87,5 +87,5 @@ function randomPipePath() {
assert.strictEqual(err.code, 'EADDRINUSE');
assert.match(err.message, /^listen EADDRINUSE: address already in use/);
}));
});
}));
}
@@ -20,18 +20,18 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const net = require('net');
// Server should only fire listen callback once
const server = net.createServer();
server.on('close', function() {
server.on('close', common.mustCall(() => {
const listeners = server.listeners('listening');
console.log('Closed, listeners:', listeners.length);
assert.strictEqual(listeners.length, 0);
});
}, 2));
server.listen(0, function() {
server.close();
@@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const net = require('net');
@@ -28,10 +28,10 @@ function createConnection(index) {
sent.push(msg);
});
connection.on('error', function(err) {
connection.on('error', common.mustCallAtLeast((err) => {
assert.strictEqual(err.code, 'ECONNRESET');
resolve();
});
}, 0));
connection.on('data', function(e) {
console.error(`connection ${index} received response`);
@@ -49,12 +49,12 @@ function makeConnection(index) {
const c = net.createConnection(server.address().port);
let gotData = false;
c.on('connect', function() {
c.on('connect', common.mustCall(() => {
if (index + 1 < N) {
makeConnection(index + 1);
}
c.on('close', function() {
c.on('close', common.mustCall(() => {
console.error(`closed ${index}`);
closes++;
@@ -81,15 +81,15 @@ function makeConnection(index) {
assert.strictEqual(gotData, false,
`${index} got data, but shouldn't have`);
}
});
});
}));
}));
c.on('end', function() { c.end(); });
c.on('data', function(b) {
c.on('data', common.mustCallAtLeast((b) => {
gotData = true;
assert.ok(b.length > 0);
});
}, 0));
c.on('error', function(e) {
// Retry if SmartOS and ECONNREFUSED. See
@@ -28,8 +28,8 @@ let stopped = true;
let server1Sock;
const server1ConnHandler = (socket) => {
socket.on('data', function(data) {
const server1ConnHandler = common.mustCall((socket) => {
socket.on('data', common.mustCall((data) => {
if (stopped) {
assert.fail('data event should not have happened yet');
}
@@ -37,15 +37,15 @@ const server1ConnHandler = (socket) => {
assert.strictEqual(data.toString(), msg);
socket.end();
server1.close();
});
}));
server1Sock = socket;
};
});
const server1 = net.createServer({ pauseOnConnect: true }, server1ConnHandler);
const server2ConnHandler = (socket) => {
socket.on('data', function(data) {
const server2ConnHandler = common.mustCall((socket) => {
socket.on('data', common.mustCall((data) => {
assert.strictEqual(data.toString(), msg);
socket.end();
server2.close();
@@ -53,19 +53,19 @@ const server2ConnHandler = (socket) => {
assert.strictEqual(server1Sock.bytesRead, 0);
server1Sock.resume();
stopped = false;
});
};
}));
});
const server2 = net.createServer({ pauseOnConnect: false }, server2ConnHandler);
server1.listen(0, function() {
server1.listen(0, common.mustCall(function() {
const clientHandler = common.mustCall(function() {
server2.listen(0, function() {
net.createConnection({ port: this.address().port }).write(msg);
});
});
net.createConnection({ port: this.address().port }).write(msg, clientHandler);
});
}));
process.on('exit', function() {
assert.strictEqual(stopped, false);
+4 -4
View File
@@ -5,7 +5,7 @@ const net = require('net');
const sockets = [];
const server = net.createServer(function(c) {
const server = net.createServer(common.mustCallAtLeast((c) => {
c.on('close', common.mustCall());
sockets.push(c);
@@ -14,11 +14,11 @@ const server = net.createServer(function(c) {
assert.strictEqual(server.close(), server);
sockets.forEach((c) => c.resetAndDestroy());
}
});
}));
server.on('close', common.mustCall());
assert.strictEqual(server, server.listen(0, () => {
assert.strictEqual(server, server.listen(0, common.mustCall(() => {
net.createConnection(server.address().port)
.on('error', common.mustCall((error) => {
assert.strictEqual(error.code, 'ECONNRESET');
@@ -27,4 +27,4 @@ assert.strictEqual(server, server.listen(0, () => {
.on('error', common.mustCall((error) => {
assert.strictEqual(error.code, 'ECONNRESET');
}));
}));
})));
+2 -2
View File
@@ -33,7 +33,7 @@ const server = net.createServer(common.mustCall((c) => {
c.write('hello');
}));
server.listen(0, function() {
server.listen(0, common.mustCall(function() {
const socket = net.createConnection(this.address().port, 'localhost');
const s = socket.setTimeout(T, common.mustNotCall());
@@ -47,4 +47,4 @@ server.listen(0, function() {
}));
socket.setTimeout(0);
});
}));
@@ -6,7 +6,7 @@ const net = require('net');
const server = net.createServer();
server.on('connection', (socket) => {
server.on('connection', common.mustCall((socket) => {
let endEmitted = false;
socket.once('readable', () => {
@@ -17,12 +17,12 @@ server.on('connection', (socket) => {
socket.on('end', () => {
endEmitted = true;
});
socket.on('close', () => {
socket.on('close', common.mustCall(() => {
assert(endEmitted);
server.close();
});
}));
socket.end('foo');
});
}));
server.listen(common.mustCall(() => {
const socket = net.createConnection(server.address().port, () => {
+5 -5
View File
@@ -1,21 +1,21 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const net = require('net');
const server = net.createServer((conn) => {
conn.end();
server.close();
}).listen(0, () => {
const client = net.connect(server.address().port, () => {
}).listen(0, common.mustCall(() => {
const client = net.connect(server.address().port, common.mustCall(() => {
assert.strictEqual(client.connecting, false);
// Legacy getter
assert.strictEqual(client._connecting, false);
client.end();
});
}));
assert.strictEqual(client.connecting, true);
// Legacy getter
assert.strictEqual(client._connecting, true);
});
}));
@@ -5,7 +5,7 @@ require('../common');
// enforcer from `stream.Duplex`.
const { Socket } = require('net');
const { strictEqual } = require('assert');
const assert = require('assert');
const socket = new Socket({ allowHalfOpen: false });
strictEqual(socket.listenerCount('end'), 1);
assert.strictEqual(socket.listenerCount('end'), 1);
+4 -4
View File
@@ -6,14 +6,14 @@ const net = require('net');
const truthyValues = [true, 1, 'true', {}, []];
const falseyValues = [false, 0, ''];
const genSetNoDelay = (desiredArg) => (enable) => {
const genSetNoDelay = common.mustCall((desiredArg) => common.mustCall((enable) => {
assert.strictEqual(enable, desiredArg);
};
}), 2);
// setNoDelay should default to true
let socket = new net.Socket({
handle: {
setNoDelay: common.mustCall(genSetNoDelay(true)),
setNoDelay: genSetNoDelay(true),
readStart() {}
}
});
@@ -21,7 +21,7 @@ socket.setNoDelay();
socket = new net.Socket({
handle: {
setNoDelay: common.mustCall(genSetNoDelay(true), 1),
setNoDelay: genSetNoDelay(true),
readStart() {}
}
});
+2 -2
View File
@@ -7,7 +7,7 @@ const assert = require('assert');
const server = net.createServer().listen(0, connectToServer);
function connectToServer() {
const client = net.createConnection(this.address().port, () => {
const client = net.createConnection(this.address().port, common.mustCall(() => {
client.on('error', common.mustNotCall());
assert.throws(() => {
client.write(1337);
@@ -17,6 +17,6 @@ function connectToServer() {
});
client.destroy();
})
}))
.on('close', () => server.close());
}
+5 -5
View File
@@ -29,7 +29,7 @@ const SIZE = 2E6;
const N = 10;
const buf = Buffer.alloc(SIZE, 'a');
const server = net.createServer(function(socket) {
const server = net.createServer(common.mustCall((socket) => {
socket.setNoDelay();
socket.on('error', common.mustCall(() => socket.destroy()))
@@ -40,12 +40,12 @@ const server = net.createServer(function(socket) {
}
socket.end();
}).listen(0, function() {
})).listen(0, common.mustCall(function() {
const conn = net.connect(this.address().port);
conn.on('data', function(buf) {
conn.on('data', common.mustCall((buf) => {
assert.strictEqual(conn, conn.pause());
setTimeout(function() {
conn.destroy();
}, 20);
});
});
}));
}));
+4 -4
View File
@@ -9,10 +9,10 @@ const server = net.createServer(handle);
const N = 100;
const buf = Buffer.alloc(2, 'a');
server.listen(0, function() {
server.listen(0, common.mustCall(function() {
const conn = net.connect(this.address().port);
conn.on('connect', () => {
conn.on('connect', common.mustCall(() => {
let res = true;
let i = 0;
for (; i < N && res; i++) {
@@ -23,8 +23,8 @@ server.listen(0, function() {
}
assert.strictEqual(i, N);
conn.end();
});
});
}));
}));
function handle(socket) {
socket.resume();
+9 -9
View File
@@ -20,7 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const net = require('net');
const debuglog = require('util').debuglog('test');
@@ -29,7 +29,7 @@ let chars_recved = 0;
let npauses = 0;
let totalLength = 0;
const server = net.createServer((connection) => {
const server = net.createServer(common.mustCallAtLeast((connection) => {
const body = 'C'.repeat(1024);
let n = 1;
debuglog('starting write loop');
@@ -49,15 +49,15 @@ const server = net.createServer((connection) => {
`writableLength: ${connection.writableLength}, totalLength: ${totalLength}`,
);
connection.end();
});
}));
server.listen(0, () => {
server.listen(0, common.mustCall(() => {
const port = server.address().port;
debuglog(`server started on port ${port}`);
let paused = false;
const client = net.createConnection(port);
client.setEncoding('ascii');
client.on('data', (d) => {
client.on('data', common.mustCallAtLeast((d) => {
chars_recved += d.length;
debuglog(`got ${chars_recved}`);
if (!paused) {
@@ -66,20 +66,20 @@ server.listen(0, () => {
paused = true;
debuglog('pause');
const x = chars_recved;
setTimeout(() => {
setTimeout(common.mustCall(() => {
assert.strictEqual(chars_recved, x);
client.resume();
debuglog('resume');
paused = false;
}, 100);
}), 100);
}
});
}));
client.on('end', () => {
server.close();
client.end();
});
});
}));
process.on('exit', () => {
+4 -4
View File
@@ -35,8 +35,8 @@ const server = net.createServer(common.mustCall(function(socket) {
socket.on('error', common.mustNotCall());
}));
server.listen(0, function() {
const client = net.connect(this.address().port, function() {
server.listen(0, common.mustCall(function() {
const client = net.connect(this.address().port, common.mustCall(() => {
// client.end() will close both the readable and writable side
// of the duplex because allowHalfOpen defaults to false.
// Then 'end' will be emitted when it receives a FIN packet from
@@ -48,5 +48,5 @@ server.listen(0, function() {
}));
}));
client.end();
});
});
}));
}));
+4 -4
View File
@@ -10,8 +10,8 @@ const { expectsError, mustCall } = common;
// after sockets ending will be emitted in the next tick.
const server = net.createServer(mustCall((socket) => {
socket.end();
})).listen(() => {
const client = net.connect(server.address().port, () => {
})).listen(mustCall(() => {
const client = net.connect(server.address().port, mustCall(() => {
let hasError = false;
client.on('error', mustCall((err) => {
hasError = true;
@@ -28,5 +28,5 @@ const server = net.createServer(mustCall((socket) => {
assert(!hasError, 'The error should be emitted in the next tick.');
}));
client.end();
});
});
}));
}));
+3 -3
View File
@@ -20,7 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const order = [];
@@ -57,7 +57,7 @@ testNextTickWith('str');
testNextTickWith({});
testNextTickWith([]);
process.on('uncaughtException', function(err, errorOrigin) {
process.on('uncaughtException', common.mustCall((err, errorOrigin) => {
assert.strictEqual(errorOrigin, 'uncaughtException');
if (!exceptionHandled) {
@@ -67,7 +67,7 @@ process.on('uncaughtException', function(err, errorOrigin) {
// If we get here then the first process.nextTick got called twice
order.push('OOPS!');
}
});
}));
process.on('exit', function() {
assert.deepStrictEqual(order, ['A', 'B', 'C']);
+55 -60
View File
@@ -4,102 +4,97 @@
const common = require('../common');
const { NodeEventTarget } = require('internal/event_target');
const {
deepStrictEqual,
ok,
strictEqual,
throws,
} = require('assert');
const assert = require('assert');
const { on } = require('events');
{
const eventTarget = new NodeEventTarget();
strictEqual(eventTarget.listenerCount('foo'), 0);
deepStrictEqual(eventTarget.eventNames(), []);
assert.strictEqual(eventTarget.listenerCount('foo'), 0);
assert.deepStrictEqual(eventTarget.eventNames(), []);
const ev1 = common.mustCall(function(event) {
strictEqual(event.type, 'foo');
strictEqual(this, eventTarget);
assert.strictEqual(event.type, 'foo');
assert.strictEqual(this, eventTarget);
}, 2);
const ev2 = {
handleEvent: common.mustCall(function(event) {
strictEqual(event.type, 'foo');
strictEqual(this, ev2);
assert.strictEqual(event.type, 'foo');
assert.strictEqual(this, ev2);
})
};
eventTarget.addEventListener('foo', ev1);
eventTarget.addEventListener('foo', ev2, { once: true });
strictEqual(eventTarget.listenerCount('foo'), 2);
ok(eventTarget.dispatchEvent(new Event('foo')));
strictEqual(eventTarget.listenerCount('foo'), 1);
assert.strictEqual(eventTarget.listenerCount('foo'), 2);
assert.ok(eventTarget.dispatchEvent(new Event('foo')));
assert.strictEqual(eventTarget.listenerCount('foo'), 1);
eventTarget.dispatchEvent(new Event('foo'));
eventTarget.removeEventListener('foo', ev1);
strictEqual(eventTarget.listenerCount('foo'), 0);
assert.strictEqual(eventTarget.listenerCount('foo'), 0);
eventTarget.dispatchEvent(new Event('foo'));
}
{
const eventTarget = new NodeEventTarget();
strictEqual(eventTarget.listenerCount('foo'), 0);
deepStrictEqual(eventTarget.eventNames(), []);
assert.strictEqual(eventTarget.listenerCount('foo'), 0);
assert.deepStrictEqual(eventTarget.eventNames(), []);
const ev1 = common.mustCall((event) => {
strictEqual(event.type, 'foo');
assert.strictEqual(event.type, 'foo');
}, 2);
const ev2 = {
handleEvent: common.mustCall((event) => {
strictEqual(event.type, 'foo');
assert.strictEqual(event.type, 'foo');
})
};
strictEqual(eventTarget.on('foo', ev1), eventTarget);
strictEqual(eventTarget.once('foo', ev2, { once: true }), eventTarget);
strictEqual(eventTarget.listenerCount('foo'), 2);
assert.strictEqual(eventTarget.on('foo', ev1), eventTarget);
assert.strictEqual(eventTarget.once('foo', ev2, { once: true }), eventTarget);
assert.strictEqual(eventTarget.listenerCount('foo'), 2);
eventTarget.dispatchEvent(new Event('foo'));
strictEqual(eventTarget.listenerCount('foo'), 1);
assert.strictEqual(eventTarget.listenerCount('foo'), 1);
eventTarget.dispatchEvent(new Event('foo'));
strictEqual(eventTarget.off('foo', ev1), eventTarget);
strictEqual(eventTarget.listenerCount('foo'), 0);
assert.strictEqual(eventTarget.off('foo', ev1), eventTarget);
assert.strictEqual(eventTarget.listenerCount('foo'), 0);
eventTarget.dispatchEvent(new Event('foo'));
}
{
const eventTarget = new NodeEventTarget();
strictEqual(eventTarget.listenerCount('foo'), 0);
deepStrictEqual(eventTarget.eventNames(), []);
assert.strictEqual(eventTarget.listenerCount('foo'), 0);
assert.deepStrictEqual(eventTarget.eventNames(), []);
const ev1 = common.mustCall((event) => {
strictEqual(event.type, 'foo');
assert.strictEqual(event.type, 'foo');
}, 2);
const ev2 = {
handleEvent: common.mustCall((event) => {
strictEqual(event.type, 'foo');
assert.strictEqual(event.type, 'foo');
})
};
eventTarget.addListener('foo', ev1);
eventTarget.once('foo', ev2, { once: true });
strictEqual(eventTarget.listenerCount('foo'), 2);
assert.strictEqual(eventTarget.listenerCount('foo'), 2);
eventTarget.dispatchEvent(new Event('foo'));
strictEqual(eventTarget.listenerCount('foo'), 1);
assert.strictEqual(eventTarget.listenerCount('foo'), 1);
eventTarget.dispatchEvent(new Event('foo'));
eventTarget.removeListener('foo', ev1);
strictEqual(eventTarget.listenerCount('foo'), 0);
assert.strictEqual(eventTarget.listenerCount('foo'), 0);
eventTarget.dispatchEvent(new Event('foo'));
}
{
const eventTarget = new NodeEventTarget();
strictEqual(eventTarget.listenerCount('foo'), 0);
deepStrictEqual(eventTarget.eventNames(), []);
assert.strictEqual(eventTarget.listenerCount('foo'), 0);
assert.deepStrictEqual(eventTarget.eventNames(), []);
// Won't actually be called.
const ev1 = () => {};
@@ -112,33 +107,33 @@ const { on } = require('events');
eventTarget.once('foo', ev2, { once: true });
eventTarget.once('foo', ev2, { once: false });
eventTarget.on('bar', ev1);
strictEqual(eventTarget.listenerCount('foo'), 2);
strictEqual(eventTarget.listenerCount('bar'), 1);
deepStrictEqual(eventTarget.eventNames(), ['foo', 'bar']);
strictEqual(eventTarget.removeAllListeners('foo'), eventTarget);
strictEqual(eventTarget.listenerCount('foo'), 0);
strictEqual(eventTarget.listenerCount('bar'), 1);
deepStrictEqual(eventTarget.eventNames(), ['bar']);
strictEqual(eventTarget.removeAllListeners(), eventTarget);
strictEqual(eventTarget.listenerCount('foo'), 0);
strictEqual(eventTarget.listenerCount('bar'), 0);
deepStrictEqual(eventTarget.eventNames(), []);
assert.strictEqual(eventTarget.listenerCount('foo'), 2);
assert.strictEqual(eventTarget.listenerCount('bar'), 1);
assert.deepStrictEqual(eventTarget.eventNames(), ['foo', 'bar']);
assert.strictEqual(eventTarget.removeAllListeners('foo'), eventTarget);
assert.strictEqual(eventTarget.listenerCount('foo'), 0);
assert.strictEqual(eventTarget.listenerCount('bar'), 1);
assert.deepStrictEqual(eventTarget.eventNames(), ['bar']);
assert.strictEqual(eventTarget.removeAllListeners(), eventTarget);
assert.strictEqual(eventTarget.listenerCount('foo'), 0);
assert.strictEqual(eventTarget.listenerCount('bar'), 0);
assert.deepStrictEqual(eventTarget.eventNames(), []);
}
{
const target = new NodeEventTarget();
process.on('warning', common.mustCall((warning) => {
ok(warning instanceof Error);
strictEqual(warning.name, 'MaxListenersExceededWarning');
strictEqual(warning.target, target);
strictEqual(warning.count, 2);
strictEqual(warning.type, 'foo');
ok(warning.message.includes(
assert.ok(warning instanceof Error);
assert.strictEqual(warning.name, 'MaxListenersExceededWarning');
assert.strictEqual(warning.target, target);
assert.strictEqual(warning.count, 2);
assert.strictEqual(warning.type, 'foo');
assert.ok(warning.message.includes(
'2 foo listeners added to NodeEventTarget'));
}));
strictEqual(target.getMaxListeners(), NodeEventTarget.defaultMaxListeners);
assert.strictEqual(target.getMaxListeners(), NodeEventTarget.defaultMaxListeners);
target.setMaxListeners(1);
target.on('foo', () => {});
target.on('foo', () => {});
@@ -147,20 +142,20 @@ const { on } = require('events');
// Test NodeEventTarget emit
const emitter = new NodeEventTarget();
emitter.addEventListener('foo', common.mustCall((e) => {
strictEqual(e.type, 'foo');
strictEqual(e.detail, 'bar');
ok(e instanceof Event);
assert.strictEqual(e.type, 'foo');
assert.strictEqual(e.detail, 'bar');
assert.ok(e instanceof Event);
}), { once: true });
emitter.once('foo', common.mustCall((e, droppedAdditionalArgument) => {
strictEqual(e, 'bar');
strictEqual(droppedAdditionalArgument, undefined);
assert.strictEqual(e, 'bar');
assert.strictEqual(droppedAdditionalArgument, undefined);
}));
emitter.emit('foo', 'bar', 'baz');
}
{
// Test NodeEventTarget emit unsupported usage
const emitter = new NodeEventTarget();
throws(() => {
assert.throws(() => {
emitter.emit();
}, /ERR_INVALID_ARG_TYPE/);
}
@@ -174,7 +169,7 @@ const { on } = require('events');
let count = 0;
for await (const [ item ] of on(emitter, 'foo')) {
count++;
strictEqual(item.type, 'foo');
assert.strictEqual(item.type, 'foo');
if (count > 5) {
break;
}