Skip to content

Commit

Permalink
Fix the race condition to detect if the time out has fired. Simply set
Browse files Browse the repository at this point in the history
a 'timed_out' flag true instead of testing the wall time duration.
  • Loading branch information
mheffner committed Apr 26, 2012
1 parent fe91370 commit e6c6ce0
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions test/graphite_tests.js
Expand Up @@ -42,24 +42,25 @@ var statsd_send = function(data,sock,host,port,cb){
var collect_for = function(server,timeout,cb){
var received = [];
var in_flight = 0;
var start_time = new Date().getTime();
var timed_out = false;
var collector = function(req,res){
in_flight += 1;
var body = '';
req.on('data',function(data){ body += data; });
req.on('end',function(){
received = received.concat(body.split("\n"));
in_flight -= 1;
if((in_flight < 1) && (new Date().getTime() > (start_time + timeout))){
if((in_flight < 1) && timed_out){
server.removeListener('request',collector);
cb(received);
}
});
}

setTimeout(function (){
server.removeListener('connection',collector);
if((in_flight < 1)){
timed_out = true;
if((in_flight < 1)) {
server.removeListener('connection',collector);
cb(received);
}
},timeout);
Expand Down

0 comments on commit e6c6ce0

Please sign in to comment.