Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Migrated bluebird versioned tests to node:test #2635

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have so many of these methods that do almost the same thing. It's very confusing to know which one to use.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed. I didn't know this one even existed until these tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was is really only used when you a root test that needs an agent

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll file a ticket because it looks like we have quite a few places where we are manually doing this in tests

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
Loading