Skip to content
Browse files

Add unit test and batch writing

  • Loading branch information...
1 parent fa11a3b commit 4828038995cf3e5e49c439b074455b18fa03b696 @felixge committed Nov 4, 2011
Showing with 89 additions and 8 deletions.
  1. +20 −6 lib/carbon_client.js
  2. +7 −2 test/common.js
  3. +62 −0 test/unit/test-client.js
View
26 lib/carbon_client.js
@@ -38,15 +38,29 @@ CarbonClient.createClient = function(dsn) {
};
CarbonClient.prototype.write = function(path, value, timestamp, cb) {
- if (typeof timestamp === 'function') {
- cb = timestamp;
- timestamp = Date.now();
+ var messages = [];
+ if (Array.isArray(path)) {
+ messages = path;
+ cb = value;
+ } else {
+ if (typeof timestamp === 'function') {
+ cb = timestamp;
+ timestamp = null;
+ }
+
+ messages.push({path: path, value: value, timestamp: timestamp});
}
- timestamp = timestamp / 1000;
+ var lines = messages.reduce(function(lines, message) {
+ message.timestamp = message.timestamp || Date.now();
+ message.timestamp = Math.floor(message.timestamp / 1000);
+
+ var line = [message.path, message.value, message.timestamp].join(' ');
+
+ return lines + line + '\n';
+ }, '');
- var line = [path, value, timestamp].join(' ');
- this.socket.write(line + '\n', 'utf8', cb);
+ this.socket.write(lines, 'utf8', cb);
};
CarbonClient.prototype.getJSON = function() {
View
9 test/common.js
@@ -1,4 +1,9 @@
var common = exports;
+var path = require('path');
-common.graphite = require('..');
-common.config = require('./config');
+common.dir = {}
+common.dir.root = path.dirname(__dirname);
+common.dir.lib = path.join(common.dir.root, 'lib');
+
+common.graphite = require(common.dir.root);
+common.config = require('./config');
View
62 test/unit/test-client.js
@@ -0,0 +1,62 @@
+var common = require('../common');
+var assert = require('assert');
+var CarbonClient = require(common.dir.lib + '/carbon_client');
+
+function SocketDummy() {
+ this._writes = [];
+}
+
+SocketDummy.prototype.write = function() {
+ this._writes.push(arguments);
+};
+
+(function testSingleWrite() {
+ var client = new CarbonClient(new SocketDummy());
+
+ client.write('foo.bar', 25);
+ assert.equal(client.socket._writes.length, 1);
+
+ var args = client.socket._writes.shift();
+ assert.ok(/^foo\.bar 25 [\d]+\n/.test(args[0]), args[0]);
+ assert.equal(args[1], 'utf8');
+ assert.equal(args[2], undefined);
+})();
+
+(function testSingleWriteWithCallback() {
+ var client = new CarbonClient(new SocketDummy());
+ var cb = function() {};
+
+ client.write('foo.bar', 25, cb);
+ assert.equal(client.socket._writes.length, 1);
+
+ var args = client.socket._writes.shift();
+ assert.ok(/^foo\.bar 25 [\d]+\n/.test(args[0]), args[0]);
+ assert.equal(args[2], cb);
+})();
+
+(function testSingleWriteWithTimestamp() {
+ var client = new CarbonClient(new SocketDummy());
+ var cb = function() {};
+
+ client.write('foo.bar', 25, 2001, cb);
+ assert.equal(client.socket._writes.length, 1);
+
+ var args = client.socket._writes.shift();
+ assert.ok(/^foo\.bar 25 2\n/.test(args[0]), args[0]);
+ assert.equal(args[2], cb);
+})();
+
+(function testBatchWrite() {
+ var client = new CarbonClient(new SocketDummy());
+ var cb = function() {};
+
+ client.write([
+ {path: 'one', value: 30, timestamp: 9000},
+ {path: 'two', value: 33, timestamp: 8000},
+ ], cb);
+ assert.equal(client.socket._writes.length, 1);
+
+ var args = client.socket._writes.shift();
+ assert.ok(/^one 30 9\ntwo 33 8\n/.test(args[0]), args[0]);
+ assert.equal(args[2], cb);
+})();

0 comments on commit 4828038

Please sign in to comment.
Something went wrong with that request. Please try again.