Skip to content

Commit

Permalink
optimized tests and event handling on quit
Browse files Browse the repository at this point in the history
  • Loading branch information
M. Peter committed May 19, 2016
1 parent eb216df commit 5f9d6e4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ Emitted on general redis error
## Release History
|Version|Date|Description|
|:--:|:--:|:--|
|0.2.1|2016-05-19|optimized tests and event handling on quit|
|0.2.0|2016-05-18|usable from windows; added error event; updated dependencies; better tests|
|0.1.0|2016-01-07|Added metric `p_cpu` to measure the process cpu load. Added optional `d_avail` with the current free disk space. Trigger `beforeMetric` even if no `metricsKey` is defined. So you can grab the data without saving it to redis (eg. writing it to elasticsearch or a queue)|
|0.0.9|2015-12-15|updated dependencies to be used with node 4.2|
Expand Down
7 changes: 4 additions & 3 deletions _src/lib/heartbeat.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class Heartbeat extends Redisconnector
###
quit: =>
@stop()
@removeAllListeners()
@redis.quit()
return

Expand Down Expand Up @@ -306,9 +307,9 @@ class Heartbeat extends Redisconnector
@api private
###
_getUsage: ( cb )=>
_getUsage: ( cb )->
if usage?
usage.lookup process.pid, (err, _usage)=>
usage.lookup process.pid, (err, _usage)->
if err
cb( err )
return
Expand Down Expand Up @@ -403,7 +404,7 @@ class Heartbeat extends Redisconnector
@api private
###
_getRedisTime: ( cb )=>
@redis.time ( err, time )=>
@redis.time ( err, time )->
if err
cb( err )
return
Expand Down
33 changes: 19 additions & 14 deletions _src/test/main.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
should = require('should')

Heartbeat = require( "../." )
redis = require( "redis" )
Heartbeat = require( "../." )
redis = require( "redis" )

METRICKEYS = ["t", "g_cpu", "g_mem", "g_memtotal", "p_id", "p_uptime", "p_mem", "p_cpu"]

Expand All @@ -14,24 +14,27 @@ describe "----- Module TESTS -----", ->
_hb.on "error", ( _err )->
_err.name.should.equal( "ENONAME" )
done()
_hb.removeAllListeners()
return
_hb.start()
return

it "without identifier option", ->
it "without identifier option", ( done )->
_hb = new Heartbeat( { name: "FAIL", autostart: false } )
_hb.on "error", ( _err )->
_err.name.should.equal( "ENOIDENTIFIER" )
done()
_hb.removeAllListeners()
return
_hb.start()
return

it "without name option", ->
it "without name option", ( done )->
_hb = new Heartbeat( { identifier: "explode", autostart: false } )
_hb.on "error", ( _err )->
_err.name.should.equal( "ENONAME" )
done()
_hb.removeAllListeners()
return
_hb.start()
return
Expand All @@ -44,12 +47,13 @@ describe "----- Module TESTS -----", ->
_ident = "bar:4223"
before ( done )->
_hb = new Heartbeat( { name: "FOO", identifier: _ident, intervalHeartbeat: 1, intervalMetrics: 5, metricCount: 10 } )
# TODO add initialisation Code
_hb.on "error", ( _err )->
console.error( "ERROR", _err )
return
done()
return

after ( done )->
# TODO teardown
_hb.quit()
done()
return
Expand All @@ -60,12 +64,12 @@ describe "----- Module TESTS -----", ->
_cH = 0
_cM = 0

_hb.on "beforeHeartbeat", ( ident )->
_hb.on "beforeHeartbeat", ( ident )->
_cH++
ident.should.equal( _ident )
return

_hb.on "beforeMetric", ( metric )->
_hb.on "beforeMetric", ( metric )->
_cM++
metric.should.have.properties( METRICKEYS )
return
Expand All @@ -85,14 +89,15 @@ describe "----- Module TESTS -----", ->
_ident = "foo:1337"

before ( done )->
_cli = redis.createClient()
_cli = redis.createClient()
_hb = new Heartbeat( { metricExpire: 10, heartbeatExpire: 10, name: "BAR", identifier: _ident, intervalHeartbeat: 1, intervalMetrics: 5, metricCount: 10, client: _cli } )
# TODO add initialisation Code
_hb.on "error", ( _err )->
console.error( "ERROR", _err )
return
done()
return

after ( done )->
# TODO teardown
_hb.quit()
done()
return
Expand All @@ -103,12 +108,12 @@ describe "----- Module TESTS -----", ->
_cH = 0
_cM = 0

_hb.on "beforeHeartbeat", ( ident )->
_hb.on "beforeHeartbeat", ( ident )->
_cH++
ident.should.equal( _ident )
return

_hb.on "beforeMetric", ( metric )->
_hb.on "beforeMetric", ( metric )->
_cM++
metric.should.have.properties( METRICKEYS )
return
Expand Down Expand Up @@ -161,4 +166,4 @@ describe "----- Module TESTS -----", ->
return

return
return
return
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redis-heartbeat",
"version": "0.2.0",
"version": "0.2.1",
"description": "",
"keywords": [],
"homepage": "https://github.com/mpneuried/redis-heartbeat",
Expand Down

0 comments on commit 5f9d6e4

Please sign in to comment.