-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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 <jasnell@gmail.com> PR-URL: #36001 Fixes: #35990 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
- Loading branch information
1 parent
eb9295b
commit 9ce9b01
Showing
4 changed files
with
178 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,79 @@ | ||
// Flags: --no-warnings | ||
'use strict'; | ||
const common = require('../common'); | ||
const { | ||
setMaxListeners, | ||
EventEmitter | ||
} = require('events'); | ||
const assert = require('assert'); | ||
|
||
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); | ||
} |