2020-07-17 11:17:18 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-09-11 14:18:22 -04:00
|
|
|
const chai = require('chai')
|
|
|
|
const { expect } = chai
|
|
|
|
const path = require('path')
|
|
|
|
const modulePath = path.join(__dirname, '../../../timeAsyncMethod.js')
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const sinon = require('sinon')
|
2020-07-17 11:17:18 -04:00
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
describe('timeAsyncMethod', function () {
|
|
|
|
beforeEach(function () {
|
2020-09-11 14:18:22 -04:00
|
|
|
this.Timer = { done: sinon.stub() }
|
|
|
|
this.TimerConstructor = sinon.stub().returns(this.Timer)
|
|
|
|
this.metrics = {
|
|
|
|
Timer: this.TimerConstructor,
|
2021-12-16 04:04:32 -05:00
|
|
|
inc: sinon.stub(),
|
2020-09-11 14:18:22 -04:00
|
|
|
}
|
|
|
|
this.timeAsyncMethod = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
2021-12-16 04:04:32 -05:00
|
|
|
'./index': this.metrics,
|
|
|
|
},
|
2020-09-11 14:18:22 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
return (this.testObject = {
|
|
|
|
nextNumber(n, callback) {
|
|
|
|
return setTimeout(() => callback(null, n + 1), 100)
|
2021-12-16 04:04:32 -05:00
|
|
|
},
|
2020-09-11 14:18:22 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
it('should have the testObject behave correctly before wrapping', function (done) {
|
2020-09-11 14:18:22 -04:00
|
|
|
return this.testObject.nextNumber(2, (err, result) => {
|
|
|
|
expect(err).to.not.exist
|
|
|
|
expect(result).to.equal(3)
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
it('should wrap method without error', function (done) {
|
2020-09-11 14:18:22 -04:00
|
|
|
this.timeAsyncMethod(
|
|
|
|
this.testObject,
|
|
|
|
'nextNumber',
|
|
|
|
'someContext.TestObject'
|
|
|
|
)
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
it('should transparently wrap method invocation in timer', function (done) {
|
2020-09-11 14:18:22 -04:00
|
|
|
this.timeAsyncMethod(
|
|
|
|
this.testObject,
|
|
|
|
'nextNumber',
|
|
|
|
'someContext.TestObject'
|
|
|
|
)
|
|
|
|
return this.testObject.nextNumber(2, (err, result) => {
|
|
|
|
expect(err).to.not.exist
|
|
|
|
expect(result).to.equal(3)
|
|
|
|
expect(this.TimerConstructor.callCount).to.equal(1)
|
|
|
|
expect(this.Timer.done.callCount).to.equal(1)
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
it('should increment success count', function (done) {
|
2020-09-11 14:18:22 -04:00
|
|
|
this.metrics.inc = sinon.stub()
|
|
|
|
this.timeAsyncMethod(
|
|
|
|
this.testObject,
|
|
|
|
'nextNumber',
|
|
|
|
'someContext.TestObject'
|
|
|
|
)
|
|
|
|
return this.testObject.nextNumber(2, (err, result) => {
|
|
|
|
if (err) {
|
|
|
|
return done(err)
|
|
|
|
}
|
|
|
|
expect(this.metrics.inc.callCount).to.equal(1)
|
|
|
|
expect(
|
|
|
|
this.metrics.inc.calledWith('someContext_result', 1, {
|
|
|
|
method: 'TestObject_nextNumber',
|
2021-12-16 04:04:32 -05:00
|
|
|
status: 'success',
|
2020-09-11 14:18:22 -04:00
|
|
|
})
|
|
|
|
).to.equal(true)
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
describe('when base method produces an error', function () {
|
|
|
|
beforeEach(function () {
|
2020-09-11 14:18:22 -04:00
|
|
|
this.metrics.inc = sinon.stub()
|
2021-12-16 04:04:32 -05:00
|
|
|
return (this.testObject.nextNumber = function (n, callback) {
|
2020-09-11 14:18:22 -04:00
|
|
|
return setTimeout(() => callback(new Error('woops')), 100)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
it('should propagate the error transparently', function (done) {
|
2020-09-11 14:18:22 -04:00
|
|
|
this.timeAsyncMethod(
|
|
|
|
this.testObject,
|
|
|
|
'nextNumber',
|
|
|
|
'someContext.TestObject'
|
|
|
|
)
|
|
|
|
return this.testObject.nextNumber(2, (err, result) => {
|
|
|
|
expect(err).to.exist
|
|
|
|
expect(err).to.be.instanceof(Error)
|
|
|
|
expect(result).to.not.exist
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
return it('should increment failure count', function (done) {
|
2020-09-11 14:18:22 -04:00
|
|
|
this.timeAsyncMethod(
|
|
|
|
this.testObject,
|
|
|
|
'nextNumber',
|
|
|
|
'someContext.TestObject'
|
|
|
|
)
|
|
|
|
return this.testObject.nextNumber(2, (err, result) => {
|
|
|
|
expect(err).to.exist
|
|
|
|
expect(this.metrics.inc.callCount).to.equal(1)
|
|
|
|
expect(
|
|
|
|
this.metrics.inc.calledWith('someContext_result', 1, {
|
|
|
|
method: 'TestObject_nextNumber',
|
2021-12-16 04:04:32 -05:00
|
|
|
status: 'failed',
|
2020-09-11 14:18:22 -04:00
|
|
|
})
|
|
|
|
).to.equal(true)
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
describe('when a logger is supplied', function () {
|
|
|
|
beforeEach(function () {
|
2020-09-11 14:18:22 -04:00
|
|
|
return (this.logger = { log: sinon.stub() })
|
|
|
|
})
|
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
return it('should also call logger.log', function (done) {
|
2020-09-11 14:18:22 -04:00
|
|
|
this.timeAsyncMethod(
|
|
|
|
this.testObject,
|
|
|
|
'nextNumber',
|
|
|
|
'someContext.TestObject',
|
|
|
|
this.logger
|
|
|
|
)
|
|
|
|
return this.testObject.nextNumber(2, (err, result) => {
|
|
|
|
expect(err).to.not.exist
|
|
|
|
expect(result).to.equal(3)
|
|
|
|
expect(this.TimerConstructor.callCount).to.equal(1)
|
|
|
|
expect(this.Timer.done.callCount).to.equal(1)
|
|
|
|
expect(this.logger.log.callCount).to.equal(1)
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
describe('when the wrapper cannot be applied', function () {
|
|
|
|
beforeEach(function () {})
|
2020-09-11 14:18:22 -04:00
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
return it('should raise an error', function () {
|
2020-09-11 14:18:22 -04:00
|
|
|
const badWrap = () => {
|
|
|
|
return this.timeAsyncMethod(
|
|
|
|
this.testObject,
|
|
|
|
'DEFINITELY_NOT_A_REAL_METHOD',
|
|
|
|
'someContext.TestObject'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return expect(badWrap).to.throw(
|
|
|
|
/^.*expected object property 'DEFINITELY_NOT_A_REAL_METHOD' to be a function.*$/
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
return describe('when the wrapped function is not using a callback', function () {
|
|
|
|
beforeEach(function () {
|
2020-09-11 14:18:22 -04:00
|
|
|
this.realMethod = sinon.stub().returns(42)
|
|
|
|
return (this.testObject.nextNumber = this.realMethod)
|
|
|
|
})
|
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
it('should not throw an error', function () {
|
2020-09-11 14:18:22 -04:00
|
|
|
this.timeAsyncMethod(
|
|
|
|
this.testObject,
|
|
|
|
'nextNumber',
|
|
|
|
'someContext.TestObject'
|
|
|
|
)
|
|
|
|
const badCall = () => {
|
|
|
|
return this.testObject.nextNumber(2)
|
|
|
|
}
|
|
|
|
return expect(badCall).to.not.throw(Error)
|
|
|
|
})
|
|
|
|
|
2021-12-16 04:04:32 -05:00
|
|
|
return it('should call the underlying method', function () {
|
2020-09-11 14:18:22 -04:00
|
|
|
this.timeAsyncMethod(
|
|
|
|
this.testObject,
|
|
|
|
'nextNumber',
|
|
|
|
'someContext.TestObject'
|
|
|
|
)
|
|
|
|
const result = this.testObject.nextNumber(12)
|
|
|
|
expect(this.realMethod.callCount).to.equal(1)
|
|
|
|
expect(this.realMethod.calledWith(12)).to.equal(true)
|
|
|
|
return expect(result).to.equal(42)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|