Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
test-pummel: Add call validation in net-write-callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Dec 19, 2012
1 parent f3f4e29 commit f63af64
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
26 changes: 6 additions & 20 deletions lib/_stream_writable.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down
19 changes: 16 additions & 3 deletions test/pummel/test-net-write-callbacks.js
Expand Up @@ -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();
});
Expand Down

0 comments on commit f63af64

Please sign in to comment.