2017-03-16 05:49:45 -04:00
|
|
|
require('coffee-script')
|
|
|
|
chai = require('chai')
|
|
|
|
should = chai.should()
|
|
|
|
expect = chai.expect
|
|
|
|
path = require('path')
|
|
|
|
modulePath = path.join __dirname, '../../../timeAsyncMethod.coffee'
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
sinon = require("sinon")
|
|
|
|
|
|
|
|
|
|
|
|
describe 'timeAsyncMethod', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@Timer = {done: sinon.stub()}
|
|
|
|
@TimerConstructor = sinon.stub().returns(@Timer)
|
|
|
|
@metrics = {
|
|
|
|
Timer: @TimerConstructor
|
2017-03-20 12:54:40 -04:00
|
|
|
inc: sinon.stub()
|
2017-03-16 05:49:45 -04:00
|
|
|
}
|
|
|
|
@timeAsyncMethod = SandboxedModule.require modulePath, requires:
|
|
|
|
'./metrics': @metrics
|
|
|
|
|
|
|
|
@testObject = {
|
|
|
|
nextNumber: (n, callback=(err, result)->) ->
|
|
|
|
setTimeout(
|
|
|
|
() ->
|
|
|
|
callback(null, n+1)
|
|
|
|
, 100
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
it 'should have the testObject behave correctly before wrapping', (done) ->
|
|
|
|
@testObject.nextNumber 2, (err, result) ->
|
|
|
|
expect(err).to.not.exist
|
|
|
|
expect(result).to.equal 3
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should wrap method without error', (done) ->
|
2017-03-20 12:54:40 -04:00
|
|
|
@timeAsyncMethod @testObject, 'nextNumber', 'someContext.TestObject'
|
2017-03-16 05:49:45 -04:00
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should transparently wrap method invocation in timer', (done) ->
|
2017-03-20 12:54:40 -04:00
|
|
|
@timeAsyncMethod @testObject, 'nextNumber', 'someContext.TestObject'
|
2017-03-16 05:49:45 -04:00
|
|
|
@testObject.nextNumber 2, (err, result) =>
|
|
|
|
expect(err).to.not.exist
|
|
|
|
expect(result).to.equal 3
|
|
|
|
expect(@TimerConstructor.callCount).to.equal 1
|
|
|
|
expect(@Timer.done.callCount).to.equal 1
|
|
|
|
done()
|
|
|
|
|
2017-03-20 12:54:40 -04:00
|
|
|
it 'should increment success count', (done) ->
|
|
|
|
@metrics.inc = sinon.stub()
|
|
|
|
@timeAsyncMethod @testObject, 'nextNumber', 'someContext.TestObject'
|
|
|
|
@testObject.nextNumber 2, (err, result) =>
|
|
|
|
expect(@metrics.inc.callCount).to.equal 1
|
|
|
|
expect(@metrics.inc.calledWith('someContext.TestObject.nextNumber.success')).to.equal true
|
|
|
|
done()
|
|
|
|
|
2017-03-16 05:55:07 -04:00
|
|
|
describe 'when base method produces an error', ->
|
|
|
|
beforeEach ->
|
2017-03-20 12:54:40 -04:00
|
|
|
@metrics.inc = sinon.stub()
|
2017-03-16 05:55:07 -04:00
|
|
|
@testObject.nextNumber = (n, callback=(err, result)->) ->
|
|
|
|
setTimeout(
|
|
|
|
() ->
|
|
|
|
callback(new Error('woops'))
|
|
|
|
, 100
|
|
|
|
)
|
|
|
|
|
|
|
|
it 'should propagate the error transparently', (done) ->
|
2017-03-20 12:54:40 -04:00
|
|
|
@timeAsyncMethod @testObject, 'nextNumber', 'someContext.TestObject'
|
2017-03-16 05:55:07 -04:00
|
|
|
@testObject.nextNumber 2, (err, result) =>
|
|
|
|
expect(err).to.exist
|
|
|
|
expect(err).to.be.instanceof Error
|
|
|
|
expect(result).to.not.exist
|
|
|
|
done()
|
|
|
|
|
2017-03-20 12:54:40 -04:00
|
|
|
it 'should increment failure count', (done) ->
|
|
|
|
@timeAsyncMethod @testObject, 'nextNumber', 'someContext.TestObject'
|
|
|
|
@testObject.nextNumber 2, (err, result) =>
|
|
|
|
expect(@metrics.inc.callCount).to.equal 1
|
|
|
|
expect(@metrics.inc.calledWith('someContext.TestObject.nextNumber.failure')).to.equal true
|
|
|
|
done()
|
|
|
|
|
2017-03-16 05:55:07 -04:00
|
|
|
describe 'when a logger is supplied', ->
|
|
|
|
beforeEach ->
|
|
|
|
@logger = {log: sinon.stub()}
|
|
|
|
|
|
|
|
it 'should also call logger.log', (done) ->
|
2017-03-20 12:54:40 -04:00
|
|
|
@timeAsyncMethod @testObject, 'nextNumber', 'someContext.TestObject', @logger
|
2017-03-16 05:55:07 -04:00
|
|
|
@testObject.nextNumber 2, (err, result) =>
|
|
|
|
expect(err).to.not.exist
|
|
|
|
expect(result).to.equal 3
|
|
|
|
expect(@TimerConstructor.callCount).to.equal 1
|
|
|
|
expect(@Timer.done.callCount).to.equal 1
|
|
|
|
expect(@logger.log.callCount).to.equal 1
|
|
|
|
done()
|
2017-03-16 06:02:19 -04:00
|
|
|
|
|
|
|
describe 'when the wrapper cannot be applied', ->
|
|
|
|
beforeEach ->
|
|
|
|
|
|
|
|
it 'should raise an error', ->
|
|
|
|
badWrap = () =>
|
2017-03-20 12:54:40 -04:00
|
|
|
@timeAsyncMethod @testObject, 'DEFINITELY_NOT_A_REAL_METHOD', 'someContext.TestObject'
|
2017-03-16 06:02:19 -04:00
|
|
|
expect(badWrap).to.throw(
|
|
|
|
/^.*expected object property 'DEFINITELY_NOT_A_REAL_METHOD' to be a function.*$/
|
|
|
|
)
|
2017-03-16 06:07:52 -04:00
|
|
|
|
|
|
|
describe 'when the wrapped function is not using a callback', ->
|
|
|
|
beforeEach ->
|
2017-03-20 12:37:53 -04:00
|
|
|
@realMethod = sinon.stub().returns(42)
|
|
|
|
@testObject.nextNumber = @realMethod
|
2017-03-16 06:07:52 -04:00
|
|
|
|
2017-03-20 12:17:22 -04:00
|
|
|
it 'should not throw an error', ->
|
2017-03-20 12:54:40 -04:00
|
|
|
@timeAsyncMethod @testObject, 'nextNumber', 'someContext.TestObject'
|
2017-03-16 06:07:52 -04:00
|
|
|
badCall = () =>
|
|
|
|
@testObject.nextNumber 2
|
2017-03-20 12:17:22 -04:00
|
|
|
expect(badCall).to.not.throw(Error)
|
2017-03-16 06:07:52 -04:00
|
|
|
|
2017-03-20 12:37:53 -04:00
|
|
|
it 'should call the underlying method', ->
|
2017-03-20 12:54:40 -04:00
|
|
|
@timeAsyncMethod @testObject, 'nextNumber', 'someContext.TestObject'
|
2017-03-20 12:37:53 -04:00
|
|
|
result = @testObject.nextNumber(12)
|
|
|
|
expect(@realMethod.callCount).to.equal 1
|
|
|
|
expect(@realMethod.calledWith(12)).to.equal true
|
|
|
|
expect(result).to.equal 42
|
|
|
|
|
2017-03-16 06:07:52 -04:00
|
|
|
|