Skip to content

Commit

Permalink
[api test] Added .coalesce() reactor and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Mar 12, 2013
1 parent b84a118 commit 5bb3ab1
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
53 changes: 53 additions & 0 deletions lib/godot/reactor/coalesce.js
@@ -0,0 +1,53 @@
/*
* coalesce.js: Stream responsible for remembering and emitting vectors of events for `host/service` combinations.
*
* (C) 2012, Nodejitsu Inc.
*
*/

var util = require('util'),
common = require('../common'),
ReadWriteStream = common.ReadWriteStream;

//
// ### function Coalesce (formatFn)
// Constructor function of the Coalesce stream responsible for remembering
// and emitting vectors of events for `host/service` combinations.
//
var Coalesce = module.exports = function (formatFn) {
ReadWriteStream.call(this);

this.keys = {};
this.events = [];
};

//
// Inherit from ReadWriteStream
//
util.inherits(Coalesce, ReadWriteStream);

//
// ### function write (data)
// #### @data {Object} JSON to output
// Attempts to set the event for the `data.host` and
// `data.service` combination and pushes it to `this.events`.
// If it already exists, replaces it in `this.events` before
// emitting data.
//
Coalesce.prototype.write = function (data) {
var key = data.host + '/' + data.service;

if (this.keys[key]) {
this.events.splice(
this.events.indexOf(this.keys[key]),
1,
data
);
}
else {
this.events.push(data);
}

this.keys[key] = data;
this.emit('data', this.events.slice());
};
24 changes: 24 additions & 0 deletions test/fixtures/coalesce.json
@@ -0,0 +1,24 @@
[
{
"host": "127.0.0.1",
"service": "charlie/app/health/heartbeat",
"metric": 1,
"ttl": 50
},
{
"host": "127.0.0.1",
"service": "charlie/app/health/memory",
"ttl": 50
},
{
"host": "127.0.0.2",
"service": "charlie/app/health/heartbeat",
"ttl": 50
},
{
"host": "127.0.0.1",
"service": "charlie/app/health/heartbeat",
"metric": 0,
"ttl": 50
}
]
26 changes: 26 additions & 0 deletions test/reactor/coalesce-test.js
@@ -0,0 +1,26 @@
/*
* coalesce-test.js: Tests for the Coalesce reactor stream.
*
* (C) 2012, Nodejitsu Inc.
*
*/

var assert = require('assert'),
vows = require('vows'),
godot = require('../../lib/godot'),
macros = require('../macros').reactor;

vows.describe('godot/reactor/coalesce').addBatch({
"Godot coalesce": macros.shouldEmitDataSync(
godot
.reactor()
.coalesce(),
'coalesce',
4,
function (events) {
[1, 2, 3, 3].forEach(function (len, i) {
assert.lengthOf(events[i], len);
})
}
)
}).export(module);

0 comments on commit 5bb3ab1

Please sign in to comment.