Skip to content

Commit

Permalink
test: Migrated test/unit/util to use node:test (newrelic#2546)
Browse files Browse the repository at this point in the history
  • Loading branch information
bizob2828 authored and sumitsuthar committed Sep 20, 2024
1 parent d65ae85 commit ce3da2a
Show file tree
Hide file tree
Showing 13 changed files with 594 additions and 596 deletions.
158 changes: 77 additions & 81 deletions test/unit/util/application-logging.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
*/

'use strict'

const tap = require('tap')
const assert = require('node:assert')
const test = require('node:test')
const sinon = require('sinon')
const loggingUtils = require('../../../lib/util/application-logging')
const { LOGGING } = require('../../../lib/metrics/names')

tap.test('truncate', (t) => {
t.autoend()
t.test('Should truncate string > 1024 chars', (t) => {
test('truncate', async (t) => {
await t.test('Should truncate string > 1024 chars', () => {
const longString =
'1111111111111111111111111111111111111111111111111111111111111111' +
'1111111111111111111111111111111111111111111111111111111111111111' +
Expand All @@ -35,19 +34,16 @@ tap.test('truncate', (t) => {

const processedStr = loggingUtils.truncate(longString)

t.equal(processedStr.length, 1024)
t.equal(processedStr.substring(processedStr.length - 3), '...')

t.end()
assert.equal(processedStr.length, 1024)
assert.equal(processedStr.substring(processedStr.length - 3), '...')
})

t.test('Should return non-truncated string when <= 1024 chars', (t) => {
await t.test('Should return non-truncated string when <= 1024 chars', () => {
const str = 'kenny loggins'

const processedStr = loggingUtils.truncate(str)

t.equal(processedStr, str)
t.end()
assert.equal(processedStr, str)
})

const negativeTests = [
Expand All @@ -58,27 +54,25 @@ tap.test('truncate', (t) => {
{ value: [], type: 'array' },
{ value: function () {}, type: 'function' }
]
negativeTests.forEach(({ value, type }) => {
t.test(`should not truncate ${type}`, (t) => {
for (const negativeTest of negativeTests) {
const { value, type } = negativeTest
await t.test(`should not truncate ${type}`, () => {
const newValue = loggingUtils.truncate(value)
t.same(value, newValue)
t.end()
assert.deepEqual(value, newValue)
})
})
}
})

tap.test('Application Logging Config Tests', (t) => {
t.autoend()
test('Application Logging Config Tests', async (t) => {
const features = [
{ feature: 'metrics', method: 'isMetricsEnabled' },
{ feature: 'forwarding', method: 'isLogForwardingEnabled' },
{ feature: 'local_decorating', method: 'isLocalDecoratingEnabled' }
]

let config = {}

t.beforeEach(() => {
config = {
t.beforeEach((ctx) => {
ctx.nr = {}
ctx.nr.config = {
application_logging: {
enabled: true,
metrics: {
Expand All @@ -94,88 +88,90 @@ tap.test('Application Logging Config Tests', (t) => {
}
})

features.forEach(({ feature, method }) => {
t.test(
`isApplicationLoggingEnabled should be true when application_logging and ${feature} is truthy`,
(t) => {
config.application_logging[feature].enabled = true
t.equal(loggingUtils.isApplicationLoggingEnabled(config), true)
t.end()
}
)
await Promise.all(
features.map(async ({ feature, method }) => {
await t.test(
`isApplicationLoggingEnabled should be true when application_logging and ${feature} is truthy`,
(t) => {
const { config } = t.nr
config.application_logging[feature].enabled = true
assert.equal(loggingUtils.isApplicationLoggingEnabled(config), true)
}
)

t.test(`${method} should be true when application_logging and ${feature} are truthy`, (t) => {
config.application_logging[feature].enabled = true
if (feature === 'forwarding') {
t.equal(loggingUtils[method](config, { logs: true }), true)
} else {
t.equal(loggingUtils[method](config), true)
}
t.end()
await t.test(
`${method} should be true when application_logging and ${feature} are truthy`,
(t) => {
const { config } = t.nr
config.application_logging[feature].enabled = true
if (feature === 'forwarding') {
assert.equal(loggingUtils[method](config, { logs: true }), true)
} else {
assert.equal(loggingUtils[method](config), true)
}
}
)
})
})
)

t.test('should be false when application_logging is false', (t) => {
await t.test('should be false when application_logging is false', (t) => {
const { config } = t.nr
config.application_logging.enabled = false
t.equal(loggingUtils.isApplicationLoggingEnabled(config), false)
t.end()
assert.equal(loggingUtils.isApplicationLoggingEnabled(config), false)
})

t.test('should be false when all features are false', (t) => {
t.equal(loggingUtils.isApplicationLoggingEnabled(config), false)
t.end()
await t.test('should be false when all features are false', (t) => {
const { config } = t.nr
assert.equal(loggingUtils.isApplicationLoggingEnabled(config), false)
})
})

tap.test('incrementLoggingLinesMetrics', (t) => {
t.autoend()
let callCountStub = null
let metricsStub = null
t.beforeEach(() => {
callCountStub = { incrementCallCount: sinon.stub() }
metricsStub = {
test('incrementLoggingLinesMetrics', async (t) => {
t.beforeEach((ctx) => {
console.log('before test')
ctx.nr = {}
const callCountStub = { incrementCallCount: sinon.stub() }
ctx.nr.metricsStub = {
getOrCreateMetric: sinon.stub().returns(callCountStub)
}
})

t.afterEach(() => {
callCountStub = null
metricsStub = null
ctx.nr.callCountStub = callCountStub
})

const levels = Object.keys(LOGGING.LEVELS)
levels.forEach((level) => {
const levelLowercase = level.toLowerCase()
t.test(`should increment logging lines metrics for level: ${levelLowercase}`, (t) => {
loggingUtils.incrementLoggingLinesMetrics(levelLowercase, metricsStub)
t.equal(
metricsStub.getOrCreateMetric.args[0][0],
LOGGING.LINES,
`should create ${LOGGING.LINES} metric`
)
t.equal(
metricsStub.getOrCreateMetric.args[1][0],
LOGGING.LEVELS[level],
`should create ${LOGGING.LEVELS[level]} metric`
)
t.equal(callCountStub.incrementCallCount.callCount, 2, 'should increment each metric')
t.end()
await Promise.all(
levels.map(async (level) => {
const levelLowercase = level.toLowerCase()
await t.test(`should increment logging lines metrics for level: ${levelLowercase}`, (t) => {
const { metricsStub, callCountStub } = t.nr
loggingUtils.incrementLoggingLinesMetrics(levelLowercase, metricsStub)
assert.equal(
metricsStub.getOrCreateMetric.args[0][0],
LOGGING.LINES,
`should create ${LOGGING.LINES} metric`
)
assert.equal(
metricsStub.getOrCreateMetric.args[1][0],
LOGGING.LEVELS[level],
`should create ${LOGGING.LEVELS[level]} metric`
)
assert.equal(callCountStub.incrementCallCount.callCount, 2, 'should increment each metric')
})
})
})
)

t.test('should default to unknown when level is undefined', (t) => {
await t.test('should default to unknown when level is undefined', (t) => {
const { metricsStub, callCountStub } = t.nr
loggingUtils.incrementLoggingLinesMetrics(undefined, metricsStub)
t.equal(
assert.equal(
metricsStub.getOrCreateMetric.args[0][0],
LOGGING.LINES,
`should create ${LOGGING.LINES} metric`
)
t.equal(
assert.equal(
metricsStub.getOrCreateMetric.args[1][0],
LOGGING.LEVELS.UNKNOWN,
`should create ${LOGGING.LEVELS.UNKNOWN} metric`
)
t.equal(callCountStub.incrementCallCount.callCount, 2, 'should increment each metric')
t.end()
assert.equal(callCountStub.incrementCallCount.callCount, 2, 'should increment each metric')
})
})
22 changes: 11 additions & 11 deletions test/unit/util/async-each-limit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
*/

'use strict'

const { test } = require('tap')
const assert = require('node:assert')
const test = require('node:test')
const sinon = require('sinon')
const eachLimit = require('../../../lib/util/async-each-limit')

test('eachLimit should limit concurrent async executions', async (t) => {
test('eachLimit should limit concurrent async executions', async () => {
let firstPromiseResolve
let secondPromiseResolve
let thirdPromiseResolve
Expand Down Expand Up @@ -47,20 +47,20 @@ test('eachLimit should limit concurrent async executions', async (t) => {

const promise = eachLimit(items, mapper, 2)

t.equal(access.callCount, 2, 'should have called two promises')
t.ok(access.calledWith('foo.json'), 'should have called the first promise')
t.ok(access.calledWith('bar.json'), 'should have called the second promise')
t.notOk(access.calledWith('baz.json'), 'should not have called the third promise yet')
assert.equal(access.callCount, 2, 'should have called two promises')
assert.ok(access.calledWith('foo.json'), 'should have called the first promise')
assert.ok(access.calledWith('bar.json'), 'should have called the second promise')
assert.ok(!access.calledWith('baz.json'), 'should not have called the third promise yet')

firstPromiseResolve()
t.notOk(access.calledWith('baz.json'), 'should still not have called the third promise')
assert.ok(!access.calledWith('baz.json'), 'should still not have called the third promise')

secondPromiseResolve()
thirdPromiseResolve()

const results = await promise

t.equal(access.callCount, 3, 'should have called three promises')
t.ok(access.calledWith('baz.json'), 'should have called the third promise')
t.same(results, [true, true, true], 'should return the correct results')
assert.equal(access.callCount, 3, 'should have called three promises')
assert.ok(access.calledWith('baz.json'), 'should have called the third promise')
assert.deepEqual(results, [true, true, true], 'should return the correct results')
})
72 changes: 29 additions & 43 deletions test/unit/util/byte-limit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,79 +4,65 @@
*/

'use strict'

const { test } = require('tap')
const assert = require('node:assert')
const test = require('node:test')
const byteUtils = require('../../../lib/util/byte-limit')

test('byte-limit', (t) => {
t.autoend()

t.test('#isValidLength', (t) => {
t.autoend()
t.test('returns false when the string is larger than the limit', (t) => {
t.notOk(byteUtils.isValidLength('12345', 4))
t.end()
test('byte-limit', async (t) => {
await t.test('#isValidLength', async (t) => {
await t.test('returns false when the string is larger than the limit', () => {
assert.ok(!byteUtils.isValidLength('12345', 4))
})

t.test('returns true when the string is equal to the limit', (t) => {
t.ok(byteUtils.isValidLength('12345', 5))
t.end()
await t.test('returns true when the string is equal to the limit', () => {
assert.ok(byteUtils.isValidLength('12345', 5))
})

t.test('returns true when the string is smaller than the limit', (t) => {
t.ok(byteUtils.isValidLength('12345', 6))
t.end()
await t.test('returns true when the string is smaller than the limit', () => {
assert.ok(byteUtils.isValidLength('12345', 6))
})
})
t.test('#compareLength', (t) => {
t.autoend()
t.test('returns -1 when the string is smaller than the limit', (t) => {

await t.test('#compareLength', async (t) => {
await t.test('returns -1 when the string is smaller than the limit', () => {
const str = '123456789'
const cmpVal = byteUtils.compareLength(str, 255)
t.ok(cmpVal < 0)
t.end()
assert.ok(cmpVal < 0)
})
t.test('returns 0 when the string is equal than the limit', (t) => {
await t.test('returns 0 when the string is equal than the limit', () => {
const str = '123456789'
const cmpVal = byteUtils.compareLength(str, 9)
t.equal(cmpVal, 0)
t.end()
assert.equal(cmpVal, 0)
})
t.test('returns 1 when the string is larger than the limit', (t) => {
await t.test('returns 1 when the string is larger than the limit', () => {
const str = '123456789'
const cmpVal = byteUtils.compareLength(str, 2)
t.ok(cmpVal > 0)
t.end()
assert.ok(cmpVal > 0)
})
})

t.test('#truncate', (t) => {
t.autoend()
t.test('truncates string value to given limit', (t) => {
await t.test('#truncate', async (t) => {
await t.test('truncates string value to given limit', () => {
let str = '123456789'
str = byteUtils.truncate(str, 5)
t.equal(str, '12345')
t.end()
assert.equal(str, '12345')
})
t.test('returns original string if within limit', (t) => {
await t.test('returns original string if within limit', () => {
let str = '123456789'
str = byteUtils.truncate(str, 10)
t.equal(str, '123456789')
t.end()
assert.equal(str, '123456789')
})
t.test('respects multibyte characters', (t) => {
await t.test('respects multibyte characters', () => {
let str = '\uD87E\uDC04\uD87E\uDC04'
t.equal(Buffer.byteLength(str, 'utf8'), 8)
assert.equal(Buffer.byteLength(str, 'utf8'), 8)
str = byteUtils.truncate(str, 3)
t.equal(str, '\uD87E')
t.end()
assert.equal(str, '\uD87E')
})
t.test('should strings with split unicode characters properly', (t) => {
await t.test('should strings with split unicode characters properly', () => {
let str = '\uD87E\uDC04\uD87E\uDC04'
t.equal(Buffer.byteLength(str, 'utf8'), 8)
assert.equal(Buffer.byteLength(str, 'utf8'), 8)
str = byteUtils.truncate(str, 2)
t.equal(str, '')
t.end()
assert.equal(str, '')
})
})
})
Loading

0 comments on commit ce3da2a

Please sign in to comment.