Skip to content

Commit

Permalink
update instrumentation-tracker tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners-nr committed Sep 23, 2024
1 parent 218c1f5 commit c91ab0e
Showing 1 changed file with 42 additions and 39 deletions.
81 changes: 42 additions & 39 deletions test/unit/instrumentation-tracker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,98 +5,103 @@

'use strict'

const tap = require('tap')
const test = require('node:test')
const assert = require('node:assert')

const { match } = require('../lib/custom-assertions')

const InstrumentationTracker = require('../../lib/instrumentation-tracker')
const InstrumentationDescriptor = require('../../lib/instrumentation-descriptor')

tap.test('can inspect object type', async (t) => {
test('can inspect object type', async () => {
const tracker = new InstrumentationTracker()
t.equal(Object.prototype.toString.call(tracker), '[object InstrumentationTracker]')
assert.equal(Object.prototype.toString.call(tracker), '[object InstrumentationTracker]')
})

tap.test('track method tracks new items and updates existing ones', async (t) => {
test('track method tracks new items and updates existing ones', async () => {
const tracker = new InstrumentationTracker()
const inst1 = new InstrumentationDescriptor({ moduleName: 'foo' })

tracker.track('foo', inst1)
t.equal(tracker.getAllByName('foo').length, 1)
assert.equal(tracker.getAllByName('foo').length, 1)

// Module already tracked and instrumentation id is the same.
tracker.track('foo', inst1)
t.equal(tracker.getAllByName('foo').length, 1)
assert.equal(tracker.getAllByName('foo').length, 1)

// Module already tracked, but new instrumentation with different id.
const inst2 = new InstrumentationDescriptor({ moduleName: 'foo' })
tracker.track('foo', inst2)
t.equal(tracker.getAllByName('foo').length, 2)
assert.equal(tracker.getAllByName('foo').length, 2)
})

tap.test('can get a tracked item by instrumentation', async (t) => {
test('can get a tracked item by instrumentation', async () => {
const tracker = new InstrumentationTracker()
const inst = new InstrumentationDescriptor({ moduleName: 'foo' })

tracker.track('foo', inst)
const item = tracker.getTrackedItem('foo', inst)
t.equal(item.instrumentation, inst)
t.same(item.meta, { instrumented: false, didError: undefined })
assert.equal(item.instrumentation, inst)
assert.equal(match(item.meta, { instrumented: false, didError: undefined }), true)
})

tap.test('sets hook failure correctly', async (t) => {
test('sets hook failure correctly', async () => {
const tracker = new InstrumentationTracker()
const inst = new InstrumentationDescriptor({ moduleName: 'foo' })

tracker.track('foo', inst)
const item = tracker.getTrackedItem('foo', inst)
tracker.setHookFailure(item)
t.equal(item.meta.instrumented, false)
t.equal(item.meta.didError, true)
assert.equal(item.meta.instrumented, false)
assert.equal(item.meta.didError, true)

// Double check that the item in the map got updated.
const items = tracker.getAllByName('foo')
t.equal(items[0].meta.instrumented, false)
t.equal(items[0].meta.didError, true)
assert.equal(items[0].meta.instrumented, false)
assert.equal(items[0].meta.didError, true)
})

tap.test('sets hook success correctly', async (t) => {
test('sets hook success correctly', async () => {
const tracker = new InstrumentationTracker()
const inst = new InstrumentationDescriptor({ moduleName: 'foo' })

tracker.track('foo', inst)
const item = tracker.getTrackedItem('foo', inst)
tracker.setHookSuccess(item)
t.equal(item.meta.instrumented, true)
t.equal(item.meta.didError, false)
assert.equal(item.meta.instrumented, true)
assert.equal(item.meta.didError, false)

// Double check that the item in the map got updated.
const items = tracker.getAllByName('foo')
t.equal(items[0].meta.instrumented, true)
t.equal(items[0].meta.didError, false)
assert.equal(items[0].meta.instrumented, true)
assert.equal(items[0].meta.didError, false)
})

tap.test('setResolvedName', (t) => {
t.beforeEach((t) => {
t.context.tracker = new InstrumentationTracker()
test('setResolvedName', async (t) => {
t.beforeEach((ctx) => {
ctx.nr = {}
ctx.nr.tracker = new InstrumentationTracker()
})

t.test('throws expected error', async (t) => {
const { tracker } = t.context
t.throws(() => tracker.setResolvedName('foo', 'bar'), 'module not tracked: foo')
await t.test('throws expected error', (t) => {
const { tracker } = t.nr
assert.throws(() => tracker.setResolvedName('foo', 'bar'), Error('module not tracked: foo'))
})

t.test('skips existing tracked items', async (t) => {
const { tracker } = t.context
await t.test('skips existing tracked items', (t) => {
const { tracker } = t.nr
const inst = new InstrumentationDescriptor({
moduleName: 'foo',
resolvedName: '/opt/app/node_modules/foo'
})

tracker.track('foo', inst)
tracker.setResolvedName('foo', '/opt/app/node_modules/foo')
t.equal(tracker.getAllByName('foo').length, 1)
assert.equal(tracker.getAllByName('foo').length, 1)
})

t.test('adds new tracked item for new resolved name', async (t) => {
const { tracker } = t.context
await t.test('adds new tracked item for new resolved name', (t) => {
const { tracker } = t.nr
const inst1 = new InstrumentationDescriptor({
moduleName: 'foo',
resolvedName: '/opt/app/node_modules/foo'
Expand All @@ -106,15 +111,15 @@ tap.test('setResolvedName', (t) => {
tracker.setResolvedName('foo', '/opt/app/node_modules/transitive-dep/node_modules/foo')

const items = tracker.getAllByName('foo')
t.equal(items[0].instrumentation.resolvedName, '/opt/app/node_modules/foo')
t.equal(
assert.equal(items[0].instrumentation.resolvedName, '/opt/app/node_modules/foo')
assert.equal(
items[1].instrumentation.resolvedName,
'/opt/app/node_modules/transitive-dep/node_modules/foo'
)
})

t.test('updates all registered instrumentations with resolve name', async (t) => {
const { tracker } = t.context
await t.test('updates all registered instrumentations with resolve name', (t) => {
const { tracker } = t.nr
const inst1 = new InstrumentationDescriptor({ moduleName: 'foo' })
const inst2 = new InstrumentationDescriptor({ moduleName: 'foo' })

Expand All @@ -123,9 +128,7 @@ tap.test('setResolvedName', (t) => {
tracker.setResolvedName('foo', '/opt/app/node_modules/foo')

const items = tracker.getAllByName('foo')
t.equal(items[0].instrumentation.resolvedName, '/opt/app/node_modules/foo')
t.equal(items[1].instrumentation.resolvedName, '/opt/app/node_modules/foo')
assert.equal(items[0].instrumentation.resolvedName, '/opt/app/node_modules/foo')
assert.equal(items[1].instrumentation.resolvedName, '/opt/app/node_modules/foo')
})

t.end()
})

0 comments on commit c91ab0e

Please sign in to comment.