Browse Source

Fix race conditions in test-http-upgrade-client2

v0.7.4-release
Ryan Dahl 14 years ago
parent
commit
40f675b64b
  1. 76
      test/simple/test-http-upgrade-client2.js

76
test/simple/test-http-upgrade-client2.js

@ -12,51 +12,55 @@ server.on('upgrade', function(req, socket, head) {
socket.end(); socket.end();
}); });
}); });
server.listen(8000);
var client = http.createClient(8000);
function upgradeRequest(fn) { server.listen(common.PORT, function () {
var request = client.request('GET', '/', {
'Connection': 'Upgrade', var client = http.createClient(common.PORT);
'Upgrade': 'Test'
}); function upgradeRequest(fn) {
var request = client.request('GET', '/', {
var wasUpgrade = false; 'Connection': 'Upgrade',
'Upgrade': 'Test'
function onUpgrade(res, socket, head) { });
wasUpgrade = true;
client.removeListener('upgrade', onUpgrade); var wasUpgrade = false;
socket.end();
} function onUpgrade(res, socket, head) {
client.on('upgrade', onUpgrade); wasUpgrade = true;
function onEnd() { client.removeListener('upgrade', onUpgrade);
client.removeListener('end', onEnd); socket.end();
if (!wasUpgrade) {
throw new Error('hasn\'t received upgrade event');
} else {
fn && process.nextTick(fn);
} }
} client.on('upgrade', onUpgrade);
client.on('end', onEnd);
function onEnd() {
request.write('head'); client.removeListener('end', onEnd);
if (!wasUpgrade) {
throw new Error('hasn\'t received upgrade event');
} else {
fn && process.nextTick(fn);
}
}
client.on('end', onEnd);
request.write('head');
} }
successCount = 0; successCount = 0;
upgradeRequest(function() {
successCount++;
upgradeRequest(function() { upgradeRequest(function() {
successCount++; successCount++;
// Test pass upgradeRequest(function() {
console.log('Pass!'); successCount++;
client.end(); // Test pass
client.destroy(); console.log('Pass!');
server.close(); client.end();
client.destroy();
server.close();
});
}); });
}); });
process.on('exit', function () { process.on('exit', function () {

Loading…
Cancel
Save