|
4 | 4 | * sending a fd representing a UDP socket to the child and sending messages
|
5 | 5 | * to this endpoint, these messages are distributed to the parent and the
|
6 | 6 | * child process.
|
7 |
| - * |
8 |
| - * Because it's not really possible to predict how the messages will be |
9 |
| - * distributed among the parent and the child processes, we keep sending |
10 |
| - * messages until both the parent and the child received at least one |
11 |
| - * message. The worst case scenario is when either one never receives |
12 |
| - * a message. In this case the test runner will timeout after 60 secs |
13 |
| - * and the test will fail. |
14 | 7 | */
|
15 | 8 |
|
16 |
| -var dgram = require('dgram'); |
17 |
| -var fork = require('child_process').fork; |
18 |
| -var assert = require('assert'); |
19 |
| -var common = require('../common'); |
| 9 | +const common = require('../common'); |
| 10 | +const dgram = require('dgram'); |
| 11 | +const fork = require('child_process').fork; |
| 12 | +const assert = require('assert'); |
20 | 13 |
|
21 | 14 | if (common.isWindows) {
|
22 |
| - common.skip('Sending dgram sockets to child processes is ' + |
23 |
| - 'not supported'); |
| 15 | + common.skip('Sending dgram sockets to child processes is not supported'); |
24 | 16 | return;
|
25 | 17 | }
|
26 | 18 |
|
27 |
| -var server; |
28 | 19 | if (process.argv[2] === 'child') {
|
29 |
| - process.on('message', function removeMe(msg, clusterServer) { |
30 |
| - if (msg === 'server') { |
31 |
| - server = clusterServer; |
32 |
| - |
33 |
| - server.on('message', function() { |
34 |
| - process.send('gotMessage'); |
35 |
| - }); |
36 |
| - |
37 |
| - } else if (msg === 'stop') { |
38 |
| - server.close(); |
39 |
| - process.removeListener('message', removeMe); |
40 |
| - } |
| 20 | + let childServer; |
| 21 | + |
| 22 | + process.once('message', function(msg, clusterServer) { |
| 23 | + childServer = clusterServer; |
| 24 | + |
| 25 | + childServer.once('message', function() { |
| 26 | + process.send('gotMessage'); |
| 27 | + childServer.close(); |
| 28 | + }); |
| 29 | + |
| 30 | + process.send('handleReceived'); |
41 | 31 | });
|
42 | 32 |
|
43 | 33 | } else {
|
44 |
| - server = dgram.createSocket('udp4'); |
45 |
| - var client = dgram.createSocket('udp4'); |
46 |
| - var child = fork(__filename, ['child']); |
| 34 | + const parentServer = dgram.createSocket('udp4'); |
| 35 | + const client = dgram.createSocket('udp4'); |
| 36 | + const child = fork(__filename, ['child']); |
47 | 37 |
|
48 |
| - var msg = new Buffer('Some bytes'); |
| 38 | + const msg = Buffer.from('Some bytes'); |
49 | 39 |
|
50 | 40 | var childGotMessage = false;
|
51 | 41 | var parentGotMessage = false;
|
52 | 42 |
|
53 |
| - server.on('message', function(msg, rinfo) { |
| 43 | + parentServer.once('message', function(msg, rinfo) { |
54 | 44 | parentGotMessage = true;
|
| 45 | + parentServer.close(); |
55 | 46 | });
|
56 | 47 |
|
57 |
| - server.on('listening', function() { |
58 |
| - child.send('server', server); |
| 48 | + parentServer.on('listening', function() { |
| 49 | + child.send('server', parentServer); |
59 | 50 |
|
60 |
| - child.once('message', function(msg) { |
| 51 | + child.on('message', function(msg) { |
61 | 52 | if (msg === 'gotMessage') {
|
62 | 53 | childGotMessage = true;
|
| 54 | + } else if (msg = 'handlReceived') { |
| 55 | + sendMessages(); |
63 | 56 | }
|
64 | 57 | });
|
65 |
| - |
66 |
| - sendMessages(); |
67 | 58 | });
|
68 | 59 |
|
69 |
| - var sendMessages = function() { |
70 |
| - var timer = setInterval(function() { |
71 |
| - client.send( |
72 |
| - msg, |
73 |
| - 0, |
74 |
| - msg.length, |
75 |
| - server.address().port, |
76 |
| - '127.0.0.1', |
77 |
| - function(err) { |
78 |
| - if (err) throw err; |
79 |
| - } |
80 |
| - ); |
| 60 | + const sendMessages = function() { |
| 61 | + const serverPort = parentServer.address().port; |
81 | 62 |
|
| 63 | + const timer = setInterval(function() { |
82 | 64 | /*
|
83 | 65 | * Both the parent and the child got at least one message,
|
84 | 66 | * test passed, clean up everyting.
|
85 | 67 | */
|
86 | 68 | if (parentGotMessage && childGotMessage) {
|
87 | 69 | clearInterval(timer);
|
88 |
| - shutdown(); |
| 70 | + client.close(); |
| 71 | + } else { |
| 72 | + client.send( |
| 73 | + msg, |
| 74 | + 0, |
| 75 | + msg.length, |
| 76 | + serverPort, |
| 77 | + '127.0.0.1', |
| 78 | + function(err) { |
| 79 | + if (err) throw err; |
| 80 | + } |
| 81 | + ); |
89 | 82 | }
|
90 |
| - |
91 | 83 | }, 1);
|
92 | 84 | };
|
93 | 85 |
|
94 |
| - var shutdown = function() { |
95 |
| - child.send('stop'); |
96 |
| - |
97 |
| - server.close(); |
98 |
| - client.close(); |
99 |
| - }; |
100 |
| - |
101 |
| - server.bind(0, '127.0.0.1'); |
| 86 | + parentServer.bind(0, '127.0.0.1'); |
102 | 87 |
|
103 | 88 | process.once('exit', function() {
|
104 | 89 | assert(parentGotMessage);
|
|
0 commit comments