Skip to content

Commit

Permalink
test: Migrated bluebird versioned tests to node:test (#2635)
Browse files Browse the repository at this point in the history
  • Loading branch information
bizob2828 authored Oct 4, 2024
1 parent c37abe5 commit 6e28fad
Show file tree
Hide file tree
Showing 16 changed files with 2,828 additions and 2,506 deletions.
12 changes: 9 additions & 3 deletions test/lib/agent_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,15 @@ helper.unloadAgent = (agent, shimmer = require('../../lib/shimmer')) => {

helper.loadTestAgent = (t, conf, setState = true) => {
const agent = helper.instrumentMockedAgent(conf, setState)
t.teardown(() => {
helper.unloadAgent(agent)
})
if (t.after) {
t.after(() => {
helper.unloadAgent(agent)
})
} else {
t.teardown(() => {
helper.unloadAgent(agent)
})
}

return agent
}
Expand Down
115 changes: 115 additions & 0 deletions test/lib/promises/common-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright 2024 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'
const { tspl } = require('@matteo.collina/tspl')
const helper = require('../agent_helper')
const COUNT = 2
const { checkTransaction, end, runMultiple } = require('./helpers')

module.exports = function init({ t, agent, Promise }) {
return async function performTests(name, resolve, reject) {
const inTx = doPerformTests({ t, agent, Promise, name, resolve, reject, inTx: true })
const notInTx = doPerformTests({ t, agent, Promise, name, resolve, reject, inTx: false })
return Promise.all([inTx, notInTx])
}
}

async function doPerformTests({ t, agent, Promise, name, resolve, reject, inTx }) {
name += ' ' + (inTx ? 'with' : 'without') + ' transaction'

await t.test(name + ': does not cause JSON to crash', async function (t) {
const plan = tspl(t, { plan: 1 * COUNT + 1 })

runMultiple(
COUNT,
function (i, cb) {
if (inTx) {
helper.runInTransaction(agent, test)
} else {
test(null)
}

function test(transaction) {
const p = resolve(Promise).then(end(transaction, cb), end(transaction, cb))
const d = p.domain
delete p.domain
plan.doesNotThrow(function () {
JSON.stringify(p)
}, 'should not cause stringification to crash')
p.domain = d
}
},
function (err) {
plan.ok(!err, 'should not error')
}
)
await plan.completed
})

await t.test(name + ': preserves transaction in resolve callback', async function (t) {
const plan = tspl(t, { plan: 4 * COUNT + 1 })

runMultiple(
COUNT,
function (i, cb) {
if (inTx) {
helper.runInTransaction(agent, test)
} else {
test(null)
}

function test(transaction) {
resolve(Promise)
.then(function step() {
plan.ok(1, 'should not change execution profile')
return i
})
.then(function finalHandler(res) {
plan.equal(res, i, 'should be the correct value')
checkTransaction(plan, agent, transaction)
})
.then(end(transaction, cb), end(transaction, cb))
}
},
function (err) {
plan.ok(!err, 'should not error')
}
)
await plan.completed
})

await t.test(name + ': preserves transaction in reject callback', async function (t) {
const plan = tspl(t, { plan: 3 * COUNT + 1 })

runMultiple(
COUNT,
function (i, cb) {
if (inTx) {
helper.runInTransaction(agent, test)
} else {
test(null)
}

function test(transaction) {
const err = new Error('some error ' + i)
reject(Promise, err)
.then(function unusedStep() {
plan.ok(0, 'should not change execution profile')
})
.catch(function catchHandler(reason) {
plan.equal(reason, err, 'should be the same error')
checkTransaction(plan, agent, transaction)
})
.then(end(transaction, cb), end(transaction, cb))
}
},
function (err) {
plan.ok(!err, 'should not error')
}
)
await plan.completed
})
}
47 changes: 47 additions & 0 deletions test/lib/promises/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2024 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'

function runMultiple(count, fn, cb) {
let finished = 0
for (let i = 0; i < count; ++i) {
fn(i, function runMultipleCallback() {
if (++finished >= count) {
cb()
}
})
}
}

function checkTransaction(plan, agent, transaction) {
const currentTransaction = agent.getTransaction()

if (transaction) {
plan.ok(currentTransaction, 'should be in a transaction')
if (!currentTransaction) {
return
}
plan.equal(currentTransaction.id, transaction.id, 'should be the same transaction')
} else {
plan.ok(!currentTransaction, 'should not be in a transaction')
plan.ok(1) // Make test count match for both branches.
}
}

function end(tx, cb) {
return function () {
if (tx) {
tx.end()
}
cb()
}
}

module.exports = {
checkTransaction,
end,
runMultiple
}
Loading

0 comments on commit 6e28fad

Please sign in to comment.