Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit 44234e9

Browse files
committed
Fix a few 'listening' race conditions
in test-net-timeout test-http-client-parse-error
1 parent 0ac2ef9 commit 44234e9

File tree

2 files changed

+42
-41
lines changed

2 files changed

+42
-41
lines changed

test/pummel/test-net-timeout.js

+32-32
Original file line numberDiff line numberDiff line change
@@ -32,44 +32,44 @@ var echo_server = net.createServer(function (socket) {
3232

3333
echo_server.listen(common.PORT, function () {
3434
console.log("server listening at " + common.PORT);
35-
});
3635

37-
var client = net.createConnection(common.PORT);
38-
client.setEncoding("UTF8");
39-
client.setTimeout(0); // disable the timeout for client
40-
client.addListener("connect", function () {
41-
console.log("client connected.");
42-
client.write("hello\r\n");
43-
});
36+
var client = net.createConnection(common.PORT);
37+
client.setEncoding("UTF8");
38+
client.setTimeout(0); // disable the timeout for client
39+
client.addListener("connect", function () {
40+
console.log("client connected.");
41+
client.write("hello\r\n");
42+
});
4443

45-
client.addListener("data", function (chunk) {
46-
assert.equal("hello\r\n", chunk);
47-
if (exchanges++ < 5) {
48-
setTimeout(function () {
49-
console.log("client write 'hello'");
50-
client.write("hello\r\n");
51-
}, 500);
52-
53-
if (exchanges == 5) {
54-
console.log("wait for timeout - should come in " + timeout + " ms");
55-
starttime = new Date;
56-
console.dir(starttime);
44+
client.addListener("data", function (chunk) {
45+
assert.equal("hello\r\n", chunk);
46+
if (exchanges++ < 5) {
47+
setTimeout(function () {
48+
console.log("client write 'hello'");
49+
client.write("hello\r\n");
50+
}, 500);
51+
52+
if (exchanges == 5) {
53+
console.log("wait for timeout - should come in " + timeout + " ms");
54+
starttime = new Date;
55+
console.dir(starttime);
56+
}
5757
}
58-
}
59-
});
58+
});
6059

61-
client.addListener("timeout", function () {
62-
throw new Error("client timeout - this shouldn't happen");
63-
});
60+
client.addListener("timeout", function () {
61+
throw new Error("client timeout - this shouldn't happen");
62+
});
6463

65-
client.addListener("end", function () {
66-
console.log("client end");
67-
client.end();
68-
});
64+
client.addListener("end", function () {
65+
console.log("client end");
66+
client.end();
67+
});
6968

70-
client.addListener("close", function () {
71-
console.log("client disconnect");
72-
echo_server.close();
69+
client.addListener("close", function () {
70+
console.log("client disconnect");
71+
echo_server.close();
72+
});
7373
});
7474

7575
process.addListener("exit", function () {

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

+10-9
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,19 @@ var srv = net.createServer(function(c) {
1212

1313
c.addListener('end', function() { c.end(); });
1414
});
15-
srv.listen(common.PORT, '127.0.0.1');
16-
17-
var hc = http.createClient(common.PORT, '127.0.0.1');
18-
hc.request('GET', '/').end();
1915

2016
var parseError = false;
2117

22-
hc.on('error', function (e) {
23-
console.log("got error from client");
24-
srv.close();
25-
assert.ok(e.message.indexOf("Parse Error") >= 0);
26-
parseError = true;
18+
srv.listen(common.PORT, '127.0.0.1', function () {
19+
var hc = http.createClient(common.PORT, '127.0.0.1');
20+
hc.request('GET', '/').end();
21+
22+
hc.on('error', function (e) {
23+
console.log("got error from client");
24+
srv.close();
25+
assert.ok(e.message.indexOf("Parse Error") >= 0);
26+
parseError = true;
27+
});
2728
});
2829

2930

0 commit comments

Comments
 (0)