Browse Source

events,test: fix TypeError in EventEmitter warning

Allows Symbol to be converted to String so it can be included in the
error.

Fixes: https://github.com/nodejs/node/issues/9003
PR-URL: https://github.com/nodejs/node/pull/9021
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
v6
jseagull 8 years ago
committed by James M Snell
parent
commit
254ab63832
  1. 5
      lib/events.js
  2. 12
      test/parallel/test-event-emitter-check-listener-leaks.js
  3. 22
      test/parallel/test-event-emitter-max-listeners-warning-for-null.js
  4. 24
      test/parallel/test-event-emitter-max-listeners-warning-for-symbol.js
  5. 1
      test/parallel/test-event-emitter-max-listeners-warning.js

5
lib/events.js

@ -257,8 +257,9 @@ function _addListener(target, type, listener, prepend) {
if (m && m > 0 && existing.length > m) { if (m && m > 0 && existing.length > m) {
existing.warned = true; existing.warned = true;
const w = new Error('Possible EventEmitter memory leak detected. ' + const w = new Error('Possible EventEmitter memory leak detected. ' +
`${existing.length} ${type} listeners added. ` + `${existing.length} ${String(type)} listeners ` +
'Use emitter.setMaxListeners() to increase limit'); 'added. Use emitter.setMaxListeners() to ' +
'increase limit');
w.name = 'MaxListenersExceededWarning'; w.name = 'MaxListenersExceededWarning';
w.emitter = target; w.emitter = target;
w.type = type; w.type = type;

12
test/parallel/test-event-emitter-check-listener-leaks.js

@ -1,7 +1,7 @@
'use strict'; 'use strict';
require('../common'); require('../common');
var assert = require('assert'); const assert = require('assert');
var events = require('events'); const events = require('events');
var e = new events.EventEmitter(); var e = new events.EventEmitter();
@ -13,6 +13,14 @@ assert.ok(!e._events['default'].hasOwnProperty('warned'));
e.on('default', function() {}); e.on('default', function() {});
assert.ok(e._events['default'].warned); assert.ok(e._events['default'].warned);
// symbol
const symbol = Symbol('symbol');
e.setMaxListeners(1);
e.on(symbol, function() {});
assert.ok(!e._events[symbol].hasOwnProperty('warned'));
e.on(symbol, function() {});
assert.ok(e._events[symbol].hasOwnProperty('warned'));
// specific // specific
e.setMaxListeners(5); e.setMaxListeners(5);
for (let i = 0; i < 5; i++) { for (let i = 0; i < 5; i++) {

22
test/parallel/test-event-emitter-max-listeners-warning-for-null.js

@ -0,0 +1,22 @@
// Flags: --no-warnings
// The flag suppresses stderr output but the warning event will still emit
'use strict';
const common = require('../common');
const events = require('events');
const assert = require('assert');
const e = new events.EventEmitter();
e.setMaxListeners(1);
process.on('warning', common.mustCall((warning) => {
assert.ok(warning instanceof Error);
assert.strictEqual(warning.name, 'MaxListenersExceededWarning');
assert.strictEqual(warning.emitter, e);
assert.strictEqual(warning.count, 2);
assert.strictEqual(warning.type, null);
assert.ok(warning.message.includes('2 null listeners added.'));
}));
e.on(null, function() {});
e.on(null, function() {});

24
test/parallel/test-event-emitter-max-listeners-warning-for-symbol.js

@ -0,0 +1,24 @@
// Flags: --no-warnings
// The flag suppresses stderr output but the warning event will still emit
'use strict';
const common = require('../common');
const events = require('events');
const assert = require('assert');
const symbol = Symbol('symbol');
const e = new events.EventEmitter();
e.setMaxListeners(1);
process.on('warning', common.mustCall((warning) => {
assert.ok(warning instanceof Error);
assert.strictEqual(warning.name, 'MaxListenersExceededWarning');
assert.strictEqual(warning.emitter, e);
assert.strictEqual(warning.count, 2);
assert.strictEqual(warning.type, symbol);
assert.ok(warning.message.includes('2 Symbol(symbol) listeners added.'));
}));
e.on(symbol, function() {});
e.on(symbol, function() {});

1
test/parallel/test-event-emitter-max-listeners-warning.js

@ -15,6 +15,7 @@ process.on('warning', common.mustCall((warning) => {
assert.strictEqual(warning.emitter, e); assert.strictEqual(warning.emitter, e);
assert.strictEqual(warning.count, 2); assert.strictEqual(warning.count, 2);
assert.strictEqual(warning.type, 'event-type'); assert.strictEqual(warning.type, 'event-type');
assert.ok(warning.message.includes('2 event-type listeners added.'));
})); }));
e.on('event-type', function() {}); e.on('event-type', function() {});

Loading…
Cancel
Save