Commit 5aa08a8d authored by Alan Szlosek's avatar Alan Szlosek

use tap for unit tests, and separate files

parent a548ab89
var tap = require('tap');
var moment = require('moment');
var Harbinger = require('../index');
tap.test('digest', function(test) {
test.plan(2);
var start = moment().unix();
var callback = function(counts, cache) {
var end = moment().unix();
// Make sure callback took 2 seconds to fire
test.equal(end - start, 2);
// Make sure we've seen two instances of message id 123
test.equal(counts[123], 2);
digest.stop();
test.end();
};
var digest = new Harbinger.Digest('10 second', 10, callback);
digest.add(123, {message: 'Test'});
digest.add(123, {message: 'Another'});
});
var tap = require('tap');
var Harbinger = require('../index');
tap.test('linebuffer', function(test) {
var lines = [
'hello',
'my name',
'',
'is Harbinger'
];
var lb = new Harbinger.LineBuffer(function(line) {
var expectedLine = lines.shift();
test.equals(expectedLine, line);
if (lines.length == 0) {
test.done();
}
});
test.plan(4);
lb.data("hel");
lb.data("lo\nmy");
// Currently LineBuffer is configured to send empty lines
lb.data(" name\n\n");
lb.data("is Harbinger\n");
});
var tap = require('tap');
var Harbinger = require('../index');
tap.test('createMessage', function(test) {
//var message = Harbinger.createMessage(payload, source, sourceFile, remoteAddress, localAddress, localPort);
var payload = {
sample: "payload"
};
var message = Harbinger.createMessage(payload, "customSource", "/tmp/source.log", "192.168.1.1", "192.168.1.2", 10000);
test.plan(6);
test.equal(message.payload.sample, 'payload');
test.equal(message.source, 'customSource')
test.equal(message.sourceFile, '/tmp/source.log')
test.equal(message.remoteAddress, '192.168.1.1')
test.equal(message.localAddress, '192.168.1.2')
test.equal(message.localPort, 10000);
// TODO: should also make sure it has receivedMilliseconds
test.done();
});
var tap = require('tap');
var server = new harbinger.Input.StatsD();
tap.test('statsd', function(test) {
setUp: function(callback) {
callback();
},
tearDown: function(callback) {
this.server.close();
callback();
},
basic: function(test) {
var message = "application.module.request.count:1|c\n";
var out = {
metric: 'application.module.request.count',
value: 1,
type: 'c',
sampleRate: null,
remoteAddress: '127.0.0.1'
};
var cb = function(err, metric, value, type, sampleRate, remoteAddress) {
test.equal(metric, out.metric);
test.equal(value, out.value);
test.equal(type, out.type);
test.equal(sampleRate, out.sampleRate);
test.equal(remoteAddress, out.remoteAddress);
test.done();
};
test.expect(5);
this.server.callback(cb);
this.server.bind(8125, '127.0.0.1');
udp.send(message, 0, message.length, 8125, '127.0.0.1');
}
});
var tap = require('tap');
tap.test('syslog.singleLines', function(test) {
var dt = new Date();
var dateTime = moment(dt).format('MMM D H:mm:ss');
var lines = [
// Where'd the year go?
"<9>" + dateTime + " satellite CRON[3666]: (alan) CMD (php /home/alan/coding/projects/profiling/scripts/process.php)"
];
var objects = [
{
facility: 1,
severity: 1,
tag: 'CRON[3666]',
time: '' + dt,
hostname: 'satellite',
msg: '(alan) CMD (php /home/alan/coding/projects/profiling/scripts/process.php)'
}
];
test.expect(1);
for (var i = 0; i < lines.length; i++) {
var out = harbinger.Parse.syslog( lines[i] );
// Eh, these fields
out.time = '' + out.time;
test.deepEqual(out, objects[i]);
}
test.done();
});
This diff is collapsed.
var tap = require('tap');
var Harbinger = require('../index');
tap.test('digest', function(test) {
setUp: function(callback) {
var redisClient = this.redisClient = redis.createClient(6379, 'redis');
redisClient.on("error", function (err) {
console.log("Error " + err);
});
redisClient.on("end", function(err) {
//console.log("Redis connection closed");
});
callback();
},
tearDown: function(callback) {
this.redisClient.quit();
callback();
},
count: {
single: function(test) {
var self = this;
var ts = new Harbinger.Timeseries(this.redisClient, 60, 'testing');
var secondsBucket = moment().unix() - 100;
test.expect(2);
ts.count(secondsBucket, 3, function(err) {
if (err) {
test.done();
return;
}
ts.getCounts(secondsBucket, secondsBucket, function(err, result) {
if (err) {
console.log(err);
test.done();
return;
}
test.equals(1, result.length);
test.equals(3, result[0]);
test.done();
});
});
},
// Calculate Error Rate every second, alert if rate goes above N
/*
To calculate the error rate, we have to read from multiple keys. Can use Redis mget command to do so
*/
bla: function(test) {
test.done();
},
multiple: function(test) {
var self = this;
var ts = new Harbinger.Timeseries(this.redisClient, 60, 'testing2');
var secondsBucket = moment().unix() - 200;
test.expect(2);
series(
Harbinger.Timeseries.prototype.count,
ts,
[
[secondsBucket, 3],
[secondsBucket + 2, 4]
],
function(err, out) {
if (err) {
console.log('Err', err);
test.done();
return;
}
test.ok(true);
ts.getCounts(
secondsBucket,
secondsBucket + 2,
function(err, out) {
if (err) {
console.log('Err2', err);
test.done();
return;
}
test.equals( out.join(','), '3,0,4');
test.done();
}
);
}
);
},
statistics: function(test) {
var self = this;
var ts = new Harbinger.Timeseries(this.redisClient, 60, 'testing2');
var secondsBucket = moment().unix() - 300;
test.expect(4);
series(
Harbinger.Timeseries.prototype.count,
ts,
[
[secondsBucket, 3],
[secondsBucket + 2, 6]
],
function(err, out) {
if (err) {
test.done();
return;
}
test.ok(true);
ts.getCountStatistics(
secondsBucket,
secondsBucket + 2,
function(err, stats) {
if (err) {
test.done();
return;
}
test.equals(3, stats.mean);
test.equals(3, stats.count);
test.equals(9, stats.sum);
test.done();
}
);
}
);
}
},
times: function (test) {
var self = this;
var ts = new Harbinger.Timeseries(this.redisClient, 60, 'testing-times');
var secondsBucket = moment().unix() - 200;
test.expect(2);
series(
Harbinger.Timeseries.prototype.time,
ts,
[
[secondsBucket, 200],
[secondsBucket + 2, 900]
],
function(err, out) {
if (err) {
console.log('Err', err);
test.done();
return;
}
test.ok(true);
ts.getTimes(
secondsBucket,
secondsBucket + 2,
function(err, out) {
if (err) {
console.log('Err2', err);
test.done();
return;
}
test.equals( out.join(','), '200,900');
test.done();
}
);
}
);
}
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment