Created
July 12, 2018 01:46
-
-
Save bdeitte/4ae7fd92edb490e2adcd22e38d62a5a6 to your computer and use it in GitHub Desktop.
Example for tests for https://github.com/brightcove/hot-shots/pull/79
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var dgram = require('dgram'); | |
var assert = require('assert'); | |
var net = require('net'); | |
var StatsD = require('../').StatsD; | |
/** | |
* Since sampling uses random, we need to patch Math.random() to always give | |
* a consistent result | |
*/ | |
Math.random = function () { | |
return 0.42; | |
}; | |
function createTCPServer(onListening) { | |
var server = net.createServer(function (socket) { | |
socket.setEncoding('ascii'); | |
socket.on('data', function (data) { | |
var metrics; | |
if (data) { | |
metrics = data.split('\n').filter(function (part) { | |
return part !== ''; | |
}); | |
server.emit('metrics', metrics); | |
} | |
}); | |
}); | |
server.on('listening', function () { | |
onListening(server.address()); | |
}); | |
server.listen(0, '127.0.0.1'); | |
return server; | |
} | |
function createUDPServer(onListening){ | |
var server = dgram.createSocket("udp4"); | |
server.on('message', function(message){ | |
var metrics = message.toString(); | |
server.emit('metrics', metrics); | |
}); | |
server.on('listening', function(){ | |
onListening(server.address()); | |
}); | |
server.bind(0, '127.0.0.1'); | |
return server; | |
} | |
describe('StatsD', function () { | |
var server; | |
var statsd; | |
afterEach(function () { | |
// Remove it from the namespace to not fail other tests | |
delete global.statsd; | |
statsd = null; | |
}); | |
describe('#globalTags TCP', function () { | |
it('should not add global tags if they are not specified', function (done) { | |
server = createTCPServer(function (address) { | |
statsd = new StatsD({ | |
host: address.address, | |
port: address.port, | |
protocol: 'tcp' | |
}); | |
statsd.increment('test'); | |
}); | |
server.on('metrics', function (metrics) { | |
assert.equal(metrics, 'test:1|c'); | |
server.close(); | |
done(); | |
}); | |
}); | |
it('should add global tags if they are specified', function (done) { | |
server = createTCPServer(function (address) { | |
statsd = new StatsD({ | |
host: address.address, | |
port: address.port, | |
global_tags: ['gtag'], | |
protocol: 'tcp' | |
}); | |
statsd.increment('test'); | |
}); | |
server.on('metrics', function (metrics) { | |
assert.equal(metrics, 'test:1|c|#gtag'); | |
server.close(); | |
done(); | |
}); | |
}); | |
it('should combine global tags and metric tags', function (done) { | |
server = createTCPServer(function (address) { | |
statsd = new StatsD({ | |
host: address.address, | |
port: address.port, | |
global_tags: ['gtag'], | |
protocol: 'tcp' | |
}); | |
statsd.increment('test', 1337, ['foo']); | |
}); | |
server.on('metrics', function (metrics) { | |
assert.equal(metrics, 'test:1337|c|#gtag,foo'); | |
server.close(); | |
done(); | |
}); | |
}); | |
}); | |
describe('#globalTags UDP', function () { | |
it('should not add global tags if they are not specified', function (done) { | |
server = createUDPServer(function (address) { | |
console.log(address); | |
statsd = new StatsD({ | |
host: address.address, | |
port: address.port, | |
}); | |
statsd.increment('test'); | |
}); | |
server.on('metrics', function (metrics) { | |
assert.equal(metrics, 'test:1|c'); | |
server.close(); | |
done(); | |
}); | |
}); | |
it('should add global tags if they are specified', function (done) { | |
server = createUDPServer(function (address) { | |
statsd = new StatsD({ | |
host: address.address, | |
port: address.port, | |
global_tags: ['gtag'], | |
}); | |
statsd.increment('test'); | |
}); | |
server.on('metrics', function (metrics) { | |
assert.equal(metrics, 'test:1|c|#gtag'); | |
server.close(); | |
done(); | |
}); | |
}); | |
it('should combine global tags and metric tags', function (done) { | |
server = createUDPServer(function (address) { | |
statsd = new StatsD({ | |
host: address.address, | |
port: address.port, | |
global_tags: ['gtag'], | |
}); | |
statsd.increment('test', 1337, ['foo']); | |
}); | |
server.on('metrics', function (metrics) { | |
assert.equal(metrics, 'test:1337|c|#gtag,foo'); | |
server.close(); | |
done(); | |
}); | |
}); | |
}); | |
}.bind(null, StatsD)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment