test-pummel: Add call validation in net-write-callbacks
authorisaacs <i@izs.me>
Wed, 19 Dec 2012 16:57:05 +0000 (08:57 -0800)
committerisaacs <i@izs.me>
Wed, 19 Dec 2012 18:55:23 +0000 (10:55 -0800)
lib/_stream_writable.js
test/pummel/test-net-write-callbacks.js

index fc70b1d..d2e3d8f 100644 (file)
@@ -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
index 45d1d59..aca4182 100644 (file)
@@ -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();
   });