8
8
* Because it's not really possible to predict how the messages will be
9
9
* distributed among the parent and the child processes, we keep sending
10
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 .
11
+ * message. When either the parent or child receives a message we close
12
+ * the server on that side so the next message is guaranteed to be
13
+ * received by the other .
14
14
*/
15
15
16
16
const common = require ( '../common' ) ;
@@ -26,22 +26,30 @@ if (common.isWindows) {
26
26
27
27
var server ;
28
28
if ( process . argv [ 2 ] === 'child' ) {
29
+ var serverClosed = false ;
29
30
process . on ( 'message' , function removeMe ( msg , clusterServer ) {
30
31
if ( msg === 'server' ) {
31
32
server = clusterServer ;
32
33
33
34
server . on ( 'message' , function ( ) {
34
35
process . send ( 'gotMessage' ) ;
36
+ // got a message so close the server to make sure
37
+ // the parent also gets a message
38
+ serverClosed = true ;
39
+ server . close ( ) ;
35
40
} ) ;
36
41
37
42
} else if ( msg === 'stop' ) {
38
- server . close ( ) ;
39
43
process . removeListener ( 'message' , removeMe ) ;
44
+ if ( ! serverClosed ) {
45
+ server . close ( ) ;
46
+ }
40
47
}
41
48
} ) ;
42
49
43
50
} else {
44
51
server = dgram . createSocket ( 'udp4' ) ;
52
+ var serverPort = null ;
45
53
var client = dgram . createSocket ( 'udp4' ) ;
46
54
var child = fork ( __filename , [ 'child' ] ) ;
47
55
@@ -52,9 +60,13 @@ if (process.argv[2] === 'child') {
52
60
53
61
server . on ( 'message' , function ( msg , rinfo ) {
54
62
parentGotMessage = true ;
63
+ // got a message so close the server to make sure
64
+ // the child also gets a message
65
+ server . close ( ) ;
55
66
} ) ;
56
67
57
68
server . on ( 'listening' , function ( ) {
69
+ serverPort = server . address ( ) . port ;
58
70
child . send ( 'server' , server ) ;
59
71
60
72
child . once ( 'message' , function ( msg ) {
@@ -66,13 +78,16 @@ if (process.argv[2] === 'child') {
66
78
sendMessages ( ) ;
67
79
} ) ;
68
80
81
+ var iterations = 0 ;
69
82
var sendMessages = function ( ) {
70
83
var timer = setInterval ( function ( ) {
84
+ iterations ++ ;
85
+
71
86
client . send (
72
87
msg ,
73
88
0 ,
74
89
msg . length ,
75
- server . address ( ) . port ,
90
+ serverPort ,
76
91
'127.0.0.1' ,
77
92
function ( err ) {
78
93
if ( err ) throw err ;
@@ -83,7 +98,8 @@ if (process.argv[2] === 'child') {
83
98
* Both the parent and the child got at least one message,
84
99
* test passed, clean up everyting.
85
100
*/
86
- if ( parentGotMessage && childGotMessage ) {
101
+ if ( ( parentGotMessage && childGotMessage ) ||
102
+ ( ( iterations / 1000 ) > 45 ) ) {
87
103
clearInterval ( timer ) ;
88
104
shutdown ( ) ;
89
105
}
@@ -94,7 +110,9 @@ if (process.argv[2] === 'child') {
94
110
var shutdown = function ( ) {
95
111
child . send ( 'stop' ) ;
96
112
97
- server . close ( ) ;
113
+ if ( ! parentGotMessage ) {
114
+ server . close ( ) ;
115
+ }
98
116
client . close ( ) ;
99
117
} ;
100
118
0 commit comments