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: Replace distributed tracing tests with node:test #2527

Merged
merged 1 commit into from
Aug 30, 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
41 changes: 19 additions & 22 deletions test/unit/custom-events/custom-event-aggregator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
*/

'use strict'
const tap = require('tap')
const test = require('node:test')
const assert = require('node:assert')
const CustomEventAggregator = require('../../../lib/custom-events/custom-event-aggregator')
const Metrics = require('../../../lib/metrics')
const NAMES = require('../../../lib/metrics/names')
Expand All @@ -14,12 +15,10 @@ const RUN_ID = 1337
const LIMIT = 5
const EXPECTED_METHOD = 'custom_event_data'

tap.test('Custom Event Aggregator', (t) => {
t.autoend()
let eventAggregator

t.beforeEach(() => {
eventAggregator = new CustomEventAggregator(
test('Custom Event Aggregator', async (t) => {
t.beforeEach((ctx) => {
ctx.nr = {}
ctx.nr.eventAggregator = new CustomEventAggregator(
{
runId: RUN_ID,
limit: LIMIT,
Expand All @@ -33,36 +32,34 @@ tap.test('Custom Event Aggregator', (t) => {
)
})

t.afterEach(() => {
eventAggregator = null
t.afterEach((ctx) => {
ctx.nr.eventAggregator = null
})
Comment on lines +35 to 37
Copy link
Contributor

Choose a reason for hiding this comment

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

Technically this is not necessary because each test will have a new scope. Thus, the ctx.nr.eventAggregator from test A will not be the same one in test B. But having the afterEach clear it is not harming anything. So this comment is informational, not blocking.


t.test('should set the correct default method', (t) => {
await t.test('should set the correct default method', (ctx) => {
const { eventAggregator } = ctx.nr
const method = eventAggregator.method

t.equal(method, EXPECTED_METHOD)
t.end()
assert.equal(method, EXPECTED_METHOD)
})

t.test('toPayloadSync() should return json format of data', (t) => {
await t.test('toPayloadSync() should return json format of data', (ctx) => {
const { eventAggregator } = ctx.nr
const rawEvent = [{ type: 'Custom' }, { foo: 'bar' }]

eventAggregator.add(rawEvent)

const payload = eventAggregator._toPayloadSync()
t.equal(payload.length, 2)
assert.equal(payload.length, 2)

const [runId, eventData] = payload

t.equal(runId, RUN_ID)
t.same(eventData, [rawEvent])
t.end()
assert.equal(runId, RUN_ID)
assert.deepStrictEqual(eventData, [rawEvent])
})

t.test('toPayloadSync() should return nothing with no event data', (t) => {
await t.test('toPayloadSync() should return nothing with no event data', (ctx) => {
const { eventAggregator } = ctx.nr
const payload = eventAggregator._toPayloadSync()

t.notOk(payload)
t.end()
assert.equal(payload, null)
})
})
45 changes: 23 additions & 22 deletions test/unit/distributed_tracing/dt-cats.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
*/

'use strict'
const tap = require('tap')
const test = require('node:test')
const assert = require('node:assert')
const Exception = require('../../../lib/errors').Exception
const helper = require('../../lib/agent_helper')
const recorder = require('../../../lib/metrics/recorders/distributed-trace')
Expand All @@ -14,21 +15,21 @@ const recordSupportability = require('../../../lib/agent').prototype.recordSuppo

const testCases = require('../../lib/cross_agent_tests/distributed_tracing/distributed_tracing.json')

tap.test('distributed tracing', function (t) {
t.autoend()
t.beforeEach((t) => {
test('distributed tracing', async function (t) {
t.beforeEach((ctx) => {
ctx.nr = {}
const agent = helper.loadMockedAgent({ distributed_tracing: { enabled: true } })
agent.recordSupportability = recordSupportability
t.context.agent = agent
ctx.nr.agent = agent
})

t.afterEach((t) => {
helper.unloadAgent(t.context.agent)
t.afterEach((ctx) => {
helper.unloadAgent(ctx.nr.agent)
})

testCases.forEach((testCase) => {
t.test(testCase.test_name, (t) => {
const { agent } = t.context
for (const testCase of testCases) {
await t.test(testCase.test_name, (ctx, end) => {
const { agent } = ctx.nr
agent.config.trusted_account_key = testCase.trusted_account_key
agent.config.account_id = testCase.account_id
agent.config.primary_application_id = 'test app'
Expand Down Expand Up @@ -71,21 +72,21 @@ tap.test('distributed tracing', function (t) {
Object.keys(exact).forEach((key) => {
const match = keyRegex.exec(key)
if (match) {
t.equal(created.d[match[1]], exact[key])
assert.equal(created.d[match[1]], exact[key])
} else {
t.same(created.v, exact.v)
assert.deepStrictEqual(created.v, exact.v)
}
})

if (outbound.expected) {
outbound.expected.forEach((key) => {
t.ok(created.d.hasOwnProperty(keyRegex.exec(key)[1]))
assert.ok(created.d.hasOwnProperty(keyRegex.exec(key)[1]))
})
}

if (outbound.unexpected) {
outbound.unexpected.forEach((key) => {
t.notOk(created.d.hasOwnProperty(keyRegex.exec(key)[1]))
assert.ok(!created.d.hasOwnProperty(keyRegex.exec(key)[1]))
})
}
})
Expand All @@ -95,7 +96,7 @@ tap.test('distributed tracing', function (t) {
tx.end()
const intrinsics = testCase.intrinsics
intrinsics.target_events.forEach((type) => {
t.ok(['Transaction', 'TransactionError', 'Span'].includes(type))
assert.ok(['Transaction', 'TransactionError', 'Span'].includes(type))

const common = intrinsics.common
const specific = intrinsics[type] || {}
Expand All @@ -116,7 +117,7 @@ tap.test('distributed tracing', function (t) {
const arbitrary = (specific.expected || []).concat(common.expected || [])
const unexpected = (specific.unexpected || []).concat(common.unexpected || [])

t.ok(toCheck.length > 0)
assert.ok(toCheck.length > 0)
toCheck.forEach((event) => {
// Span events are not payload-formatted straight out of the
// aggregator.
Expand All @@ -126,13 +127,13 @@ tap.test('distributed tracing', function (t) {

const attributes = event[0]
arbitrary.forEach((key) => {
t.ok(attributes[`${key}`], `${type} should have ${key}`)
assert.ok(attributes[`${key}`], `${type} should have ${key}`)
})
unexpected.forEach((key) => {
t.notOk(attributes[`${key}`], `${type} should not have ${key}`)
assert.ok(!attributes[`${key}`], `${type} should not have ${key}`)
Copy link
Contributor

Choose a reason for hiding this comment

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

assert.notOk is also available. I would find that easier to read than !something.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see it in this list (https://nodejs.org/api/assert.html) and whenever I use it it says that assert.notOk is not a function. Maybe I have a different version?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry. I must be thinking of notEqual.

})
Object.keys(exact).forEach((key) => {
t.equal(attributes[key], exact[key], `${type} should have equal ${key}`)
assert.equal(attributes[key], exact[key], `${type} should have equal ${key}`)
})
})
})
Expand All @@ -142,10 +143,10 @@ tap.test('distributed tracing', function (t) {
const metricName = metricPair[0]
const callCount = metrics.getOrCreateMetric(metricName).callCount
const metricCount = metricPair[1]
t.equal(callCount, metricCount, `${metricName} should have ${metricCount} samples`)
assert.equal(callCount, metricCount, `${metricName} should have ${metricCount} samples`)
})
t.end()
end()
})
})
})
}
})
33 changes: 14 additions & 19 deletions test/unit/distributed_tracing/dt-payload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,51 @@
*/

'use strict'
const tap = require('tap')
const test = require('node:test')
const assert = require('node:assert')
const DistributedTracePayload = require('../../../lib/transaction/dt-payload')
const DistributedTracePayloadStub = DistributedTracePayload.Stub

tap.test('DistributedTracePayload', function (t) {
t.test('has a text method that returns the stringified payload', function (t) {
test('DistributedTracePayload', async function (t) {
await t.test('has a text method that returns the stringified payload', function () {
const payload = {
a: 1,
b: 'test'
}
const dt = new DistributedTracePayload(payload)
const output = JSON.parse(dt.text())
t.ok(Array.isArray(output.v))
t.same(output.d, payload)
t.end()
assert.ok(Array.isArray(output.v))
assert.deepStrictEqual(output.d, payload)
})

t.test('has a httpSafe method that returns the base64 encoded payload', function (t) {
await t.test('has a httpSafe method that returns the base64 encoded payload', function () {
const payload = {
a: 1,
b: 'test'
}
const dt = new DistributedTracePayload(payload)
const output = JSON.parse(Buffer.from(dt.httpSafe(), 'base64').toString('utf-8'))
t.ok(Array.isArray(output.v))
t.same(output.d, payload)
t.end()
assert.ok(Array.isArray(output.v))
assert.deepStrictEqual(output.d, payload)
})
t.end()
})

tap.test('DistributedTracePayloadStub', function (t) {
t.test('has a httpSafe method that returns an empty string', function (t) {
test('DistributedTracePayloadStub', async function (t) {
await t.test('has a httpSafe method that returns an empty string', function () {
const payload = {
a: 1,
b: 'test'
}
const dt = new DistributedTracePayloadStub(payload)
t.equal(dt.httpSafe(), '')
t.end()
assert.equal(dt.httpSafe(), '')
})

t.test('has a text method that returns an empty string', function (t) {
await t.test('has a text method that returns an empty string', function () {
const payload = {
a: 1,
b: 'test'
}
const dt = new DistributedTracePayloadStub(payload)
t.equal(dt.text(), '')
t.end()
assert.equal(dt.text(), '')
})
t.end()
})
Loading
Loading