summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorisaacs <i@izs.me>2012-12-19 08:57:05 -0800
committerisaacs <i@izs.me>2012-12-19 10:55:23 -0800
commitf63af64eb8da6424243ead150e0b2dd718bd95e9 (patch)
tree1449196029900960576979eec9221cf42a90df80
parentf3f4e290e0d7c38c6d3b8b60c104edd762949423 (diff)
downloadnode-new-f63af64eb8da6424243ead150e0b2dd718bd95e9.tar.gz
test-pummel: Add call validation in net-write-callbacks
-rw-r--r--lib/_stream_writable.js26
-rw-r--r--test/pummel/test-net-write-callbacks.js19
2 files changed, 22 insertions, 23 deletions
diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js
index fc70b1d7e6..d2e3d8fc4e 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 45d1d59f3c..aca418257a 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();
});