Skip to content

Commit

Permalink
test: Replace distributed tracing tests with node:test (newrelic#2527)
Browse files Browse the repository at this point in the history
  • Loading branch information
amychisholm03 authored Aug 30, 2024
1 parent 1534a73 commit 8184c56
Show file tree
Hide file tree
Showing 4 changed files with 392 additions and 419 deletions.
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
})

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}`)
})
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

0 comments on commit 8184c56

Please sign in to comment.