Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 45 additions & 46 deletions test/report/test-report-worker.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,48 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const { Worker } = require('worker_threads');
const { once } = require('events');
const helper = require('../common/report');

async function basic() {
// Test that the report includes basic information about Worker threads.

const w = new Worker(`
const { parentPort } = require('worker_threads');
parentPort.once('message', () => {
/* Wait for message to stop the Worker */
});
`, { eval: true });

await once(w, 'online');

const report = process.report.getReport();
helper.validateContent(report);
assert.strictEqual(report.workers.length, 1);
helper.validateContent(report.workers[0]);
assert.strictEqual(report.workers[0].header.threadId, w.threadId);

w.postMessage({});

await once(w, 'exit');
}
require('../common');

async function interruptingJS() {
// Test that the report also works when Worker threads are busy in JS land.

const w = new Worker('while (true);', { eval: true });

await once(w, 'online');

const report = process.report.getReport();
helper.validateContent(report);
assert.strictEqual(report.workers.length, 1);
helper.validateContent(report.workers[0]);

await w.terminate();
}

(async function() {
await basic();
await interruptingJS();
})().then(common.mustCall());
const helper = require('../common/report');
const assert = require('node:assert');
const { test } = require('node:test');
const { Worker } = require('node:worker_threads');
const { once } = require('node:events');

test('Worker threads report basic information', async (t) => {
await t.test('should include basic information about Worker threads', async () => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Could we also use describe/it to avoid using await t.test?

Not entirely sure about this: could we set concurrency: true, or is sequentiality needed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I followed what you said, thank you, the test is more readable

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope I understood correctly, when I used --test-concurrency I saw that the tests were successfully synchronized

const w = new Worker(`
const { parentPort } = require('worker_threads');
parentPort.once('message', () => {
/* Wait for message to stop the Worker */
});
`, { eval: true });

await once(w, 'online');

const report = process.report.getReport();
helper.validateContent(report);
const workerLengthMessage = 'Report should include one Worker';
const threadIdMessage = 'Thread ID should match the Worker thread ID';
assert.strictEqual(report.workers.length, 1, workerLengthMessage);
helper.validateContent(report.workers[0]);
assert.strictEqual(report.workers[0].header.threadId, w.threadId, threadIdMessage);

w.postMessage({});

await once(w, 'exit');
});

await t.test('should generate report when Workers are busy in JS land', async () => {
const w = new Worker('while (true);', { eval: true });

await once(w, 'online');

const report = process.report.getReport();
helper.validateContent(report);
const workerLengthMessage = 'Report should include one Worker';
assert.strictEqual(report.workers.length, 1, workerLengthMessage);
helper.validateContent(report.workers[0]);

await w.terminate();
});
});