forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
events: add max listener warning for EventTarget
Signed-off-by: James M Snell <[email protected]> PR-URL: nodejs#36001 Fixes: nodejs#35990 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Rich Trott <[email protected]>
- Loading branch information
Showing
4 changed files
with
180 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Flags: --no-warnings --expose-internals --experimental-abortcontroller | ||
'use strict'; | ||
const common = require('../common'); | ||
const { | ||
setMaxListeners, | ||
EventEmitter | ||
} = require('events'); | ||
const assert = require('assert'); | ||
const { MessageChannel } = require('worker_threads'); | ||
const { EventTarget } = require('internal/event_target'); | ||
|
||
common.expectWarning({ | ||
MaxListenersExceededWarning: [ | ||
['Possible EventTarget memory leak detected. 3 foo listeners added to ' + | ||
'EventTarget. Use events.setMaxListeners() ' + | ||
'to increase limit'], | ||
['Possible EventTarget memory leak detected. 3 foo listeners added to ' + | ||
'[MessagePort [EventTarget]]. ' + | ||
'Use events.setMaxListeners() to increase ' + | ||
'limit'], | ||
['Possible EventTarget memory leak detected. 3 foo listeners added to ' + | ||
'[MessagePort [EventTarget]]. ' + | ||
'Use events.setMaxListeners() to increase ' + | ||
'limit'], | ||
['Possible EventTarget memory leak detected. 3 foo listeners added to ' + | ||
'[AbortSignal [EventTarget]]. ' + | ||
'Use events.setMaxListeners() to increase ' + | ||
'limit'], | ||
], | ||
ExperimentalWarning: [[ | ||
'AbortController is an experimental feature. This feature could change ' + | ||
'at any time' | ||
]] | ||
}); | ||
|
||
|
||
{ | ||
const et = new EventTarget(); | ||
setMaxListeners(2, et); | ||
et.addEventListener('foo', () => {}); | ||
et.addEventListener('foo', () => {}); | ||
et.addEventListener('foo', () => {}); | ||
} | ||
|
||
{ | ||
// No warning emitted because prior limit was only for that | ||
// one EventTarget. | ||
const et = new EventTarget(); | ||
et.addEventListener('foo', () => {}); | ||
et.addEventListener('foo', () => {}); | ||
et.addEventListener('foo', () => {}); | ||
} | ||
|
||
{ | ||
const mc = new MessageChannel(); | ||
setMaxListeners(2, mc.port1); | ||
mc.port1.addEventListener('foo', () => {}); | ||
mc.port1.addEventListener('foo', () => {}); | ||
mc.port1.addEventListener('foo', () => {}); | ||
} | ||
|
||
{ | ||
// Set the default for newly created EventTargets | ||
setMaxListeners(2); | ||
const mc = new MessageChannel(); | ||
mc.port1.addEventListener('foo', () => {}); | ||
mc.port1.addEventListener('foo', () => {}); | ||
mc.port1.addEventListener('foo', () => {}); | ||
|
||
const ac = new AbortController(); | ||
ac.signal.addEventListener('foo', () => {}); | ||
ac.signal.addEventListener('foo', () => {}); | ||
ac.signal.addEventListener('foo', () => {}); | ||
} | ||
|
||
{ | ||
// It works for EventEmitters also | ||
const ee = new EventEmitter(); | ||
setMaxListeners(2, ee); | ||
assert.strictEqual(ee.getMaxListeners(), 2); | ||
} |