From 7639390032a17cd3c8101962dee822beefecdb45 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 18 Sep 2018 13:12:17 +0200 Subject: [PATCH] worker,coverage: support V8 coverage generation PR-URL: https://github.com/nodejs/node/pull/22928 Reviewed-By: Colin Ihrig Reviewed-By: Ruben Bridgewater Reviewed-By: Luigi Pinca Reviewed-By: James M Snell --- lib/internal/process/coverage.js | 8 ++++---- test/fixtures/v8-coverage/worker.js | 5 +++++ test/parallel/test-v8-coverage.js | 17 +++++++++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/v8-coverage/worker.js diff --git a/lib/internal/process/coverage.js b/lib/internal/process/coverage.js index c2349d4ce0e24e..df45285baaee02 100644 --- a/lib/internal/process/coverage.js +++ b/lib/internal/process/coverage.js @@ -1,9 +1,7 @@ 'use strict'; const path = require('path'); const { mkdirSync, writeFileSync } = require('fs'); -// TODO(addaleax): add support for coverage to worker threads. -const hasInspector = process.config.variables.v8_enable_inspector === 1 && - require('internal/worker').isMainThread; +const hasInspector = process.config.variables.v8_enable_inspector === 1; let inspector = null; if (hasInspector) inspector = require('inspector'); @@ -14,7 +12,9 @@ function writeCoverage() { return; } - const filename = `coverage-${process.pid}-${Date.now()}.json`; + const { threadId } = require('internal/worker'); + + const filename = `coverage-${process.pid}-${Date.now()}-${threadId}.json`; try { // TODO(bcoe): switch to mkdirp once #22302 is addressed. mkdirSync(process.env.NODE_V8_COVERAGE); diff --git a/test/fixtures/v8-coverage/worker.js b/test/fixtures/v8-coverage/worker.js new file mode 100644 index 00000000000000..901794e97ac8ab --- /dev/null +++ b/test/fixtures/v8-coverage/worker.js @@ -0,0 +1,5 @@ +'use strict'; +const { Worker } = require('worker_threads'); +const path = require('path'); + +new Worker(path.resolve(__dirname, 'subprocess.js')); diff --git a/test/parallel/test-v8-coverage.js b/test/parallel/test-v8-coverage.js index 91581a5a073460..16a55b0fc4b8e8 100644 --- a/test/parallel/test-v8-coverage.js +++ b/test/parallel/test-v8-coverage.js @@ -79,6 +79,23 @@ function nextdir() { assert.strictEqual(fixtureCoverage.functions[2].ranges[1].count, 0); } +// outputs coverage from worker. +{ + const coverageDirectory = path.join(tmpdir.path, nextdir()); + const output = spawnSync(process.execPath, [ + '--experimental-worker', + require.resolve('../fixtures/v8-coverage/worker') + ], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } }); + assert.strictEqual(output.status, 0); + const fixtureCoverage = getFixtureCoverage('subprocess.js', + coverageDirectory); + assert.ok(fixtureCoverage); + // first branch executed. + assert.strictEqual(fixtureCoverage.functions[2].ranges[0].count, 1); + // second branch did not execute. + assert.strictEqual(fixtureCoverage.functions[2].ranges[1].count, 0); +} + // does not output coverage if NODE_V8_COVERAGE is empty. { const coverageDirectory = path.join(tmpdir.path, nextdir());