Skip to content

Commit

Permalink
Fixed open file descriptor bug (#181) when using output option.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas committed Aug 8, 2016
1 parent 9cfde90 commit d31ff62
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/needle.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,10 @@ Needle.prototype.send_request = function(count, method, uri, config, post_data,
var file = fs.createWriteStream(config.output);
file.on('error', had_error);

out.on('end', function() {
if (file.writable) file.end();
});

out.on('readable', function() {
var chunk;
while (chunk = this.read()) {
Expand Down
39 changes: 39 additions & 0 deletions test/output_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ describe('with output option', function() {
stream.on('end', cb);
}

// this will only work in UNICES
function get_open_file_descriptors() {
var list = fs.readdirSync('/proc/self/fd');
return list.length;
}

var send_request = send_request_cb;

before(function(){
Expand Down Expand Up @@ -103,6 +109,17 @@ describe('with output option', function() {
})
})

it('closes the file descriptor', function(done) {
var open_descriptors = get_open_file_descriptors();
send_request(file + Math.random(), function(err, resp) {
setTimeout(function() {
var current_descriptors = get_open_file_descriptors();
open_descriptors.should.eql(current_descriptors);
done()
}, 10)
})
})

})

describe('for a JSON response', function() {
Expand Down Expand Up @@ -156,6 +173,17 @@ describe('with output option', function() {
})
})

it('closes the file descriptor', function(done) {
var open_descriptors = get_open_file_descriptors();
send_request(file + Math.random(), function(err, resp) {
setTimeout(function() {
var current_descriptors = get_open_file_descriptors();
open_descriptors.should.eql(current_descriptors);
done()
}, 10)
})
})

})

describe('for a binary file', function() {
Expand Down Expand Up @@ -213,6 +241,17 @@ describe('with output option', function() {
})
})

it('closes the file descriptor', function(done) {
var open_descriptors = get_open_file_descriptors();
send_request(file + Math.random(), function(err, resp) {
setTimeout(function() {
var current_descriptors = get_open_file_descriptors();
open_descriptors.should.eql(current_descriptors);
done()
}, 10)
})
})

})

})
Expand Down

0 comments on commit d31ff62

Please sign in to comment.