|
| 1 | +// Copyright Joyent, Inc. and other Node contributors. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | +// copy of this software and associated documentation files (the |
| 5 | +// "Software"), to deal in the Software without restriction, including |
| 6 | +// without limitation the rights to use, copy, modify, merge, publish, |
| 7 | +// distribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 | +// persons to whom the Software is furnished to do so, subject to the |
| 9 | +// following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included |
| 12 | +// in all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 17 | +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 18 | +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 19 | +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 20 | +// USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + |
| 22 | + |
| 23 | +var common = require('../common'); |
| 24 | +var assert = require('assert'); |
| 25 | +var cluster = require('cluster'); |
| 26 | + |
| 27 | +// Cluster setup |
| 28 | +if (cluster.isWorker) { |
| 29 | + var http = require('http'); |
| 30 | + http.Server(function() { |
| 31 | + |
| 32 | + }).listen(common.PORT, '127.0.0.1'); |
| 33 | + |
| 34 | +} else if (process.argv[2] === 'cluster') { |
| 35 | + |
| 36 | + var totalWorkers = 2; |
| 37 | + |
| 38 | + // Send PID to testcase process |
| 39 | + var forkNum = 0; |
| 40 | + cluster.on('fork', function forkEvent(worker) { |
| 41 | + |
| 42 | + // Send PID |
| 43 | + process.send({ |
| 44 | + cmd: 'worker', |
| 45 | + workerPID: worker.process.pid |
| 46 | + }); |
| 47 | + |
| 48 | + // Stop listening when done |
| 49 | + if (++forkNum === totalWorkers) { |
| 50 | + cluster.removeListener('fork', forkEvent); |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + // Throw accidently error when all workers are listening |
| 55 | + var listeningNum = 0; |
| 56 | + cluster.on('listening', function listeningEvent() { |
| 57 | + |
| 58 | + // When all workers are listening |
| 59 | + if (++listeningNum === totalWorkers) { |
| 60 | + // Stop listening |
| 61 | + cluster.removeListener('listening', listeningEvent); |
| 62 | + |
| 63 | + // throw accidently error |
| 64 | + process.nextTick(function() { |
| 65 | + throw 'accidently error'; |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + }); |
| 70 | + |
| 71 | + // Startup a basic cluster |
| 72 | + cluster.fork(); |
| 73 | + cluster.fork(); |
| 74 | + |
| 75 | +} else { |
| 76 | + // This is the testcase |
| 77 | + |
| 78 | + var fork = require('child_process').fork; |
| 79 | + |
| 80 | + var isAlive = function(pid) { |
| 81 | + try { |
| 82 | + //this will throw an error if the process is dead |
| 83 | + process.kill(pid, 0); |
| 84 | + |
| 85 | + return true; |
| 86 | + } catch (e) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + var existMaster = false; |
| 92 | + var existWorker = false; |
| 93 | + |
| 94 | + // List all workers |
| 95 | + var workers = []; |
| 96 | + |
| 97 | + // Spawn a cluster process |
| 98 | + var master = fork(process.argv[1], ['cluster'], {silent: true}); |
| 99 | + |
| 100 | + // Handle messages from the cluster |
| 101 | + master.on('message', function(data) { |
| 102 | + |
| 103 | + // Add worker pid to list and progress tracker |
| 104 | + if (data.cmd === 'worker') { |
| 105 | + workers.push(data.workerPID); |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + // When cluster is dead |
| 110 | + master.on('exit', function(code) { |
| 111 | + |
| 112 | + // Check that the cluster died accidently |
| 113 | + existMaster = (code === 1); |
| 114 | + |
| 115 | + // When master is dead all workers should be dead to |
| 116 | + var alive = false; |
| 117 | + workers.forEach(function(pid) { |
| 118 | + if (isAlive(pid)) { |
| 119 | + alive = true; |
| 120 | + } |
| 121 | + }); |
| 122 | + |
| 123 | + // If a worker was alive this did not act as expected |
| 124 | + existWorker = !alive; |
| 125 | + }); |
| 126 | + |
| 127 | + process.once('exit', function() { |
| 128 | + assert.ok(existMaster, 'The master did not die after an error was throwed'); |
| 129 | + assert.ok(existWorker, 'The workers did not die after an error in the master'); |
| 130 | + }); |
| 131 | + |
| 132 | +} |
0 commit comments