-
Notifications
You must be signed in to change notification settings - Fork 398
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Migrated bluebird versioned tests to
node:test
(#2635)
- Loading branch information
Showing
16 changed files
with
2,828 additions
and
2,506 deletions.
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,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 | ||
}) | ||
} |
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,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 | ||
} |
Oops, something went wrong.