summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2011-01-12 13:29:43 -0800
committerRyan Dahl <ry@tinyclouds.org>2011-01-12 15:58:50 -0800
commitaab4f37e20a0a66819e852fc7e89007d2addd7cc (patch)
treed2b9d8cd892c7e65bf762bcef8f87a3e28d20b3a
parent82afd0da357ad14bfdf17d63a7da68a0bcf6593d (diff)
downloadnode-aab4f37e20a0a66819e852fc7e89007d2addd7cc.tar.gz
Fix test-net-connect-buffer
Change to end() behavior in 82afd0 was breaking it. end() should wait for connection before dumping. Changed test-net-connect-timeout to use destroy() instead.
-rw-r--r--lib/net.js8
-rw-r--r--test/simple/test-net-connect-timeout.js60
2 files changed, 52 insertions, 16 deletions
diff --git a/lib/net.js b/lib/net.js
index 17fc0e8bf..725d131e8 100644
--- a/lib/net.js
+++ b/lib/net.js
@@ -825,6 +825,8 @@ Stream.prototype.setEncoding = function (encoding) {
function doConnect (socket, port, host) {
+ timeout.active(socket);
+
try {
connect(socket.fd, port, host);
} catch (e) {
@@ -885,7 +887,7 @@ Stream.prototype.connect = function () {
if (self.fd) throw new Error('Stream already opened');
if (!self._readWatcher) throw new Error('No readWatcher');
- timeout.active(socket);
+ timeout.active(self);
self._connecting = true; // set false in doConnect
self.writable = true;
@@ -1039,9 +1041,7 @@ Stream.prototype.close = function (data, encoding) {
};
Stream.prototype.end = function (data, encoding) {
- if (this._connecting) {
- this.destroy();
- } else if (this.writable) {
+ if (this.writable) {
if (data) this.write(data, encoding);
if (this._writeQueueLast() !== END_OF_FILE) {
this._writeQueue.push(END_OF_FILE);
diff --git a/test/simple/test-net-connect-timeout.js b/test/simple/test-net-connect-timeout.js
index cd0cc47ee..ad7dfa92a 100644
--- a/test/simple/test-net-connect-timeout.js
+++ b/test/simple/test-net-connect-timeout.js
@@ -1,37 +1,73 @@
// This example attempts to time out before the connection is established
// https://groups.google.com/forum/#!topic/nodejs/UE0ZbfLt6t8
// https://groups.google.com/forum/#!topic/nodejs-dev/jR7-5UDqXkw
+//
+// TODO: how to do this without relying on the responses of specific sites?
var common = require('../common');
var net = require('net');
var assert = require('assert');
var start = new Date();
-var gotTimeout = false;
-var gotConnect = false;
+
+var gotTimeout0 = false;
+var gotTimeout1 = false;
+
+var gotConnect0 = false;
+var gotConnect1 = false;
+
var T = 100;
-var socket = net.createConnection(9999, '23.23.23.23');
-socket.setTimeout(T);
+// With DNS
+
+var socket0 = net.createConnection(9999, 'google.com');
+
+socket0.setTimeout(T);
+
+socket0.on('timeout', function() {
+ console.error("timeout");
+ gotTimeout0 = true;
+ var now = new Date();
+ assert.ok(now - start < T + 500);
+ socket0.destroy();
+});
+
+socket0.on('connect', function() {
+ console.error("connect");
+ gotConnect0 = true;
+ socket0.destroy();
+});
+
+
+// Without DNS
+var socket1 = net.createConnection(9999, '24.24.24.24');
-socket.on('timeout', function() {
+socket1.setTimeout(T);
+
+socket1.on('timeout', function() {
console.error("timeout");
- gotTimeout = true;
+ gotTimeout1 = true;
var now = new Date();
assert.ok(now - start < T + 500);
- socket.end();
+ socket1.destroy();
});
-socket.on('connect', function() {
+socket1.on('connect', function() {
console.error("connect");
- gotConnect = true;
- socket.end();
+ gotConnect1 = true;
+ socket1.destroy();
});
+
+
+
process.on('exit', function() {
- assert.ok(gotTimeout);
- assert.ok(!gotConnect);
+ assert.ok(gotTimeout0);
+ assert.ok(!gotConnect0);
+
+ assert.ok(gotTimeout1);
+ assert.ok(!gotConnect1);
});