Roman Reiss | f29762f | 2015-05-19 11:00:06 | [diff] [blame] | 1 | 'use strict'; |
Julien Gilli | 90d1147 | 2014-08-16 00:20:32 | [diff] [blame] | 2 | var cluster = require('cluster'); |
| 3 | var assert = require('assert'); |
| 4 | var util = require('util'); |
| 5 | |
| 6 | if (cluster.isMaster) { |
| 7 | var worker = cluster.fork(); |
| 8 | |
| 9 | assert.ok(worker.isConnected(), |
Roman Reiss | f29762f | 2015-05-19 11:00:06 | [diff] [blame] | 10 | 'isConnected() should return true as soon as the worker has ' + |
| 11 | 'been created.'); |
Julien Gilli | 90d1147 | 2014-08-16 00:20:32 | [diff] [blame] | 12 | |
| 13 | worker.on('disconnect', function() { |
| 14 | assert.ok(!worker.isConnected(), |
Roman Reiss | f29762f | 2015-05-19 11:00:06 | [diff] [blame] | 15 | 'After a disconnect event has been emitted, ' + |
| 16 | 'isConncted should return false'); |
Julien Gilli | 90d1147 | 2014-08-16 00:20:32 | [diff] [blame] | 17 | }); |
| 18 | |
| 19 | worker.on('message', function(msg) { |
| 20 | if (msg === 'readyToDisconnect') { |
| 21 | worker.disconnect(); |
| 22 | } |
Roman Reiss | f29762f | 2015-05-19 11:00:06 | [diff] [blame] | 23 | }); |
Julien Gilli | 90d1147 | 2014-08-16 00:20:32 | [diff] [blame] | 24 | |
| 25 | } else { |
| 26 | assert.ok(cluster.worker.isConnected(), |
Roman Reiss | f29762f | 2015-05-19 11:00:06 | [diff] [blame] | 27 | 'isConnected() should return true from within a worker at all ' + |
| 28 | 'times.'); |
Julien Gilli | 90d1147 | 2014-08-16 00:20:32 | [diff] [blame] | 29 | |
| 30 | cluster.worker.process.on('disconnect', function() { |
| 31 | assert.ok(!cluster.worker.isConnected(), |
Roman Reiss | f29762f | 2015-05-19 11:00:06 | [diff] [blame] | 32 | 'isConnected() should return false from within a worker ' + |
| 33 | 'after its underlying process has been disconnected from ' + |
| 34 | 'the master'); |
| 35 | }); |
Julien Gilli | 90d1147 | 2014-08-16 00:20:32 | [diff] [blame] | 36 | |
| 37 | process.send('readyToDisconnect'); |
| 38 | } |