Skip to content

Commit

Permalink
Revert "test: Migrated test/unit/util to use node:test (newrelic#…
Browse files Browse the repository at this point in the history
…2546)"

This reverts commit ce3da2a.
  • Loading branch information
sumitsuthar authored Sep 20, 2024
1 parent 5c805f3 commit c6e4781
Show file tree
Hide file tree
Showing 13 changed files with 596 additions and 594 deletions.
158 changes: 81 additions & 77 deletions test/unit/util/application-logging.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
*/

'use strict'
const assert = require('node:assert')
const test = require('node:test')

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

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

const processedStr = loggingUtils.truncate(longString)

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

t.end()
})

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

const processedStr = loggingUtils.truncate(str)

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

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

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

t.beforeEach((ctx) => {
ctx.nr = {}
ctx.nr.config = {
let config = {}

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

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)
}
)
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 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(`${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('should be false when application_logging is false', (t) => {
const { config } = t.nr
t.test('should be false when application_logging is false', (t) => {
config.application_logging.enabled = false
assert.equal(loggingUtils.isApplicationLoggingEnabled(config), false)
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)
t.test('should be false when all features are false', (t) => {
t.equal(loggingUtils.isApplicationLoggingEnabled(config), false)
t.end()
})
})

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

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

const levels = Object.keys(LOGGING.LEVELS)
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')
})
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 t.test('should default to unknown when level is undefined', (t) => {
const { metricsStub, callCountStub } = t.nr
t.test('should default to unknown when level is undefined', (t) => {
loggingUtils.incrementLoggingLinesMetrics(undefined, metricsStub)
assert.equal(
t.equal(
metricsStub.getOrCreateMetric.args[0][0],
LOGGING.LINES,
`should create ${LOGGING.LINES} metric`
)
assert.equal(
t.equal(
metricsStub.getOrCreateMetric.args[1][0],
LOGGING.LEVELS.UNKNOWN,
`should create ${LOGGING.LEVELS.UNKNOWN} metric`
)
assert.equal(callCountStub.incrementCallCount.callCount, 2, 'should increment each metric')
t.equal(callCountStub.incrementCallCount.callCount, 2, 'should increment each metric')
t.end()
})
})
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 assert = require('node:assert')
const test = require('node:test')

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

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

const promise = eachLimit(items, mapper, 2)

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')
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')

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

secondPromiseResolve()
thirdPromiseResolve()

const results = await promise

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')
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')
})
72 changes: 43 additions & 29 deletions test/unit/util/byte-limit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,79 @@
*/

'use strict'
const assert = require('node:assert')
const test = require('node:test')

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

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

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 equal to the limit', (t) => {
t.ok(byteUtils.isValidLength('12345', 5))
t.end()
})

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

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

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

0 comments on commit c6e4781

Please sign in to comment.