-
Notifications
You must be signed in to change notification settings - Fork 41
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: Updated unit tests to use node:test
#282
Merged
svetlanabrennan
merged 6 commits into
newrelic:main
from
svetlanabrennan:migrate-unit-tests-to-node-test
Dec 10, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8ac0f9a
updated unit tests to use node test
svetlanabrennan 8a9dfad
fix comment
svetlanabrennan 374885b
fix unit script
svetlanabrennan d8387d6
fix test and unit script
svetlanabrennan 9c5147f
add expose gc to unit script
svetlanabrennan 3ee289c
refactored comments in unit tests
svetlanabrennan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 was deleted.
Oops, something went wrong.
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,42 @@ | ||
/* | ||
* Copyright 2020 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
const test = require('node:test') | ||
const assert = require('node:assert') | ||
|
||
test('GC Metrics', async () => { | ||
const metricEmitter = require('../..')() | ||
|
||
global.gc() | ||
|
||
const gcs = metricEmitter.getGCMetrics() | ||
const keys = Object.keys(gcs) | ||
assert.ok(keys.length > 0, 'should notice at least one GC') | ||
assert.equal(typeof keys[0], 'string', 'should have strings as keys') | ||
|
||
// GC stats objects | ||
const stats = gcs[keys[0]] | ||
assert.equal(typeof stats, 'object', 'should have stats objects') | ||
assert.equal(typeof stats.typeId, 'number', 'should have the type ID') | ||
assert.equal(typeof stats.type, 'string', 'should have the type name') | ||
assert.equal(typeof stats.metrics, 'object', 'should have a metrics object') | ||
|
||
// GC stats metrics | ||
const metrics = gcs[keys[0]].metrics | ||
assert.equal(typeof metrics.total, 'number', 'should have total field') | ||
assert.equal(typeof metrics.min, 'number', 'should have min field') | ||
assert.equal(typeof metrics.max, 'number', 'should have max field') | ||
assert.equal(typeof metrics.sumOfSquares, 'number', 'should have sumOfSquares field') | ||
assert.equal(typeof metrics.count, 'number', 'should have count field') | ||
|
||
assert.ok(metrics.total > 0, 'should have reasonable values for total') | ||
assert.ok(metrics.min > 0, 'should have reasonable values for min') | ||
assert.ok(metrics.max > 0, 'should have reasonable values for max') | ||
assert.ok(metrics.max >= metrics.min, 'should have a max larger than a min') | ||
assert.ok(metrics.sumOfSquares > 0, 'should have reasonable values for sumOfSquares') | ||
assert.ok(metrics.count > 0, 'should have reasonable values for count') | ||
}) |
This file was deleted.
Oops, something went wrong.
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,79 @@ | ||
/* | ||
* Copyright 2020 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
const test = require('node:test') | ||
const assert = require('node:assert') | ||
|
||
test('Loop Metrics', async (t) => { | ||
const MICRO_TO_MILLIS = 1e-3 | ||
const SPIN_TIME = 2000 | ||
const CPU_EPSILON = SPIN_TIME * 0.05 // Allowed fudge factor for CPU times in MS | ||
const metricEmitter = require('../..')({ timeout: 10 }) | ||
const testStart = Date.now() | ||
|
||
t.after(() => { | ||
metricEmitter.unbind() | ||
}) | ||
|
||
// Check the structure of the metric object. | ||
let metric = metricEmitter.getLoopMetrics().usage | ||
assert.strictEqual(typeof metric, 'object', 'should provide a metric object') | ||
assert.strictEqual(typeof metric.total, 'number', 'should have a total') | ||
assert.strictEqual(typeof metric.min, 'number', 'should have a min') | ||
assert.strictEqual(typeof metric.max, 'number', 'should have a max') | ||
assert.strictEqual(typeof metric.sumOfSquares, 'number', 'should have a sumOfSquares') | ||
assert.strictEqual(typeof metric.count, 'number', 'should have a count') | ||
|
||
// Check that the values are reset after the first call. Since this is | ||
// synchronous with the previous call, all results should be zero. | ||
metric = metricEmitter.getLoopMetrics().usage | ||
assert.deepStrictEqual( | ||
metric, | ||
{ | ||
total: 0, | ||
min: 0, | ||
max: 0, | ||
sumOfSquares: 0, | ||
count: 0 | ||
}, | ||
'should reset all the values' | ||
) | ||
|
||
// Queue up a loop with some CPU burn. | ||
await new Promise((resolve) => setTimeout(resolve, 100)) | ||
|
||
// spinning cpu... | ||
const start = Date.now() | ||
while (Date.now() - start < SPIN_TIME) {} // Spin the CPU for 2 seconds. | ||
|
||
// Wait another tick and then check the loop stats. | ||
await new Promise((resolve) => setTimeout(resolve, 100)) | ||
|
||
metric = metricEmitter.getLoopMetrics() | ||
const testDuration = Date.now() - testStart + CPU_EPSILON | ||
const durationSquare = testDuration * testDuration | ||
const usage = metric.usage | ||
|
||
const meanTime = usage.total / usage.count | ||
|
||
assert.ok( | ||
usage.total * MICRO_TO_MILLIS > SPIN_TIME - CPU_EPSILON, | ||
'should have total greater than spin time' | ||
) | ||
assert.ok( | ||
usage.total * MICRO_TO_MILLIS <= testDuration, | ||
'should have total less than wall-clock time' | ||
) | ||
assert.ok(usage.min <= meanTime, 'should have min less than the mean usage time') | ||
assert.ok(usage.max >= meanTime, 'should have max greater than the mean usage time') | ||
assert.ok(usage.max * MICRO_TO_MILLIS > SPIN_TIME - CPU_EPSILON, 'should have expected max') | ||
assert.ok( | ||
usage.sumOfSquares * MICRO_TO_MILLIS * MICRO_TO_MILLIS < durationSquare, | ||
'should have expected sumOfSquares' | ||
) | ||
assert.ok(usage.count >= 2, 'should have expected count') | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is also addressing #280