From 7059be19ecc90c0ad2d68b60672e84169b6990a2 Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Thu, 12 Aug 2010 01:38:42 +0200 Subject: [PATCH] Fix http and net tests failing due to race condition Plus some minor cosmetic corrections --- test/simple/test-http-1.0.js | 28 ++++--- test/simple/test-http-chunked.js | 18 +++-- test/simple/test-http-client-race-2.js | 94 +++++++++++----------- test/simple/test-http-client-race.js | 34 ++++---- test/simple/test-http-client-upload.js | 38 ++++----- test/simple/test-http-head-response-has-no-body.js | 22 ++--- test/simple/test-http-malformed-request.js | 20 ++--- test/simple/test-http-proxy.js | 2 +- test/simple/test-http-server.js | 57 ++++++------- test/simple/test-http-tls.js | 92 ++++++++++----------- test/simple/test-http-wget.js | 42 +++++----- test/simple/test-http.js | 41 +++++----- test/simple/test-net-binary.js | 40 ++++----- test/simple/test-net-keepalive.js | 23 +++--- test/simple/test-net-tls.js | 66 +++++++-------- 15 files changed, 322 insertions(+), 295 deletions(-) diff --git a/test/simple/test-http-1.0.js b/test/simple/test-http-1.0.js index 7d010dd..6dbad06 100644 --- a/test/simple/test-http-1.0.js +++ b/test/simple/test-http-1.0.js @@ -16,23 +16,25 @@ var server = http.createServer(function (req, res) { }) server.listen(common.PORT); -var c = net.createConnection(common.PORT); +server.addListener("listening", function() { + var c = net.createConnection(common.PORT); -c.setEncoding("utf8"); + c.setEncoding("utf8"); -c.addListener("connect", function () { - c.write( "GET / HTTP/1.0\r\n\r\n" ); -}); + c.addListener("connect", function () { + c.write( "GET / HTTP/1.0\r\n\r\n" ); + }); -c.addListener("data", function (chunk) { - console.log(chunk); - server_response += chunk; -}); + c.addListener("data", function (chunk) { + console.log(chunk); + server_response += chunk; + }); -c.addListener("end", function () { - client_got_eof = true; - c.end(); - server.close(); + c.addListener("end", function () { + client_got_eof = true; + c.end(); + server.close(); + }); }); process.addListener("exit", function () { diff --git a/test/simple/test-http-chunked.js b/test/simple/test-http-chunked.js index fa1127b..9a6b2e1 100644 --- a/test/simple/test-http-chunked.js +++ b/test/simple/test-http-chunked.js @@ -10,11 +10,13 @@ var server = http.createServer(function(req, res) { }); server.listen(common.PORT); -http.cat("http://127.0.0.1:"+common.PORT+"/", "utf8", function (err, data) { - if (err) throw err; - assert.equal('string', typeof data); - console.log('here is the response:'); - assert.equal(UTF8_STRING, data); - console.log(data); - server.close(); -}) +server.addListener("listening", function() { + http.cat("http://127.0.0.1:"+common.PORT+"/", "utf8", function (err, data) { + if (err) throw err; + assert.equal('string', typeof data); + console.log('here is the response:'); + assert.equal(UTF8_STRING, data); + console.log(data); + server.close(); + }); +}); \ No newline at end of file diff --git a/test/simple/test-http-client-race-2.js b/test/simple/test-http-client-race-2.js index fbfa957..51caf39 100644 --- a/test/simple/test-http-client-race-2.js +++ b/test/simple/test-http-client-race-2.js @@ -23,68 +23,70 @@ var server = http.createServer(function (req, res) { }; res.writeHead(200, { "Content-Type": "text/plain" - , "Content-Length": body.length - }); + , "Content-Length": body.length + }); res.end(body); }); server.listen(common.PORT); -var client = http.createClient(common.PORT); - var body1 = ""; var body2 = ""; var body3 = ""; -// -// Client #1 is assigned Parser #1 -// -var req1 = client.request("/1") -req1.end(); -req1.addListener('response', function (res1) { - res1.setBodyEncoding("utf8"); +server.addListener("listening", function() { + var client = http.createClient(common.PORT); - res1.addListener('data', function (chunk) { - body1 += chunk; - }); + // + // Client #1 is assigned Parser #1 + // + var req1 = client.request("/1") + req1.end(); + req1.addListener('response', function (res1) { + res1.setEncoding("utf8"); - res1.addListener('end', function () { - // - // Delay execution a little to allow the "close" event to be processed - // (required to trigger this bug!) - // - setTimeout(function () { - // - // The bug would introduce itself here: Client #2 would be allocated the - // parser that previously belonged to Client #1. But we're not finished - // with Client #1 yet! - // - var client2 = http.createClient(common.PORT); + res1.addListener('data', function (chunk) { + body1 += chunk; + }); + res1.addListener('end', function () { // - // At this point, the bug would manifest itself and crash because the - // internal state of the parser was no longer valid for use by Client #1. + // Delay execution a little to allow the "close" event to be processed + // (required to trigger this bug!) // - var req2 = client.request("/2"); - req2.end(); - req2.addListener('response', function (res2) { - res2.setBodyEncoding("utf8"); - res2.addListener('data', function (chunk) { body2 += chunk; }); - res2.addListener('end', function () { + setTimeout(function () { + // + // The bug would introduce itself here: Client #2 would be allocated the + // parser that previously belonged to Client #1. But we're not finished + // with Client #1 yet! + // + var client2 = http.createClient(common.PORT); + + // + // At this point, the bug would manifest itself and crash because the + // internal state of the parser was no longer valid for use by Client #1. + // + var req2 = client.request("/2"); + req2.end(); + req2.addListener('response', function (res2) { + res2.setEncoding("utf8"); + res2.addListener('data', function (chunk) { body2 += chunk; }); + res2.addListener('end', function () { - // - // Just to be really sure we've covered all our bases, execute a - // request using client2. - // - var req3 = client2.request("/3"); - req3.end(); - req3.addListener('response', function (res3) { - res3.setBodyEncoding("utf8"); - res3.addListener('data', function (chunk) { body3 += chunk }); - res3.addListener('end', function() { server.close(); }); + // + // Just to be really sure we've covered all our bases, execute a + // request using client2. + // + var req3 = client2.request("/3"); + req3.end(); + req3.addListener('response', function (res3) { + res3.setEncoding("utf8"); + res3.addListener('data', function (chunk) { body3 += chunk }); + res3.addListener('end', function() { server.close(); }); + }); }); }); - }); - }, 500); + }, 500); + }); }); }); diff --git a/test/simple/test-http-client-race.js b/test/simple/test-http-client-race.js index 3e80d2f..deb1990 100644 --- a/test/simple/test-http-client-race.js +++ b/test/simple/test-http-client-race.js @@ -15,27 +15,29 @@ var server = http.createServer(function (req, res) { }); server.listen(common.PORT); -var client = http.createClient(common.PORT); - var body1 = ""; var body2 = ""; -var req1 = client.request("/1") -req1.end(); -req1.addListener('response', function (res1) { - res1.setBodyEncoding("utf8"); +server.addListener("listening", function() { + var client = http.createClient(common.PORT); - res1.addListener('data', function (chunk) { - body1 += chunk; - }); + var req1 = client.request("/1") + req1.end(); + req1.addListener('response', function (res1) { + res1.setEncoding("utf8"); + + res1.addListener('data', function (chunk) { + body1 += chunk; + }); - res1.addListener('end', function () { - var req2 = client.request("/2"); - req2.end(); - req2.addListener('response', function (res2) { - res2.setBodyEncoding("utf8"); - res2.addListener('data', function (chunk) { body2 += chunk; }); - res2.addListener('end', function () { server.close(); }); + res1.addListener('end', function () { + var req2 = client.request("/2"); + req2.end(); + req2.addListener('response', function (res2) { + res2.setEncoding("utf8"); + res2.addListener('data', function (chunk) { body2 += chunk; }); + res2.addListener('end', function () { server.close(); }); + }); }); }); }); diff --git a/test/simple/test-http-client-upload.js b/test/simple/test-http-client-upload.js index 22136c1..cac5e88 100644 --- a/test/simple/test-http-client-upload.js +++ b/test/simple/test-http-client-upload.js @@ -8,7 +8,7 @@ var client_res_complete = false; var server = http.createServer(function(req, res) { assert.equal("POST", req.method); - req.setBodyEncoding("utf8"); + req.setEncoding("utf8"); req.addListener('data', function (chunk) { console.log("server got: " + JSON.stringify(chunk)); @@ -25,23 +25,25 @@ var server = http.createServer(function(req, res) { }); server.listen(common.PORT); -var client = http.createClient(common.PORT); -var req = client.request('POST', '/'); -req.write('1\n'); -req.write('2\n'); -req.write('3\n'); -req.end(); - -common.error("client finished sending request"); - -req.addListener('response', function(res) { - res.setEncoding("utf8"); - res.addListener('data', function(chunk) { - console.log(chunk); - }); - res.addListener('end', function() { - client_res_complete = true; - server.close(); +server.addListener("listening", function() { + var client = http.createClient(common.PORT); + var req = client.request('POST', '/'); + req.write('1\n'); + req.write('2\n'); + req.write('3\n'); + req.end(); + + common.error("client finished sending request"); + + req.addListener('response', function(res) { + res.setEncoding("utf8"); + res.addListener('data', function(chunk) { + console.log(chunk); + }); + res.addListener('end', function() { + client_res_complete = true; + server.close(); + }); }); }); diff --git a/test/simple/test-http-head-response-has-no-body.js b/test/simple/test-http-head-response-has-no-body.js index 987fa50..13b3ead 100644 --- a/test/simple/test-http-head-response-has-no-body.js +++ b/test/simple/test-http-head-response-has-no-body.js @@ -13,17 +13,19 @@ var server = http.createServer(function(req, res) { }); server.listen(common.PORT); -responseComplete = false; +var responseComplete = false; -var req = http.createClient(common.PORT).request('HEAD', '/') -common.error('req'); -req.end(); -req.addListener('response', function (res) { - common.error('response'); - res.addListener('end', function() { - common.error('response end'); - server.close(); - responseComplete = true; +server.addListener("listening", function() { + var req = http.createClient(common.PORT).request('HEAD', '/') + common.error('req'); + req.end(); + req.addListener('response', function (res) { + common.error('response'); + res.addListener('end', function() { + common.error('response end'); + server.close(); + responseComplete = true; + }); }); }); diff --git a/test/simple/test-http-malformed-request.js b/test/simple/test-http-malformed-request.js index aedd6c4..b47cae5 100644 --- a/test/simple/test-http-malformed-request.js +++ b/test/simple/test-http-malformed-request.js @@ -10,24 +10,26 @@ url = require("url"); nrequests_completed = 0; nrequests_expected = 1; -var s = http.createServer(function (req, res) { +var server = http.createServer(function (req, res) { console.log("req: " + JSON.stringify(url.parse(req.url))); res.writeHead(200, {"Content-Type": "text/plain"}); res.write("Hello World"); res.end(); - if (++nrequests_completed == nrequests_expected) s.close(); + if (++nrequests_completed == nrequests_expected) server.close(); }); -s.listen(common.PORT); +server.listen(common.PORT); -var c = net.createConnection(common.PORT); -c.addListener("connect", function () { - c.write("GET /hello?foo=%99bar HTTP/1.1\r\n\r\n"); - c.end(); -}); +server.addListener("listening", function() { + var c = net.createConnection(common.PORT); + c.addListener("connect", function () { + c.write("GET /hello?foo=%99bar HTTP/1.1\r\n\r\n"); + c.end(); + }); -// TODO add more! + // TODO add more! +}); process.addListener("exit", function () { assert.equal(nrequests_expected, nrequests_completed); diff --git a/test/simple/test-http-proxy.js b/test/simple/test-http-proxy.js index 6f355f8..8a0a013 100644 --- a/test/simple/test-http-proxy.js +++ b/test/simple/test-http-proxy.js @@ -43,7 +43,7 @@ function startReq () { req.addListener('response', function (res) { common.debug("got res"); assert.equal(200, res.statusCode); - res.setBodyEncoding("utf8"); + res.setEncoding("utf8"); res.addListener('data', function (chunk) { body += chunk; }); res.addListener('end', function () { proxy.close(); diff --git a/test/simple/test-http-server.js b/test/simple/test-http-server.js index f3da98d..4b9b776 100644 --- a/test/simple/test-http-server.js +++ b/test/simple/test-http-server.js @@ -10,7 +10,7 @@ var requests_sent = 0; var server_response = ""; var client_got_eof = false; -http.createServer(function (req, res) { +var server = http.createServer(function (req, res) { res.id = request_number; req.id = request_number++; @@ -45,41 +45,44 @@ http.createServer(function (req, res) { res.end(); }, 1); -}).listen(common.PORT); +}); +server.listen(common.PORT); -var c = net.createConnection(common.PORT); +server.addListener("listening", function() { + var c = net.createConnection(common.PORT); -c.setEncoding("utf8"); + c.setEncoding("utf8"); -c.addListener("connect", function () { - c.write( "GET /hello?hello=world&foo=b==ar HTTP/1.1\r\n\r\n" ); - requests_sent += 1; -}); + c.addListener("connect", function () { + c.write( "GET /hello?hello=world&foo=b==ar HTTP/1.1\r\n\r\n" ); + requests_sent += 1; + }); -c.addListener("data", function (chunk) { - server_response += chunk; + c.addListener("data", function (chunk) { + server_response += chunk; - if (requests_sent == 1) { - c.write("POST /quit HTTP/1.1\r\n\r\n"); - requests_sent += 1; - } + if (requests_sent == 1) { + c.write("POST /quit HTTP/1.1\r\n\r\n"); + requests_sent += 1; + } - if (requests_sent == 2) { - c.write("GET / HTTP/1.1\r\nX-X: foo\r\n\r\n" - +"GET / HTTP/1.1\r\nX-X: bar\r\n\r\n"); - c.end(); - assert.equal(c.readyState, "readOnly"); - requests_sent += 2; - } + if (requests_sent == 2) { + c.write("GET / HTTP/1.1\r\nX-X: foo\r\n\r\n" + +"GET / HTTP/1.1\r\nX-X: bar\r\n\r\n"); + c.end(); + assert.equal(c.readyState, "readOnly"); + requests_sent += 2; + } -}); + }); -c.addListener("end", function () { - client_got_eof = true; -}); + c.addListener("end", function () { + client_got_eof = true; + }); -c.addListener("close", function () { - assert.equal(c.readyState, "closed"); + c.addListener("close", function () { + assert.equal(c.readyState, "closed"); + }); }); process.addListener("exit", function () { diff --git a/test/simple/test-http-tls.js b/test/simple/test-http-tls.js index 05dab64..373e4dd 100644 --- a/test/simple/test-http-tls.js +++ b/test/simple/test-http-tls.js @@ -37,11 +37,11 @@ var https_server = http.createServer(function (req, res) { var peerDN = JSON.stringify(req.connection.getPeerCertificate()); assert.equal(verified, true); assert.equal(peerDN, '{"subject":"/C=UK/ST=Acknack Ltd/L=Rhys Jones' - + '/O=node.js/OU=Test TLS Certificate/CN=localhost",' - + '"issuer":"/C=UK/ST=Acknack Ltd/L=Rhys Jones/O=node.js' - + '/OU=Test TLS Certificate/CN=localhost","valid_from":' - + '"Nov 11 09:52:22 2009 GMT","valid_to":' - + '"Nov 6 09:52:22 2029 GMT"}'); + + '/O=node.js/OU=Test TLS Certificate/CN=localhost",' + + '"issuer":"/C=UK/ST=Acknack Ltd/L=Rhys Jones/O=node.js' + + '/OU=Test TLS Certificate/CN=localhost","valid_from":' + + '"Nov 11 09:52:22 2009 GMT","valid_to":' + + '"Nov 6 09:52:22 2029 GMT"}'); if (req.id == 0) { assert.equal("GET", req.method); @@ -74,52 +74,54 @@ var https_server = http.createServer(function (req, res) { https_server.setSecure(credentials); https_server.listen(common.PORT); -var c = net.createConnection(common.PORT); - -c.setEncoding("utf8"); +https_server.addListener("listening", function() { + var c = net.createConnection(common.PORT); + + c.setEncoding("utf8"); + + c.addListener("connect", function () { + c.setSecure(credentials); + }); + + c.addListener("secure", function () { + var verified = c.verifyPeer(); + var peerDN = JSON.stringify(c.getPeerCertificate()); + assert.equal(verified, true); + assert.equal(peerDN, '{"subject":"/C=UK/ST=Acknack Ltd/L=Rhys Jones' + + '/O=node.js/OU=Test TLS Certificate/CN=localhost",' + + '"issuer":"/C=UK/ST=Acknack Ltd/L=Rhys Jones/O=node.js' + + '/OU=Test TLS Certificate/CN=localhost","valid_from":' + + '"Nov 11 09:52:22 2009 GMT","valid_to":' + + '"Nov 6 09:52:22 2029 GMT"}'); + c.write( "GET /hello?hello=world&foo=b==ar HTTP/1.1\r\n\r\n" ); + requests_sent += 1; + }); -c.addListener("connect", function () { - c.setSecure(credentials); -}); + c.addListener("data", function (chunk) { + server_response += chunk; -c.addListener("secure", function () { - var verified = c.verifyPeer(); - var peerDN = JSON.stringify(c.getPeerCertificate()); - assert.equal(verified, true); - assert.equal(peerDN, '{"subject":"/C=UK/ST=Acknack Ltd/L=Rhys Jones' - + '/O=node.js/OU=Test TLS Certificate/CN=localhost",' - + '"issuer":"/C=UK/ST=Acknack Ltd/L=Rhys Jones/O=node.js' - + '/OU=Test TLS Certificate/CN=localhost","valid_from":' - + '"Nov 11 09:52:22 2009 GMT","valid_to":' - + '"Nov 6 09:52:22 2029 GMT"}'); - c.write( "GET /hello?hello=world&foo=b==ar HTTP/1.1\r\n\r\n" ); - requests_sent += 1; -}); + if (requests_sent == 1) { + c.write("POST /quit HTTP/1.1\r\n\r\n"); + requests_sent += 1; + } -c.addListener("data", function (chunk) { - server_response += chunk; + if (requests_sent == 2) { + c.write("GET / HTTP/1.1\r\nX-X: foo\r\n\r\n" + +"GET / HTTP/1.1\r\nX-X: bar\r\n\r\n"); + c.end(); + assert.equal(c.readyState, "readOnly"); + requests_sent += 2; + } - if (requests_sent == 1) { - c.write("POST /quit HTTP/1.1\r\n\r\n"); - requests_sent += 1; - } + }); - if (requests_sent == 2) { - c.write("GET / HTTP/1.1\r\nX-X: foo\r\n\r\n" - +"GET / HTTP/1.1\r\nX-X: bar\r\n\r\n"); - c.end(); - assert.equal(c.readyState, "readOnly"); - requests_sent += 2; - } - -}); - -c.addListener("end", function () { - client_got_eof = true; -}); + c.addListener("end", function () { + client_got_eof = true; + }); -c.addListener("close", function () { - assert.equal(c.readyState, "closed"); + c.addListener("close", function () { + assert.equal(c.readyState, "closed"); + }); }); process.addListener("exit", function () { diff --git a/test/simple/test-http-wget.js b/test/simple/test-http-wget.js index 3863a8d..266f5f5 100644 --- a/test/simple/test-http-wget.js +++ b/test/simple/test-http-wget.js @@ -27,33 +27,35 @@ var server = http.createServer(function (req, res) { res.write("hello "); res.write("world\n"); res.end(); -}) +}); server.listen(common.PORT); -var c = net.createConnection(common.PORT); +server.addListener("listening", function() { + var c = net.createConnection(common.PORT); -c.setEncoding("utf8"); + c.setEncoding("utf8"); -c.addListener("connect", function () { - c.write("GET / HTTP/1.0\r\n" + - "Connection: Keep-Alive\r\n\r\n"); -}); + c.addListener("connect", function () { + c.write("GET / HTTP/1.0\r\n" + + "Connection: Keep-Alive\r\n\r\n"); + }); -c.addListener("data", function (chunk) { - console.log(chunk); - server_response += chunk; -}); + c.addListener("data", function (chunk) { + console.log(chunk); + server_response += chunk; + }); -c.addListener("end", function () { - client_got_eof = true; - console.log('got end'); - c.end(); -}); + c.addListener("end", function () { + client_got_eof = true; + console.log('got end'); + c.end(); + }); -c.addListener("close", function () { - connection_was_closed = true; - console.log('got close'); - server.close(); + c.addListener("close", function () { + connection_was_closed = true; + console.log('got close'); + server.close(); + }); }); process.addListener("exit", function () { diff --git a/test/simple/test-http.js b/test/simple/test-http.js index 5ad1b2c..96a452e 100644 --- a/test/simple/test-http.js +++ b/test/simple/test-http.js @@ -12,7 +12,7 @@ var responses_recvd = 0; var body0 = ""; var body1 = ""; -http.createServer(function (req, res) { +var server = http.createServer(function (req, res) { if (responses_sent == 0) { assert.equal("GET", req.method); assert.equal("/hello", url.parse(req.url).pathname); @@ -39,30 +39,33 @@ http.createServer(function (req, res) { }); //assert.equal("127.0.0.1", res.connection.remoteAddress); -}).listen(common.PORT); - -var client = http.createClient(common.PORT); -var req = client.request("/hello", {"Accept": "*/*", "Foo": "bar"}); -req.end(); -req.addListener('response', function (res) { - assert.equal(200, res.statusCode); - responses_recvd += 1; - res.setEncoding("utf8"); - res.addListener('data', function (chunk) { body0 += chunk; }); - common.debug("Got /hello response"); }); +server.listen(common.PORT); -setTimeout(function () { - req = client.request("POST", "/world"); +server.addListener("listening", function() { + var client = http.createClient(common.PORT); + var req = client.request("/hello", {"Accept": "*/*", "Foo": "bar"}); req.end(); - req.addListener('response',function (res) { + req.addListener('response', function (res) { assert.equal(200, res.statusCode); responses_recvd += 1; - res.setBodyEncoding("utf8"); - res.addListener('data', function (chunk) { body1 += chunk; }); - common.debug("Got /world response"); + res.setEncoding("utf8"); + res.addListener('data', function (chunk) { body0 += chunk; }); + common.debug("Got /hello response"); }); -}, 1); + + setTimeout(function () { + req = client.request("POST", "/world"); + req.end(); + req.addListener('response',function (res) { + assert.equal(200, res.statusCode); + responses_recvd += 1; + res.setEncoding("utf8"); + res.addListener('data', function (chunk) { body1 += chunk; }); + common.debug("Got /world response"); + }); + }, 1); +}); process.addListener("exit", function () { common.debug("responses_recvd: " + responses_recvd); diff --git a/test/simple/test-net-binary.js b/test/simple/test-net-binary.js index 89d4105..572e6f7 100644 --- a/test/simple/test-net-binary.js +++ b/test/simple/test-net-binary.js @@ -32,29 +32,31 @@ var echoServer = tcp.createServer(function (connection) { echoServer.listen(common.PORT); var recv = ""; -var j = 0; -var c = tcp.createConnection(common.PORT); +echoServer.addListener("listening", function() { + var j = 0; + var c = tcp.createConnection(common.PORT); -c.setEncoding("binary"); -c.addListener("data", function (chunk) { - if (j < 256) { - common.error("write " + j); - c.write(String.fromCharCode(j), "binary"); - j++; - } else { - c.end(); - } - recv += chunk; -}); + c.setEncoding("binary"); + c.addListener("data", function (chunk) { + if (j < 256) { + common.error("write " + j); + c.write(String.fromCharCode(j), "binary"); + j++; + } else { + c.end(); + } + recv += chunk; + }); -c.addListener("connect", function () { - c.write(binaryString, "binary"); -}); + c.addListener("connect", function () { + c.write(binaryString, "binary"); + }); -c.addListener("close", function () { - p(recv); - echoServer.close(); + c.addListener("close", function () { + p(recv); + echoServer.close(); + }); }); process.addListener("exit", function () { diff --git a/test/simple/test-net-keepalive.js b/test/simple/test-net-keepalive.js index 56221ae..11698aa 100644 --- a/test/simple/test-net-keepalive.js +++ b/test/simple/test-net-keepalive.js @@ -15,15 +15,16 @@ var echoServer = net.createServer(function (connection) { }); echoServer.listen(common.PORT); -var clientConnection = net.createConnection(common.PORT); -clientConnection.setTimeout(0); - -setTimeout( function() { - // make sure both connections are still open - assert.equal(serverConnection.readyState,"open"); - assert.equal(clientConnection.readyState,"open"); - serverConnection.end(); - clientConnection.end(); - echoServer.close(); -}, 1200); +echoServer.addListener("listening", function() { + var clientConnection = net.createConnection(common.PORT); + clientConnection.setTimeout(0); + setTimeout( function() { + // make sure both connections are still open + assert.equal(serverConnection.readyState,"open"); + assert.equal(clientConnection.readyState,"open"); + serverConnection.end(); + clientConnection.end(); + echoServer.close(); + }, 1200); +}); \ No newline at end of file diff --git a/test/simple/test-net-tls.js b/test/simple/test-net-tls.js index 0f757ca..4054b05 100644 --- a/test/simple/test-net-tls.js +++ b/test/simple/test-net-tls.js @@ -37,11 +37,11 @@ var secureServer = net.createServer(function (connection) { var peerDN = JSON.stringify(connection.getPeerCertificate()); assert.equal(verified, true); assert.equal(peerDN, '{"subject":"/C=UK/ST=Acknack Ltd/L=Rhys Jones' - + '/O=node.js/OU=Test TLS Certificate/CN=localhost",' - + '"issuer":"/C=UK/ST=Acknack Ltd/L=Rhys Jones/O=node.js' - + '/OU=Test TLS Certificate/CN=localhost","valid_from":' - + '"Nov 11 09:52:22 2009 GMT","valid_to":' - + '"Nov 6 09:52:22 2029 GMT"}'); + + '/O=node.js/OU=Test TLS Certificate/CN=localhost",' + + '"issuer":"/C=UK/ST=Acknack Ltd/L=Rhys Jones/O=node.js' + + '/OU=Test TLS Certificate/CN=localhost","valid_from":' + + '"Nov 11 09:52:22 2009 GMT","valid_to":' + + '"Nov 6 09:52:22 2029 GMT"}'); }); @@ -58,40 +58,40 @@ var secureServer = net.createServer(function (connection) { }); secureServer.listen(common.PORT); -var secureClient = net.createConnection(common.PORT); +secureServer.addListener("listening", function() { + var secureClient = net.createConnection(common.PORT); -secureClient.setEncoding("UTF8"); -secureClient.addListener("connect", function () { - secureClient.setSecure(credentials); -}); + secureClient.setEncoding("UTF8"); + secureClient.addListener("connect", function () { + secureClient.setSecure(credentials); + }); -secureClient.addListener("secure", function () { - gotSecureClient = true; - var verified = secureClient.verifyPeer(); - var peerDN = JSON.stringify(secureClient.getPeerCertificate()); - assert.equal(verified, true); - assert.equal(peerDN, '{"subject":"/C=UK/ST=Acknack Ltd/L=Rhys Jones' - + '/O=node.js/OU=Test TLS Certificate/CN=localhost",' - + '"issuer":"/C=UK/ST=Acknack Ltd/L=Rhys Jones/O=node.js' - + '/OU=Test TLS Certificate/CN=localhost","valid_from":' - + '"Nov 11 09:52:22 2009 GMT","valid_to":' - + '"Nov 6 09:52:22 2029 GMT"}'); - - secureClient.write(testData); - secureClient.end(); -}); + secureClient.addListener("secure", function () { + gotSecureClient = true; + var verified = secureClient.verifyPeer(); + var peerDN = JSON.stringify(secureClient.getPeerCertificate()); + assert.equal(verified, true); + assert.equal(peerDN, '{"subject":"/C=UK/ST=Acknack Ltd/L=Rhys Jones' + + '/O=node.js/OU=Test TLS Certificate/CN=localhost",' + + '"issuer":"/C=UK/ST=Acknack Ltd/L=Rhys Jones/O=node.js' + + '/OU=Test TLS Certificate/CN=localhost","valid_from":' + + '"Nov 11 09:52:22 2009 GMT","valid_to":' + + '"Nov 6 09:52:22 2029 GMT"}'); + + secureClient.write(testData); + secureClient.end(); + }); -secureClient.addListener("data", function (chunk) { - clientData += chunk; -}); + secureClient.addListener("data", function (chunk) { + clientData += chunk; + }); -secureClient.addListener("end", function () { - assert.equal(clientData, testData); + secureClient.addListener("end", function () { + assert.equal(clientData, testData); + }); }); process.addListener("exit", function () { assert.ok(gotSecureServer, "Did not get secure event for server"); assert.ok(gotSecureClient, "Did not get secure event for clientr"); -}); - - +}); \ No newline at end of file -- 2.7.4