From ae1fc497bbf026007c9d44ea425f1f060ade609f Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Sun, 7 Feb 2010 21:56:36 -0800 Subject: [PATCH] Fix stdout flush issue http://github.com/ry/node/issues#issue/53 --- src/node.cc | 2 ++ src/node_stdio.cc | 13 ++++++++ src/node_stdio.h | 1 + test/mjsunit/fixtures/print-chars.js | 2 -- test/mjsunit/test-stdout-flush.js | 50 ++++++++++------------------ 5 files changed, 33 insertions(+), 35 deletions(-) diff --git a/src/node.cc b/src/node.cc index c7dac8e2e8..5726578fdb 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1160,6 +1160,8 @@ int main(int argc, char *argv[]) { // so your next reading stop should be node::Load()! node::Load(argc, argv); + node::Stdio::Flush(); + #ifndef NDEBUG // Clean up. context.Dispose(); diff --git a/src/node_stdio.cc b/src/node_stdio.cc index 2b1da6c055..adcaff2557 100644 --- a/src/node_stdio.cc +++ b/src/node_stdio.cc @@ -197,6 +197,19 @@ Close (const Arguments& args) return Undefined(); } +void Stdio::Flush() { + if (stdout_fd >= 0) { + close(stdout_fd); + stdout_fd = -1; + } + + if (stdout_coupling) { + coupling_join(stdout_coupling); + coupling_destroy(stdout_coupling); + stdout_coupling = NULL; + } +} + void Stdio::Initialize (v8::Handle target) { diff --git a/src/node_stdio.h b/src/node_stdio.h index 11dd5aeddc..60fa2912fd 100644 --- a/src/node_stdio.h +++ b/src/node_stdio.h @@ -11,6 +11,7 @@ namespace node { class Stdio { public: static void Initialize (v8::Handle target); + static void Flush (); }; } // namespace node diff --git a/test/mjsunit/fixtures/print-chars.js b/test/mjsunit/fixtures/print-chars.js index c4202b6036..ba539ffb76 100644 --- a/test/mjsunit/fixtures/print-chars.js +++ b/test/mjsunit/fixtures/print-chars.js @@ -8,5 +8,3 @@ for (var i = 0; i < n-1; i++) { } puts(s); // \n is the nth char. - -process.exit(0); diff --git a/test/mjsunit/test-stdout-flush.js b/test/mjsunit/test-stdout-flush.js index 52d9633641..58c945bea9 100644 --- a/test/mjsunit/test-stdout-flush.js +++ b/test/mjsunit/test-stdout-flush.js @@ -2,43 +2,27 @@ process.mixin(require("./common")); var sub = path.join(fixturesDir, 'print-chars.js'); -completedTests = 0; +n = 100000; -function test (n, cb) { - var child = process.createChildProcess(process.argv[0], [sub, n]); +var child = process.createChildProcess(process.argv[0], [sub, n]); - var count = 0; +var count = 0; - child.addListener("error", function (data){ - if (data) { - puts("parent stderr: " + data); - assert.ok(false); - } - }); - - child.addListener("output", function (data){ - if (data) { - count += data.length; - } - }); - - child.addListener("exit", function (data) { - assert.equal(n, count); - puts(n + " okay"); - completedTests++; - if (cb) cb(); - }); -} - - - -test(5000, function () { - test(50000, function () { - test(500000); - }); +child.addListener("error", function (data){ + if (data) { + puts("parent stderr: " + data); + assert.ok(false); + } }); +child.addListener("output", function (data){ + if (data) { + count += data.length; + puts(count); + } +}); -process.addListener('exit', function () { - assert.equal(3, completedTests); +child.addListener("exit", function (data) { + assert.equal(n, count); + puts("okay"); });