Skip to content

Commit

Permalink
Cleanup async handling in tests, fix auto-reconnect after a close, de…
Browse files Browse the repository at this point in the history
…-dupe logged event
  • Loading branch information
djMax committed Oct 19, 2016
1 parent aa378b4 commit b2a7bf1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
sudo: false
language: node_js
node_js:
- "7"
- "6"
- "5"
- "4"
- "0.12"
- "0.11"
- "0.10"
- "0.8"
- "iojs"
env: NODE_TLS_REJECT_UNAUTHORIZED=0
9 changes: 8 additions & 1 deletion lib/winston-logstash.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ Logstash.prototype.connect = function () {
this.socket.on('close', function (had_error) {
self.connected = false;

if (self.terminating) {
return;
}

if (self.max_connect_retries < 0 || self.retries < self.max_connect_retries) {
if (!self.connecting) {
setTimeout(function () {
Expand Down Expand Up @@ -232,7 +236,6 @@ Logstash.prototype.flush = function () {

for (var i = 0; i < self.log_queue.length; i++) {
self.sendLog(self.log_queue[i].message, self.log_queue[i].callback);
self.emit('logged');
}
self.log_queue.length = 0;
};
Expand All @@ -244,3 +247,7 @@ Logstash.prototype.sendLog = function (message, callback) {
self.socket.write(message + '\n');
callback();
};

Logstash.prototype.getQueueLength = function () {
return this.log_queue.length;
};
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "winston-logstash",
"version": "0.2.11",
"version": "0.3.0",
"description": "A Logstash transport for winston",
"main": "./lib/winston-logstash",
"homepage": "https://github.com/jaakkos/winston-logstash",
Expand All @@ -18,10 +18,10 @@
"winston": ">=0.7.3"
},
"devDependencies": {
"winston": ">=0.7.3",
"mocha": ">=2.2.4",
"chai": ">=2.3.0",
"timekeeper": ">=0.0.5"
"mocha": ">=2.2.4",
"timekeeper": ">=0.0.5",
"winston": ">=0.7.3"
},
"keywords": [
"logging",
Expand All @@ -36,6 +36,7 @@
"contributors": [
"Jaakko Suutarla <jaakko@suutarla.com>",
"Chris Saylor <cjsaylor@gmail.com>",
"djmax",
"gwhitelaw",
"Ivan Fraixedes",
"kgoerlitz",
Expand Down
30 changes: 17 additions & 13 deletions test/winston-logstash_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ describe('winston-logstash transport', function() {
}

function createTestServer(port, on_data) {
var server = net.createServer(port, function (socket) {
var server = net.createServer(function (socket) {
socket.on('end', function () { });
socket.on('data', on_data);
socket.once('data', on_data);
});
server.listen(port, function() {});

Expand All @@ -50,7 +50,7 @@ describe('winston-logstash transport', function() {
};
var server = tls.createServer(serverOptions, function(socket) {
socket.on('end', function () { });
socket.on('data', on_data);
socket.once('data', on_data);
});
server.listen(port, function() {});

Expand Down Expand Up @@ -143,16 +143,18 @@ describe('winston-logstash transport', function() {
});

// Teardown
afterEach(function () {
afterEach(function (done) {
if (logger) {
logger.close();
}
timekeeper.reset();
if (test_server) {
test_server.close(function () {});
test_server.close(function () {
test_server = null;
logger = null;
done();
});
}
timekeeper.reset();
test_server = null;
logger = null;
});

});
Expand Down Expand Up @@ -225,16 +227,18 @@ describe('winston-logstash transport', function() {
});

// Teardown
afterEach(function () {
afterEach(function (done) {
if (logger) {
logger.close();
}
timekeeper.reset();
if (test_server) {
test_server.close(function () {});
test_server.close(function () {
test_server = null;
logger = null;
done();
});
}
timekeeper.reset();
test_server = null;
logger = null;
});
});

Expand Down

0 comments on commit b2a7bf1

Please sign in to comment.