Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix parallel/test-cluster-primary-error.js #44066

Closed
Closed
Changes from all commits
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
27 changes: 21 additions & 6 deletions test/parallel/test-cluster-primary-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@
const common = require('../common');
const assert = require('assert');
const cluster = require('cluster');
const fs = require('fs');
const http = require('http');

const totalWorkers = 2;

// Cluster setup
if (cluster.isWorker) {
const http = require('http');
const filepath = `${process.env.filedir}/${process.pid}-test-cluster-primary-error`;
fs.writeFileSync(filepath, 'hello');
process.on('exit', () => {
fs.unlinkSync(filepath);
});
http.Server(() => {}).listen(0, '127.0.0.1');
} else if (process.argv[2] === 'cluster') {
// Send PID to testcase process
Expand Down Expand Up @@ -66,14 +72,20 @@ if (cluster.isWorker) {
cluster.fork();
} else {
// This is the testcase

const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
// Make sure the child process have write permission
fs.chmodSync(tmpdir.path, 0o777);
const fork = require('child_process').fork;

// List all workers
const workers = [];

// Spawn a cluster process
const primary = fork(process.argv[1], ['cluster'], { silent: true });
const primary = fork(process.argv[1], ['cluster'], {
silent: true,
env: { filedir: tmpdir.path }
});

// Handle messages from the cluster
primary.on('message', common.mustCall((data) => {
Expand All @@ -87,13 +99,16 @@ if (cluster.isWorker) {
primary.on('exit', common.mustCall((code) => {
// Check that the cluster died accidentally (non-zero exit code)
assert.strictEqual(code, 1);

// XXX(addaleax): The fact that this uses raw PIDs makes the test inherently
// flaky – another process might end up being started right after the
// workers finished and receive the same PID.
// workers finished and receive the same PID. So we also need to determine
// whether the file created by the process still exists
const pollWorkers = () => {
// When primary is dead all workers should be dead too
if (workers.some((pid) => common.isAlive(pid))) {
if (workers.some((pid) => {
const filepath = `${tmpdir.path}/${pid}-test-cluster-primary-error`;
return common.isAlive(pid) && fs.existsSync(filepath);
})) {
setTimeout(pollWorkers, 50);
}
};
Expand Down