Browse Source

Adjust a few more tests to work with net2

v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
0918bb2070
  1. 61
      lib/net.js
  2. 6
      test/pummel/test-tcp-pingpong-delay.js
  3. 8
      test/pummel/test-tcp-pingpong.js
  4. 8
      test/pummel/test-tcp-throttle.js

61
lib/net.js

@ -73,6 +73,22 @@ function allocRecvBuffer () {
recvBuffer.used = 0; 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) { function initSocket (self) {
self._readWatcher = ioWatchers.alloc(); self._readWatcher = ioWatchers.alloc();
self._readWatcher.callback = function () { self._readWatcher.callback = function () {
@ -163,22 +179,9 @@ function initSocket (self) {
self._writeQueueSize = 0; // in bytes, not to be confused with _writeQueue.length! self._writeQueueSize = 0; // in bytes, not to be confused with _writeQueue.length!
self._writeMessageQueueSize = 0; // number of messages remaining to be sent 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 = ioWatchers.alloc();
self._writeWatcher.callback = self._doFlush; self._writeWatcher.socket = self;
self._writeWatcher.callback = _doFlush;
self.writable = false; 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 () { Socket.prototype._allocateSendBuffer = function () {
var b = buffers.alloc(1024); var b = buffers.alloc(1024);
b.used = 0; b.used = 0;
@ -351,7 +369,7 @@ Socket.prototype.flush = function () {
if (b == END_OF_FILE) { if (b == END_OF_FILE) {
self._shutdown(); self._shutdown();
return false; return true;
} }
if (b.sent == b.used) { if (b.sent == b.used) {
@ -379,19 +397,19 @@ Socket.prototype.flush = function () {
} }
if (bytesWritten === null) { if (bytesWritten === null) {
// could not flush everything // EAGAIN
debug('write EAGAIN');
self._writeWatcher.start(); self._writeWatcher.start();
assert(self._writeQueueSize > 0); assert(self._writeQueueSize > 0);
return false; return false;
} } else if (b.isFd) {
if (b.isFd) {
b.sent = b.used; b.sent = b.used;
self._writeMessageQueueSize -= 1; self._writeMessageQueueSize -= 1;
//debug('sent fd: ' + fdToSend); //debug('sent fd: ' + fdToSend);
} else { } else {
b.sent += bytesWritten; b.sent += bytesWritten;
self._writeQueueSize -= bytesWritten; self._writeQueueSize -= bytesWritten;
//debug('bytes sent: ' + b.sent); debug('bytes sent: ' + b.sent);
} }
} }
if (self._writeWatcher) self._writeWatcher.stop(); if (self._writeWatcher) self._writeWatcher.stop();
@ -453,11 +471,8 @@ Socket.prototype.connect = function () {
self.type = 'tcp'; self.type = 'tcp';
// TODO dns resolution on arguments[1] // TODO dns resolution on arguments[1]
var port = arguments[0]; var port = arguments[0];
var yyy = xxx++;
lookupDomainName(arguments[1], function (ip) { lookupDomainName(arguments[1], function (ip) {
debug('doConnect ' + self.fd + ' yyy=' + yyy);
doConnect(self, port, ip); doConnect(self, port, ip);
debug('doConnect done ' + self.fd + ' yyy=' + yyy);
}); });
} }
}; };

6
test/pummel/test-tcp-pingpong-delay.js

@ -1,5 +1,5 @@
require("../common"); require("../common");
tcp = require("tcp"); net = require("net");
var tests_run = 0; var tests_run = 0;
@ -10,7 +10,7 @@ function pingPongTest (port, host, on_complete) {
var count = 0; var count = 0;
var client_closed = false; var client_closed = false;
var server = tcp.createServer(function (socket) { var server = net.createServer(function (socket) {
socket.setEncoding("utf8"); socket.setEncoding("utf8");
socket.addListener("data", function (data) { socket.addListener("data", function (data) {
@ -44,7 +44,7 @@ function pingPongTest (port, host, on_complete) {
}); });
server.listen(port, host); server.listen(port, host);
var client = tcp.createConnection(port, host); var client = net.createConnection(port, host);
client.setEncoding("utf8"); client.setEncoding("utf8");

8
test/pummel/test-tcp-pingpong.js

@ -1,5 +1,5 @@
require("../common"); require("../common");
tcp = require("tcp"); net = require("net");
var tests_run = 0; var tests_run = 0;
@ -8,13 +8,13 @@ function pingPongTest (port, host, on_complete) {
var count = 0; var count = 0;
var sent_final_ping = false; 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 !== null);
assert.equal(true, socket.remoteAddress !== undefined); assert.equal(true, socket.remoteAddress !== undefined);
if (host === "127.0.0.1") if (host === "127.0.0.1")
assert.equal(socket.remoteAddress, "127.0.0.1"); assert.equal(socket.remoteAddress, "127.0.0.1");
else if (host == null) else if (host == null)
assert.equal(socket.remoteAddress, "127.0.0.1"); assert.equal(socket.remoteAddress, "::1");
socket.setEncoding("utf8"); socket.setEncoding("utf8");
socket.setNoDelay(); socket.setNoDelay();
@ -42,7 +42,7 @@ function pingPongTest (port, host, on_complete) {
}); });
server.listen(port, host); server.listen(port, host);
var client = tcp.createConnection(port, host); var client = net.createConnection(port, host);
client.setEncoding("utf8"); client.setEncoding("utf8");

8
test/pummel/test-tcp-throttle.js

@ -1,6 +1,6 @@
require("../common"); require("../common");
tcp = require("tcp"); net = require("net");
N = 60*1024; // 30kb N = 160*1024; // 30kb
puts("build big string"); puts("build big string");
var body = ""; var body = "";
@ -10,7 +10,7 @@ for (var i = 0; i < N; i++) {
puts("start server on port " + PORT); puts("start server on port " + PORT);
server = tcp.createServer(function (connection) { server = net.createServer(function (connection) {
connection.addListener("connect", function () { connection.addListener("connect", function () {
assert.equal(false, connection.write(body)); assert.equal(false, connection.write(body));
connection.close(); connection.close();
@ -24,7 +24,7 @@ npauses = 0;
var paused = false; var paused = false;
client = tcp.createConnection(PORT); client = net.createConnection(PORT);
client.setEncoding("ascii"); client.setEncoding("ascii");
client.addListener("data", function (d) { client.addListener("data", function (d) {
chars_recved += d.length; chars_recved += d.length;

Loading…
Cancel
Save