23
23
const common = require ( '../common' ) ;
24
24
const assert = require ( 'assert' ) ;
25
25
const cluster = require ( 'cluster' ) ;
26
+ const fs = require ( 'fs' ) ;
27
+ const http = require ( 'http' ) ;
26
28
27
29
const totalWorkers = 2 ;
28
30
29
31
// Cluster setup
30
32
if ( cluster . isWorker ) {
31
- const http = require ( 'http' ) ;
33
+ const filepath = `${ process . env . filedir } /${ process . pid } -test-cluster-primary-error` ;
34
+ fs . writeFileSync ( filepath , 'hello' ) ;
35
+ process . on ( 'exit' , ( ) => {
36
+ fs . unlinkSync ( filepath ) ;
37
+ } ) ;
32
38
http . Server ( ( ) => { } ) . listen ( 0 , '127.0.0.1' ) ;
33
39
} else if ( process . argv [ 2 ] === 'cluster' ) {
34
40
// Send PID to testcase process
@@ -66,14 +72,20 @@ if (cluster.isWorker) {
66
72
cluster . fork ( ) ;
67
73
} else {
68
74
// This is the testcase
69
-
75
+ const tmpdir = require ( '../common/tmpdir' ) ;
76
+ tmpdir . refresh ( ) ;
77
+ // Make sure the child process have write permission
78
+ fs . chmodSync ( tmpdir . path , 0o777 ) ;
70
79
const fork = require ( 'child_process' ) . fork ;
71
80
72
81
// List all workers
73
82
const workers = [ ] ;
74
83
75
84
// Spawn a cluster process
76
- const primary = fork ( process . argv [ 1 ] , [ 'cluster' ] , { silent : true } ) ;
85
+ const primary = fork ( process . argv [ 1 ] , [ 'cluster' ] , {
86
+ silent : true ,
87
+ env : { filedir : tmpdir . path }
88
+ } ) ;
77
89
78
90
// Handle messages from the cluster
79
91
primary . on ( 'message' , common . mustCall ( ( data ) => {
@@ -87,13 +99,16 @@ if (cluster.isWorker) {
87
99
primary . on ( 'exit' , common . mustCall ( ( code ) => {
88
100
// Check that the cluster died accidentally (non-zero exit code)
89
101
assert . strictEqual ( code , 1 ) ;
90
-
91
102
// XXX(addaleax): The fact that this uses raw PIDs makes the test inherently
92
103
// flaky – another process might end up being started right after the
93
- // workers finished and receive the same PID.
104
+ // workers finished and receive the same PID. So we also need to determine
105
+ // whether the file created by the process still exists
94
106
const pollWorkers = ( ) => {
95
107
// When primary is dead all workers should be dead too
96
- if ( workers . some ( ( pid ) => common . isAlive ( pid ) ) ) {
108
+ if ( workers . some ( ( pid ) => {
109
+ const filepath = `${ tmpdir . path } /${ pid } -test-cluster-primary-error` ;
110
+ return common . isAlive ( pid ) && fs . existsSync ( filepath ) ;
111
+ } ) ) {
97
112
setTimeout ( pollWorkers , 50 ) ;
98
113
}
99
114
} ;
0 commit comments