GeezerVison is showing github.com — test: make listen-fd-cluster/server more robust · nodejs/node@0ee4df9
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-listen-fd-cluster.js +50-35Lines changed: 50 additions & 35 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,63 +7,62 @@ var PORT = common.PORT; | 7 | 7 | var spawn = require('child_process').spawn; | 8 | 8 | var cluster = require('cluster'); | 9 | 9 |
| 10 | | -console.error('Cluster listen fd test', process.argv.slice(2)); | | 10 | +console.error('Cluster listen fd test', process.argv[2] || 'runner'); | 11 | 11 |
| 12 | 12 | if (common.isWindows) { | 13 | 13 | console.log('1..0 # Skipped: This test is disabled on windows.'); | 14 | 14 | return; | 15 | 15 | } | 16 | 16 |
| | 17 | +// Process relationship is: | | 18 | +// | | 19 | +// parent: the test main script | | 20 | +// -> master: the cluster master | | 21 | +// -> worker: the cluster worker | 17 | 22 | switch (process.argv[2]) { | 18 | 23 | case 'master': return master(); | 19 | 24 | case 'worker': return worker(); | 20 | 25 | case 'parent': return parent(); | 21 | | - default: return test(); | 22 | 26 | } | 23 | 27 |
| | 28 | +var ok; | | 29 | + | | 30 | +process.on('exit', function() { | | 31 | + assert.ok(ok); | | 32 | +}); | | 33 | + | 24 | 34 | // spawn the parent, and listen for it to tell us the pid of the cluster. | 25 | 35 | // WARNING: This is an example of listening on some arbitrary FD number | 26 | 36 | // that has already been bound elsewhere in advance. However, binding | 27 | 37 | // server handles to stdio fd's is NOT a good or reliable way to do | 28 | 38 | // concurrency in HTTP servers! Use the cluster module, or if you want | 29 | 39 | // a more low-level approach, use child process IPC manually. | 30 | | -function test() { | 31 | | - var parent = spawn(process.execPath, [__filename, 'parent'], { | 32 | | - stdio: [ 0, 'pipe', 2 ] | 33 | | - }); | 34 | | - var json = ''; | 35 | | - parent.stdout.on('data', function(c) { | 36 | | - json += c.toString(); | 37 | | - if (json.indexOf('\n') !== -1) next(); | 38 | | - }); | 39 | | - function next() { | 40 | | - console.error('output from parent = %s', json); | 41 | | - var cluster = JSON.parse(json); | 42 | | - // now make sure that we can request to the worker, then kill it. | 43 | | - http.get({ | 44 | | - server: 'localhost', | 45 | | - port: PORT, | 46 | | - path: '/', | 47 | | - }).on('response', function(res) { | 48 | | - var s = ''; | 49 | | - res.on('data', function(c) { | 50 | | - s += c.toString(); | 51 | | - }); | 52 | | - res.on('end', function() { | 53 | | - // kill the worker before we start doing asserts. | 54 | | - // it's really annoying when tests leave orphans! | 55 | | - parent.kill(); | 56 | | - process.kill(cluster.master, 'SIGKILL'); | 57 | | - | | 40 | +test(function(parent) { | | 41 | + // now make sure that we can request to the worker, then kill it. | | 42 | + http.get({ | | 43 | + server: 'localhost', | | 44 | + port: PORT, | | 45 | + path: '/', | | 46 | + }).on('response', function(res) { | | 47 | + var s = ''; | | 48 | + res.on('data', function(c) { | | 49 | + s += c.toString(); | | 50 | + }); | | 51 | + res.on('end', function() { | | 52 | + // kill the worker before we start doing asserts. | | 53 | + // it's really annoying when tests leave orphans! | | 54 | + parent.kill(); | | 55 | + parent.on('exit', function() { | 58 | 56 | assert.equal(s, 'hello from worker\n'); | 59 | 57 | assert.equal(res.statusCode, 200); | 60 | 58 | console.log('ok'); | | 59 | + ok = true; | 61 | 60 | }); | 62 | 61 | }); | 63 | | - } | 64 | | -} | | 62 | + }); | | 63 | +}); | 65 | 64 |
| 66 | | -function parent() { | | 65 | +function test(cb) { | 67 | 66 | console.error('about to listen in parent'); | 68 | 67 | var server = net.createServer(function(conn) { | 69 | 68 | console.error('connection on parent'); | @@ -73,7 +72,7 @@ function parent() { | 73 | 72 |
| 74 | 73 | var spawn = require('child_process').spawn; | 75 | 74 | var master = spawn(process.execPath, [__filename, 'master'], { | 76 | | - stdio: [ 0, 1, 2, server._handle ], | | 75 | + stdio: [ 0, 'pipe', 2, server._handle, 'ipc' ], | 77 | 76 | detached: true | 78 | 77 | }); | 79 | 78 |
| @@ -90,6 +89,11 @@ function parent() { | 90 | 89 | console.error('master closed'); | 91 | 90 | }); | 92 | 91 | console.error('master spawned'); | | 92 | + master.on('message', function(msg) { | | 93 | + if (msg === 'started worker') { | | 94 | + cb(master); | | 95 | + } | | 96 | + }); | 93 | 97 | }); | 94 | 98 | } | 95 | 99 |
| @@ -99,7 +103,17 @@ function master() { | 99 | 103 | args: [ 'worker' ] | 100 | 104 | }); | 101 | 105 | var worker = cluster.fork(); | 102 | | - console.log('%j\n', { master: process.pid, worker: worker.pid }); | | 106 | + worker.on('message', function(msg) { | | 107 | + if (msg === 'worker ready') { | | 108 | + process.send('started worker'); | | 109 | + } | | 110 | + }); | | 111 | + // Prevent outliving our parent process in case it is abnormally killed - | | 112 | + // under normal conditions our parent kills this process before exiting. | | 113 | + process.on('disconnect', function() { | | 114 | + console.error('master exit on disconnect'); | | 115 | + process.exit(0); | | 116 | + }); | 103 | 117 | } | 104 | 118 |
| 105 | 119 |
| @@ -112,5 +126,6 @@ function worker() { | 112 | 126 | res.end('hello from worker\n'); | 113 | 127 | }).listen({ fd: 3 }, function() { | 114 | 128 | console.error('worker listening on fd=3'); | | 129 | + process.send('worker ready'); | 115 | 130 | }); | 116 | 131 | } |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-listen-fd-server.js +38-44Lines changed: 38 additions & 44 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,65 +13,62 @@ if (common.isWindows) { | 13 | 13 |
| 14 | 14 | switch (process.argv[2]) { | 15 | 15 | case 'child': return child(); | 16 | | - case 'parent': return parent(); | 17 | | - default: return test(); | 18 | 16 | } | 19 | 17 |
| 20 | | -// spawn the parent, and listen for it to tell us the pid of the child. | | 18 | +var ok; | | 19 | + | | 20 | +process.on('exit', function() { | | 21 | + assert.ok(ok); | | 22 | +}); | | 23 | + | 21 | 24 | // WARNING: This is an example of listening on some arbitrary FD number | 22 | 25 | // that has already been bound elsewhere in advance. However, binding | 23 | 26 | // server handles to stdio fd's is NOT a good or reliable way to do | 24 | 27 | // concurrency in HTTP servers! Use the cluster module, or if you want | 25 | 28 | // a more low-level approach, use child process IPC manually. | 26 | | -function test() { | 27 | | - var parent = spawn(process.execPath, [__filename, 'parent'], { | 28 | | - stdio: [ 0, 'pipe', 2 ] | 29 | | - }); | 30 | | - var json = ''; | 31 | | - parent.stdout.on('data', function(c) { | 32 | | - json += c.toString(); | 33 | | - if (json.indexOf('\n') !== -1) next(); | 34 | | - }); | 35 | | - function next() { | 36 | | - console.error('output from parent = %s', json); | 37 | | - var child = JSON.parse(json); | 38 | | - // now make sure that we can request to the child, then kill it. | 39 | | - http.get({ | 40 | | - server: 'localhost', | 41 | | - port: PORT, | 42 | | - path: '/', | 43 | | - }).on('response', function(res) { | 44 | | - var s = ''; | 45 | | - res.on('data', function(c) { | 46 | | - s += c.toString(); | 47 | | - }); | 48 | | - res.on('end', function() { | 49 | | - // kill the child before we start doing asserts. | 50 | | - // it's really annoying when tests leave orphans! | 51 | | - process.kill(child.pid, 'SIGKILL'); | 52 | | - try { | 53 | | - parent.kill(); | 54 | | - } catch (e) {} | 55 | | - | | 29 | +test(function(child) { | | 30 | + // now make sure that we can request to the child, then kill it. | | 31 | + http.get({ | | 32 | + server: 'localhost', | | 33 | + port: PORT, | | 34 | + path: '/', | | 35 | + }).on('response', function(res) { | | 36 | + var s = ''; | | 37 | + res.on('data', function(c) { | | 38 | + s += c.toString(); | | 39 | + }); | | 40 | + res.on('end', function() { | | 41 | + child.kill(); | | 42 | + child.on('exit', function() { | 56 | 43 | assert.equal(s, 'hello from child\n'); | 57 | 44 | assert.equal(res.statusCode, 200); | | 45 | + console.log('ok'); | | 46 | + ok = true; | 58 | 47 | }); | 59 | 48 | }); | 60 | | - } | 61 | | -} | | 49 | + }); | | 50 | +}); | 62 | 51 |
| 63 | 52 | function child() { | | 53 | + // Prevent outliving the parent process in case it is terminated before | | 54 | + // killing this child process. | | 55 | + process.on('disconnect', function() { | | 56 | + console.error('exit on disconnect'); | | 57 | + process.exit(0); | | 58 | + }); | | 59 | + | 64 | 60 | // start a server on fd=3 | 65 | 61 | http.createServer(function(req, res) { | 66 | 62 | console.error('request on child'); | 67 | 63 | console.error('%s %s', req.method, req.url, req.headers); | 68 | 64 | res.end('hello from child\n'); | 69 | 65 | }).listen({ fd: 3 }, function() { | 70 | 66 | console.error('child listening on fd=3'); | | 67 | + process.send('listening'); | 71 | 68 | }); | 72 | 69 | } | 73 | 70 |
| 74 | | -function parent() { | | 71 | +function test(cb) { | 75 | 72 | var server = net.createServer(function(conn) { | 76 | 73 | console.error('connection on parent'); | 77 | 74 | conn.end('hello from parent\n'); | @@ -80,7 +77,7 @@ function parent() { | 80 | 77 |
| 81 | 78 | var spawn = require('child_process').spawn; | 82 | 79 | var child = spawn(process.execPath, [__filename, 'child'], { | 83 | | - stdio: [ 0, 1, 2, server._handle ] | | 80 | + stdio: [ 0, 1, 2, server._handle, 'ipc' ] | 84 | 81 | }); | 85 | 82 |
| 86 | 83 | console.log('%j\n', { pid: child.pid }); | @@ -90,13 +87,10 @@ function parent() { | 90 | 87 | // be accepted, because the child has the fd open. | 91 | 88 | server.close(); | 92 | 89 |
| 93 | | - child.on('exit', function(code) { | 94 | | - console.error('child exited', code); | 95 | | - }); | 96 | | - | 97 | | - child.on('close', function() { | 98 | | - console.error('child closed'); | | 90 | + child.on('message', function(msg) { | | 91 | + if (msg === 'listening') { | | 92 | + cb(child); | | 93 | + } | 99 | 94 | }); | 100 | | - console.error('child spawned'); | 101 | 95 | }); | 102 | 96 | } |
|