| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 34430ab commit 6b45bf3
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,16 +29,11 @@ const totalWorkers = 2; | |||
| 29 | 29 | // Cluster setup | |
| 30 | 30 | if (cluster.isWorker) { | |
| 31 | 31 | const http = require('http'); | |
| 32 | - http.Server(() => { | ||
| 33 | - | ||
| 34 | - }).listen(0, '127.0.0.1'); | ||
| 35 | - | ||
| 32 | + http.Server(() => {}).listen(0, '127.0.0.1'); | ||
| 36 | 33 | } else if (process.argv[2] === 'cluster') { | |
| 37 | - | ||
| 38 | 34 | // Send PID to testcase process | |
| 39 | 35 | let forkNum = 0; | |
| 40 | 36 | cluster.on('fork', common.mustCall(function forkEvent(worker) { | |
| 41 | - | ||
| 42 | 37 | // Send PID | |
| 43 | 38 | process.send({ | |
| 44 | 39 | cmd: 'worker', | |
@@ -49,12 +44,11 @@ if (cluster.isWorker) { | |||
| 49 | 44 | if (++forkNum === totalWorkers) { | |
| 50 | 45 | cluster.removeListener('fork', forkEvent); | |
| 51 | 46 | } | |
| 52 | - })); | ||
| 47 | + }, totalWorkers)); | ||
| 53 | 48 | ||
| 54 | 49 | // Throw accidental error when all workers are listening | |
| 55 | 50 | let listeningNum = 0; | |
| 56 | 51 | cluster.on('listening', common.mustCall(function listeningEvent() { | |
| 57 | - | ||
| 58 | 52 | // When all workers are listening | |
| 59 | 53 | if (++listeningNum === totalWorkers) { | |
| 60 | 54 | // Stop listening | |
@@ -65,21 +59,16 @@ if (cluster.isWorker) { | |||
| 65 | 59 | throw new Error('accidental error'); | |
| 66 | 60 | }); | |
| 67 | 61 | } | |
| 68 | - | ||
| 69 | - })); | ||
| 62 | + }, totalWorkers)); | ||
| 70 | 63 | ||
| 71 | 64 | // Startup a basic cluster | |
| 72 | 65 | cluster.fork(); | |
| 73 | 66 | cluster.fork(); | |
| 74 | - | ||
| 75 | 67 | } else { | |
| 76 | 68 | // This is the testcase | |
| 77 | 69 | ||
| 78 | 70 | const fork = require('child_process').fork; | |
| 79 | 71 | ||
| 80 | - let masterExited = false; | ||
| 81 | - let workersExited = false; | ||
| 82 | - | ||
| 83 | 72 | // List all workers | |
| 84 | 73 | const workers = []; | |
| 85 | 74 | ||
@@ -88,7 +77,6 @@ if (cluster.isWorker) { | |||
| 88 | 77 | ||
| 89 | 78 | // Handle messages from the cluster | |
| 90 | 79 | master.on('message', common.mustCall((data) => { | |
| 91 | - | ||
| 92 | 80 | // Add worker pid to list and progress tracker | |
| 93 | 81 | if (data.cmd === 'worker') { | |
| 94 | 82 | workers.push(data.workerPID); | |
@@ -97,30 +85,20 @@ if (cluster.isWorker) { | |||
| 97 | 85 | ||
| 98 | 86 | // When cluster is dead | |
| 99 | 87 | master.on('exit', common.mustCall((code) => { | |
| 100 | - | ||
| 101 | 88 | // Check that the cluster died accidentally (non-zero exit code) | |
| 102 | - masterExited = !!code; | ||
| 89 | + assert.strictEqual(code, 1); | ||
| 103 | 90 | ||
| 91 | + // XXX(addaleax): The fact that this uses raw PIDs makes the test inherently | ||
| 92 | + // flaky – another process might end up being started right after the | ||
| 93 | + // workers finished and receive the same PID. | ||
| 104 | 94 | const pollWorkers = () => { | |
| 105 | 95 | // When master is dead all workers should be dead too | |
| 106 | - let alive = false; | ||
| 107 | - workers.forEach((pid) => alive = common.isAlive(pid)); | ||
| 108 | - if (alive) { | ||
| 96 | + if (workers.some((pid) => common.isAlive(pid))) { | ||
| 109 | 97 | setTimeout(pollWorkers, 50); | |
| 110 | - } else { | ||
| 111 | - workersExited = true; | ||
| 112 | 98 | } | |
| 113 | 99 | }; | |
| 114 | 100 | ||
| 115 | 101 | // Loop indefinitely until worker exit | |
| 116 | 102 | pollWorkers(); | |
| 117 | 103 | })); | |
| 118 | - | ||
| 119 | - process.once('exit', () => { | ||
| 120 | - assert(masterExited, | ||
| 121 | - 'The master did not die after an error was thrown'); | ||
| 122 | - assert(workersExited, | ||
| 123 | - 'The workers did not die after an error in the master'); | ||
| 124 | - }); | ||
| 125 | - | ||
| 126 | 104 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments