Commit 77688f49 authored by Alan Szlosek's avatar Alan Szlosek

migrate more tests to tap

parent 27492cf0
Pipeline #11106547 passed with stage
in 56 seconds
......@@ -13,7 +13,7 @@ tap.test('linebuffer', function(test) {
test.equals(expectedLine, line);
if (lines.length == 0) {
test.done();
test.end();
}
});
test.plan(4);
......
......@@ -17,6 +17,6 @@ tap.test('createMessage', function(test) {
test.equal(message.localAddress, '192.168.1.2')
test.equal(message.localPort, 10000);
// TODO: should also make sure it has receivedMilliseconds
test.done();
test.end();
});
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();
},
var Harbinger = require('../index');
var server = new Harbinger.Input.StatsD();
var udp = require('dgram').createSocket('udp4');
udp.unref();
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);
tap.test('statsd.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.end();
test.done();
};
server.close();
};
test.expect(5);
this.server.callback(cb);
this.server.bind(8125, '127.0.0.1');
test.plan(5);
server.callback(cb);
server.bind(8125, '127.0.0.1');
udp.send(message, 0, message.length, 8125, '127.0.0.1');
}
udp.send(message, 0, message.length, 8125, '127.0.0.1');
});
var tap = require('tap');
var moment = require('moment');
var harbinger = require('../index');
tap.test('syslog.singleLines', function(test) {
var dt = new Date();
......@@ -17,7 +20,7 @@ tap.test('syslog.singleLines', function(test) {
msg: '(alan) CMD (php /home/alan/coding/projects/profiling/scripts/process.php)'
}
];
test.expect(1);
test.plan(1);
for (var i = 0; i < lines.length; i++) {
......
var tap = require('tap');
var redis = require('redis');
var moment = require('moment');
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();
}
);
}
);
var redisClient = redis.createClient(6379, 'redis');
redisClient.on("error", function (err) {
console.log("Error " + err);
});
redisClient.on("end", function(err) {
//console.log("Redis connection closed");
});
var series = function(func, thisarg, args, callback) {
var out = [];
var trap = function(err) {
if (err) {
// What
callback(err);
return;
}
var result = Array.prototype.slice.call(arguments);
result.shift();
out.push( result );
next();
};
var next = function() {
// Get next args array
var arg;
if (args.length == 0) {
callback(null, out);
return;
}
arg = args.shift();
// Make sure last arg is our callback
arg.push( trap );
//console.log('calling ', arg);
func.apply(thisarg, arg);
};
next();
};
tap.test('timeseries.count.single', function(test) {
var self = this;
var ts = new Harbinger.Timeseries(redisClient, 60, 'testing');
var secondsBucket = moment().unix() - 100;
test.plan(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
*/
tap.test('timeseries.getCounts', function(test) {
var self = this;
var ts = new Harbinger.Timeseries(redisClient, 60, 'testing2');
var secondsBucket = moment().unix() - 200;
test.plan(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();
}
);
}
);
});
tap.test('timeseries.getCountStatistics', function(test) {
var self = this;
var ts = new Harbinger.Timeseries(redisClient, 60, 'testing2');
var secondsBucket = moment().unix() - 300;
test.plan(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();
}
);
}
);
});
tap.test('timeseries.getTimes', function(test) {
var self = this;
var ts = new Harbinger.Timeseries(redisClient, 60, 'testing-times');
var secondsBucket = moment().unix() - 200;
test.plan(2);
series(
Harbinger.Timeseries.prototype.time,
ts,
[
[secondsBucket, 200],
[secondsBucket + 2, 900]
],
function(err, out) {
if (err) {
console.log('Err', err);
test.end();
return;
}
test.ok(true);
ts.getTimes(
secondsBucket,
secondsBucket + 2,
function(err, out) {
if (err) {
console.log('Err2', err);
test.end();
return;
}
test.equals( out.join(','), '200,900');
test.end();
}
);
}
);
});
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