Skip to content

Commit

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

This reverts commit 32d1ad3.
  • Loading branch information
sumitsuthar authored Sep 20, 2024
1 parent af5bcf6 commit a86162d
Show file tree
Hide file tree
Showing 10 changed files with 651 additions and 621 deletions.
25 changes: 13 additions & 12 deletions test/unit/spans/base-span-streamer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,35 @@
*/

'use strict'
const assert = require('node:assert')
const test = require('node:test')
const tap = require('tap')
const { createFakeConnection, createMetricAggregator } = require('./span-streamer-helpers')
const BaseSpanStreamer = require('../../../lib/spans/base-span-streamer')

test('SpanStreamer', async (t) => {
t.beforeEach((ctx) => {
ctx.nr = {}
tap.test('SpanStreamer', (t) => {
t.autoend()
let spanStreamer

t.beforeEach(() => {
const fakeConnection = createFakeConnection()

ctx.nr.spanStreamer = new BaseSpanStreamer(
spanStreamer = new BaseSpanStreamer(
'fake-license-key',
fakeConnection,
createMetricAggregator(),
2
)
})

for (const method of ['addToQueue', 'sendQueue']) {
await t.test(`should throw error when ${method} is called`, (t) => {
const { spanStreamer } = t.nr
assert.throws(
;['addToQueue', 'sendQueue'].forEach((method) => {
t.test(`should throw error when ${method} is called`, (t) => {
t.throws(
() => {
spanStreamer[method]()
},
Error,
`${method} is not implemented`
)

t.end()
})
}
})
})
125 changes: 65 additions & 60 deletions test/unit/spans/batch-span-streamer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,212 +4,216 @@
*/

'use strict'
const assert = require('node:assert')
const test = require('node:test')
const tap = require('tap')
const sinon = require('sinon')
const SpanStreamerEvent = require('../../../lib/spans/streaming-span-event.js')
const METRIC_NAMES = require('../../../lib/metrics/names')
const { createFakeConnection, createMetricAggregator } = require('./span-streamer-helpers')
const BatchSpanStreamer = require('../../../lib/spans/batch-span-streamer')

test('BatchSpanStreamer', async (t) => {
t.beforeEach((ctx) => {
ctx.nr = {}
const fakeConnection = createFakeConnection()
tap.test('BatchSpanStreamer', (t) => {
t.autoend()
let fakeConnection
let spanStreamer

ctx.nr.spanStreamer = new BatchSpanStreamer(
t.beforeEach(() => {
fakeConnection = createFakeConnection()

spanStreamer = new BatchSpanStreamer(
'fake-license-key',
fakeConnection,
createMetricAggregator(),
2
)
fakeConnection.connectSpans()
ctx.nr.fakeConnection = fakeConnection
})

t.afterEach((ctx) => {
const { spanStreamer } = ctx.nr
t.afterEach(() => {
if (spanStreamer.stream) {
spanStreamer.stream.destroy()
}
})

await t.test('should create a spanStreamer instance', (t) => {
const { spanStreamer } = t.nr
assert.ok(spanStreamer, 'instantiated the object')
t.test('should create a spanStreamer instance', (t) => {
t.ok(spanStreamer, 'instantiated the object')
t.end()
})

await t.test('should setup flush queue for every 5 seconds on connect', (t) => {
const { fakeConnection, spanStreamer } = t.nr
assert.ok(spanStreamer.sendTimer)
assert.ok(!spanStreamer.sendTimer._destroyed)
t.test('should setup flush queue for every 5 seconds on connect', (t) => {
t.ok(spanStreamer.sendTimer)
t.notOk(spanStreamer.sendTimer._destroyed)
fakeConnection.disconnect()
assert.ok(spanStreamer.sendTimer._destroyed)
t.ok(spanStreamer.sendTimer._destroyed)
t.end()
})

await t.test('Should increment SEEN metric on write', (t) => {
const { spanStreamer } = t.nr
t.test('Should increment SEEN metric on write', (t) => {
const metricsSpy = sinon.spy(spanStreamer._metrics, 'getOrCreateMetric')
const fakeSpan = new SpanStreamerEvent('sandwich', {}, {})
spanStreamer.write(fakeSpan)

assert.ok(metricsSpy.firstCall.calledWith(METRIC_NAMES.INFINITE_TRACING.SEEN), 'SEEN metric')
t.ok(metricsSpy.firstCall.calledWith(METRIC_NAMES.INFINITE_TRACING.SEEN), 'SEEN metric')

t.end()
})

await t.test('Should add span to queue on backpressure', (t) => {
const { spanStreamer } = t.nr
t.test('Should add span to queue on backpressure', (t) => {
spanStreamer._writable = false
assert.equal(spanStreamer.spans.length, 0, 'no spans queued')
t.equal(spanStreamer.spans.length, 0, 'no spans queued')
const fakeSpan = new SpanStreamerEvent('sandwich', {}, {})
spanStreamer.write(fakeSpan)

assert.equal(spanStreamer.spans.length, 1, 'one span queued')
t.equal(spanStreamer.spans.length, 1, 'one span queued')

t.end()
})

await t.test('Should drain span queue on stream drain event', (t) => {
const { fakeConnection, spanStreamer } = t.nr
t.test('Should drain span queue on stream drain event', (t) => {
/* simulate backpressure */
fakeConnection.stream.write = () => false
spanStreamer.queue_size = 1
const metrics = spanStreamer._metrics

assert.equal(spanStreamer.spans.length, 0, 'no spans queued')
t.equal(spanStreamer.spans.length, 0, 'no spans queued')
const fakeSpan = {
toStreamingFormat: () => {}
}

spanStreamer.write(fakeSpan)
spanStreamer.write(fakeSpan)

assert.equal(spanStreamer.spans.length, 1, 'one span queued')
t.equal(spanStreamer.spans.length, 1, 'one span queued')

/* emit drain event and allow writes */
spanStreamer.stream.emit('drain', (fakeConnection.stream.write = () => true))

assert.equal(spanStreamer.spans.length, 0, 'drained spans')
assert.equal(
t.equal(spanStreamer.spans.length, 0, 'drained spans')
t.equal(
metrics.getOrCreateMetric(METRIC_NAMES.INFINITE_TRACING.DRAIN_DURATION).callCount,
1,
'DRAIN_DURATION metric'
)

assert.equal(
t.equal(
metrics.getOrCreateMetric(METRIC_NAMES.INFINITE_TRACING.SENT).callCount,
2,
'SENT metric incremented'
)

t.end()
})

await t.test('Should properly format spans sent from the queue', (t) => {
const { fakeConnection, spanStreamer } = t.nr
t.test('Should properly format spans sent from the queue', (t) => {
/* simulate backpressure */
fakeConnection.stream.write = () => false
spanStreamer.queue_size = 1
const metrics = spanStreamer._metrics

assert.equal(spanStreamer.spans.length, 0, 'no spans queued')
t.equal(spanStreamer.spans.length, 0, 'no spans queued')

const fakeSpan = new SpanStreamerEvent('sandwich', {}, {})
const fakeSpanQueued = new SpanStreamerEvent('porridge', {}, {})

spanStreamer.write(fakeSpan)
spanStreamer.write(fakeSpanQueued)

assert.equal(spanStreamer.spans.length, 1, 'one span queued')
t.equal(spanStreamer.spans.length, 1, 'one span queued')

// emit drain event, allow writes and check for span.trace_id
fakeConnection.stream.emit(
'drain',
(fakeConnection.stream.write = ({ spans }) => {
const [span] = spans
assert.equal(span.trace_id, 'porridge', 'Should have formatted span')
t.equal(span.trace_id, 'porridge', 'Should have formatted span')

return true
})
)

assert.equal(spanStreamer.spans.length, 0, 'drained spans')
assert.equal(
t.equal(spanStreamer.spans.length, 0, 'drained spans')
t.equal(
metrics.getOrCreateMetric(METRIC_NAMES.INFINITE_TRACING.DRAIN_DURATION).callCount,
1,
'DRAIN_DURATION metric'
)

assert.equal(
t.equal(
metrics.getOrCreateMetric(METRIC_NAMES.INFINITE_TRACING.SENT).callCount,
2,
'SENT metric incremented'
)

t.end()
})

await t.test('should send a batch if it exceeds queue', (t, end) => {
const { fakeConnection, spanStreamer } = t.nr
t.test('should send a batch if it exceeds queue', (t) => {
t.plan(11)
const metrics = spanStreamer._metrics

let i = 0
fakeConnection.stream.write = ({ spans }) => {
i++
if (i === 1) {
const [span, span2] = spans
assert.equal(span.trace_id, 'sandwich', 'batch 1 span 1 ok')
assert.equal(span2.trace_id, 'porridge', 'batch 1 span 2 ok')
t.equal(span.trace_id, 'sandwich', 'batch 1 span 1 ok')
t.equal(span2.trace_id, 'porridge', 'batch 1 span 2 ok')
} else {
const [span, span2] = spans
assert.equal(span.trace_id, 'arepa', 'batch 2 span 1 ok')
assert.equal(span2.trace_id, 'hummus', 'batch 2 span 2 ok')
end()
t.equal(span.trace_id, 'arepa', 'batch 2 span 1 ok')
t.equal(span2.trace_id, 'hummus', 'batch 2 span 2 ok')
}

return true
}

assert.equal(spanStreamer.spans.length, 0, 'no spans queued')
t.equal(spanStreamer.spans.length, 0, 'no spans queued')

const fakeSpan = new SpanStreamerEvent('sandwich', {}, {})
const fakeSpan2 = new SpanStreamerEvent('porridge', {}, {})
const fakeSpan3 = new SpanStreamerEvent('arepa', {}, {})
const fakeSpan4 = new SpanStreamerEvent('hummus', {}, {})

spanStreamer.write(fakeSpan)
assert.equal(spanStreamer.spans.length, 1, '1 span in queue')
t.equal(spanStreamer.spans.length, 1, '1 span in queue')

spanStreamer.write(fakeSpan2)

assert.equal(spanStreamer.spans.length, 0, '0 spans in queue')
assert.equal(
t.equal(spanStreamer.spans.length, 0, '0 spans in queue')
t.equal(
metrics.getOrCreateMetric(METRIC_NAMES.INFINITE_TRACING.SENT).callCount,
2,
'SENT metric incremented to 2'
)

spanStreamer.write(fakeSpan3)

assert.equal(spanStreamer.spans.length, 1, '1 span in queue')
t.equal(spanStreamer.spans.length, 1, '1 span in queue')

spanStreamer.write(fakeSpan4)

assert.equal(spanStreamer.spans.length, 0, '0 spans in queue')
assert.equal(
t.equal(spanStreamer.spans.length, 0, '0 spans in queue')
t.equal(
metrics.getOrCreateMetric(METRIC_NAMES.INFINITE_TRACING.SENT).callCount,
4,
'SENT metric incremented to 4'
)
})

await t.test('should send in appropriate batch sizes', (t) => {
const { fakeConnection, spanStreamer } = t.nr
t.diagnostic('this will simulate n full batches and the last batch being 1/3 full')
t.test('should send in appropriate batch sizes', (t) => {
t.comment('this will simulate n full batches and the last batch being 1/3 full')
const SPANS = 10000
const BATCH = 750
// set the number of expected assertions to the batches + the sent metric
t.plan(Math.ceil(SPANS / BATCH) + 1)
const metrics = spanStreamer._metrics
spanStreamer.batchSize = BATCH
spanStreamer.queue_size = SPANS
let i = 0
fakeConnection.stream.write = ({ spans }) => {
if (i === 13) {
assert.equal(spans.length, BATCH / 3)
t.equal(spans.length, BATCH / 3)
} else {
assert.equal(spans.length, BATCH)
t.equal(spans.length, BATCH)
}
i++
return true
Expand All @@ -219,10 +223,11 @@ test('BatchSpanStreamer', async (t) => {
spans.forEach((span) => {
spanStreamer.write(span)
})
assert.equal(
t.equal(
metrics.getOrCreateMetric(METRIC_NAMES.INFINITE_TRACING.SENT).callCount,
SPANS,
`SENT metric incremented to ${SPANS}`
)
t.end()
})
})
Loading

0 comments on commit a86162d

Please sign in to comment.