From 0918bb2070b8bea73cfb3d36f284904b6884239c Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Tue, 9 Mar 2010 18:37:23 -0800 Subject: [PATCH] Adjust a few more tests to work with net2 --- lib/net.js | 61 ++++++++++++++++---------- test/pummel/test-tcp-pingpong-delay.js | 6 +-- test/pummel/test-tcp-pingpong.js | 8 ++-- test/pummel/test-tcp-throttle.js | 8 ++-- 4 files changed, 49 insertions(+), 34 deletions(-) diff --git a/lib/net.js b/lib/net.js index 820a9a8eb0..a7f0f35c32 100644 --- a/lib/net.js +++ b/lib/net.js @@ -73,6 +73,22 @@ function allocRecvBuffer () { recvBuffer.used = 0; } +function _doFlush () { + var socket = this.socket; + // Socket becomes writeable on connect() but don't flush if there's + // nothing actually to write + if ((socket._writeQueueSize == 0) && (socket._writeMessageQueueSize == 0)) { + return; + } + if (socket.flush()) { + assert(socket._writeQueueSize == 0); + assert(socket._writeMessageQueueSize == 0); + + if (socket._events && socket._events['drain']) socket.emit("drain"); + if (socket.ondrain) socket.ondrain(); // Optimization + } +} + function initSocket (self) { self._readWatcher = ioWatchers.alloc(); self._readWatcher.callback = function () { @@ -163,22 +179,9 @@ function initSocket (self) { self._writeQueueSize = 0; // in bytes, not to be confused with _writeQueue.length! self._writeMessageQueueSize = 0; // number of messages remaining to be sent - self._doFlush = function () { - // Socket becomes writeable on connect() but don't flush if there's - // nothing actually to write - if ((self._writeQueueSize == 0) && (self._writeMessageQueueSize == 0)) { - return; - } - if (self.flush()) { - assert(self._writeQueueSize == 0); - assert(self._writeMessageQueueSize == 0); - - if (self._events && self._events['drain']) self.emit("drain"); - if (self.ondrain) self.ondrain(); // Optimization - } - }; self._writeWatcher = ioWatchers.alloc(); - self._writeWatcher.callback = self._doFlush; + self._writeWatcher.socket = self; + self._writeWatcher.callback = _doFlush; self.writable = false; } @@ -210,6 +213,21 @@ exports.createConnection = function (port, host) { }; +Object.defineProperty(Socket.prototype, 'readyState', { + get: function () { + if (this.readable && this.writable) { + return 'open'; + } else if (this.readable && !this.writable){ + return 'readOnly'; + } else if (!this.readable && this.writable){ + return 'writeOnly'; + } else { + return 'closed'; + } + } +}); + + Socket.prototype._allocateSendBuffer = function () { var b = buffers.alloc(1024); b.used = 0; @@ -351,7 +369,7 @@ Socket.prototype.flush = function () { if (b == END_OF_FILE) { self._shutdown(); - return false; + return true; } if (b.sent == b.used) { @@ -379,19 +397,19 @@ Socket.prototype.flush = function () { } if (bytesWritten === null) { - // could not flush everything + // EAGAIN + debug('write EAGAIN'); self._writeWatcher.start(); assert(self._writeQueueSize > 0); return false; - } - if (b.isFd) { + } else if (b.isFd) { b.sent = b.used; self._writeMessageQueueSize -= 1; //debug('sent fd: ' + fdToSend); } else { b.sent += bytesWritten; self._writeQueueSize -= bytesWritten; - //debug('bytes sent: ' + b.sent); + debug('bytes sent: ' + b.sent); } } if (self._writeWatcher) self._writeWatcher.stop(); @@ -453,11 +471,8 @@ Socket.prototype.connect = function () { self.type = 'tcp'; // TODO dns resolution on arguments[1] var port = arguments[0]; - var yyy = xxx++; lookupDomainName(arguments[1], function (ip) { - debug('doConnect ' + self.fd + ' yyy=' + yyy); doConnect(self, port, ip); - debug('doConnect done ' + self.fd + ' yyy=' + yyy); }); } }; diff --git a/test/pummel/test-tcp-pingpong-delay.js b/test/pummel/test-tcp-pingpong-delay.js index b0a2a18aaf..a2015c373e 100644 --- a/test/pummel/test-tcp-pingpong-delay.js +++ b/test/pummel/test-tcp-pingpong-delay.js @@ -1,5 +1,5 @@ require("../common"); -tcp = require("tcp"); +net = require("net"); var tests_run = 0; @@ -10,7 +10,7 @@ function pingPongTest (port, host, on_complete) { var count = 0; var client_closed = false; - var server = tcp.createServer(function (socket) { + var server = net.createServer(function (socket) { socket.setEncoding("utf8"); socket.addListener("data", function (data) { @@ -44,7 +44,7 @@ function pingPongTest (port, host, on_complete) { }); server.listen(port, host); - var client = tcp.createConnection(port, host); + var client = net.createConnection(port, host); client.setEncoding("utf8"); diff --git a/test/pummel/test-tcp-pingpong.js b/test/pummel/test-tcp-pingpong.js index 135b9a5958..9a3266c967 100644 --- a/test/pummel/test-tcp-pingpong.js +++ b/test/pummel/test-tcp-pingpong.js @@ -1,5 +1,5 @@ require("../common"); -tcp = require("tcp"); +net = require("net"); var tests_run = 0; @@ -8,13 +8,13 @@ function pingPongTest (port, host, on_complete) { var count = 0; var sent_final_ping = false; - var server = tcp.createServer(function (socket) { + var server = net.createServer(function (socket) { assert.equal(true, socket.remoteAddress !== null); assert.equal(true, socket.remoteAddress !== undefined); if (host === "127.0.0.1") assert.equal(socket.remoteAddress, "127.0.0.1"); else if (host == null) - assert.equal(socket.remoteAddress, "127.0.0.1"); + assert.equal(socket.remoteAddress, "::1"); socket.setEncoding("utf8"); socket.setNoDelay(); @@ -42,7 +42,7 @@ function pingPongTest (port, host, on_complete) { }); server.listen(port, host); - var client = tcp.createConnection(port, host); + var client = net.createConnection(port, host); client.setEncoding("utf8"); diff --git a/test/pummel/test-tcp-throttle.js b/test/pummel/test-tcp-throttle.js index 2e6fdef3a1..029d364572 100644 --- a/test/pummel/test-tcp-throttle.js +++ b/test/pummel/test-tcp-throttle.js @@ -1,6 +1,6 @@ require("../common"); -tcp = require("tcp"); -N = 60*1024; // 30kb +net = require("net"); +N = 160*1024; // 30kb puts("build big string"); var body = ""; @@ -10,7 +10,7 @@ for (var i = 0; i < N; i++) { puts("start server on port " + PORT); -server = tcp.createServer(function (connection) { +server = net.createServer(function (connection) { connection.addListener("connect", function () { assert.equal(false, connection.write(body)); connection.close(); @@ -24,7 +24,7 @@ npauses = 0; var paused = false; -client = tcp.createConnection(PORT); +client = net.createConnection(PORT); client.setEncoding("ascii"); client.addListener("data", function (d) { chars_recved += d.length;