Skip to content

Commit fbd8c9f

Browse files
committed
test: improve test-cluster-net-send.js
This commit improves test-cluster-net-send as below - change function to arrow function - ensure data event is emitted exactly once
1 parent 4e259b2 commit fbd8c9f

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

test/parallel/test-cluster-net-send.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,22 @@ if (process.argv[2] !== 'child') {
88
console.error('[%d] master', process.pid);
99

1010
const worker = fork(__filename, ['child']);
11-
let called = false;
11+
let result = '';
1212

13-
worker.once('message', common.mustCall(function(msg, handle) {
13+
worker.once('message', common.mustCall((msg, handle) => {
1414
assert.strictEqual(msg, 'handle');
1515
assert.ok(handle);
1616
worker.send('got');
1717

18-
handle.on('data', function(data) {
19-
called = true;
20-
assert.strictEqual(data.toString(), 'hello');
18+
handle.on('data', (data) => {
19+
result += data.toString();
2120
});
2221

23-
handle.on('end', function() {
22+
handle.on('end', () => {
23+
assert.strictEqual(result, 'hello');
2424
worker.kill();
2525
});
2626
}));
27-
28-
process.once('exit', function() {
29-
assert.ok(called);
30-
});
3127
} else {
3228
console.error('[%d] worker', process.pid);
3329

@@ -38,20 +34,20 @@ if (process.argv[2] !== 'child') {
3834
process.send('handle', socket);
3935
}
4036

41-
const server = net.createServer(function(c) {
42-
process.once('message', common.mustCall(function(msg) {
37+
const server = net.createServer((c) => {
38+
process.once('message', common.mustCall((msg) => {
4339
assert.strictEqual(msg, 'got');
4440
c.end('hello');
4541
}));
4642
socketConnected();
4743
});
4844

49-
server.listen(common.PORT, function() {
50-
socket = net.connect(common.PORT, '127.0.0.1', socketConnected);
45+
server.listen(common.PORT, () => {
46+
socket = net.connect(server.address().port, server.address().address,
47+
socketConnected);
5148
});
5249

53-
process.on('disconnect', function() {
54-
process.exit();
50+
process.on('disconnect', () => {
5551
server.close();
5652
});
5753
}

0 commit comments

Comments
 (0)