Browse Source

Fix http and net tests failing due to race condition

Plus some minor cosmetic corrections
v0.7.4-release
Bert Belder 15 years ago
committed by Ryan Dahl
parent
commit
7059be19ec
  1. 2
      test/simple/test-http-1.0.js
  2. 4
      test/simple/test-http-chunked.js
  3. 12
      test/simple/test-http-client-race-2.js
  4. 10
      test/simple/test-http-client-race.js
  5. 4
      test/simple/test-http-client-upload.js
  6. 4
      test/simple/test-http-head-response-has-no-body.js
  7. 8
      test/simple/test-http-malformed-request.js
  8. 2
      test/simple/test-http-proxy.js
  9. 7
      test/simple/test-http-server.js
  10. 2
      test/simple/test-http-tls.js
  11. 4
      test/simple/test-http-wget.js
  12. 9
      test/simple/test-http.js
  13. 4
      test/simple/test-net-binary.js
  14. 3
      test/simple/test-net-keepalive.js
  15. 4
      test/simple/test-net-tls.js

2
test/simple/test-http-1.0.js

@ -16,6 +16,7 @@ var server = http.createServer(function (req, res) {
}) })
server.listen(common.PORT); server.listen(common.PORT);
server.addListener("listening", function() {
var c = net.createConnection(common.PORT); var c = net.createConnection(common.PORT);
c.setEncoding("utf8"); c.setEncoding("utf8");
@ -34,6 +35,7 @@ c.addListener("end", function () {
c.end(); c.end();
server.close(); server.close();
}); });
});
process.addListener("exit", function () { process.addListener("exit", function () {
var m = server_response.split("\r\n\r\n"); var m = server_response.split("\r\n\r\n");

4
test/simple/test-http-chunked.js

@ -10,6 +10,7 @@ var server = http.createServer(function(req, res) {
}); });
server.listen(common.PORT); server.listen(common.PORT);
server.addListener("listening", function() {
http.cat("http://127.0.0.1:"+common.PORT+"/", "utf8", function (err, data) { http.cat("http://127.0.0.1:"+common.PORT+"/", "utf8", function (err, data) {
if (err) throw err; if (err) throw err;
assert.equal('string', typeof data); assert.equal('string', typeof data);
@ -17,4 +18,5 @@ http.cat("http://127.0.0.1:"+common.PORT+"/", "utf8", function (err, data) {
assert.equal(UTF8_STRING, data); assert.equal(UTF8_STRING, data);
console.log(data); console.log(data);
server.close(); server.close();
}) });
});

12
test/simple/test-http-client-race-2.js

@ -29,19 +29,20 @@ var server = http.createServer(function (req, res) {
}); });
server.listen(common.PORT); server.listen(common.PORT);
var client = http.createClient(common.PORT);
var body1 = ""; var body1 = "";
var body2 = ""; var body2 = "";
var body3 = ""; var body3 = "";
server.addListener("listening", function() {
var client = http.createClient(common.PORT);
// //
// Client #1 is assigned Parser #1 // Client #1 is assigned Parser #1
// //
var req1 = client.request("/1") var req1 = client.request("/1")
req1.end(); req1.end();
req1.addListener('response', function (res1) { req1.addListener('response', function (res1) {
res1.setBodyEncoding("utf8"); res1.setEncoding("utf8");
res1.addListener('data', function (chunk) { res1.addListener('data', function (chunk) {
body1 += chunk; body1 += chunk;
@ -67,7 +68,7 @@ req1.addListener('response', function (res1) {
var req2 = client.request("/2"); var req2 = client.request("/2");
req2.end(); req2.end();
req2.addListener('response', function (res2) { req2.addListener('response', function (res2) {
res2.setBodyEncoding("utf8"); res2.setEncoding("utf8");
res2.addListener('data', function (chunk) { body2 += chunk; }); res2.addListener('data', function (chunk) { body2 += chunk; });
res2.addListener('end', function () { res2.addListener('end', function () {
@ -78,7 +79,7 @@ req1.addListener('response', function (res1) {
var req3 = client2.request("/3"); var req3 = client2.request("/3");
req3.end(); req3.end();
req3.addListener('response', function (res3) { req3.addListener('response', function (res3) {
res3.setBodyEncoding("utf8"); res3.setEncoding("utf8");
res3.addListener('data', function (chunk) { body3 += chunk }); res3.addListener('data', function (chunk) { body3 += chunk });
res3.addListener('end', function() { server.close(); }); res3.addListener('end', function() { server.close(); });
}); });
@ -87,6 +88,7 @@ req1.addListener('response', function (res1) {
}, 500); }, 500);
}); });
}); });
});
process.addListener("exit", function () { process.addListener("exit", function () {
assert.equal(body1_s, body1); assert.equal(body1_s, body1);

10
test/simple/test-http-client-race.js

@ -15,15 +15,16 @@ var server = http.createServer(function (req, res) {
}); });
server.listen(common.PORT); server.listen(common.PORT);
var client = http.createClient(common.PORT);
var body1 = ""; var body1 = "";
var body2 = ""; var body2 = "";
server.addListener("listening", function() {
var client = http.createClient(common.PORT);
var req1 = client.request("/1") var req1 = client.request("/1")
req1.end(); req1.end();
req1.addListener('response', function (res1) { req1.addListener('response', function (res1) {
res1.setBodyEncoding("utf8"); res1.setEncoding("utf8");
res1.addListener('data', function (chunk) { res1.addListener('data', function (chunk) {
body1 += chunk; body1 += chunk;
@ -33,12 +34,13 @@ req1.addListener('response', function (res1) {
var req2 = client.request("/2"); var req2 = client.request("/2");
req2.end(); req2.end();
req2.addListener('response', function (res2) { req2.addListener('response', function (res2) {
res2.setBodyEncoding("utf8"); res2.setEncoding("utf8");
res2.addListener('data', function (chunk) { body2 += chunk; }); res2.addListener('data', function (chunk) { body2 += chunk; });
res2.addListener('end', function () { server.close(); }); res2.addListener('end', function () { server.close(); });
}); });
}); });
}); });
});
process.addListener("exit", function () { process.addListener("exit", function () {
assert.equal(body1_s, body1); assert.equal(body1_s, body1);

4
test/simple/test-http-client-upload.js

@ -8,7 +8,7 @@ var client_res_complete = false;
var server = http.createServer(function(req, res) { var server = http.createServer(function(req, res) {
assert.equal("POST", req.method); assert.equal("POST", req.method);
req.setBodyEncoding("utf8"); req.setEncoding("utf8");
req.addListener('data', function (chunk) { req.addListener('data', function (chunk) {
console.log("server got: " + JSON.stringify(chunk)); console.log("server got: " + JSON.stringify(chunk));
@ -25,6 +25,7 @@ var server = http.createServer(function(req, res) {
}); });
server.listen(common.PORT); server.listen(common.PORT);
server.addListener("listening", function() {
var client = http.createClient(common.PORT); var client = http.createClient(common.PORT);
var req = client.request('POST', '/'); var req = client.request('POST', '/');
req.write('1\n'); req.write('1\n');
@ -44,6 +45,7 @@ req.addListener('response', function(res) {
server.close(); server.close();
}); });
}); });
});
process.addListener("exit", function () { process.addListener("exit", function () {
assert.equal("1\n2\n3\n", sent_body); assert.equal("1\n2\n3\n", sent_body);

4
test/simple/test-http-head-response-has-no-body.js

@ -13,8 +13,9 @@ var server = http.createServer(function(req, res) {
}); });
server.listen(common.PORT); server.listen(common.PORT);
responseComplete = false; var responseComplete = false;
server.addListener("listening", function() {
var req = http.createClient(common.PORT).request('HEAD', '/') var req = http.createClient(common.PORT).request('HEAD', '/')
common.error('req'); common.error('req');
req.end(); req.end();
@ -26,6 +27,7 @@ req.addListener('response', function (res) {
responseComplete = true; responseComplete = true;
}); });
}); });
});
process.addListener('exit', function () { process.addListener('exit', function () {
assert.ok(responseComplete); assert.ok(responseComplete);

8
test/simple/test-http-malformed-request.js

@ -10,17 +10,18 @@ url = require("url");
nrequests_completed = 0; nrequests_completed = 0;
nrequests_expected = 1; 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))); console.log("req: " + JSON.stringify(url.parse(req.url)));
res.writeHead(200, {"Content-Type": "text/plain"}); res.writeHead(200, {"Content-Type": "text/plain"});
res.write("Hello World"); res.write("Hello World");
res.end(); res.end();
if (++nrequests_completed == nrequests_expected) s.close(); if (++nrequests_completed == nrequests_expected) server.close();
}); });
s.listen(common.PORT); server.listen(common.PORT);
server.addListener("listening", function() {
var c = net.createConnection(common.PORT); var c = net.createConnection(common.PORT);
c.addListener("connect", function () { c.addListener("connect", function () {
c.write("GET /hello?foo=%99bar HTTP/1.1\r\n\r\n"); c.write("GET /hello?foo=%99bar HTTP/1.1\r\n\r\n");
@ -28,6 +29,7 @@ c.addListener("connect", function () {
}); });
// TODO add more! // TODO add more!
});
process.addListener("exit", function () { process.addListener("exit", function () {
assert.equal(nrequests_expected, nrequests_completed); assert.equal(nrequests_expected, nrequests_completed);

2
test/simple/test-http-proxy.js

@ -43,7 +43,7 @@ function startReq () {
req.addListener('response', function (res) { req.addListener('response', function (res) {
common.debug("got res"); common.debug("got res");
assert.equal(200, res.statusCode); assert.equal(200, res.statusCode);
res.setBodyEncoding("utf8"); res.setEncoding("utf8");
res.addListener('data', function (chunk) { body += chunk; }); res.addListener('data', function (chunk) { body += chunk; });
res.addListener('end', function () { res.addListener('end', function () {
proxy.close(); proxy.close();

7
test/simple/test-http-server.js

@ -10,7 +10,7 @@ var requests_sent = 0;
var server_response = ""; var server_response = "";
var client_got_eof = false; var client_got_eof = false;
http.createServer(function (req, res) { var server = http.createServer(function (req, res) {
res.id = request_number; res.id = request_number;
req.id = request_number++; req.id = request_number++;
@ -45,8 +45,10 @@ http.createServer(function (req, res) {
res.end(); res.end();
}, 1); }, 1);
}).listen(common.PORT); });
server.listen(common.PORT);
server.addListener("listening", function() {
var c = net.createConnection(common.PORT); var c = net.createConnection(common.PORT);
c.setEncoding("utf8"); c.setEncoding("utf8");
@ -81,6 +83,7 @@ c.addListener("end", function () {
c.addListener("close", function () { c.addListener("close", function () {
assert.equal(c.readyState, "closed"); assert.equal(c.readyState, "closed");
}); });
});
process.addListener("exit", function () { process.addListener("exit", function () {
assert.equal(4, request_number); assert.equal(4, request_number);

2
test/simple/test-http-tls.js

@ -74,6 +74,7 @@ var https_server = http.createServer(function (req, res) {
https_server.setSecure(credentials); https_server.setSecure(credentials);
https_server.listen(common.PORT); https_server.listen(common.PORT);
https_server.addListener("listening", function() {
var c = net.createConnection(common.PORT); var c = net.createConnection(common.PORT);
c.setEncoding("utf8"); c.setEncoding("utf8");
@ -121,6 +122,7 @@ c.addListener("end", function () {
c.addListener("close", function () { c.addListener("close", function () {
assert.equal(c.readyState, "closed"); assert.equal(c.readyState, "closed");
}); });
});
process.addListener("exit", function () { process.addListener("exit", function () {
assert.equal(4, request_number); assert.equal(4, request_number);

4
test/simple/test-http-wget.js

@ -27,9 +27,10 @@ var server = http.createServer(function (req, res) {
res.write("hello "); res.write("hello ");
res.write("world\n"); res.write("world\n");
res.end(); res.end();
}) });
server.listen(common.PORT); server.listen(common.PORT);
server.addListener("listening", function() {
var c = net.createConnection(common.PORT); var c = net.createConnection(common.PORT);
c.setEncoding("utf8"); c.setEncoding("utf8");
@ -55,6 +56,7 @@ c.addListener("close", function () {
console.log('got close'); console.log('got close');
server.close(); server.close();
}); });
});
process.addListener("exit", function () { process.addListener("exit", function () {
var m = server_response.split("\r\n\r\n"); var m = server_response.split("\r\n\r\n");

9
test/simple/test-http.js

@ -12,7 +12,7 @@ var responses_recvd = 0;
var body0 = ""; var body0 = "";
var body1 = ""; var body1 = "";
http.createServer(function (req, res) { var server = http.createServer(function (req, res) {
if (responses_sent == 0) { if (responses_sent == 0) {
assert.equal("GET", req.method); assert.equal("GET", req.method);
assert.equal("/hello", url.parse(req.url).pathname); assert.equal("/hello", url.parse(req.url).pathname);
@ -39,8 +39,10 @@ http.createServer(function (req, res) {
}); });
//assert.equal("127.0.0.1", res.connection.remoteAddress); //assert.equal("127.0.0.1", res.connection.remoteAddress);
}).listen(common.PORT); });
server.listen(common.PORT);
server.addListener("listening", function() {
var client = http.createClient(common.PORT); var client = http.createClient(common.PORT);
var req = client.request("/hello", {"Accept": "*/*", "Foo": "bar"}); var req = client.request("/hello", {"Accept": "*/*", "Foo": "bar"});
req.end(); req.end();
@ -58,11 +60,12 @@ setTimeout(function () {
req.addListener('response',function (res) { req.addListener('response',function (res) {
assert.equal(200, res.statusCode); assert.equal(200, res.statusCode);
responses_recvd += 1; responses_recvd += 1;
res.setBodyEncoding("utf8"); res.setEncoding("utf8");
res.addListener('data', function (chunk) { body1 += chunk; }); res.addListener('data', function (chunk) { body1 += chunk; });
common.debug("Got /world response"); common.debug("Got /world response");
}); });
}, 1); }, 1);
});
process.addListener("exit", function () { process.addListener("exit", function () {
common.debug("responses_recvd: " + responses_recvd); common.debug("responses_recvd: " + responses_recvd);

4
test/simple/test-net-binary.js

@ -32,8 +32,9 @@ var echoServer = tcp.createServer(function (connection) {
echoServer.listen(common.PORT); echoServer.listen(common.PORT);
var recv = ""; var recv = "";
var j = 0;
echoServer.addListener("listening", function() {
var j = 0;
var c = tcp.createConnection(common.PORT); var c = tcp.createConnection(common.PORT);
c.setEncoding("binary"); c.setEncoding("binary");
@ -56,6 +57,7 @@ c.addListener("close", function () {
p(recv); p(recv);
echoServer.close(); echoServer.close();
}); });
});
process.addListener("exit", function () { process.addListener("exit", function () {
console.log("recv: " + JSON.stringify(recv)); console.log("recv: " + JSON.stringify(recv));

3
test/simple/test-net-keepalive.js

@ -15,6 +15,7 @@ var echoServer = net.createServer(function (connection) {
}); });
echoServer.listen(common.PORT); echoServer.listen(common.PORT);
echoServer.addListener("listening", function() {
var clientConnection = net.createConnection(common.PORT); var clientConnection = net.createConnection(common.PORT);
clientConnection.setTimeout(0); clientConnection.setTimeout(0);
@ -26,4 +27,4 @@ setTimeout( function() {
clientConnection.end(); clientConnection.end();
echoServer.close(); echoServer.close();
}, 1200); }, 1200);
});

4
test/simple/test-net-tls.js

@ -58,6 +58,7 @@ var secureServer = net.createServer(function (connection) {
}); });
secureServer.listen(common.PORT); secureServer.listen(common.PORT);
secureServer.addListener("listening", function() {
var secureClient = net.createConnection(common.PORT); var secureClient = net.createConnection(common.PORT);
secureClient.setEncoding("UTF8"); secureClient.setEncoding("UTF8");
@ -88,10 +89,9 @@ secureClient.addListener("data", function (chunk) {
secureClient.addListener("end", function () { secureClient.addListener("end", function () {
assert.equal(clientData, testData); assert.equal(clientData, testData);
}); });
});
process.addListener("exit", function () { process.addListener("exit", function () {
assert.ok(gotSecureServer, "Did not get secure event for server"); assert.ok(gotSecureServer, "Did not get secure event for server");
assert.ok(gotSecureClient, "Did not get secure event for clientr"); assert.ok(gotSecureClient, "Did not get secure event for clientr");
}); });

Loading…
Cancel
Save