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 { 24 mustCall, 25 mustCallAtLeast, 26} = require('../common'); 27const assert = require('assert'); 28const debug = require('util').debuglog('test'); 29 30let bufsize = 0; 31 32switch (process.argv[2]) { 33 case undefined: 34 return parent(); 35 case 'child': 36 return child(); 37 default: 38 throw new Error('invalid'); 39} 40 41function parent() { 42 const spawn = require('child_process').spawn; 43 const child = spawn(process.execPath, [__filename, 'child']); 44 let sent = 0; 45 46 let n = ''; 47 child.stdout.setEncoding('ascii'); 48 child.stdout.on('data', mustCallAtLeast((c) => { 49 n += c; 50 })); 51 child.stdout.on('end', mustCall(() => { 52 assert.strictEqual(+n, sent); 53 debug('ok'); 54 })); 55 56 // Write until the buffer fills up. 57 let buf; 58 do { 59 bufsize += 1024; 60 buf = Buffer.alloc(bufsize, '.'); 61 sent += bufsize; 62 } while (child.stdin.write(buf)); 63 64 // Then write a bunch more times. 65 for (let i = 0; i < 100; i++) { 66 const buf = Buffer.alloc(bufsize, '.'); 67 sent += bufsize; 68 child.stdin.write(buf); 69 } 70 71 // Now end, before it's all flushed. 72 child.stdin.end(); 73 74 // now we wait... 75} 76 77function child() { 78 let received = 0; 79 process.stdin.on('data', mustCallAtLeast((c) => { 80 received += c.length; 81 })); 82 process.stdin.on('end', mustCall(() => { 83 // This console.log is part of the test. 84 console.log(received); 85 })); 86} 87