From f63af64eb8da6424243ead150e0b2dd718bd95e9 Mon Sep 17 00:00:00 2001 From: isaacs Date: Wed, 19 Dec 2012 08:57:05 -0800 Subject: [PATCH] test-pummel: Add call validation in net-write-callbacks --- lib/_stream_writable.js | 26 ++++++-------------------- test/pummel/test-net-write-callbacks.js | 19 ++++++++++++++++--- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index fc70b1d..d2e3d8f 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -81,6 +81,11 @@ function WritableState(options, stream) { // or on a later tick. this.sync = false; + // a flag to know if we're processing previously buffered items, which + // may call the _write() callback in the same tick, so that we don't + // end up in an overlapped onwrite situation. + this.bufferProcessing = false; + // the callback that's passed to _write(chunk,cb) this.onwrite = function(er) { onwrite(stream, er); @@ -188,8 +193,7 @@ function onwrite(stream, er) { if (cb) { // Don't call the cb until the next tick if we're in sync mode. - // Also defer if we're about to write some more right now. - if (sync || state.buffer.length) + if (sync) process.nextTick(cb); else cb(); @@ -204,24 +208,6 @@ function onwrite(stream, er) { return; } - // if there's something in the buffer waiting, then do that, too. - if (state.buffer.length) { - var chunkCb = state.buffer.shift(); - var chunk = chunkCb[0]; - cb = chunkCb[1]; - - if (false === state.decodeStrings) - l = chunk[0].length; - else - l = chunk.length; - - state.writelen = l; - state.writecb = cb; - state.writechunk = chunk; - state.writing = true; - stream._write(chunk, state.onwrite); - } - if (state.length <= state.lowWaterMark && state.needDrain) { // Must force callback to be called on nextTick, so that we don't // emit 'drain' before the write() consumer gets the 'false' return diff --git a/test/pummel/test-net-write-callbacks.js b/test/pummel/test-net-write-callbacks.js index 45d1d59..aca4182 100644 --- a/test/pummel/test-net-write-callbacks.js +++ b/test/pummel/test-net-write-callbacks.js @@ -38,14 +38,27 @@ var server = net.Server(function(socket) { }); }); +var lastCalled = -1; +function makeCallback(c) { + var called = false; + return function() { + if (called) + throw new Error('called callback #' + c + ' more than once'); + called = true; + if (c < lastCalled) + throw new Error('callbacks out of order. last=' + lastCalled + + ' current=' + c); + lastCalled = c; + cbcount++; + }; +} + server.listen(common.PORT, function() { var client = net.createConnection(common.PORT); client.on('connect', function() { for (var i = 0; i < N; i++) { - client.write('hello world', function() { - cbcount++; - }); + client.write('hello world', makeCallback(i)); } client.end(); }); -- 2.7.4