metric.mocha.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. const tx2 = require('..')
  2. const should = require('should')
  3. describe('Metric', function() {
  4. this.timeout(4000)
  5. it('should register a metric', () => {
  6. tx2.metric({
  7. name: 'test',
  8. val: () => {
  9. return 20
  10. }
  11. })
  12. })
  13. it('should metric exists', () => {
  14. should(tx2.metricExists('test')).eql(true)
  15. })
  16. it('should unknown metric not exists', () => {
  17. should(tx2.metricExists('unknowsss')).eql(false)
  18. })
  19. it('should have metric present', (done) => {
  20. tx2.once('data', (dt) => {
  21. should(dt.type).eql('axm:monitor')
  22. should(dt.data.test.value).eql(20)
  23. done()
  24. })
  25. })
  26. it('should register metric v2', () => {
  27. tx2.metric('test2', () => {
  28. return 30
  29. })
  30. })
  31. it('should have metric present', (done) => {
  32. tx2.once('data', (dt) => {
  33. should(dt.type).eql('axm:monitor')
  34. should(dt.data.test2.value).eql(30)
  35. done()
  36. })
  37. })
  38. it('should register metric v3', () => {
  39. let m = tx2.metric('test3', 0)
  40. m.set(45)
  41. })
  42. it('should have metric present', (done) => {
  43. tx2.once('data', (dt) => {
  44. should(dt.type).eql('axm:monitor')
  45. should(dt.data.test3.value).eql(45)
  46. done()
  47. })
  48. })
  49. })
  50. describe('counter', () => {
  51. describe('inc', () => {
  52. const test = ({incBy, expectedValue}) => () => {
  53. const counter = tx2.counter('Test counter')
  54. counter.inc(incBy)
  55. should(counter.val()).eql(expectedValue)
  56. }
  57. it('should increment by 1 when called with no arguments', test({expectedValue: 1}))
  58. it('should increment by 1 when called with 1', test({incBy: 1, expectedValue: 1}))
  59. it('should increment by -1 when called with -1', test({incBy: -1, expectedValue: -1}))
  60. it('should increment by 0 when called with 0', test({incBy: 0, expectedValue: 0}))
  61. it('should increment by 17.3 when called with 17.3', test({incBy: 17.3, expectedValue: 17.3}))
  62. })
  63. describe('dec', () => {
  64. const test = ({decBy, expectedValue}) => () => {
  65. const counter = tx2.counter('Test counter')
  66. counter.dec(decBy)
  67. should(counter.val()).eql(expectedValue)
  68. }
  69. it('should decrement by 1 when called with no arguments', test({expectedValue: -1}))
  70. it('should decrement by 1 when called with 1', test({decBy: 1, expectedValue: -1}))
  71. it('should decrement by -1 when called with -1', test({decBy: 1, expectedValue: -1}))
  72. it('should decrement by 0 when called with 0', test({decBy: 0, expectedValue: 0}))
  73. it('should decrement by 17.3 when called with 17.3', test({decBy: 17.3, expectedValue: -17.3}))
  74. })
  75. })