|
| 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 | +if (cluster.isWorker) { |
| 28 | + |
| 29 | + // keep the worker alive |
| 30 | + var http = require('http'); |
| 31 | + http.Server().listen(common.PORT, '127.0.0.1'); |
| 32 | + |
| 33 | +} else if (process.argv[2] === 'cluster') { |
| 34 | + |
| 35 | + var worker = cluster.fork(); |
| 36 | + |
| 37 | + // send PID info to testcase process |
| 38 | + process.send({ |
| 39 | + pid: worker.process.pid |
| 40 | + }); |
| 41 | + |
| 42 | + // terminate the cluster process |
| 43 | + worker.once('listening', function() { |
| 44 | + setTimeout(function() { |
| 45 | + process.exit(0); |
| 46 | + }, 1000); |
| 47 | + }); |
| 48 | + |
| 49 | +} else { |
| 50 | + |
| 51 | + // This is the testcase |
| 52 | + var fork = require('child_process').fork; |
| 53 | + |
| 54 | + // is process alive helper |
| 55 | + var isAlive = function(pid) { |
| 56 | + try { |
| 57 | + //this will throw an error if the process is dead |
| 58 | + process.kill(pid, 0); |
| 59 | + |
| 60 | + return true; |
| 61 | + } catch (e) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + // Spawn a cluster process |
| 67 | + var master = fork(process.argv[1], ['cluster']); |
| 68 | + |
| 69 | + // get pid info |
| 70 | + var pid = null; |
| 71 | + master.once('message', function(data) { |
| 72 | + pid = data.pid; |
| 73 | + }); |
| 74 | + |
| 75 | + // When master is dead |
| 76 | + var alive = true; |
| 77 | + master.on('exit', function(code) { |
| 78 | + |
| 79 | + // make sure that the master died by purpose |
| 80 | + assert.equal(code, 0); |
| 81 | + |
| 82 | + // check worker process status |
| 83 | + setTimeout(function() { |
| 84 | + alive = isAlive(pid); |
| 85 | + }, 200); |
| 86 | + }); |
| 87 | + |
| 88 | + process.once('exit', function() { |
| 89 | + // cleanup: kill the worker if alive |
| 90 | + if (alive) { |
| 91 | + process.kill(pid); |
| 92 | + } |
| 93 | + |
| 94 | + assert.equal(typeof pid, 'number', 'did not get worker pid info'); |
| 95 | + assert.equal(alive, false, 'worker was alive after master died'); |
| 96 | + }); |
| 97 | + |
| 98 | +} |
0 commit comments