88c1a07698c2f4c1ee14b5e6d981695467925bb8cef894a98f5861618e72fe3f3746fa854db10fd2754d3adcd6cfb2930dfbfc5a0ff4f090b838f1165f6e15 3.4 KB

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