11cb0ef41Sopenharmony_ci// Flags: --expose-gc --no-warnings
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst {
71cb0ef41Sopenharmony_ci  PerformanceObserver,
81cb0ef41Sopenharmony_ci  constants
91cb0ef41Sopenharmony_ci} = require('perf_hooks');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst {
121cb0ef41Sopenharmony_ci  NODE_PERFORMANCE_GC_MAJOR,
131cb0ef41Sopenharmony_ci  NODE_PERFORMANCE_GC_MINOR,
141cb0ef41Sopenharmony_ci  NODE_PERFORMANCE_GC_INCREMENTAL,
151cb0ef41Sopenharmony_ci  NODE_PERFORMANCE_GC_WEAKCB,
161cb0ef41Sopenharmony_ci  NODE_PERFORMANCE_GC_FLAGS_FORCED
171cb0ef41Sopenharmony_ci} = constants;
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciconst kinds = [
201cb0ef41Sopenharmony_ci  NODE_PERFORMANCE_GC_MAJOR,
211cb0ef41Sopenharmony_ci  NODE_PERFORMANCE_GC_MINOR,
221cb0ef41Sopenharmony_ci  NODE_PERFORMANCE_GC_INCREMENTAL,
231cb0ef41Sopenharmony_ci  NODE_PERFORMANCE_GC_WEAKCB,
241cb0ef41Sopenharmony_ci];
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci// Adding an observer should force at least one gc to appear
271cb0ef41Sopenharmony_ci{
281cb0ef41Sopenharmony_ci  const obs = new PerformanceObserver(common.mustCallAtLeast((list) => {
291cb0ef41Sopenharmony_ci    const entry = list.getEntries()[0];
301cb0ef41Sopenharmony_ci    assert(entry);
311cb0ef41Sopenharmony_ci    assert.strictEqual(entry.name, 'gc');
321cb0ef41Sopenharmony_ci    assert.strictEqual(entry.entryType, 'gc');
331cb0ef41Sopenharmony_ci    assert(kinds.includes(entry.kind));
341cb0ef41Sopenharmony_ci    assert(kinds.includes(entry.detail.kind));
351cb0ef41Sopenharmony_ci    assert.strictEqual(entry.flags, NODE_PERFORMANCE_GC_FLAGS_FORCED);
361cb0ef41Sopenharmony_ci    assert.strictEqual(entry.detail.flags, NODE_PERFORMANCE_GC_FLAGS_FORCED);
371cb0ef41Sopenharmony_ci    assert.strictEqual(typeof entry.startTime, 'number');
381cb0ef41Sopenharmony_ci    assert(entry.startTime < 1e4, 'startTime should be relative to performance.timeOrigin.');
391cb0ef41Sopenharmony_ci    assert.strictEqual(typeof entry.duration, 'number');
401cb0ef41Sopenharmony_ci    obs.disconnect();
411cb0ef41Sopenharmony_ci  }));
421cb0ef41Sopenharmony_ci  obs.observe({ entryTypes: ['gc'] });
431cb0ef41Sopenharmony_ci  global.gc();
441cb0ef41Sopenharmony_ci  // Keep the event loop alive to witness the GC async callback happen.
451cb0ef41Sopenharmony_ci  setImmediate(() => setImmediate(() => 0));
461cb0ef41Sopenharmony_ci}
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci// GC should not keep the event loop alive
491cb0ef41Sopenharmony_ci{
501cb0ef41Sopenharmony_ci  let didCall = false;
511cb0ef41Sopenharmony_ci  process.on('beforeExit', () => {
521cb0ef41Sopenharmony_ci    assert(!didCall);
531cb0ef41Sopenharmony_ci    didCall = true;
541cb0ef41Sopenharmony_ci    global.gc();
551cb0ef41Sopenharmony_ci  });
561cb0ef41Sopenharmony_ci}
57