Skip to content

Commit

Permalink
Created test test case
Browse files Browse the repository at this point in the history
  • Loading branch information
gictor committed Feb 17, 2017
1 parent fbad154 commit 5433fbe
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 104 deletions.
32 changes: 16 additions & 16 deletions lib/bunyan-slack.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ var BunyanSlack = function (options, error) {

if(options.rate_limit_interval !== undefined) {
this.rate_limit_interval = options.rate_limit_interval;
// Since slack can only handle one request per second
// messages are stacked on a queue and we pick the last
// message each second and send to slack.
setInterval(this.intervalRunner.bind(this), this.rate_limit_interval);
}
// Since slack can only handle one request per second
// messages are stacked on a queue and we pick the last
// message each second and send to slack.
setInterval(this.intervalRunner.bind(this), this.rate_limit_interval);
}

this.nameFromLevel = {
10: 'trace',
Expand Down Expand Up @@ -80,16 +80,16 @@ BunyanSlack.prototype.write = function Write(record) {
message = extend(base, message);

if(self.rate_limit_interval !== undefined) {
this.messageQueue.push(message);
} else {
request.post({
url: self.webhook_url,
body: JSON.stringify(message)
})
.on('error', function(err) {
return self.error(err);
});
}
this.messageQueue.push(message);
} else {
request.post({
url: self.webhook_url,
body: JSON.stringify(message)
})
.on('error', function(err) {
return self.error(err);
});
}
};

BunyanSlack.prototype.intervalRunner = function intervalRunner() {
Expand All @@ -103,7 +103,7 @@ BunyanSlack.prototype.intervalRunner = function intervalRunner() {
return self.error(err);
});

}
}
};

module.exports = BunyanSlack;
186 changes: 98 additions & 88 deletions test/bunyan_slack_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,42 +56,42 @@ describe('bunyan-slack', function() {
};
log.info('foobar');
sinon.assert.calledWith(request.post, expectedResponse);
});
});

it('should only deliver last message if there is a message burst', function() {
var log = Bunyan.createLogger({
name: 'myapp',
stream: new BunyanSlack({
webhook_url: 'mywebhookurl',
channel: '#bunyan-slack',
username: '@sethpollack',
icon_emoji: ':smile:',
icon_url: 'http://www.gravatar.com/avatar/3f5ce68fb8b38a5e08e7abe9ac0a34f1?s=200',
rate_limit_interval: 1000
}),
level: 'info'
});
it('should only deliver last message if there is a message burst', function() {
var log = Bunyan.createLogger({
name: 'myapp',
stream: new BunyanSlack({
webhook_url: 'mywebhookurl',
channel: '#bunyan-slack',
username: '@sethpollack',
icon_emoji: ':smile:',
icon_url: 'http://www.gravatar.com/avatar/3f5ce68fb8b38a5e08e7abe9ac0a34f1?s=200',
rate_limit_interval: 1000
}),
level: 'info'
});

var expectedResponse = {
body: JSON.stringify({
channel: '#bunyan-slack',
username: '@sethpollack',
icon_url: 'http://www.gravatar.com/avatar/3f5ce68fb8b38a5e08e7abe9ac0a34f1?s=200',
icon_emoji: ':smile:',
text: '[INFO] foobar 999'
}),
url: 'mywebhookurl'
};
var expectedResponse = {
body: JSON.stringify({
channel: '#bunyan-slack',
username: '@sethpollack',
icon_url: 'http://www.gravatar.com/avatar/3f5ce68fb8b38a5e08e7abe9ac0a34f1?s=200',
icon_emoji: ':smile:',
text: '[INFO] foobar 999'
}),
url: 'mywebhookurl'
};

for(var i=0; i>1000; i++) {
log.info('foobar ' + i);
}
for(var i=0; i>1000; i++) {
log.info('foobar ' + i);
}

setTimeout(function() {
sinon.assert.calledWith(request.post, expectedResponse);
}, 1000);
setTimeout(function() {
sinon.assert.calledWith(request.post, expectedResponse);
}, 1000);

});
});

it('should use the custom formatter', function() {
var log = Bunyan.createLogger({
Expand Down Expand Up @@ -146,7 +146,7 @@ describe('bunyan-slack', function() {

log.info('foobar');
sinon.assert.calledWith(request.post, expectedResponse);
});
});
});

describe('error handler', function() {
Expand Down Expand Up @@ -180,73 +180,83 @@ describe('bunyan-slack', function() {
});
log.info('foobar');
errorHandler.firstCall.args[1]('FAKE ERROR');
});
});
});

describe('logger arguments', function() {
it('should accept a single string argument', function() {
var log = Bunyan.createLogger({
name: 'myapp',
stream: new BunyanSlack({
webhook_url: 'mywebhookurl'
}),
level: 'info'
});
it('should accept a single string argument', function () {
var log = Bunyan.createLogger({
name: 'myapp',
stream: new BunyanSlack({
webhook_url: 'mywebhookurl'
}),
level: 'info'
});

var expectedResponse = {
body: JSON.stringify({
text: '[INFO] foobar'
}),
url: 'mywebhookurl'
};
var expectedResponse = {
body: JSON.stringify({
text: '[INFO] foobar'
}),
url: 'mywebhookurl'
};

log.info('foobar');
sinon.assert.calledWith(request.post, expectedResponse);
});
log.info('foobar');
sinon.assert.calledWith(request.post, expectedResponse);
});

it('should accept a single object argument', function() {
var log = Bunyan.createLogger({
name: 'myapp',
stream: new BunyanSlack({
webhook_url: 'mywebhookurl',
customFormatter: function(record, levelName) {
return {text: util.format('[%s] %s', levelName.toUpperCase(), record.error)};
}
}),
level: 'info'
});
it('should accept a single object argument', function () {
var log = Bunyan.createLogger({
name: 'myapp',
stream: new BunyanSlack({
webhook_url: 'mywebhookurl',
customFormatter: function (record, levelName) {
return {text: util.format('[%s] %s', levelName.toUpperCase(), record.error)};
}
}),
level: 'info'
});

var expectedResponse = {
body: JSON.stringify({
text: '[INFO] foobar'
}),
url: 'mywebhookurl'
};
var expectedResponse = {
body: JSON.stringify({
text: '[INFO] foobar'
}),
url: 'mywebhookurl'
};

log.info({error: 'foobar'});
sinon.assert.calledWith(request.post, expectedResponse);
log.info({error: 'foobar'});
sinon.assert.calledWith(request.post, expectedResponse);
});

it('should accept an object and string as arguments', function() {
var log = Bunyan.createLogger({
name: 'myapp',
stream: new BunyanSlack({
webhook_url: 'mywebhookurl',
customFormatter: function(record, levelName) {
return {text: util.format('[%s] %s & %s', levelName.toUpperCase(), record.error, record.msg)};
}
}),
level: 'info'
});
it('should accept an object and string as arguments', function () {
var log = Bunyan.createLogger({
name: 'myapp',
stream: new BunyanSlack({
webhook_url: 'mywebhookurl',
customFormatter: function (record, levelName) {
return {text: util.format('[%s] %s & %s', levelName.toUpperCase(), record.error, record.msg)};
}
}),
level: 'info'
});

var expectedResponse = {
body: JSON.stringify({
text: '[INFO] this is the error & this is the message'
}),
url: 'mywebhookurl'
};
log.info({error: 'this is the error'}, 'this is the message');
sinon.assert.calledWith(request.post, expectedResponse);
var expectedResponse = {
body: JSON.stringify({
text: '[INFO] this is the error & this is the message'
}),
url: 'mywebhookurl'
};
log.info({error: 'this is the error'}, 'this is the message');
sinon.assert.calledWith(request.post, expectedResponse);
});

it('should return a empty message queue after running the interval', function () {
var bs = new BunyanSlack({
webhook_url: 'mywebhookurl',
rate_limit_interval: 1000
});
bs.messageQueue.push('message');
bs.intervalRunner();
expect(bs.messageQueue.pop()).to.equal(undefined);
});
});
});
Expand Down

0 comments on commit 5433fbe

Please sign in to comment.