@ -1,39 +1,46 @@
/* eslint-disable required-modules */
/* eslint-disable required-modules */
// ordinarily test files must require('common') but that action causes
// the global console to be compiled, defeating the purpose of this test
'use strict' ;
'use strict' ;
const common = require ( '../common' ) ;
// Ordinarily test files must require('common') but that action causes
// the global console to be compiled, defeating the purpose of this test.
const assert = require ( 'assert' ) ;
const assert = require ( 'assert' ) ;
const EventEmitter = require ( 'events' ) ;
const EventEmitter = require ( 'events' ) ;
const leakWarning = /EventEmitter memory leak detected\. 2 hello listeners/ ;
const leakWarning = /EventEmitter memory leak detected\. 2 hello listeners/ ;
common . hijackStderr ( common . mustCall ( function ( data ) {
let writeTimes = 0 ;
if ( process . stderr . writeTimes === 0 ) {
let warningTimes = 0 ;
assert . ok ( leakWarning . test ( data ) ) ;
process . on ( 'warning' , ( ) => {
} else {
assert . fail ( 'stderr.write should be called only once' ) ;
}
} ) ) ;
process . on ( 'warning' , function ( warning ) {
// This will be called after the default internal
// This will be called after the default internal
// process warning handler is called. The default
// process warning handler is called. The default
// process warning writes to the console, which will
// process warning writes to the console, which will
// invoke the monkeypatched process.stderr.write
// invoke the monkeypatched process.stderr.write
// below.
// below.
assert . strictEqual ( process . stderr . writeTimes , 1 ) ;
assert . strictEqual ( writeTimes , 1 ) ;
EventEmitter . defaultMaxListeners = oldDefault ;
EventEmitter . defaultMaxListeners = oldDefault ;
// when we get here, we should be done
warningTimes ++ ;
} ) ;
process . on ( 'exit' , ( ) => {
assert . strictEqual ( warningTimes , 1 ) ;
} ) ;
} ) ;
process . stderr . write = ( data ) => {
if ( writeTimes === 0 )
assert . ok ( leakWarning . test ( data ) ) ;
else
assert . fail ( 'stderr.write should be called only once' ) ;
writeTimes ++ ;
} ;
const oldDefault = EventEmitter . defaultMaxListeners ;
const oldDefault = EventEmitter . defaultMaxListeners ;
EventEmitter . defaultMaxListeners = 1 ;
EventEmitter . defaultMaxListeners = 1 ;
const e = new EventEmitter ( ) ;
const e = new EventEmitter ( ) ;
e . on ( 'hello' , common . noop ) ;
e . on ( 'hello' , ( ) => { } ) ;
e . on ( 'hello' , common . noop ) ;
e . on ( 'hello' , ( ) => { } ) ;
// TODO: Figure out how to validate console. Currently,
// TODO: Figure out how to validate console. Currently,
// there is no obvious way of validating that console
// there is no obvious way of validating that console