Browse Source

Fix a few 'listening' race conditions

in
test-net-timeout
test-http-client-parse-error
v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
44234e9cc7
  1. 64
      test/pummel/test-net-timeout.js
  2. 19
      test/simple/test-http-client-parse-error.js

64
test/pummel/test-net-timeout.js

@ -32,44 +32,44 @@ var echo_server = net.createServer(function (socket) {
echo_server.listen(common.PORT, function () { echo_server.listen(common.PORT, function () {
console.log("server listening at " + common.PORT); console.log("server listening at " + common.PORT);
});
var client = net.createConnection(common.PORT); var client = net.createConnection(common.PORT);
client.setEncoding("UTF8"); client.setEncoding("UTF8");
client.setTimeout(0); // disable the timeout for client client.setTimeout(0); // disable the timeout for client
client.addListener("connect", function () { client.addListener("connect", function () {
console.log("client connected."); console.log("client connected.");
client.write("hello\r\n"); client.write("hello\r\n");
}); });
client.addListener("data", function (chunk) { client.addListener("data", function (chunk) {
assert.equal("hello\r\n", chunk); assert.equal("hello\r\n", chunk);
if (exchanges++ < 5) { if (exchanges++ < 5) {
setTimeout(function () { setTimeout(function () {
console.log("client write 'hello'"); console.log("client write 'hello'");
client.write("hello\r\n"); client.write("hello\r\n");
}, 500); }, 500);
if (exchanges == 5) { if (exchanges == 5) {
console.log("wait for timeout - should come in " + timeout + " ms"); console.log("wait for timeout - should come in " + timeout + " ms");
starttime = new Date; starttime = new Date;
console.dir(starttime); console.dir(starttime);
}
} }
} });
});
client.addListener("timeout", function () { client.addListener("timeout", function () {
throw new Error("client timeout - this shouldn't happen"); throw new Error("client timeout - this shouldn't happen");
}); });
client.addListener("end", function () { client.addListener("end", function () {
console.log("client end"); console.log("client end");
client.end(); client.end();
}); });
client.addListener("close", function () { client.addListener("close", function () {
console.log("client disconnect"); console.log("client disconnect");
echo_server.close(); echo_server.close();
});
}); });
process.addListener("exit", function () { process.addListener("exit", function () {

19
test/simple/test-http-client-parse-error.js

@ -12,18 +12,19 @@ var srv = net.createServer(function(c) {
c.addListener('end', function() { c.end(); }); c.addListener('end', function() { c.end(); });
}); });
srv.listen(common.PORT, '127.0.0.1');
var hc = http.createClient(common.PORT, '127.0.0.1');
hc.request('GET', '/').end();
var parseError = false; var parseError = false;
hc.on('error', function (e) { srv.listen(common.PORT, '127.0.0.1', function () {
console.log("got error from client"); var hc = http.createClient(common.PORT, '127.0.0.1');
srv.close(); hc.request('GET', '/').end();
assert.ok(e.message.indexOf("Parse Error") >= 0);
parseError = true; hc.on('error', function (e) {
console.log("got error from client");
srv.close();
assert.ok(e.message.indexOf("Parse Error") >= 0);
parseError = true;
});
}); });

Loading…
Cancel
Save