Skip to content

Commit 724e7e1

Browse files
refackaddaleax
authored andcommitted
test: make common.PIPE process unique
* includes a tiny bit of refactoring in adjacent lines. * fixes 1 test and 1 benchmark that depended on PIPE being constant. PR-URL: #14168 Fixes: #14128 Reviewed-By: James M Snell <[email protected]>
1 parent dfc46e2 commit 724e7e1

File tree

5 files changed

+45
-86
lines changed

5 files changed

+45
-86
lines changed

benchmark/http/_chunky_http_client.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// test HTTP throughput in fragmented header case
44
var common = require('../common.js');
55
var net = require('net');
6-
var test = require('../../test/common');
76

87
var bench = common.createBenchmark(main, {
98
len: [1, 4, 8, 16, 32, 64, 128],
@@ -56,7 +55,7 @@ function main(conf) {
5655
var mult = 17;
5756
var add = 11;
5857
var count = 0;
59-
var PIPE = test.PIPE;
58+
var PIPE = process.env.PIPE_NAME;
6059
var socket = net.connect(PIPE, function() {
6160
bench.start();
6261
WriteHTTPHeaders(socket, 1, len);

benchmark/http/http_server_for_chunky_client.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
'use strict';
22

33
var assert = require('assert');
4-
var path = require('path');
54
var http = require('http');
65
var fs = require('fs');
7-
var fork = require('child_process').fork;
6+
var { fork } = require('child_process');
87
var common = require('../common.js');
9-
var test = require('../../test/common');
10-
var pep = `${path.dirname(process.argv[1])}/_chunky_http_client.js`;
11-
var PIPE = test.PIPE;
8+
const { PIPE, tmpDir } = require('../../test/common');
9+
process.env.PIPE_NAME = PIPE;
1210

1311
try {
14-
fs.accessSync(test.tmpDir, fs.F_OK);
12+
fs.accessSync(tmpDir, fs.F_OK);
1513
} catch (e) {
16-
fs.mkdirSync(test.tmpDir);
14+
fs.mkdirSync(tmpDir);
1715
}
1816

1917
var server;
2018
try {
21-
fs.unlinkSync(PIPE);
19+
fs.unlinkSync(process.env.PIPE_NAME);
2220
} catch (e) { /* ignore */ }
2321

2422
server = http.createServer(function(req, res) {
@@ -33,10 +31,12 @@ server = http.createServer(function(req, res) {
3331
server.on('error', function(err) {
3432
throw new Error(`server error: ${err}`);
3533
});
36-
3734
server.listen(PIPE);
3835

39-
var child = fork(pep, process.argv.slice(2));
36+
const child = fork(
37+
`${__dirname}/_chunky_http_client.js`,
38+
process.argv.slice(2)
39+
);
4040
child.on('message', common.sendResult);
4141
child.on('close', function(code) {
4242
server.close();

test/common/index.js

+13-14
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ Object.defineProperty(exports, 'opensslCli', {get: function() {
246246

247247
Object.defineProperty(exports, 'hasCrypto', {
248248
get: function() {
249-
return process.versions.openssl ? true : false;
249+
return Boolean(process.versions.openssl);
250250
}
251251
});
252252

@@ -256,22 +256,21 @@ Object.defineProperty(exports, 'hasFipsCrypto', {
256256
}
257257
});
258258

259-
if (exports.isWindows) {
260-
exports.PIPE = '\\\\.\\pipe\\libuv-test';
261-
if (process.env.TEST_THREAD_ID) {
262-
exports.PIPE += `.${process.env.TEST_THREAD_ID}`;
263-
}
264-
} else {
265-
exports.PIPE = `${exports.tmpDir}/test.sock`;
259+
{
260+
const pipePrefix = exports.isWindows ? '\\\\.\\pipe\\' : `${exports.tmpDir}/`;
261+
const pipeName = `node-test.${process.pid}.sock`;
262+
exports.PIPE = pipePrefix + pipeName;
266263
}
267264

268-
const ifaces = os.networkInterfaces();
269-
const re = /lo/;
270-
exports.hasIPv6 = Object.keys(ifaces).some(function(name) {
271-
return re.test(name) && ifaces[name].some(function(info) {
272-
return info.family === 'IPv6';
265+
{
266+
const iFaces = os.networkInterfaces();
267+
const re = /lo/;
268+
exports.hasIPv6 = Object.keys(iFaces).some(function(name) {
269+
return re.test(name) && iFaces[name].some(function(info) {
270+
return info.family === 'IPv6';
271+
});
273272
});
274-
});
273+
}
275274

276275
/*
277276
* Check that when running a test with

test/fixtures/listen-on-socket-and-exit.js

-39
This file was deleted.

test/parallel/test-cluster-eaccess.js

+21-21
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,22 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23+
const common = require('../common');
24+
2325
// Test that errors propagated from cluster workers are properly
2426
// received in their master. Creates an EADDRINUSE condition by forking
2527
// a process in child cluster and propagates the error to the master.
2628

27-
const common = require('../common');
2829
const assert = require('assert');
2930
const cluster = require('cluster');
3031
const fork = require('child_process').fork;
31-
const fs = require('fs');
3232
const net = require('net');
3333

34-
if (cluster.isMaster) {
35-
const worker = cluster.fork();
34+
if (cluster.isMaster && process.argv.length !== 3) {
35+
// cluster.isMaster
36+
common.refreshTmpDir();
37+
const PIPE_NAME = common.PIPE;
38+
const worker = cluster.fork({PIPE_NAME});
3639

3740
// makes sure master is able to fork the worker
3841
cluster.on('fork', common.mustCall());
@@ -43,40 +46,37 @@ if (cluster.isMaster) {
4346
worker.on('message', common.mustCall(function(err) {
4447
// disconnect first, so that we will not leave zombies
4548
worker.disconnect();
46-
47-
console.log(err);
4849
assert.strictEqual('EADDRINUSE', err.code);
4950
}));
50-
51-
process.on('exit', function() {
52-
console.log('master exited');
53-
try {
54-
fs.unlinkSync(common.PIPE);
55-
} catch (e) {
56-
}
57-
});
58-
59-
} else {
60-
common.refreshTmpDir();
61-
const cp = fork(`${common.fixturesDir}/listen-on-socket-and-exit.js`,
62-
{ stdio: 'inherit' });
51+
} else if (process.argv.length !== 3) {
52+
// cluster.worker
53+
const PIPE_NAME = process.env.PIPE_NAME;
54+
const cp = fork(__filename, [PIPE_NAME], { stdio: 'inherit' });
6355

6456
// message from the child indicates it's ready and listening
6557
cp.on('message', common.mustCall(function() {
66-
const server = net.createServer().listen(common.PIPE, function() {
58+
const server = net.createServer().listen(PIPE_NAME, function() {
6759
// message child process so that it can exit
6860
cp.send('end');
6961
// inform master about the unexpected situation
7062
process.send('PIPE should have been in use.');
7163
});
7264

7365
server.on('error', function(err) {
74-
console.log('parent error, ending');
7566
// message to child process tells it to exit
7667
cp.send('end');
7768
// propagate error to parent
7869
process.send(err);
7970
});
71+
}));
72+
} else if (process.argv.length === 3) {
73+
// child process (of cluster.worker)
74+
const PIPE_NAME = process.argv[2];
8075

76+
const server = net.createServer().listen(PIPE_NAME, common.mustCall(() => {
77+
process.send('listening');
8178
}));
79+
process.once('message', common.mustCall(() => server.close()));
80+
} else {
81+
assert.fail('Impossible state');
8282
}

0 commit comments

Comments
 (0)