Repository: brightcove/hot-shots (js)

Issue:
Keep buffers from mocked calls for test inspection.
We're trying to write tests for our stats client which wraps hotshots. There are some instances where we can't supply a callback to each 'count' or 'timing' call. In order to test that our stats are emitting properly, we're having to use https://github.com/mattrobenolt/node-mock-udp to ensure that we're sending the right data.

It would be nice if hot-shots would record the outgoing buffers when `{mock: true}` is passed.

Pull request description:
Add support for recording buffers in mock mode
Fixes #77 

Patch:
diff --git a/lib/statsd.js b/lib/statsd.js
index b898cb7..2841b4e 100644
--- a/lib/statsd.js
+++ b/lib/statsd.js
@@ -64,6 +64,11 @@ var Client = function (host, port, prefix, suffix, globalize, cacheDns, mock,
   this.bufferHolder = options.isChild ? options.bufferHolder : { buffer: "" };
   this.errorHandler = options.errorHandler;
 
+  // If we're mocking the client, create a buffer to record the outgoing calls.
+  if (this.mock) {
+    this.mockBuffer = [];
+  }
+
   // We only want a single flush event per parent and all its child clients
   if(!options.isChild && this.maxBufferSize > 0) {
     this.intervalHandle = setInterval(this.onBufferFlushInterval.bind(this), this.bufferFlushInterval);
@@ -247,6 +252,7 @@ Client.prototype._send = function (message, callback) {
     }
   }
   else {
+    this.mockBuffer.push(message);
     if(typeof callback === 'function'){
       callback(null, 0);
     }


Test patch:
diff --git a/test/test_statsd.js b/test/test_statsd.js
index d325342..5733bf3 100644
--- a/test/test_statsd.js
+++ b/test/test_statsd.js
@@ -740,7 +740,7 @@ function doTests(StatsD) {
     });
 
   });
-  
+
   describe('#distribution', function(){
     it('should send proper distribution format without prefix, suffix, sampling and callback', function(finished){
       udpTest(function(message, server){
@@ -1563,6 +1563,15 @@ function doTests(StatsD) {
       statsd.send('test title');
     });
 
+    it('should record buffers when mocked', function (finished) {
+      var statsd = new StatsD({
+        mock: true
+      });
+      statsd.send('test', {}, function() {
+        assert.deepEqual(statsd.mockBuffer, ['test']);
+        finished();
+      });
+    });
   });
 
   describe('#sendMessage', function() {