|
7 | 7 |
|
8 | 8 | const common = require('../common'); |
9 | 9 | const assert = require('assert'); |
| 10 | +const { gcUntil } = require('../common/gc'); |
10 | 11 | const { |
11 | 12 | PerformanceObserver, |
12 | 13 | constants |
13 | 14 | } = require('perf_hooks'); |
14 | 15 |
|
15 | 16 | const { |
16 | | - NODE_PERFORMANCE_GC_MINOR_MARK_SWEEP, |
17 | | - NODE_PERFORMANCE_GC_FLAGS_FORCED |
| 17 | + NODE_PERFORMANCE_GC_MINOR_MARK_SWEEP |
18 | 18 | } = constants; |
19 | 19 |
|
| 20 | +let observed = false; |
20 | 21 | const obs = new PerformanceObserver(common.mustCallAtLeast((list) => { |
21 | | - const entry = list.getEntries()[0]; |
22 | | - assert(entry); |
23 | | - assert.strictEqual(entry.name, 'gc'); |
24 | | - assert.strictEqual(entry.entryType, 'gc'); |
25 | | - assert.strictEqual(entry.detail.kind, NODE_PERFORMANCE_GC_MINOR_MARK_SWEEP); |
26 | | - assert.strictEqual(entry.detail.flags, NODE_PERFORMANCE_GC_FLAGS_FORCED); |
27 | | - obs.disconnect(); |
| 22 | + for (const entry of list.getEntries()) { |
| 23 | + // Other GC kinds (e.g. a scheduled or incremental GC) may be observed in |
| 24 | + // between; only the minor mark-sweep entry is relevant here. |
| 25 | + if (entry.detail.kind === NODE_PERFORMANCE_GC_MINOR_MARK_SWEEP) { |
| 26 | + assert.strictEqual(entry.name, 'gc'); |
| 27 | + assert.strictEqual(entry.entryType, 'gc'); |
| 28 | + observed = true; |
| 29 | + obs.disconnect(); |
| 30 | + return; |
| 31 | + } |
| 32 | + } |
28 | 33 | })); |
29 | 34 | obs.observe({ entryTypes: ['gc'] }); |
30 | 35 |
|
31 | | -globalThis.gc({ type: 'minor' }); |
32 | | -// Keep the event loop alive to witness the GC async callback happen. |
33 | | -setImmediate(() => setImmediate(() => 0)); |
| 36 | +gcUntil('minor gc event', () => observed, 10, { type: 'minor' }); |
0 commit comments