Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict' | ||
|
||
const tx = require('./util/promise') | ||
|
||
module.exports = [ | ||
{ | ||
name: 'when', | ||
file: 'lib/Promise.js', | ||
versions: ['3'], | ||
patch (Promise, tracer, config) { | ||
this.wrap(Promise.prototype, 'then', tx.createWrapThen(tracer, config)) | ||
}, | ||
unpatch (Promise) { | ||
this.unwrap(Promise.prototype, 'then') | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
'use strict' | ||
|
||
const agent = require('./agent') | ||
const plugin = require('../../src/plugins/when') | ||
|
||
wrapIt() | ||
|
||
describe('Plugin', () => { | ||
let when | ||
let tracer | ||
|
||
describe('when', () => { | ||
withVersions(plugin, 'when', version => { | ||
beforeEach(() => { | ||
tracer = require('../..') | ||
}) | ||
|
||
afterEach(() => { | ||
return agent.close() | ||
}) | ||
|
||
describe('without configuration', () => { | ||
beforeEach(() => { | ||
return agent.load(plugin, 'when') | ||
.then(() => { | ||
when = require(`../../versions/when@${version}`).get() | ||
}) | ||
}) | ||
|
||
it('should run the then() callback in context where then() was called', () => { | ||
if (process.env.DD_CONTEXT_PROPAGATION === 'false') return | ||
|
||
const span = {} | ||
const deferred = when.defer() | ||
const promise = deferred.promise | ||
|
||
setImmediate(() => { | ||
tracer.scopeManager().activate({}) | ||
deferred.resolve() | ||
}) | ||
|
||
tracer.scopeManager().activate(span) | ||
|
||
return promise | ||
.then(() => { | ||
tracer.scopeManager().activate({}) | ||
}) | ||
.then(() => { | ||
const scope = tracer.scopeManager().active() | ||
|
||
expect(scope).to.not.be.null | ||
expect(scope.span()).to.equal(span) | ||
}) | ||
}) | ||
|
||
it('should run the catch() callback in context where catch() was called', () => { | ||
if (process.env.DD_CONTEXT_PROPAGATION === 'false') return | ||
|
||
const span = {} | ||
const deferred = when.defer() | ||
const promise = deferred.promise | ||
|
||
setImmediate(() => { | ||
tracer.scopeManager().activate({}) | ||
deferred.reject(new Error()) | ||
}) | ||
|
||
tracer.scopeManager().activate(span) | ||
|
||
return promise | ||
.catch(err => { | ||
tracer.scopeManager().activate({}) | ||
throw err | ||
}) | ||
.catch(() => { | ||
const scope = tracer.scopeManager().active() | ||
|
||
expect(scope).to.not.be.null | ||
expect(scope.span()).to.equal(span) | ||
}) | ||
}) | ||
|
||
it('should run the onProgress callback in context where then() was called', () => { | ||
if (process.env.DD_CONTEXT_PROPAGATION === 'false') return | ||
|
||
const span = {} | ||
const deferred = when.defer() | ||
const promise = deferred.promise | ||
|
||
setImmediate(() => { | ||
tracer.scopeManager().activate({}) | ||
deferred.resolve() | ||
}) | ||
|
||
tracer.scopeManager().activate(span) | ||
|
||
return promise | ||
.then(() => { | ||
tracer.scopeManager().activate({}) | ||
}) | ||
.then(() => {}, () => {}, () => { | ||
const scope = tracer.scopeManager().active() | ||
|
||
expect(scope).to.not.be.null | ||
expect(scope.span()).to.equal(span) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |