Skip to content

Commit 4deb6d8

Browse files
authored
test: Added a randomString helper to agent_helper and use it in ioredis to avoid flappy tests, also updated local instances of random strings in kafka, elasticsearch and opensearch tests (newrelic#3462)
1 parent 6a380ef commit 4deb6d8

File tree

8 files changed

+95
-103
lines changed

8 files changed

+95
-103
lines changed

test/lib/agent_helper.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,3 +636,12 @@ helper.readPackageVersion = function readPackageVersion(dirname, pkg) {
636636
const { version } = JSON.parse(packageFile)
637637
return version
638638
}
639+
640+
/**
641+
* Creates a random string prefixed with the provided value
642+
* @param {string} prefix value to prefix random string
643+
* @returns {string} random string
644+
*/
645+
helper.randomString = function randomString(prefix = '') {
646+
return `${prefix}${crypto.randomBytes(8).toString('hex')}`
647+
}

test/versioned/elastic/elasticsearch.test.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,11 @@ const assert = require('node:assert')
99
const helper = require('../../lib/agent_helper')
1010
const params = require('../../lib/params')
1111
const urltils = require('../../../lib/util/urltils')
12-
const crypto = require('crypto')
1312
const { assertPackageMetrics } = require('../../lib/custom-assertions')
1413
const semver = require('semver')
15-
const DB_INDEX = `test-${randomString()}`
16-
const DB_INDEX_2 = `test2-${randomString()}`
17-
const SEARCHTERM_1 = randomString()
18-
19-
function randomString() {
20-
return crypto.randomBytes(5).toString('hex')
21-
}
14+
const DB_INDEX = helper.randomString('test-')
15+
const DB_INDEX_2 = helper.randomString('test2-')
16+
const SEARCHTERM_1 = helper.randomString()
2217

2318
// request bodies are structured differently in ElasticSearch v7.x vs v8.x
2419
function setRequestBody(body, version) {
@@ -92,7 +87,7 @@ test('Elasticsearch instrumentation', async (t) => {
9287

9388
await t.test('should be able to record creating an index', async (t) => {
9489
const { agent, client } = t.nr
95-
const index = `test-index-${randomString()}`
90+
const index = helper.randomString('test-index-')
9691
t.after(async () => {
9792
await client.indices.delete({ index })
9893
})
@@ -117,7 +112,7 @@ test('Elasticsearch instrumentation', async (t) => {
117112
return
118113
}
119114

120-
const index = `test-index-${randomString()}`
115+
const index = helper.randomString('test-index-')
121116
t.after(async () => {
122117
await client.indices.delete({ index })
123118
})
@@ -368,7 +363,7 @@ test('Elasticsearch instrumentation', async (t) => {
368363

369364
await t.test('should create correct metrics', async function (t) {
370365
const { agent, client, pkgVersion, HOST_ID } = t.nr
371-
const id = `key-${randomString()}`
366+
const id = helper.randomString('key-')
372367
await helper.runInTransaction(agent, async function transactionInScope(transaction) {
373368
const documentProp = setRequestBody(
374369
{

test/versioned/elastic/elasticsearchNoop.test.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ const test = require('node:test')
88
const assert = require('node:assert')
99
const helper = require('../../lib/agent_helper')
1010
const params = require('../../lib/params')
11-
const crypto = require('crypto')
12-
const DB_INDEX = `test-${randomString()}`
13-
14-
function randomString() {
15-
return crypto.randomBytes(5).toString('hex')
16-
}
11+
const DB_INDEX = helper.randomString('test-')
1712

1813
test('Elasticsearch instrumentation', async (t) => {
1914
t.beforeEach(async (ctx) => {

test/versioned/ioredis-esm/ioredis.test.js

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ test('ioredis instrumentation', async (t) => {
2929
? agent.config.getHostnameSafe()
3030
: params.redis_host
3131
const HOST_ID = METRIC_HOST_NAME + '/' + params.redis_port
32+
const redisKey = helper.randomString('redis-key')
3233

3334
await redisClient.select(DB_INDEX)
3435
ctx.nr = {
3536
agent,
3637
redisClient,
38+
redisKey,
3739
HOST_ID,
3840
METRIC_HOST_NAME
3941
}
@@ -47,7 +49,7 @@ test('ioredis instrumentation', async (t) => {
4749
})
4850

4951
await t.test('creates expected metrics', async (t) => {
50-
const { agent, redisClient, HOST_ID } = t.nr
52+
const { agent, redisClient, redisKey, HOST_ID } = t.nr
5153
const plan = tspl(t, { plan: 6 })
5254
agent.on('transactionFinished', function (tx) {
5355
const expected = [
@@ -61,15 +63,15 @@ test('ioredis instrumentation', async (t) => {
6163
})
6264

6365
helper.runInTransaction(agent, async (transaction) => {
64-
await redisClient.set('testkey', 'testvalue')
66+
await redisClient.set(redisKey, 'testvalue')
6567
transaction.end()
6668
})
6769

6870
await plan.completed
6971
})
7072

7173
await t.test('creates expected segments', async (t) => {
72-
const { agent, redisClient } = t.nr
74+
const { agent, redisClient, redisKey } = t.nr
7375
const plan = tspl(t, { plan: 5 })
7476

7577
agent.on('transactionFinished', function (tx) {
@@ -89,20 +91,19 @@ test('ioredis instrumentation', async (t) => {
8991
})
9092

9193
helper.runInTransaction(agent, async (transaction) => {
92-
await redisClient.set('testkey', 'testvalue')
93-
const value = await redisClient.get('testkey')
94-
plan.equal(value, 'testvalue', 'should have expected value')
94+
await redisClient.set(redisKey, 'testvalue')
95+
const value = await redisClient.get(redisKey)
96+
plan.equal(value, 'testvalue')
9597
transaction.end()
9698
})
9799
await plan.completed
98100
})
99101

100102
await t.test('should add instance attributes to all redis segments', async (t) => {
101-
const { agent, redisClient, METRIC_HOST_NAME } = t.nr
103+
const { agent, redisClient, redisKey, METRIC_HOST_NAME } = t.nr
102104
agent.config.datastore_tracer.instance_reporting.enabled = true
103105
agent.config.datastore_tracer.database_name_reporting.enabled = true
104106
const plan = tspl(t, { plan: 12 })
105-
106107
agent.on('transactionFinished', function (tx) {
107108
const root = tx.trace.root
108109
const children = tx.trace.getChildren(root.id)
@@ -113,27 +114,27 @@ test('ioredis instrumentation', async (t) => {
113114
const getAttrs = getSegment.getAttributes()
114115
plan.equal(setAttrs.host, METRIC_HOST_NAME)
115116
plan.equal(setAttrs.product, 'Redis')
116-
plan.equal(setAttrs.key, '"testkey"')
117+
plan.equal(setAttrs.key, `"${redisKey}"`)
117118
plan.equal(setAttrs.port_path_or_id, params.redis_port.toString())
118119
plan.equal(setAttrs.database_name, String(DB_INDEX))
119120
plan.equal(getAttrs.host, METRIC_HOST_NAME)
120121
plan.equal(getAttrs.product, 'Redis')
121-
plan.equal(getAttrs.key, '"testkey"')
122+
plan.equal(getAttrs.key, `"${redisKey}"`)
122123
plan.equal(getAttrs.port_path_or_id, params.redis_port.toString())
123124
plan.equal(getAttrs.database_name, String(DB_INDEX))
124125
})
125126

126127
helper.runInTransaction(agent, async (transaction) => {
127-
await redisClient.set('testkey', 'testvalue')
128-
const value = await redisClient.get('testkey')
129-
plan.equal(value, 'testvalue', 'should have expected value')
128+
await redisClient.set(redisKey, 'testvalue')
129+
const value = await redisClient.get(redisKey)
130+
plan.equal(value, 'testvalue')
130131
transaction.end()
131132
})
132133
await plan.completed
133134
})
134135

135136
await t.test('should not add instance attributes to redis segments when disabled', async (t) => {
136-
const { agent, redisClient, HOST_ID } = t.nr
137+
const { agent, redisClient, redisKey, HOST_ID } = t.nr
137138
const plan = tspl(t, { plan: 13 })
138139
agent.config.datastore_tracer.instance_reporting.enabled = false
139140
agent.config.datastore_tracer.database_name_reporting.enabled = false
@@ -148,29 +149,29 @@ test('ioredis instrumentation', async (t) => {
148149
const getAttrs = getSegment.getAttributes()
149150
plan.equal(setAttrs.host, undefined)
150151
plan.equal(setAttrs.product, 'Redis')
151-
plan.equal(setAttrs.key, '"testkey"')
152+
plan.equal(setAttrs.key, `"${redisKey}"`)
152153
plan.equal(setAttrs.port_path_or_id, undefined)
153154
plan.equal(setAttrs.database_name, undefined)
154155
plan.equal(getAttrs.host, undefined)
155156
plan.equal(getAttrs.product, 'Redis')
156-
plan.equal(getAttrs.key, '"testkey"')
157+
plan.equal(getAttrs.key, `"${redisKey}"`)
157158
plan.equal(getAttrs.port_path_or_id, undefined)
158159
plan.equal(getAttrs.database_name, undefined)
159160
const unscoped = tx.metrics.unscoped
160161
plan.equal(unscoped[`Datastore/instance/Redis/${HOST_ID}`], undefined)
161162
})
162163

163164
helper.runInTransaction(agent, async (transaction) => {
164-
await redisClient.set('testkey', 'testvalue')
165-
const value = await redisClient.get('testkey')
166-
plan.equal(value, 'testvalue', 'should have expected value')
165+
await redisClient.set(redisKey, 'testvalue')
166+
const value = await redisClient.get(redisKey)
167+
plan.equal(value, 'testvalue')
167168
transaction.end()
168169
})
169170
await plan.completed
170171
})
171172

172173
await t.test('should follow selected database', async (t) => {
173-
const { agent, redisClient } = t.nr
174+
const { agent, redisClient, redisKey } = t.nr
174175
const plan = tspl(t, { plan: 7 })
175176
const SELECTED_DB = 8
176177

@@ -189,20 +190,20 @@ test('ioredis instrumentation', async (t) => {
189190
})
190191

191192
helper.runInTransaction(agent, async (transaction) => {
192-
await redisClient.set('testkey', 'testvalue')
193+
await redisClient.set(redisKey, 'testvalue')
193194
await redisClient.select(SELECTED_DB)
194-
await redisClient.set('testkey2', 'testvalue')
195+
await redisClient.set(`${redisKey}2`, 'testvalue')
195196
transaction.end()
196197
})
197198
await plan.completed
198199
})
199200

200201
// NODE-1524 regression
201202
await t.test('does not crash when ending out of transaction', (t, end) => {
202-
const { agent, redisClient } = t.nr
203+
const { agent, redisClient, redisKey } = t.nr
203204
helper.runInTransaction(agent, (transaction) => {
204205
assert.ok(agent.getTransaction(), 'transaction should be in progress')
205-
redisClient.set('testkey', 'testvalue').then(function () {
206+
redisClient.set(redisKey, 'testvalue').then(function () {
206207
assert.ok(!agent.getTransaction(), 'transaction should have ended')
207208
end()
208209
})

0 commit comments

Comments
 (0)