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 | var common = require('./common');
|
---|
23 | var assert = require('assert');
|
---|
24 | var events = require('../');
|
---|
25 |
|
---|
26 | // Redirect warning output to tape.
|
---|
27 | var consoleWarn = console.warn;
|
---|
28 | console.warn = common.test.comment;
|
---|
29 |
|
---|
30 | common.test.on('end', function () {
|
---|
31 | console.warn = consoleWarn;
|
---|
32 | });
|
---|
33 |
|
---|
34 | // default
|
---|
35 | {
|
---|
36 | var e = new events.EventEmitter();
|
---|
37 |
|
---|
38 | for (var i = 0; i < 10; i++) {
|
---|
39 | e.on('default', common.mustNotCall());
|
---|
40 | }
|
---|
41 | assert.ok(!e._events['default'].hasOwnProperty('warned'));
|
---|
42 | e.on('default', common.mustNotCall());
|
---|
43 | assert.ok(e._events['default'].warned);
|
---|
44 |
|
---|
45 | // specific
|
---|
46 | e.setMaxListeners(5);
|
---|
47 | for (var i = 0; i < 5; i++) {
|
---|
48 | e.on('specific', common.mustNotCall());
|
---|
49 | }
|
---|
50 | assert.ok(!e._events['specific'].hasOwnProperty('warned'));
|
---|
51 | e.on('specific', common.mustNotCall());
|
---|
52 | assert.ok(e._events['specific'].warned);
|
---|
53 |
|
---|
54 | // only one
|
---|
55 | e.setMaxListeners(1);
|
---|
56 | e.on('only one', common.mustNotCall());
|
---|
57 | assert.ok(!e._events['only one'].hasOwnProperty('warned'));
|
---|
58 | e.on('only one', common.mustNotCall());
|
---|
59 | assert.ok(e._events['only one'].hasOwnProperty('warned'));
|
---|
60 |
|
---|
61 | // unlimited
|
---|
62 | e.setMaxListeners(0);
|
---|
63 | for (var i = 0; i < 1000; i++) {
|
---|
64 | e.on('unlimited', common.mustNotCall());
|
---|
65 | }
|
---|
66 | assert.ok(!e._events['unlimited'].hasOwnProperty('warned'));
|
---|
67 | }
|
---|
68 |
|
---|
69 | // process-wide
|
---|
70 | {
|
---|
71 | events.EventEmitter.defaultMaxListeners = 42;
|
---|
72 | var e = new events.EventEmitter();
|
---|
73 |
|
---|
74 | for (var i = 0; i < 42; ++i) {
|
---|
75 | e.on('fortytwo', common.mustNotCall());
|
---|
76 | }
|
---|
77 | assert.ok(!e._events['fortytwo'].hasOwnProperty('warned'));
|
---|
78 | e.on('fortytwo', common.mustNotCall());
|
---|
79 | assert.ok(e._events['fortytwo'].hasOwnProperty('warned'));
|
---|
80 | delete e._events['fortytwo'].warned;
|
---|
81 |
|
---|
82 | events.EventEmitter.defaultMaxListeners = 44;
|
---|
83 | e.on('fortytwo', common.mustNotCall());
|
---|
84 | assert.ok(!e._events['fortytwo'].hasOwnProperty('warned'));
|
---|
85 | e.on('fortytwo', common.mustNotCall());
|
---|
86 | assert.ok(e._events['fortytwo'].hasOwnProperty('warned'));
|
---|
87 | }
|
---|
88 |
|
---|
89 | // but _maxListeners still has precedence over defaultMaxListeners
|
---|
90 | {
|
---|
91 | events.EventEmitter.defaultMaxListeners = 42;
|
---|
92 | var e = new events.EventEmitter();
|
---|
93 | e.setMaxListeners(1);
|
---|
94 | e.on('uno', common.mustNotCall());
|
---|
95 | assert.ok(!e._events['uno'].hasOwnProperty('warned'));
|
---|
96 | e.on('uno', common.mustNotCall());
|
---|
97 | assert.ok(e._events['uno'].hasOwnProperty('warned'));
|
---|
98 |
|
---|
99 | // chainable
|
---|
100 | assert.strictEqual(e, e.setMaxListeners(1));
|
---|
101 | }
|
---|