11cb0ef41Sopenharmony_ci// Flags: --expose-internals --no-warnings --expose-gc
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst {
51cb0ef41Sopenharmony_ci  setMaxListeners,
61cb0ef41Sopenharmony_ci  EventEmitter,
71cb0ef41Sopenharmony_ci} = require('events');
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst { kWeakHandler } = require('internal/event_target');
101cb0ef41Sopenharmony_ciconst { setTimeout } = require('timers/promises');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_cicommon.expectWarning({
131cb0ef41Sopenharmony_ci  MaxListenersExceededWarning: [
141cb0ef41Sopenharmony_ci    ['Possible EventTarget memory leak detected. 3 foo listeners added to ' +
151cb0ef41Sopenharmony_ci     'EventTarget. Use events.setMaxListeners() ' +
161cb0ef41Sopenharmony_ci     'to increase limit'],
171cb0ef41Sopenharmony_ci    ['Possible EventTarget memory leak detected. 3 foo listeners added to ' +
181cb0ef41Sopenharmony_ci     '[MessagePort [EventTarget]]. ' +
191cb0ef41Sopenharmony_ci     'Use events.setMaxListeners() to increase ' +
201cb0ef41Sopenharmony_ci     'limit'],
211cb0ef41Sopenharmony_ci    ['Possible EventTarget memory leak detected. 3 foo listeners added to ' +
221cb0ef41Sopenharmony_ci     '[MessagePort [EventTarget]]. ' +
231cb0ef41Sopenharmony_ci     'Use events.setMaxListeners() to increase ' +
241cb0ef41Sopenharmony_ci     'limit'],
251cb0ef41Sopenharmony_ci    ['Possible EventTarget memory leak detected. 3 foo listeners added to ' +
261cb0ef41Sopenharmony_ci     '[AbortSignal]. ' +
271cb0ef41Sopenharmony_ci     'Use events.setMaxListeners() to increase ' +
281cb0ef41Sopenharmony_ci     'limit'],
291cb0ef41Sopenharmony_ci  ],
301cb0ef41Sopenharmony_ci});
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci{
341cb0ef41Sopenharmony_ci  const et = new EventTarget();
351cb0ef41Sopenharmony_ci  setMaxListeners(2, et);
361cb0ef41Sopenharmony_ci  et.addEventListener('foo', () => {});
371cb0ef41Sopenharmony_ci  et.addEventListener('foo', () => {});
381cb0ef41Sopenharmony_ci  et.addEventListener('foo', () => {});
391cb0ef41Sopenharmony_ci}
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci{
421cb0ef41Sopenharmony_ci  // No warning emitted because prior limit was only for that
431cb0ef41Sopenharmony_ci  // one EventTarget.
441cb0ef41Sopenharmony_ci  const et = new EventTarget();
451cb0ef41Sopenharmony_ci  et.addEventListener('foo', () => {});
461cb0ef41Sopenharmony_ci  et.addEventListener('foo', () => {});
471cb0ef41Sopenharmony_ci  et.addEventListener('foo', () => {});
481cb0ef41Sopenharmony_ci}
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci{
511cb0ef41Sopenharmony_ci  const mc = new MessageChannel();
521cb0ef41Sopenharmony_ci  setMaxListeners(2, mc.port1);
531cb0ef41Sopenharmony_ci  mc.port1.addEventListener('foo', () => {});
541cb0ef41Sopenharmony_ci  mc.port1.addEventListener('foo', () => {});
551cb0ef41Sopenharmony_ci  mc.port1.addEventListener('foo', () => {});
561cb0ef41Sopenharmony_ci}
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci{
591cb0ef41Sopenharmony_ci  // Set the default for newly created EventTargets
601cb0ef41Sopenharmony_ci  setMaxListeners(2);
611cb0ef41Sopenharmony_ci  const mc = new MessageChannel();
621cb0ef41Sopenharmony_ci  mc.port1.addEventListener('foo', () => {});
631cb0ef41Sopenharmony_ci  mc.port1.addEventListener('foo', () => {});
641cb0ef41Sopenharmony_ci  mc.port1.addEventListener('foo', () => {});
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  const ac = new AbortController();
671cb0ef41Sopenharmony_ci  ac.signal.addEventListener('foo', () => {});
681cb0ef41Sopenharmony_ci  ac.signal.addEventListener('foo', () => {});
691cb0ef41Sopenharmony_ci  ac.signal.addEventListener('foo', () => {});
701cb0ef41Sopenharmony_ci}
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci{
731cb0ef41Sopenharmony_ci  // It works for EventEmitters also
741cb0ef41Sopenharmony_ci  const ee = new EventEmitter();
751cb0ef41Sopenharmony_ci  setMaxListeners(2, ee);
761cb0ef41Sopenharmony_ci  assert.strictEqual(ee.getMaxListeners(), 2);
771cb0ef41Sopenharmony_ci}
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci{
801cb0ef41Sopenharmony_ci  (async () => {
811cb0ef41Sopenharmony_ci    // Test that EventTarget listener don't emit MaxListenersExceededWarning for weak listeners that GCed
821cb0ef41Sopenharmony_ci    const et = new EventTarget();
831cb0ef41Sopenharmony_ci    setMaxListeners(2, et);
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci    for (let i = 0; i <= 3; i++) {
861cb0ef41Sopenharmony_ci      et.addEventListener('foo', () => {}, {
871cb0ef41Sopenharmony_ci        [kWeakHandler]: {},
881cb0ef41Sopenharmony_ci      });
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci      await setTimeout(0);
911cb0ef41Sopenharmony_ci      global.gc();
921cb0ef41Sopenharmony_ci    }
931cb0ef41Sopenharmony_ci  })().then(common.mustCall(), common.mustNotCall());
941cb0ef41Sopenharmony_ci}
95