test: refactor event-emitter-check-listener-leaks
* add block-scoping * use common.mustCall() on callbacks that should not execute PR-URL: https://github.com/nodejs/node/pull/13164 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
parent
169c07dae1
commit
16357f2d72
@ -25,73 +25,79 @@ const common = require('../common');
|
|||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const events = require('events');
|
const events = require('events');
|
||||||
|
|
||||||
let e = new events.EventEmitter();
|
|
||||||
|
|
||||||
// default
|
// default
|
||||||
for (let i = 0; i < 10; i++) {
|
{
|
||||||
e.on('default', common.noop);
|
const e = new events.EventEmitter();
|
||||||
|
|
||||||
|
for (let i = 0; i < 10; i++) {
|
||||||
|
e.on('default', common.mustNotCall());
|
||||||
|
}
|
||||||
|
assert.ok(!e._events['default'].hasOwnProperty('warned'));
|
||||||
|
e.on('default', common.mustNotCall());
|
||||||
|
assert.ok(e._events['default'].warned);
|
||||||
|
|
||||||
|
// symbol
|
||||||
|
const symbol = Symbol('symbol');
|
||||||
|
e.setMaxListeners(1);
|
||||||
|
e.on(symbol, common.mustNotCall());
|
||||||
|
assert.ok(!e._events[symbol].hasOwnProperty('warned'));
|
||||||
|
e.on(symbol, common.mustNotCall());
|
||||||
|
assert.ok(e._events[symbol].hasOwnProperty('warned'));
|
||||||
|
|
||||||
|
// specific
|
||||||
|
e.setMaxListeners(5);
|
||||||
|
for (let i = 0; i < 5; i++) {
|
||||||
|
e.on('specific', common.mustNotCall());
|
||||||
|
}
|
||||||
|
assert.ok(!e._events['specific'].hasOwnProperty('warned'));
|
||||||
|
e.on('specific', common.mustNotCall());
|
||||||
|
assert.ok(e._events['specific'].warned);
|
||||||
|
|
||||||
|
// only one
|
||||||
|
e.setMaxListeners(1);
|
||||||
|
e.on('only one', common.mustNotCall());
|
||||||
|
assert.ok(!e._events['only one'].hasOwnProperty('warned'));
|
||||||
|
e.on('only one', common.mustNotCall());
|
||||||
|
assert.ok(e._events['only one'].hasOwnProperty('warned'));
|
||||||
|
|
||||||
|
// unlimited
|
||||||
|
e.setMaxListeners(0);
|
||||||
|
for (let i = 0; i < 1000; i++) {
|
||||||
|
e.on('unlimited', common.mustNotCall());
|
||||||
|
}
|
||||||
|
assert.ok(!e._events['unlimited'].hasOwnProperty('warned'));
|
||||||
}
|
}
|
||||||
assert.ok(!e._events['default'].hasOwnProperty('warned'));
|
|
||||||
e.on('default', common.noop);
|
|
||||||
assert.ok(e._events['default'].warned);
|
|
||||||
|
|
||||||
// symbol
|
|
||||||
const symbol = Symbol('symbol');
|
|
||||||
e.setMaxListeners(1);
|
|
||||||
e.on(symbol, common.noop);
|
|
||||||
assert.ok(!e._events[symbol].hasOwnProperty('warned'));
|
|
||||||
e.on(symbol, common.noop);
|
|
||||||
assert.ok(e._events[symbol].hasOwnProperty('warned'));
|
|
||||||
|
|
||||||
// specific
|
|
||||||
e.setMaxListeners(5);
|
|
||||||
for (let i = 0; i < 5; i++) {
|
|
||||||
e.on('specific', common.noop);
|
|
||||||
}
|
|
||||||
assert.ok(!e._events['specific'].hasOwnProperty('warned'));
|
|
||||||
e.on('specific', common.noop);
|
|
||||||
assert.ok(e._events['specific'].warned);
|
|
||||||
|
|
||||||
// only one
|
|
||||||
e.setMaxListeners(1);
|
|
||||||
e.on('only one', common.noop);
|
|
||||||
assert.ok(!e._events['only one'].hasOwnProperty('warned'));
|
|
||||||
e.on('only one', common.noop);
|
|
||||||
assert.ok(e._events['only one'].hasOwnProperty('warned'));
|
|
||||||
|
|
||||||
// unlimited
|
|
||||||
e.setMaxListeners(0);
|
|
||||||
for (let i = 0; i < 1000; i++) {
|
|
||||||
e.on('unlimited', common.noop);
|
|
||||||
}
|
|
||||||
assert.ok(!e._events['unlimited'].hasOwnProperty('warned'));
|
|
||||||
|
|
||||||
// process-wide
|
// process-wide
|
||||||
events.EventEmitter.defaultMaxListeners = 42;
|
{
|
||||||
e = new events.EventEmitter();
|
events.EventEmitter.defaultMaxListeners = 42;
|
||||||
|
const e = new events.EventEmitter();
|
||||||
|
|
||||||
for (let i = 0; i < 42; ++i) {
|
for (let i = 0; i < 42; ++i) {
|
||||||
e.on('fortytwo', common.noop);
|
e.on('fortytwo', common.mustNotCall());
|
||||||
|
}
|
||||||
|
assert.ok(!e._events['fortytwo'].hasOwnProperty('warned'));
|
||||||
|
e.on('fortytwo', common.mustNotCall());
|
||||||
|
assert.ok(e._events['fortytwo'].hasOwnProperty('warned'));
|
||||||
|
delete e._events['fortytwo'].warned;
|
||||||
|
|
||||||
|
events.EventEmitter.defaultMaxListeners = 44;
|
||||||
|
e.on('fortytwo', common.mustNotCall());
|
||||||
|
assert.ok(!e._events['fortytwo'].hasOwnProperty('warned'));
|
||||||
|
e.on('fortytwo', common.mustNotCall());
|
||||||
|
assert.ok(e._events['fortytwo'].hasOwnProperty('warned'));
|
||||||
}
|
}
|
||||||
assert.ok(!e._events['fortytwo'].hasOwnProperty('warned'));
|
|
||||||
e.on('fortytwo', common.noop);
|
|
||||||
assert.ok(e._events['fortytwo'].hasOwnProperty('warned'));
|
|
||||||
delete e._events['fortytwo'].warned;
|
|
||||||
|
|
||||||
events.EventEmitter.defaultMaxListeners = 44;
|
|
||||||
e.on('fortytwo', common.noop);
|
|
||||||
assert.ok(!e._events['fortytwo'].hasOwnProperty('warned'));
|
|
||||||
e.on('fortytwo', common.noop);
|
|
||||||
assert.ok(e._events['fortytwo'].hasOwnProperty('warned'));
|
|
||||||
|
|
||||||
// but _maxListeners still has precedence over defaultMaxListeners
|
// but _maxListeners still has precedence over defaultMaxListeners
|
||||||
events.EventEmitter.defaultMaxListeners = 42;
|
{
|
||||||
e = new events.EventEmitter();
|
events.EventEmitter.defaultMaxListeners = 42;
|
||||||
e.setMaxListeners(1);
|
const e = new events.EventEmitter();
|
||||||
e.on('uno', common.noop);
|
e.setMaxListeners(1);
|
||||||
assert.ok(!e._events['uno'].hasOwnProperty('warned'));
|
e.on('uno', common.mustNotCall());
|
||||||
e.on('uno', common.noop);
|
assert.ok(!e._events['uno'].hasOwnProperty('warned'));
|
||||||
assert.ok(e._events['uno'].hasOwnProperty('warned'));
|
e.on('uno', common.mustNotCall());
|
||||||
|
assert.ok(e._events['uno'].hasOwnProperty('warned'));
|
||||||
|
|
||||||
// chainable
|
// chainable
|
||||||
assert.strictEqual(e, e.setMaxListeners(1));
|
assert.strictEqual(e, e.setMaxListeners(1));
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user