Skip to content

Commit

Permalink
test: Convert grpc, lib, and utilization tests to node:test (#2532)
Browse files Browse the repository at this point in the history
  • Loading branch information
amychisholm03 authored Aug 30, 2024
1 parent 8184c56 commit c207e1e
Show file tree
Hide file tree
Showing 5 changed files with 322 additions and 366 deletions.
192 changes: 83 additions & 109 deletions test/unit/grpc/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
*/

'use strict'
const tap = require('tap')
const test = require('node:test')
const assert = require('node:assert')
const sinon = require('sinon')
const GrpcConnection = require('../../../lib/grpc/connection')
const connectionStates = require('../../../lib/grpc/connection/states')
Expand Down Expand Up @@ -52,20 +53,20 @@ const createMetricAggregatorForTests = () => {
)
}

tap.test('GrpcConnection logic tests', (test) => {
test('GrpcConnection logic tests', async (t) => {
const metrics = createMetricAggregatorForTests()

test.test('test metadata generation', (t) => {
await t.test('test metadata generation', () => {
const connection = new GrpcConnection(fakeTraceObserverConfig, metrics)

// only sets the license and run id
const metadataFirst = connection._getMetadata('fake-license', 'fake-run-id', {}, {})
t.equal(metadataFirst.get('license_key').shift(), 'fake-license', 'license key set')
t.equal(metadataFirst.get('agent_run_token').shift(), 'fake-run-id', 'run id set')
t.equal(metadataFirst.get('flaky').length, 0, 'flaky not set')
t.equal(metadataFirst.get('delay').length, 0, 'delay not set')
t.equal(metadataFirst.get('flaky_code').length, 0, 'flaky_code not set')
t.equal(metadataFirst.get('success_delay_ms').length, 0, 'success_delay_ms not set')
assert.equal(metadataFirst.get('license_key').shift(), 'fake-license', 'license key set')
assert.equal(metadataFirst.get('agent_run_token').shift(), 'fake-run-id', 'run id set')
assert.equal(metadataFirst.get('flaky').length, 0, 'flaky not set')
assert.equal(metadataFirst.get('delay').length, 0, 'delay not set')
assert.equal(metadataFirst.get('flaky_code').length, 0, 'flaky_code not set')
assert.equal(metadataFirst.get('success_delay_ms').length, 0, 'success_delay_ms not set')

// tests that env based params get set
const metadataSecond = connection._getMetadata(
Expand All @@ -80,12 +81,12 @@ tap.test('GrpcConnection logic tests', (test) => {
}
)

t.equal(metadataSecond.get('license_key').shift(), 'fake-license', 'license key set')
t.equal(metadataSecond.get('agent_run_token').shift(), 'fake-run-id', 'run id set')
t.equal(metadataSecond.get('flaky').shift(), 10, 'flaky set')
t.equal(metadataSecond.get('delay').shift(), 20, 'delay set')
t.equal(metadataSecond.get('flaky_code').shift(), 7, 'flaky_code set')
t.equal(metadataSecond.get('success_delay_ms').shift(), 400, 'success_delay_ms set')
assert.equal(metadataSecond.get('license_key').shift(), 'fake-license', 'license key set')
assert.equal(metadataSecond.get('agent_run_token').shift(), 'fake-run-id', 'run id set')
assert.equal(metadataSecond.get('flaky').shift(), 10, 'flaky set')
assert.equal(metadataSecond.get('delay').shift(), 20, 'delay set')
assert.equal(metadataSecond.get('flaky_code').shift(), 7, 'flaky_code set')
assert.equal(metadataSecond.get('success_delay_ms').shift(), 400, 'success_delay_ms set')

// tests that env based params get set
const metadataThird = connection._getMetadata(
Expand All @@ -100,24 +101,22 @@ tap.test('GrpcConnection logic tests', (test) => {
}
)

t.equal(metadataThird.get('license_key').shift(), 'fake-license', 'license key set')
t.equal(metadataThird.get('agent_run_token').shift(), 'fake-run-id', 'run id set')
t.equal(metadataThird.get('flaky').length, 0, 'flaky not set')
t.equal(metadataThird.get('delay').length, 0, 'delay not set')
t.equal(metadataFirst.get('flaky_code').length, 0, 'flaky_code not set')
t.equal(metadataFirst.get('success_delay_ms').length, 0, 'success_delay_ms not set')
t.end()
assert.equal(metadataThird.get('license_key').shift(), 'fake-license', 'license key set')
assert.equal(metadataThird.get('agent_run_token').shift(), 'fake-run-id', 'run id set')
assert.equal(metadataThird.get('flaky').length, 0, 'flaky not set')
assert.equal(metadataThird.get('delay').length, 0, 'delay not set')
assert.equal(metadataFirst.get('flaky_code').length, 0, 'flaky_code not set')
assert.equal(metadataFirst.get('success_delay_ms').length, 0, 'success_delay_ms not set')
})

test.test('ensure fake enum is consistent', (t) => {
await t.test('ensure fake enum is consistent', () => {
for (const [key, value] of Object.entries(connectionStates)) {
/* eslint-disable-next-line eqeqeq */
t.ok(key == connectionStates[value], 'found paired value for ' + key)
assert.ok(key == connectionStates[value], 'found paired value for ' + key)
}
t.end()
})

test.test('should apply request headers map with lowercase keys', (t) => {
await t.test('should apply request headers map with lowercase keys', () => {
const connection = new GrpcConnection(fakeTraceObserverConfig, metrics)

const requestHeadersMap = {
Expand All @@ -128,58 +127,44 @@ tap.test('GrpcConnection logic tests', (test) => {
// only sets the license and run id
const metadata = connection._getMetadata('fake-license', 'fake-run-id', requestHeadersMap, {})

t.same(metadata.get('key_1'), ['VALUE 1'])
t.same(metadata.get('key_2'), ['VALUE 2'])

t.end()
assert.deepStrictEqual(metadata.get('key_1'), ['VALUE 1'])
assert.deepStrictEqual(metadata.get('key_2'), ['VALUE 2'])
})

test.end()
})

tap.test('grpc connection error handling', (test) => {
test.test('should catch error when proto loader fails', (t) => {
test('grpc connection error handling', async (t) => {
await t.test('should catch error when proto loader fails', (t, end) => {
const stub = sinon.stub(protoLoader, 'loadSync').returns({})

t.teardown(() => {
stub.restore()
})

const connection = new GrpcConnection(fakeTraceObserverConfig)

connection.on('disconnected', () => {
t.equal(connection._state, connectionStates.disconnected)
t.end()
assert.equal(connection._state, connectionStates.disconnected)
end()
})

connection.connectSpans()
stub.restore()
})

test.test(
await t.test(
'should catch error when loadPackageDefinition returns invalid service definition',
(t) => {
(t, end) => {
const stub = sinon.stub(grpcApi, 'loadPackageDefinition').returns({})
t.teardown(() => {
stub.restore()
})

const connection = new GrpcConnection(fakeTraceObserverConfig)

connection.on('disconnected', () => {
t.equal(connection._state, connectionStates.disconnected)

t.end()
assert.equal(connection._state, connectionStates.disconnected)
end()
})

connection.connectSpans()
stub.restore()
}
)

test.end()
})

tap.test('grpc stream event handling', (test) => {
test.test('should immediately reconnect with OK status', (t) => {
test('grpc stream event handling', async (t) => {
await t.test('should immediately reconnect with OK status', (t, end) => {
const metrics = createMetricAggregatorForTests()
const fakeStream = new FakeStreamer()

Expand All @@ -188,8 +173,8 @@ tap.test('grpc stream event handling', (test) => {
const connection = new GrpcConnection(fakeTraceObserverConfig, metrics)

connection._reconnect = (delay) => {
t.notOk(delay, 'should not have delay')
t.end()
assert.ok(!delay, 'should not have delay')
end()
}

const status = {
Expand All @@ -203,14 +188,14 @@ tap.test('grpc stream event handling', (test) => {
fakeStream.removeAllListeners()
})

test.test('should disconnect, no reconnect, with UNIMPLEMENTED status', (t) => {
await t.test('should disconnect, no reconnect, with UNIMPLEMENTED status', () => {
const metrics = createMetricAggregatorForTests()
const fakeStream = new FakeStreamer()

const connection = new GrpcConnection(fakeTraceObserverConfig, metrics)

connection._reconnect = () => {
t.fail('should not call reconnect')
assert.fail('should not call reconnect')
}

let disconnectCalled = false
Expand All @@ -228,21 +213,19 @@ tap.test('grpc stream event handling', (test) => {

fakeStream.removeAllListeners()

t.ok(disconnectCalled)

t.end()
assert.ok(disconnectCalled)
})

test.test('should delay reconnect when status not UNPLIMENTED or OK', (t) => {
await t.test('should delay reconnect when status not UNPLIMENTED or OK', (t, end) => {
const metrics = createMetricAggregatorForTests()
const fakeStream = new FakeStreamer()

const expectedDelayMs = 5
const connection = new GrpcConnection(fakeTraceObserverConfig, metrics, expectedDelayMs)

connection._reconnect = (delay) => {
t.equal(delay, expectedDelayMs)
t.end()
assert.equal(delay, expectedDelayMs)
end()
}

const statusName = 'DEADLINE_EXCEEDED'
Expand All @@ -258,32 +241,35 @@ tap.test('grpc stream event handling', (test) => {
fakeStream.removeAllListeners()
})

test.test('should default delay 15 second reconnect when status not UNPLIMENTED or OK', (t) => {
const metrics = createMetricAggregatorForTests()
const fakeStream = new FakeStreamer()
await t.test(
'should default delay 15 second reconnect when status not UNPLIMENTED or OK',
(t, end) => {
const metrics = createMetricAggregatorForTests()
const fakeStream = new FakeStreamer()

const expectedDelayMs = 15 * 1000
const connection = new GrpcConnection(fakeTraceObserverConfig, metrics)
const expectedDelayMs = 15 * 1000
const connection = new GrpcConnection(fakeTraceObserverConfig, metrics)

connection._reconnect = (delay) => {
t.equal(delay, expectedDelayMs)
t.end()
}
connection._reconnect = (delay) => {
assert.equal(delay, expectedDelayMs)
end()
}

const statusName = 'DEADLINE_EXCEEDED'
const statusName = 'DEADLINE_EXCEEDED'

const status = {
code: grpcApi.status[statusName]
}
const status = {
code: grpcApi.status[statusName]
}

connection._setupSpanStreamObservers(fakeStream)
connection._setupSpanStreamObservers(fakeStream)

fakeStream.emitStatus(status)
fakeStream.emitStatus(status)

fakeStream.removeAllListeners()
})
fakeStream.removeAllListeners()
}
)

test.test('should not generate metric with OK status', (t) => {
await t.test('should not generate metric with OK status', () => {
const metrics = createMetricAggregatorForTests()
const fakeStream = new FakeStreamer()

Expand All @@ -307,12 +293,10 @@ tap.test('grpc stream event handling', (test) => {

fakeStream.removeAllListeners()

t.notOk(calledMetrics, 'grpc status OK - no metric incremented')

t.end()
assert.ok(!calledMetrics, 'grpc status OK - no metric incremented')
})

test.test('should increment UNIMPLEMENTED metric on UNIMPLEMENTED status', (t) => {
await t.test('should increment UNIMPLEMENTED metric on UNIMPLEMENTED status', () => {
const metrics = createMetricAggregatorForTests()
const fakeStream = new FakeStreamer()

Expand All @@ -334,14 +318,12 @@ tap.test('grpc stream event handling', (test) => {
NAMES.INFINITE_TRACING.SPAN_RESPONSE_GRPC_UNIMPLEMENTED
)

t.equal(metric.callCount, 1, 'incremented metric')

t.end()
assert.equal(metric.callCount, 1, 'incremented metric')
})

test.test(
await t.test(
'should increment SPAN_RESPONSE_GRPC_STATUS metric when status not UNPLIMENTED or OK',
(t) => {
() => {
const metrics = createMetricAggregatorForTests()
const fakeStream = new FakeStreamer()

Expand All @@ -365,49 +347,41 @@ tap.test('grpc stream event handling', (test) => {
util.format(NAMES.INFINITE_TRACING.SPAN_RESPONSE_GRPC_STATUS, statusName)
)

t.equal(metric.callCount, 1, 'incremented metric')

t.end()
assert.equal(metric.callCount, 1, 'incremented metric')
}
)

test.end()
})

tap.test('_createClient', (t) => {
t.autoend()

t.test(
test('_createClient', async (t) => {
await t.test(
'should create client with compression when config.infinite_tracing.compression is true',
(t) => {
() => {
const metrics = createMetricAggregatorForTests()
const config = { ...fakeTraceObserverConfig, compression: true }
const connection = new GrpcConnection(config, metrics)
connection._createClient()
const metric = metrics.getOrCreateMetric(`${NAMES.INFINITE_TRACING.COMPRESSION}/enabled`)
t.equal(metric.callCount, 1, 'incremented compression enabled')
assert.equal(metric.callCount, 1, 'incremented compression enabled')
const disabledMetric = metrics.getOrCreateMetric(
`${NAMES.INFINITE_TRACING.COMPRESSION}/disabled`
)
t.not(disabledMetric.callCount)
t.end()
assert.notEqual(disabledMetric.callCount, null)
}
)

t.test(
await t.test(
'should create client without compression when config.infinite_tracing.compression is false',
(t) => {
() => {
const metrics = createMetricAggregatorForTests()
const config = { ...fakeTraceObserverConfig, compression: false }
const connection = new GrpcConnection(config, metrics)
connection._createClient()
const metric = metrics.getOrCreateMetric(`${NAMES.INFINITE_TRACING.COMPRESSION}/disabled`)
t.equal(metric.callCount, 1, 'incremented compression disabled')
assert.equal(metric.callCount, 1, 'incremented compression disabled')
const enabledMetric = metrics.getOrCreateMetric(
`${NAMES.INFINITE_TRACING.COMPRESSION}/disabled`
)
t.not(enabledMetric.callCount)
t.end()
assert.notEqual(enabledMetric.callCount, null)
}
)
})
Loading

0 comments on commit c207e1e

Please sign in to comment.