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'use strict'; 23const common = require('../common'); 24const assert = require('assert'); 25const cluster = require('cluster'); 26 27if (cluster.isWorker) { 28 29 // Keep the worker alive 30 const http = require('http'); 31 http.Server().listen(0, '127.0.0.1'); 32 33} else if (process.argv[2] === 'cluster') { 34 35 const 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', common.mustCall(() => { 44 setTimeout(() => { 45 process.exit(0); 46 }, 1000); 47 })); 48 49} else { 50 51 // This is the testcase 52 const fork = require('child_process').fork; 53 54 // Spawn a cluster process 55 const master = fork(process.argv[1], ['cluster']); 56 57 // get pid info 58 let pid = null; 59 master.once('message', (data) => { 60 pid = data.pid; 61 }); 62 63 // When master is dead 64 let alive = true; 65 master.on('exit', common.mustCall((code) => { 66 67 // Make sure that the master died on purpose 68 assert.strictEqual(code, 0); 69 70 // Check worker process status 71 const pollWorker = () => { 72 alive = common.isAlive(pid); 73 if (alive) { 74 setTimeout(pollWorker, 50); 75 } 76 }; 77 // Loop indefinitely until worker exit. 78 pollWorker(); 79 })); 80 81 process.once('exit', () => { 82 assert.strictEqual(typeof pid, 'number', 83 `got ${pid} instead of a worker pid`); 84 assert.strictEqual(alive, false, 85 `worker was alive after master died (alive = ${alive})`); 86 }); 87 88} 89