From 90ecc9baa2321c2195a084140fd52c124b31c770 Mon Sep 17 00:00:00 2001 From: Tony Gorez Date: Mon, 28 Mar 2022 22:15:08 +0200 Subject: [PATCH] test: add trace-gc flag test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/42471 Reviewed-By: James M Snell Reviewed-By: Gireesh Punathil Reviewed-By: Darshan Sen Reviewed-By: Luigi Pinca Reviewed-By: Gerhard Stöbich --- test/fixtures/gc.js | 9 ++++++ test/v8-updates/test-trace-gc-flag.js | 40 +++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 test/fixtures/gc.js create mode 100644 test/v8-updates/test-trace-gc-flag.js diff --git a/test/fixtures/gc.js b/test/fixtures/gc.js new file mode 100644 index 00000000000000..1e965f336e4dea --- /dev/null +++ b/test/fixtures/gc.js @@ -0,0 +1,9 @@ +let arr = new Array(300_000).fill('a'); + +for (let index = 0; index < arr.length; index++) { + arr[index] = Math.random(); +} + +arr = []; +// .gc() is called to generate a Mark-sweep event +global.gc(); diff --git a/test/v8-updates/test-trace-gc-flag.js b/test/v8-updates/test-trace-gc-flag.js new file mode 100644 index 00000000000000..d84e359525127c --- /dev/null +++ b/test/v8-updates/test-trace-gc-flag.js @@ -0,0 +1,40 @@ +'use strict'; + +// This test verifies that `--trace-gc` flag is well integrated. +// We'll check here, that the console outputs gc events properly. +require('../common'); + +const assert = require('assert'); +const { spawnSync } = require('child_process'); + +const fixtures = require('../common/fixtures'); + +{ + const childProcess = spawnSync(process.execPath, [ + '--trace-gc', + '--expose-gc', + fixtures.path('gc.js'), + ]); + const output = childProcess.stdout.toString().trim(); + const lines = splitByLine(output); + + const scavengeRegex = /\bScavenge\b/; + const expectedOutput = [ + scavengeRegex, + scavengeRegex, + scavengeRegex, + scavengeRegex, + /\bMark-sweep\b/, + ]; + lines.forEach((line, index) => { + assert.match(line, expectedOutput[index]); + }); +} + +/** + * HELPERS + */ + +function splitByLine(str) { + return str.split(/\n/); +}