From c38ce9bc0a143141abfa638289b458ddaaac26d6 Mon Sep 17 00:00:00 2001 From: isaacs Date: Wed, 8 May 2013 12:54:29 -0700 Subject: stream: Guarantee ordering of 'finish' event In synchronous Writable streams (where the _write cb is called on the current tick), the 'finish' event (and thus the end() callback) can in some cases be called before all the write() callbacks are called. Use a counter, and have stream.Transform rely on the 'prefinish' event instead of the 'finish' event. This has zero effect on most streams, but it corrects an edge case and makes it perform more deterministically, which is a Good Thing. --- test/simple/test-stream2-writable.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'test') diff --git a/test/simple/test-stream2-writable.js b/test/simple/test-stream2-writable.js index e0f384cb2a..704100c0da 100644 --- a/test/simple/test-stream2-writable.js +++ b/test/simple/test-stream2-writable.js @@ -375,3 +375,19 @@ test('finish does not come before write cb', function(t) { w.write(Buffer(0)); w.end(); }); + +test('finish does not come before sync _write cb', function(t) { + var w = new W(); + var writeCb = false; + w._write = function(chunk, e, cb) { + cb(); + }; + w.on('finish', function() { + assert(writeCb); + t.end(); + }); + w.write(Buffer(0), function(er) { + writeCb = true; + }); + w.end(); +}); -- cgit v1.2.3