11cb0ef41Sopenharmony_ci/* eslint-disable node-core/require-common-first, node-core/required-modules */
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci'use strict';
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci// Ordinarily test files must require('common') but that action causes
61cb0ef41Sopenharmony_ci// the global console to be compiled, defeating the purpose of this test.
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst EventEmitter = require('events');
101cb0ef41Sopenharmony_ciconst leakWarning = /EventEmitter memory leak detected\. 2 hello listeners/;
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_cilet writeTimes = 0;
131cb0ef41Sopenharmony_cilet warningTimes = 0;
141cb0ef41Sopenharmony_ciprocess.on('warning', () => {
151cb0ef41Sopenharmony_ci  // This will be called after the default internal
161cb0ef41Sopenharmony_ci  // process warning handler is called. The default
171cb0ef41Sopenharmony_ci  // process warning writes to the console, which will
181cb0ef41Sopenharmony_ci  // invoke the monkeypatched process.stderr.write
191cb0ef41Sopenharmony_ci  // below.
201cb0ef41Sopenharmony_ci  assert.strictEqual(writeTimes, 1);
211cb0ef41Sopenharmony_ci  EventEmitter.defaultMaxListeners = oldDefault;
221cb0ef41Sopenharmony_ci  warningTimes++;
231cb0ef41Sopenharmony_ci});
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciprocess.on('exit', () => {
261cb0ef41Sopenharmony_ci  assert.strictEqual(warningTimes, 1);
271cb0ef41Sopenharmony_ci});
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ciprocess.stderr.write = (data) => {
301cb0ef41Sopenharmony_ci  if (writeTimes === 0)
311cb0ef41Sopenharmony_ci    assert.match(data, leakWarning);
321cb0ef41Sopenharmony_ci  else
331cb0ef41Sopenharmony_ci    assert.fail('stderr.write should be called only once');
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  writeTimes++;
361cb0ef41Sopenharmony_ci};
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ciconst oldDefault = EventEmitter.defaultMaxListeners;
391cb0ef41Sopenharmony_ciEventEmitter.defaultMaxListeners = 1;
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ciconst e = new EventEmitter();
421cb0ef41Sopenharmony_cie.on('hello', () => {});
431cb0ef41Sopenharmony_cie.on('hello', () => {});
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci// TODO: Figure out how to validate console. Currently,
461cb0ef41Sopenharmony_ci// there is no obvious way of validating that console
471cb0ef41Sopenharmony_ci// exists here exactly when it should.
48