Skip to content

Instantly share code, notes, and snippets.

@ry
Created August 24, 2011 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ry/1169327 to your computer and use it in GitHub Desktop.
Save ry/1169327 to your computer and use it in GitHub Desktop.
commit 6801d161d9e73ebafc1d9168fa04010c70deeda4
Author: Ryan Dahl <ry@tinyclouds.org>
Date: Thu Aug 11 23:57:37 2011 -0700
fix double-pipe WIP
diff --git a/test/fixtures/cat.js b/test/fixtures/cat.js
new file mode 100644
index 0000000..b5f13d3
--- /dev/null
+++ b/test/fixtures/cat.js
@@ -0,0 +1,9 @@
+
+var stdin = process.openStdin();
+stdin.on('data', function(d) {
+ process.stdout.write(d);
+});
+
+stdin.on('end', function(d) {
+ process.stdout.end(d);
+});
diff --git a/test/simple/test-child-process-double-pipe.js b/test/simple/test-child-process-double-pipe.js
index 7322cbe..22a752b 100644
--- a/test/simple/test-child-process-double-pipe.js
+++ b/test/simple/test-child-process-double-pipe.js
@@ -19,20 +19,31 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
-var assert = require('assert'),
- util = require('util'),
+var assert = require('assert'),
+ util = require('util'),
spawn = require('child_process').spawn;
+var common = require('../common');
+var join = require('path').join;
+
// We're trying to reproduce:
-// $ echo "hello\nnode\nand\nworld" | grep o | sed s/o/a/
+// $ cat0 "hello\nnode\nand\nworld" | cat1
+
+var toEcho = 'hello\nnode\nand\nworld';
+
+function startCat() {
+ return spawn(process.execPath, [ join(common.fixturesDir, 'cat.js') ]);
+}
+
-var echo = spawn('echo', ['hello\nnode\nand\nworld\n']),
- grep = spawn('grep', ['o']),
- sed = spawn('sed', ['s/o/O/']);
+var cat0 = startCat();
+var cat1 = startCat();
+var cat2 = startCat();
/*
- * grep and sed hang if the spawn function leaks file descriptors to child
+ * cat1 and cat2 hang if the spawn function leaks file descriptors to child
* processes.
+ *
* This happens when calling pipe(2) and then forgetting to set the
* FD_CLOEXEC flag on the resulting file descriptors.
*
@@ -40,67 +51,44 @@ var echo = spawn('echo', ['hello\nnode\nand\nworld\n']),
* explained above.
*/
+cat0.stdin.end(toEcho);
-
-// pipe echo | grep
-echo.stdout.on('data', function(data) {
- console.error("grep stdin write " + data.length);
- if (!grep.stdin.write(data)) {
- echo.stdout.pause();
- }
-});
-
-grep.stdin.on('drain', function(data) {
- echo.stdout.resume();
+// pipe cat0 | cat1
+cat0.stdout.on('data', function(data) {
+ console.log("cat0 stdout " + JSON.stringify(data));
+ cat1.stdin.write(data);
});
-// propagate end from echo to grep
-echo.stdout.on('end', function(code) {
- grep.stdin.end();
+// propagate end from cat0 to cat1
+cat0.stdout.on('end', function(data) {
+ console.log("cat0 end " + JSON.stringify(data));
+ cat1.stdin.end(data);
});
-echo.on('exit', function() {
- console.error("echo exit");
-})
-grep.on('exit', function() {
- console.error("grep exit");
-})
-
-sed.on('exit', function() {
- console.error("sed exit");
-})
-
-
-
-// pipe grep | sed
-grep.stdout.on('data', function(data) {
- console.error("grep stdout " + data.length);
- if (!sed.stdin.write(data)) {
- grep.stdout.pause();
- }
+// pipe cat1 | sed
+cat1.stdout.on('data', function(data) {
+ console.log("cat1 data " + JSON.stringify(data));
+ cat2.stdin.write(data);
});
-sed.stdin.on('drain', function(data) {
- grep.stdout.resume();
+cat1.stdout.on('end', function(data) {
+ console.log("cat1 end " + JSON.stringify(data));
+ cat2.stdin.end(data);
});
-// propagate end from grep to sed
-grep.stdout.on('end', function(code) {
- console.error("grep stdout end");
- sed.stdin.end();
-});
+var out = '';
+cat2.stdout.setEncoding('utf8');
+cat2.stdout.on('data', function(d) {
+ console.log("cat2 data " + JSON.stringify(data));
+ out += d;
+ console.log(d);
+});
-var result = '';
-// print sed's output
-sed.stdout.on('data', function(data) {
- result += data.toString('utf8', 0, data.length);
- util.print(data);
+process.on('exit', function() {
+ assert.equal(toEcho, out);
});
-sed.stdout.on('end', function(code) {
- assert.equal(result, 'hellO\nnOde\nwOrld\n');
-});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment