Skip to content
This repository has been archived by the owner on Dec 8, 2023. It is now read-only.

Commit

Permalink
Add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanmikaelian committed Jun 5, 2017
1 parent 0bd014c commit 799a63e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/librato.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ var flushStats = function libratoFlush(ts, metrics) {
// Add the payload
measurements.push(measure);
// Post measurements and clear arrays if past batch size
if (measurements.length >= maxBatchSize || writeToLegacy && counters.length + gauges.length >= maxBatchSize) {
if (measurements.length >= maxBatchSize || (writeToLegacy && counters.length + gauges.length >= maxBatchSize)) {
postMetrics(measureTime, gauges, counters, measurements);
if (measurements.length >= maxBatchSize) {
measurements = [];
Expand Down
44 changes: 44 additions & 0 deletions test/librato_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const serverPort = 36001;
const librato = require('../lib/librato.js');
const nock = require('nock');
const sinon = require('sinon');
const http = require('http');

const config = {
debug: false,
Expand Down Expand Up @@ -94,10 +95,12 @@ module.exports.tags = {
.defaultReplyHeaders({'Content-Type': 'application/json'});

librato.init(null, config, this.emitter);
this.httpSpy = sinon.spy(http, 'request');
callback();
},

tearDown: function(callback) {
http.request.restore();
callback();
},

Expand Down Expand Up @@ -262,6 +265,47 @@ module.exports.tags = {
this.emitter.emit('flush', 123, metrics);
},

testBatchProperRequestCount: function(test) {
test.expect(3);
var gauges = {};
for (var i = 0; i < 500; i++) {
var key = 'gauge' + i;
gauges[key] = 1;
}
var metrics = {gauges: gauges};
this.apiServer.post('/v1/measurements')
.reply(200, (uri, requestBody) => {
test.ok(requestBody.measurements);
});

this.emitter.emit('flush', 123, metrics);

// Let some time pass...
setTimeout(() => {
// There should have ever been one submission because we hit our maxBatchSize
test.ok(this.httpSpy.calledOnce);

// Try with a bigger batch...
for (var i = 0; i < 1500; i++) {
var key = 'gauge' + i;
gauges[key] = 1;
}
metrics = {gauges: gauges};
// Reset the spy
this.httpSpy.reset();

// Flush
this.emitter.emit('flush', 123, metrics);

// Let some time pass...
setTimeout(() => {
// One request, per batch of 500 metrics....
test.ok(this.httpSpy.calledThrice);
test.done();
});
}, 500);
},

testValidMeasurementTopLevelTag: function(test) {
config.librato.host = '127.0.0.1';
config.librato.tags = {test: true};
Expand Down

0 comments on commit 799a63e

Please sign in to comment.