Skip to content

Commit

Permalink
resolve merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
lazywithclass committed Apr 9, 2020
2 parents 2f94eca + 23c1f36 commit 6108e91
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
15 changes: 11 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ var util = require('util'),
assign = require('lodash.assign'),
isError = require('lodash.iserror'),
stringify = require('./lib/utils').stringify,
debug = require('./lib/utils').debug;

debug = require('./lib/utils').debug,
defaultFlushTimeoutMs = 10000;

var WinstonCloudWatch = function(options) {
winston.Transport.call(this, options);
Expand Down Expand Up @@ -132,10 +132,17 @@ WinstonCloudWatch.prototype.submit = function(callback) {
);
};

WinstonCloudWatch.prototype.kthxbye = function(callback) {
WinstonCloudWatch.prototype.kthxbye = function(callback) {
clearInterval(this.intervalId);
this.intervalId = null;
this.submit(callback);
this.flushTimeout = this.flushTimeout || (Date.now() + defaultFlushTimeoutMs);

this.submit((function(error) {
if (error) return callback(error);
if (isEmpty(this.logEvents)) return callback();
if (Date.now() > this.flushTimeout) return callback(new Error('Timeout reached while waiting for logs to submit'));
else setTimeout(this.kthxbye.bind(this, callback), 0);
}).bind(this));
};

winston.transports.CloudWatch = WinstonCloudWatch;
Expand Down
39 changes: 35 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe('index', function() {
after(function() {
mockery.deregisterAll();
mockery.disable();
clock.restore();
});

describe('construtor', function() {
Expand Down Expand Up @@ -212,9 +213,7 @@ describe('index', function() {
});

afterEach(function() {
stubbedCloudwatchIntegration = {
upload: sinon.spy()
};
stubbedCloudwatchIntegration.upload = sinon.spy();
console.error.restore();
});

Expand Down Expand Up @@ -246,7 +245,10 @@ describe('index', function() {
sinon.stub(global, 'setInterval');
sinon.stub(global, 'clearInterval');
transport = new WinstonCloudWatch({});
sinon.stub(transport, 'submit').yields();
sinon.stub(transport, 'submit').callsFake(function(cb){
this.logEvents.splice(0, 20);
cb();
});
});

afterEach(function() {
Expand All @@ -271,6 +273,35 @@ describe('index', function() {
done();
});
});

it('should not send all messages if called while posting', function(done) {
for (var index = 0; index < 30; index++) {
transport.add({ message: 'message' + index });
}

transport.kthxbye(function() {
transport.logEvents.length.should.equal(0);
done();
});

clock.tick(1);
});

it('should exit if logs are not cleared by the timeout period', function(done) {
transport.add({ message: 'message' });
transport.submit.callsFake(function(cb){
clock.tick(500);
cb(); // callback is called but logEvents is not cleared
});

transport.kthxbye(function(error) {
error.should.be.Error();
transport.logEvents.length.should.equal(1);
done();
});

clock.tick(1);
});
});

});

0 comments on commit 6108e91

Please sign in to comment.