|
| 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 | +// Test that a Linux specific quirk in the handle passing protocol is handled |
| 23 | +// correctly. See https://github.com/joyent/node/issues/5330 for details. |
| 24 | + |
| 25 | +var common = require('../common'); |
| 26 | +var assert = require('assert'); |
| 27 | +var net = require('net'); |
| 28 | +var spawn = require('child_process').spawn; |
| 29 | + |
| 30 | +if (process.argv[2] === 'worker') |
| 31 | + worker(); |
| 32 | +else |
| 33 | + master(); |
| 34 | + |
| 35 | +function master() { |
| 36 | + // spawn() can only create one IPC channel so we use stdin/stdout as an |
| 37 | + // ad-hoc command channel. |
| 38 | + var proc = spawn(process.execPath, [__filename, 'worker'], { |
| 39 | + stdio: ['pipe', 'pipe', 'pipe', 'ipc'] |
| 40 | + }); |
| 41 | + var handle = null; |
| 42 | + proc.on('exit', function() { |
| 43 | + handle.close(); |
| 44 | + }); |
| 45 | + proc.stdout.on('data', function(data) { |
| 46 | + assert.equal(data, 'ok\r\n'); |
| 47 | + net.createServer(assert.fail).listen(common.PORT, function() { |
| 48 | + handle = this._handle; |
| 49 | + proc.send('one'); |
| 50 | + proc.send('two', handle); |
| 51 | + proc.send('three'); |
| 52 | + proc.stdin.write('ok\r\n'); |
| 53 | + }); |
| 54 | + }); |
| 55 | + proc.stderr.pipe(process.stderr); |
| 56 | +} |
| 57 | + |
| 58 | +function worker() { |
| 59 | + process._channel.readStop(); // Make messages batch up. |
| 60 | + process.stdout.ref(); |
| 61 | + process.stdout.write('ok\r\n'); |
| 62 | + process.stdin.once('data', function(data) { |
| 63 | + assert.equal(data, 'ok\r\n'); |
| 64 | + process._channel.readStart(); |
| 65 | + }); |
| 66 | + var n = 0; |
| 67 | + process.on('message', function(msg, handle) { |
| 68 | + n += 1; |
| 69 | + if (n === 1) { |
| 70 | + assert.equal(msg, 'one'); |
| 71 | + assert.equal(handle, undefined); |
| 72 | + } |
| 73 | + else if (n === 2) { |
| 74 | + assert.equal(msg, 'two'); |
| 75 | + assert.equal(typeof handle, 'object'); // Also matches null, therefore... |
| 76 | + assert.ok(handle); // also check that it's truthy. |
| 77 | + handle.close(); |
| 78 | + } |
| 79 | + else if (n === 3) { |
| 80 | + assert.equal(msg, 'three'); |
| 81 | + assert.equal(handle, undefined); |
| 82 | + process.exit(); |
| 83 | + } |
| 84 | + }); |
| 85 | +} |
0 commit comments