• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* eslint-disable node-core/require-common-first, node-core/required-modules */
2
3'use strict';
4
5// Ordinarily test files must require('common') but that action causes
6// the global console to be compiled, defeating the purpose of this test.
7
8const assert = require('assert');
9const EventEmitter = require('events');
10const leakWarning = /EventEmitter memory leak detected\. 2 hello listeners/;
11
12let writeTimes = 0;
13let warningTimes = 0;
14process.on('warning', () => {
15  // This will be called after the default internal
16  // process warning handler is called. The default
17  // process warning writes to the console, which will
18  // invoke the monkeypatched process.stderr.write
19  // below.
20  assert.strictEqual(writeTimes, 1);
21  EventEmitter.defaultMaxListeners = oldDefault;
22  warningTimes++;
23});
24
25process.on('exit', () => {
26  assert.strictEqual(warningTimes, 1);
27});
28
29process.stderr.write = (data) => {
30  if (writeTimes === 0)
31    assert.match(data, leakWarning);
32  else
33    assert.fail('stderr.write should be called only once');
34
35  writeTimes++;
36};
37
38const oldDefault = EventEmitter.defaultMaxListeners;
39EventEmitter.defaultMaxListeners = 1;
40
41const e = new EventEmitter();
42e.on('hello', () => {});
43e.on('hello', () => {});
44
45// TODO: Figure out how to validate console. Currently,
46// there is no obvious way of validating that console
47// exists here exactly when it should.
48