Skip to content

Commit

Permalink
test: Convert metric and metrics-recorder tests to node:test (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
amychisholm03 authored Sep 11, 2024
1 parent 8f7aebe commit 7ae4af4
Show file tree
Hide file tree
Showing 12 changed files with 1,098 additions and 1,106 deletions.
93 changes: 93 additions & 0 deletions test/lib/custom-assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

'use strict'
const assert = require('node:assert')
const { isSimpleObject } = require('../../lib/util/objects')

function assertExactClmAttrs(segmentStub, expectedAttrs) {
const attrs = segmentStub.addAttribute.args
Expand Down Expand Up @@ -232,9 +233,101 @@ function match(actual, expected) {
return true
}

/**
* @param {Metrics} metrics metrics under test
* @param {Array} expected Array of metric data where metric data is in this form:
* [
* {
* “name”:”name of metric”,
* “scope”:”scope of metric”,
* },
* [count,
* total time,
* exclusive time,
* min time,
* max time,
* sum of squares]
* ]
* @param {boolean} exclusive When true, found and expected metric lengths should match
* @param {boolean} assertValues When true, metric values must match expected
*/
function assertMetrics(metrics, expected, exclusive, assertValues) {
// Assertions about arguments because maybe something returned undefined
// unexpectedly and is passed in, or a return type changed. This will
// hopefully help catch that and make it obvious.
assert.ok(isSimpleObject(metrics), 'first argument required to be an Metrics object')
assert.ok(Array.isArray(expected), 'second argument required to be an array of metrics')
assert.ok(typeof exclusive === 'boolean', 'third argument required to be a boolean if provided')

if (assertValues === undefined) {
assertValues = true
}

for (let i = 0, len = expected.length; i < len; i++) {
const expectedMetric = expected[i]
const metric = metrics.getMetric(expectedMetric[0].name, expectedMetric[0].scope)
assert.ok(metric, `should find ${expectedMetric[0].name}`)
if (assertValues) {
assert.deepEqual(metric.toJSON(), expectedMetric[1])
}
}

if (exclusive) {
const metricsList = metrics.toJSON()
assert.equal(metricsList.length, expected.length)
}
}

/**
* @param {Transaction} transaction Nodejs agent transaction
* @param {Array} expected Array of metric data where metric data is in this form:
* [
* {
* “name”:”name of metric”,
* “scope”:”scope of metric”,
* },
* [count,
* total time,
* exclusive time,
* min time,
* max time,
* sum of squares]
* ]
* @param {boolean} exact When true, found and expected metric lengths should match
*/
function assertMetricValues(transaction, expected, exact) {
const metrics = transaction.metrics

for (let i = 0; i < expected.length; ++i) {
let expectedMetric = Object.assign({}, expected[i])
let name = null
let scope = null

if (typeof expectedMetric === 'string') {
name = expectedMetric
expectedMetric = {}
} else {
name = expectedMetric[0].name
scope = expectedMetric[0].scope
}

const metric = metrics.getMetric(name, scope)
assert.ok(metric, 'should have expected metric name')

assert.deepStrictEqual(metric.toJSON(), expectedMetric[1], 'metric values should match')
}

if (exact) {
const metricsJSON = metrics.toJSON()
assert.equal(metricsJSON.length, expected.length, 'metrics length should match')
}
}

module.exports = {
assertCLMAttrs,
assertExactClmAttrs,
assertMetrics,
assertMetricValues,
assertSegments,
compareSegments,
isNonWritable,
Expand Down
28 changes: 14 additions & 14 deletions test/unit/metric/datastore-instance.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@

'use strict'

const tap = require('tap')

const test = require('node:test')
const assert = require('node:assert')
const helper = require('../../lib/agent_helper')
const DatastoreShim = require('../../../lib/shim/datastore-shim')
const tests = require('../../lib/cross_agent_tests/datastores/datastore_instances')
const DatastoreParameters = require('../../../lib/shim/specs/params/datastore')

tap.test('Datastore instance metrics collected via the datastore shim', function (t) {
t.autoend()
t.beforeEach(function (t) {
t.context.agent = helper.loadMockedAgent()
test('Datastore instance metrics collected via the datastore shim', async function (t) {
t.beforeEach(function (ctx) {
ctx.nr = {}
ctx.nr.agent = helper.loadMockedAgent()
})

t.afterEach(function (t) {
const { agent } = t.context
t.afterEach(function (ctx) {
const { agent } = ctx.nr
if (agent) {
helper.unloadAgent(agent)
}
})

tests.forEach(function (test) {
t.test(test.name, function (t) {
const { agent } = t.context
for (const test of tests) {
await t.test(test.name, function (t, end) {
const { agent } = t.nr
agent.config.getHostnameSafe = function () {
return test.system_hostname
}
Expand Down Expand Up @@ -65,11 +65,11 @@ tap.test('Datastore instance metrics collected via the datastore shim', function
testInstrumented.query()

tx.end()
t.ok(getMetrics(agent).unscoped[test.expected_instance_metric])
t.end()
assert.ok(getMetrics(agent).unscoped[test.expected_instance_metric])
end()
})
})
})
}
})

function getMetrics(agent) {
Expand Down
Loading

0 comments on commit 7ae4af4

Please sign in to comment.