timeAsyncMethodTests.coffee 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. require('coffee-script')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. path = require('path')
  6. modulePath = path.join __dirname, '../../../timeAsyncMethod.coffee'
  7. SandboxedModule = require('sandboxed-module')
  8. sinon = require("sinon")
  9. describe 'timeAsyncMethod', ->
  10. beforeEach ->
  11. @Timer = {done: sinon.stub()}
  12. @TimerConstructor = sinon.stub().returns(@Timer)
  13. @metrics = {
  14. Timer: @TimerConstructor
  15. }
  16. @timeAsyncMethod = SandboxedModule.require modulePath, requires:
  17. './metrics': @metrics
  18. @testObject = {
  19. nextNumber: (n, callback=(err, result)->) ->
  20. setTimeout(
  21. () ->
  22. callback(null, n+1)
  23. , 100
  24. )
  25. }
  26. it 'should have the testObject behave correctly before wrapping', (done) ->
  27. @testObject.nextNumber 2, (err, result) ->
  28. expect(err).to.not.exist
  29. expect(result).to.equal 3
  30. done()
  31. it 'should wrap method without error', (done) ->
  32. @timeAsyncMethod @testObject, 'nextNumber', 'test.nextNumber'
  33. done()
  34. it 'should transparently wrap method invocation in timer', (done) ->
  35. @timeAsyncMethod @testObject, 'nextNumber', 'test.nextNumber'
  36. @testObject.nextNumber 2, (err, result) =>
  37. expect(err).to.not.exist
  38. expect(result).to.equal 3
  39. expect(@TimerConstructor.callCount).to.equal 1
  40. expect(@Timer.done.callCount).to.equal 1
  41. done()
  42. describe 'when base method produces an error', ->
  43. beforeEach ->
  44. @testObject.nextNumber = (n, callback=(err, result)->) ->
  45. setTimeout(
  46. () ->
  47. callback(new Error('woops'))
  48. , 100
  49. )
  50. it 'should propagate the error transparently', (done) ->
  51. @timeAsyncMethod @testObject, 'nextNumber', 'test.nextNumber'
  52. @testObject.nextNumber 2, (err, result) =>
  53. expect(err).to.exist
  54. expect(err).to.be.instanceof Error
  55. expect(result).to.not.exist
  56. done()
  57. describe 'when a logger is supplied', ->
  58. beforeEach ->
  59. @logger = {log: sinon.stub()}
  60. it 'should also call logger.log', (done) ->
  61. @timeAsyncMethod @testObject, 'nextNumber', 'test.nextNumber', @logger
  62. @testObject.nextNumber 2, (err, result) =>
  63. expect(err).to.not.exist
  64. expect(result).to.equal 3
  65. expect(@TimerConstructor.callCount).to.equal 1
  66. expect(@Timer.done.callCount).to.equal 1
  67. expect(@logger.log.callCount).to.equal 1
  68. done()
  69. describe 'when the wrapper cannot be applied', ->
  70. beforeEach ->
  71. it 'should raise an error', ->
  72. badWrap = () =>
  73. @timeAsyncMethod @testObject, 'DEFINITELY_NOT_A_REAL_METHOD', 'test.nextNumber'
  74. expect(badWrap).to.throw(
  75. /^.*expected object property 'DEFINITELY_NOT_A_REAL_METHOD' to be a function.*$/
  76. )
  77. describe 'when the wrapped function is not using a callback', ->
  78. beforeEach ->
  79. @testObject.nextNumber = (n) ->
  80. return n+1
  81. it 'should throw an error', ->
  82. @timeAsyncMethod @testObject, 'nextNumber', 'test.nextNumber'
  83. badCall = () =>
  84. @testObject.nextNumber 2
  85. expect(badCall).to.throw(
  86. /^.*expected wrapped method 'nextNumber' to be invoked with a callback.*$/
  87. )