Skip to content
Browse files

Fix event listener leak check timing

  • Loading branch information...
1 parent 9a3dd75 commit 9353c80002f91d2c96e848d3656ddba079b8acb0 @koichik committed May 12, 2011
Showing with 32 additions and 3 deletions.
  1. +3 −3 lib/events.js
  2. +29 −0 test/simple/test-event-emitter-check-listener-leaks.js
View
6 lib/events.js
@@ -105,6 +105,9 @@ EventEmitter.prototype.addListener = function(type, listener) {
this._events[type] = listener;
} else if (isArray(this._events[type])) {
+ // If we've already got an array, just append.
+ this._events[type].push(listener);
+
// Check for listener leak
if (!this._events[type].warned) {
var m;
@@ -123,9 +126,6 @@ EventEmitter.prototype.addListener = function(type, listener) {
console.trace();
}
}
-
- // If we've already got an array, just append.
- this._events[type].push(listener);
} else {
// Adding the second element, need to change to array.
this._events[type] = [this._events[type], listener];
View
29 test/simple/test-event-emitter-check-listener-leaks.js
@@ -0,0 +1,29 @@
+var assert = require('assert');
+var events = require('events');
+
+var e = new events.EventEmitter();
+
+// default
+for (var i = 0; i < 10; i++) {
+ e.on('default', function() {});
+}
+assert.ok(!e._events['default'].hasOwnProperty('warned'));
+e.on('default', function() {});
+assert.ok(e._events['default'].warned);
+
+// specific
+e.setMaxListeners(5);
+for (var i = 0; i < 5; i++) {
+ e.on('specific', function() {});
+}
+assert.ok(!e._events['specific'].hasOwnProperty('warned'));
+e.on('specific', function() {});
+assert.ok(e._events['specific'].warned);
+
+// unlimited
+e.setMaxListeners(0);
+for (var i = 0; i < 1000; i++) {
+ e.on('unlimited', function() {});
+}
+assert.ok(!e._events['unlimited'].hasOwnProperty('warned'));
+

0 comments on commit 9353c80

Please sign in to comment.
Something went wrong with that request. Please try again.