Skip to content

Commit 7a53a19

Browse files
test: Updated unit tests to use node:test (#282)
1 parent 869e524 commit 7a53a19

File tree

5 files changed

+123
-144
lines changed

5 files changed

+123
-144
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"lint": "eslint .",
99
"lint:fix": "eslint . --fix",
1010
"lint:lockfile": "lockfile-lint --path package-lock.json --type npm --allowed-hosts npm --validate-https --validate-integrity",
11-
"unit": "c8 -o ./coverage/unit tap --expose-gc --jobs=1 --no-coverage tests/unit/*.tap.js",
11+
"unit": "c8 -o ./coverage/unit borp --expose-gc --timeout 180000 'tests/unit/*.test.js'",
1212
"integration": "c8 -o ./coverage/integration tap --timeout 360000 --jobs=1 --no-coverage tests/integration/*.tap.js",
1313
"native": "node tests/native/*.js",
1414
"test": "npm run unit && npm run integration",
@@ -80,6 +80,7 @@
8080
"@newrelic/proxy": "^2.0.0",
8181
"async": "^3.2.2",
8282
"aws-sdk": "^2.266.1",
83+
"borp": "^0.19.0",
8384
"c8": "^8.0.0",
8485
"eslint": "^7.32.0",
8586
"eslint-config-prettier": "^8.3.0",

tests/unit/gc-metrics.tap.js

-46
This file was deleted.

tests/unit/gc-metrics.test.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2020 New Relic Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
'use strict'
7+
8+
const test = require('node:test')
9+
const assert = require('node:assert')
10+
11+
test('GC Metrics', async () => {
12+
const metricEmitter = require('../..')()
13+
14+
global.gc()
15+
16+
const gcs = metricEmitter.getGCMetrics()
17+
const keys = Object.keys(gcs)
18+
assert.ok(keys.length > 0, 'should notice at least one GC')
19+
assert.equal(typeof keys[0], 'string', 'should have strings as keys')
20+
21+
// GC stats objects
22+
const stats = gcs[keys[0]]
23+
assert.equal(typeof stats, 'object', 'should have stats objects')
24+
assert.equal(typeof stats.typeId, 'number', 'should have the type ID')
25+
assert.equal(typeof stats.type, 'string', 'should have the type name')
26+
assert.equal(typeof stats.metrics, 'object', 'should have a metrics object')
27+
28+
// GC stats metrics
29+
const metrics = gcs[keys[0]].metrics
30+
assert.equal(typeof metrics.total, 'number', 'should have total field')
31+
assert.equal(typeof metrics.min, 'number', 'should have min field')
32+
assert.equal(typeof metrics.max, 'number', 'should have max field')
33+
assert.equal(typeof metrics.sumOfSquares, 'number', 'should have sumOfSquares field')
34+
assert.equal(typeof metrics.count, 'number', 'should have count field')
35+
36+
assert.ok(metrics.total > 0, 'should have reasonable values for total')
37+
assert.ok(metrics.min > 0, 'should have reasonable values for min')
38+
assert.ok(metrics.max > 0, 'should have reasonable values for max')
39+
assert.ok(metrics.max >= metrics.min, 'should have a max larger than a min')
40+
assert.ok(metrics.sumOfSquares > 0, 'should have reasonable values for sumOfSquares')
41+
assert.ok(metrics.count > 0, 'should have reasonable values for count')
42+
})

tests/unit/loop-metrics.tap.js

-97
This file was deleted.

tests/unit/loop-metrics.test.js

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2020 New Relic Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
'use strict'
7+
8+
const test = require('node:test')
9+
const assert = require('node:assert')
10+
11+
test('Loop Metrics', async (t) => {
12+
const MICRO_TO_MILLIS = 1e-3
13+
const SPIN_TIME = 2000
14+
const CPU_EPSILON = SPIN_TIME * 0.05 // Allowed fudge factor for CPU times in MS
15+
const metricEmitter = require('../..')({ timeout: 10 })
16+
const testStart = Date.now()
17+
18+
t.after(() => {
19+
metricEmitter.unbind()
20+
})
21+
22+
// Check the structure of the metric object.
23+
let metric = metricEmitter.getLoopMetrics().usage
24+
assert.strictEqual(typeof metric, 'object', 'should provide a metric object')
25+
assert.strictEqual(typeof metric.total, 'number', 'should have a total')
26+
assert.strictEqual(typeof metric.min, 'number', 'should have a min')
27+
assert.strictEqual(typeof metric.max, 'number', 'should have a max')
28+
assert.strictEqual(typeof metric.sumOfSquares, 'number', 'should have a sumOfSquares')
29+
assert.strictEqual(typeof metric.count, 'number', 'should have a count')
30+
31+
// Check that the values are reset after the first call. Since this is
32+
// synchronous with the previous call, all results should be zero.
33+
metric = metricEmitter.getLoopMetrics().usage
34+
assert.deepStrictEqual(
35+
metric,
36+
{
37+
total: 0,
38+
min: 0,
39+
max: 0,
40+
sumOfSquares: 0,
41+
count: 0
42+
},
43+
'should reset all the values'
44+
)
45+
46+
// Queue up a loop with some CPU burn.
47+
await new Promise((resolve) => setTimeout(resolve, 100))
48+
49+
// spinning cpu...
50+
const start = Date.now()
51+
while (Date.now() - start < SPIN_TIME) {} // Spin the CPU for 2 seconds.
52+
53+
// Wait another tick and then check the loop stats.
54+
await new Promise((resolve) => setTimeout(resolve, 100))
55+
56+
metric = metricEmitter.getLoopMetrics()
57+
const testDuration = Date.now() - testStart + CPU_EPSILON
58+
const durationSquare = testDuration * testDuration
59+
const usage = metric.usage
60+
61+
const meanTime = usage.total / usage.count
62+
63+
assert.ok(
64+
usage.total * MICRO_TO_MILLIS > SPIN_TIME - CPU_EPSILON,
65+
'should have total greater than spin time'
66+
)
67+
assert.ok(
68+
usage.total * MICRO_TO_MILLIS <= testDuration,
69+
'should have total less than wall-clock time'
70+
)
71+
assert.ok(usage.min <= meanTime, 'should have min less than the mean usage time')
72+
assert.ok(usage.max >= meanTime, 'should have max greater than the mean usage time')
73+
assert.ok(usage.max * MICRO_TO_MILLIS > SPIN_TIME - CPU_EPSILON, 'should have expected max')
74+
assert.ok(
75+
usage.sumOfSquares * MICRO_TO_MILLIS * MICRO_TO_MILLIS < durationSquare,
76+
'should have expected sumOfSquares'
77+
)
78+
assert.ok(usage.count >= 2, 'should have expected count')
79+
})

0 commit comments

Comments
 (0)