• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23
24const common = require('../common');
25const assert = require('assert');
26const events = require('events');
27
28// default
29{
30  const e = new events.EventEmitter();
31
32  for (let i = 0; i < 10; i++) {
33    e.on('default', common.mustNotCall());
34  }
35  assert.ok(!e._events.default.hasOwnProperty('warned'));
36  e.on('default', common.mustNotCall());
37  assert.ok(e._events.default.warned);
38
39  // symbol
40  const symbol = Symbol('symbol');
41  e.setMaxListeners(1);
42  e.on(symbol, common.mustNotCall());
43  assert.ok(!e._events[symbol].hasOwnProperty('warned'));
44  e.on(symbol, common.mustNotCall());
45  assert.ok(e._events[symbol].hasOwnProperty('warned'));
46
47  // specific
48  e.setMaxListeners(5);
49  for (let i = 0; i < 5; i++) {
50    e.on('specific', common.mustNotCall());
51  }
52  assert.ok(!e._events.specific.hasOwnProperty('warned'));
53  e.on('specific', common.mustNotCall());
54  assert.ok(e._events.specific.warned);
55
56  // only one
57  e.setMaxListeners(1);
58  e.on('only one', common.mustNotCall());
59  assert.ok(!e._events['only one'].hasOwnProperty('warned'));
60  e.on('only one', common.mustNotCall());
61  assert.ok(e._events['only one'].hasOwnProperty('warned'));
62
63  // unlimited
64  e.setMaxListeners(0);
65  for (let i = 0; i < 1000; i++) {
66    e.on('unlimited', common.mustNotCall());
67  }
68  assert.ok(!e._events.unlimited.hasOwnProperty('warned'));
69}
70
71// process-wide
72{
73  events.EventEmitter.defaultMaxListeners = 42;
74  const e = new events.EventEmitter();
75
76  for (let i = 0; i < 42; ++i) {
77    e.on('fortytwo', common.mustNotCall());
78  }
79  assert.ok(!e._events.fortytwo.hasOwnProperty('warned'));
80  e.on('fortytwo', common.mustNotCall());
81  assert.ok(e._events.fortytwo.hasOwnProperty('warned'));
82  delete e._events.fortytwo.warned;
83
84  events.EventEmitter.defaultMaxListeners = 44;
85  e.on('fortytwo', common.mustNotCall());
86  assert.ok(!e._events.fortytwo.hasOwnProperty('warned'));
87  e.on('fortytwo', common.mustNotCall());
88  assert.ok(e._events.fortytwo.hasOwnProperty('warned'));
89}
90
91// But _maxListeners still has precedence over defaultMaxListeners
92{
93  events.EventEmitter.defaultMaxListeners = 42;
94  const e = new events.EventEmitter();
95  e.setMaxListeners(1);
96  e.on('uno', common.mustNotCall());
97  assert.ok(!e._events.uno.hasOwnProperty('warned'));
98  e.on('uno', common.mustNotCall());
99  assert.ok(e._events.uno.hasOwnProperty('warned'));
100
101  // chainable
102  assert.strictEqual(e, e.setMaxListeners(1));
103}
104