File tree Expand file tree Collapse file tree 2 files changed +50
-1
lines changed
Expand file tree Collapse file tree 2 files changed +50
-1
lines changed Original file line number Diff line number Diff line change @@ -18,7 +18,20 @@ EventEmitter.prototype._maxListeners = undefined;
1818
1919// By default EventEmitters will print a warning if more than 10 listeners are
2020// added to it. This is a useful default which helps finding memory leaks.
21- EventEmitter . defaultMaxListeners = 10 ;
21+ var defaultMaxListeners = 10 ;
22+
23+ Object . defineProperty ( EventEmitter , 'defaultMaxListeners' , {
24+ enumerable : true ,
25+ get : function ( ) {
26+ return defaultMaxListeners ;
27+ } ,
28+ set : function ( arg ) {
29+ // force global console to be compiled.
30+ // see https://github.com/nodejs/node/issues/4467
31+ console ;
32+ defaultMaxListeners = arg ;
33+ }
34+ } ) ;
2235
2336EventEmitter . init = function ( ) {
2437 this . domain = null ;
Original file line number Diff line number Diff line change 1+ /* eslint-disable required-modules */
2+ // ordinarily test files must require('common') but that action causes
3+ // the global console to be compiled, defeating the purpose of this test
4+
5+ 'use strict' ;
6+
7+ const assert = require ( 'assert' ) ;
8+ const EventEmitter = require ( 'events' ) ;
9+ const leak_warning = / E v e n t E m i t t e r m e m o r y l e a k d e t e c t e d \. 2 h e l l o l i s t e n e r s / ;
10+
11+ var write_calls = 0 ;
12+ process . stderr . write = function ( data ) {
13+ if ( write_calls === 0 )
14+ assert . ok ( data . match ( leak_warning ) ) ;
15+ else if ( write_calls === 1 )
16+ assert . ok ( data . match ( / T r a c e / ) ) ;
17+ else
18+ assert . ok ( false , 'stderr.write should be called only twice' ) ;
19+
20+ write_calls ++ ;
21+ } ;
22+
23+ const old_default = EventEmitter . defaultMaxListeners ;
24+ EventEmitter . defaultMaxListeners = 1 ;
25+
26+ const e = new EventEmitter ( ) ;
27+ e . on ( 'hello' , function ( ) { } ) ;
28+ e . on ( 'hello' , function ( ) { } ) ;
29+
30+ // TODO: figure out how to validate console. Currently,
31+ // there is no obvious way of validating that console
32+ // exists here exactly when it should.
33+
34+ assert . equal ( write_calls , 2 ) ;
35+
36+ EventEmitter . defaultMaxListeners = old_default ;
You can’t perform that action at this time.
0 commit comments