From 1158f44599ece58276196cf298e1f4ba1023a195 Mon Sep 17 00:00:00 2001 From: jseagull Date: Mon, 10 Oct 2016 18:47:07 +0800 Subject: [PATCH] events,test: fix TypeError in EventEmitter warning Allows Symbol to be converted to String so it can be included in the error. Conflicts: lib/events.js Fixes: #9003 Backport-PR-URL: https://github.com/nodejs/node/pull/12459 PR-URL: https://github.com/nodejs/node/pull/9021 Reviewed-By: Rich Trott Reviewed-By: Luigi Pinca Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Sakthipriyan Vairamani Reviewed-By: Santiago Gimeno --- lib/events.js | 5 ++-- ...test-event-emitter-check-listener-leaks.js | 8 +++++++ ...-emitter-max-listeners-warning-for-null.js | 22 +++++++++++++++++ ...mitter-max-listeners-warning-for-symbol.js | 24 +++++++++++++++++++ ...est-event-emitter-max-listeners-warning.js | 1 + 5 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-event-emitter-max-listeners-warning-for-null.js create mode 100644 test/parallel/test-event-emitter-max-listeners-warning-for-symbol.js diff --git a/lib/events.js b/lib/events.js index ac18327ef14..5cb837cc6c3 100644 --- a/lib/events.js +++ b/lib/events.js @@ -257,8 +257,9 @@ function _addListener(target, type, listener, prepend) { if (m && m > 0 && existing.length > m) { existing.warned = true; const w = new Error('Possible EventEmitter memory leak detected. ' + - `${existing.length} ${type} listeners added. ` + - 'Use emitter.setMaxListeners() to increase limit'); + `${existing.length} ${String(type)} listeners ` + + 'added. Use emitter.setMaxListeners() to ' + + 'increase limit'); w.name = 'Warning'; w.emitter = target; w.type = type; diff --git a/test/parallel/test-event-emitter-check-listener-leaks.js b/test/parallel/test-event-emitter-check-listener-leaks.js index 244bfa97bfa..5b9787f1b83 100644 --- a/test/parallel/test-event-emitter-check-listener-leaks.js +++ b/test/parallel/test-event-emitter-check-listener-leaks.js @@ -13,6 +13,14 @@ assert.ok(!e._events['default'].hasOwnProperty('warned')); e.on('default', function() {}); 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 e.setMaxListeners(5); for (let i = 0; i < 5; i++) { diff --git a/test/parallel/test-event-emitter-max-listeners-warning-for-null.js b/test/parallel/test-event-emitter-max-listeners-warning-for-null.js new file mode 100644 index 00000000000..62b06f67d92 --- /dev/null +++ b/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, 'Warning'); + 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() {}); diff --git a/test/parallel/test-event-emitter-max-listeners-warning-for-symbol.js b/test/parallel/test-event-emitter-max-listeners-warning-for-symbol.js new file mode 100644 index 00000000000..97bf1892703 --- /dev/null +++ b/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, 'Warning'); + 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() {}); diff --git a/test/parallel/test-event-emitter-max-listeners-warning.js b/test/parallel/test-event-emitter-max-listeners-warning.js index bcbb5832658..df363e43d9c 100644 --- a/test/parallel/test-event-emitter-max-listeners-warning.js +++ b/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.count, 2); assert.strictEqual(warning.type, 'event-type'); + assert.ok(warning.message.includes('2 event-type listeners added.')); })); e.on('event-type', function() {});