Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[api test] Added
.coalesce()
reactor and tests.
- Loading branch information
Showing
3 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |