Skip to content

Commit

Permalink
Merge pull request #21 from goodeggs/flush-callback
Browse files Browse the repository at this point in the history
Accept a callback for ::flush
  • Loading branch information
Aaron Borden committed Feb 7, 2015
2 parents 4d174f9 + 99aa15e commit ef1d61d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
16 changes: 9 additions & 7 deletions src/librato.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,19 @@ librato.measure = librato.timing # alias
librato.start = ->
worker.start()

librato.stop = ->
librato.stop = (cb) ->
worker.stop()
librato.flush()
librato.flush = ->
librato.flush(cb)

librato.flush = (cb = ->) ->
gauges = []
collector.flushTo gauges
measurement.source = config.source for measurement in gauges
if gauges.length
client.send {gauges}, (err) ->
librato.emit 'error', err if err?
return process.nextTick cb unless gauges.length

client.send {gauges}, (err) ->
librato.emit 'error', err if err?
cb(err)

librato.middleware = middleware(librato)

Expand Down
14 changes: 12 additions & 2 deletions test/librato.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ describe 'librato', ->
args = Client::send.getCall(0).args
names = _(args[0].gauges).pluck('name').value()
expect(names).to.contain 'this_is_:a_test_2'

describe '::flush', ->
beforeEach ->
librato.increment('foo')
librato.timing('bar')
librato.flush()

it 'sends data to Librato', ->
expect(Client::send.calledOnce).to.be true
args = Client::send.getCall(0).args
Expand All @@ -71,3 +71,13 @@ describe 'librato', ->
librato.flush()
expect(Client::send.calledOnce).to.be true

it 'call callback immediately when queue is empty', (done) ->
librato.flush (err) ->
expect(Client::send.callCount).to.be 1
done(err)

it 'accepts a callback', (done) ->
librato.increment('messages')
librato.flush (err) ->
expect(Client::send.callCount).to.be 2
done(err)

0 comments on commit ef1d61d

Please sign in to comment.