|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | + |
| 4 | + |
| 5 | +if (common.isWindows) |
| 6 | + common.skip('test not supported on Windows'); |
| 7 | + |
| 8 | +const assert = require('assert'); |
| 9 | + |
| 10 | +const validateDateStringInHeapSnapshotFile = (fileName) => { |
| 11 | + const currentDate = new Date(); |
| 12 | + const currentYear = currentDate.getFullYear(); |
| 13 | + const currentMonth = (currentDate.getMonth() + 1).toString().padStart(2, '0'); |
| 14 | + const currentDateValue = currentDate.getDate().toString().padStart(2, '0'); |
| 15 | + |
| 16 | + const expectedDateString = `${currentYear}${currentMonth}${currentDateValue}`; |
| 17 | + const fileDateString = fileName.split('.')[1]; |
| 18 | + assert.deepStrictEqual(fileDateString, expectedDateString); |
| 19 | +}; |
| 20 | + |
| 21 | +const validateHeapSnapshotFile = () => { |
| 22 | + const fs = require('fs'); |
| 23 | + |
| 24 | + assert.strictEqual(process.listenerCount('SIGUSR2'), 1); |
| 25 | + process.kill(process.pid, 'SIGUSR2'); |
| 26 | + process.kill(process.pid, 'SIGUSR2'); |
| 27 | + |
| 28 | + // Asynchronously wait for the snapshot. Use an async loop to be a bit more |
| 29 | + // robust in case platform or machine differences throw off the timing. |
| 30 | + (function validate() { |
| 31 | + const files = fs.readdirSync(process.cwd()); |
| 32 | + |
| 33 | + if (files.length === 0) |
| 34 | + return setImmediate(validate); |
| 35 | + |
| 36 | + assert.strictEqual(files.length, 2); |
| 37 | + |
| 38 | + for (let i = 0; i < files.length; i++) { |
| 39 | + assert.match(files[i], /^Heap\..+\.heapsnapshot$/); |
| 40 | + |
| 41 | + // Check the heapsnapshot file contains the date as YYYYMMDD format |
| 42 | + validateDateStringInHeapSnapshotFile(files[i]); |
| 43 | + |
| 44 | + // Check the file is parsable as JSON |
| 45 | + JSON.parse(fs.readFileSync(files[i])); |
| 46 | + } |
| 47 | + })(); |
| 48 | +}; |
| 49 | + |
| 50 | +if (process.argv[2] === 'child') { |
| 51 | + validateHeapSnapshotFile(); |
| 52 | +} else { |
| 53 | + // Modify the timezone. So we can check the file date string still returning correctly. |
| 54 | + process.env.TZ = 'America/New_York'; |
| 55 | + const { spawnSync } = require('child_process'); |
| 56 | + const tmpdir = require('../common/tmpdir'); |
| 57 | + |
| 58 | + tmpdir.refresh(); |
| 59 | + const args = ['--heapsnapshot-signal', 'SIGUSR2', '--diagnostic-dir', tmpdir.path, __filename, 'child']; |
| 60 | + const child = spawnSync(process.execPath, args, { cwd: tmpdir.path }); |
| 61 | + |
| 62 | + assert.strictEqual(child.status, 0); |
| 63 | + assert.strictEqual(child.signal, null); |
| 64 | +} |
0 commit comments