Skip to content

Commit

Permalink
Merge pull request newrelic#95 from michaelgoin/promise-context-test-…
Browse files Browse the repository at this point in the history
…update

Modifies koa test to remove context-less async hop.
  • Loading branch information
garbados authored Dec 3, 2021
2 parents d432e55 + cf89395 commit 126e21f
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions lib/instrumentation/koa/tests/versioned/koa.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,39 +259,37 @@ tap.test('Koa instrumentation', (t) => {
})

t.test('maintains transaction state between middleware', (t) => {
t.plan(5)
t.plan(7)

let tx

var tasks = []
var intervalId = setInterval(function () {
while (tasks.length) {
tasks.pop()()
}
}, 10)
app.use(async function one(ctx, next) {
tx = helper.agent.getTransaction()

t.tearDown(function () {
clearInterval(intervalId)
await next()

t.transaction(tx)
})
var tx

app.use(function one(ctx, next) {
tx = helper.agent.getTransaction()
return new Promise(executor)
app.use(async function two(ctx, next) {
t.transaction(tx, 'two has transaction context')
await next()
})

function executor(resolve) {
tasks.push(function () {
next().then(function () {
t.transaction(tx)
app.use(function three(ctx, next) {
t.transaction(tx, 'three has transaction context')
return new Promise((resolve) => {
setImmediate(() => {
next().then(() => {
t.transaction(tx, 'still have context after in-context timer hop')
resolve()
})
})
}
})
app.use(function two(ctx, next) {
t.transaction(tx, 'two has transaction context')
return next()
})
})
app.use(function three(ctx) {
t.transaction(tx, 'three has transaction context')

app.use(function four(ctx) {
t.transaction(tx, 'four has transaction context')
ctx.body = 'done'
})

Expand Down

0 comments on commit 126e21f

Please sign in to comment.