Skip to content

Commit 66694c6

Browse files
authored
test: Updated tests to mock outgoing calls to avoid issues in CI (newrelic#3234)
1 parent 4f04db0 commit 66694c6

File tree

2 files changed

+29
-41
lines changed

2 files changed

+29
-41
lines changed

test/integration/instrumentation/http-outbound.test.js

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const helper = require('../../lib/agent_helper')
99
const test = require('node:test')
1010
const assert = require('node:assert')
1111
const symbols = require('../../../lib/symbols')
12+
const nock = require('nock')
1213

1314
test('external requests', async function (t) {
1415
t.beforeEach((ctx) => {
@@ -159,42 +160,15 @@ test('external requests', async function (t) {
159160
})
160161
})
161162

162-
await t.test('should not duplicate the external segment', function (t, end) {
163-
const { agent } = t.nr
164-
const https = require('https')
165-
166-
helper.runInTransaction(agent, function inTransaction() {
167-
https.get('https://example.com:443/', function onResponse(res) {
168-
res.once('end', check)
169-
res.resume()
170-
})
171-
})
172-
173-
function check() {
174-
const tx = agent.getTransaction()
175-
const [segment] = tx.trace.getChildren(tx.trace.root.id)
176-
177-
assert.equal(segment.name, 'External/example.com/', 'should be named')
178-
assert.ok(segment.timer.start, 'should have started')
179-
assert.ok(segment.timer.hasEnd(), 'should have ended')
180-
const segmentChildren = tx.trace.getChildren(segment.id)
181-
assert.equal(segmentChildren.length, 1, 'should have 1 child')
182-
183-
const notDuped = segmentChildren[0]
184-
assert.notEqual(
185-
notDuped.name,
186-
segment.name,
187-
'child should not be named the same as the external segment'
188-
)
189-
190-
end()
191-
}
192-
})
193-
194163
await t.test('NODE-1647 should not interfere with `got`', { timeout: 5000 }, function (t, end) {
195164
const { agent } = t.nr
196165
// Our way of wrapping HTTP response objects caused `got` to hang. This was
197166
// resolved in agent 2.5.1.
167+
nock.disableNetConnect()
168+
t.after(() => {
169+
nock.enableNetConnect()
170+
})
171+
nock('https://example.com').get('/').reply(200)
198172
const got = require('got')
199173
helper.runInTransaction(agent, function () {
200174
const req = got('https://example.com/')
@@ -215,8 +189,13 @@ test('external requests', async function (t) {
215189

216190
await t.test('should record requests to default ports', (t, end) => {
217191
const { agent, http } = t.nr
192+
nock.disableNetConnect()
193+
t.after(() => {
194+
nock.enableNetConnect()
195+
})
196+
nock('http://example.com').get('/').reply(200)
218197
helper.runInTransaction(agent, (tx) => {
219-
http.get('http://example.com', (res) => {
198+
http.get('http://example.com/', (res) => {
220199
res.resume()
221200
res.on('end', () => {
222201
const [segment] = tx.trace.getChildren(tx.trace.root.id)
@@ -229,9 +208,14 @@ test('external requests', async function (t) {
229208

230209
await t.test('should expose the external segment on the http request', (t, end) => {
231210
const { agent, http } = t.nr
211+
nock.disableNetConnect()
212+
t.after(() => {
213+
nock.enableNetConnect()
214+
})
215+
nock('http://example.com').get('/').reply(200)
232216
helper.runInTransaction(agent, (tx) => {
233217
let reqSegment = null
234-
const req = http.get('http://example.com', (res) => {
218+
const req = http.get('http://example.com/', (res) => {
235219
res.resume()
236220
res.on('end', () => {
237221
const [segment] = tx.trace.getChildren(tx.trace.root.id)

test/unit/spans/span-event.test.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ const assert = require('node:assert')
88
const test = require('node:test')
99
const DatastoreShim = require('../../../lib/shim/datastore-shim')
1010
const helper = require('../../lib/agent_helper')
11-
const https = require('https')
11+
const http = require('http')
1212
const SpanEvent = require('../../../lib/spans/span-event')
1313
const DatastoreParameters = require('../../../lib/shim/specs/params/datastore')
1414
const { QuerySpec } = require('../../../lib/shim/specs')
15+
const nock = require('nock')
1516

1617
test('#constructor() should construct an empty span event', () => {
1718
const attrs = {}
@@ -43,6 +44,7 @@ test('#constructor() should construct an empty span event', () => {
4344

4445
test('fromSegment()', async (t) => {
4546
t.beforeEach((ctx) => {
47+
nock.disableNetConnect()
4648
ctx.nr = {}
4749
ctx.nr.agent = helper.instrumentMockedAgent({
4850
distributed_tracing: {
@@ -52,6 +54,7 @@ test('fromSegment()', async (t) => {
5254
})
5355

5456
t.afterEach((ctx) => {
57+
nock.enableNetConnect()
5558
helper.unloadAgent(ctx.nr.agent)
5659
})
5760

@@ -132,8 +135,9 @@ test('fromSegment()', async (t) => {
132135
helper.runInTransaction(agent, (transaction) => {
133136
transaction.sampled = true
134137
transaction.priority = 42
138+
nock('http://example.com').get('/?foo=bar').reply(200)
135139

136-
https.get('https://example.com?foo=bar', (res) => {
140+
http.get('http://example.com?foo=bar', (res) => {
137141
res.resume()
138142
res.on('end', () => {
139143
const tx = agent.tracer.getTransaction()
@@ -159,7 +163,7 @@ test('fromSegment()', async (t) => {
159163
assert.equal(span.intrinsics.name, 'External/example.com/')
160164
assert.equal(span.intrinsics.timestamp, segment.timer.start)
161165

162-
assert.ok(span.intrinsics.duration >= 0.01 && span.intrinsics.duration <= 2)
166+
assert.ok(span.intrinsics.duration > 0 && span.intrinsics.duration <= 2)
163167

164168
// Should have type-specific intrinsics
165169
assert.equal(span.intrinsics.component, 'http')
@@ -169,13 +173,12 @@ test('fromSegment()', async (t) => {
169173
const attributes = span.attributes
170174

171175
// Should have (most) http properties.
172-
assert.equal(attributes['http.url'], 'https://example.com/')
176+
assert.equal(attributes['http.url'], 'http://example.com/')
173177
assert.equal(attributes['server.address'], 'example.com')
174-
assert.equal(attributes['server.port'], 443)
178+
assert.equal(attributes['server.port'], 80)
175179
assert.ok(attributes['http.method'])
176180
assert.ok(attributes['http.request.method'])
177181
assert.equal(attributes['http.statusCode'], 200)
178-
assert.equal(attributes['http.statusText'], 'OK')
179182

180183
// should nullify mapped properties
181184
assert.ok(!attributes.library)
@@ -353,8 +356,9 @@ test('fromSegment()', async (t) => {
353356

354357
await t.test('should handle truncated http spans', (t, end) => {
355358
const { agent } = t.nr
359+
nock('http://www.example.com').get('/path?foo=bar').reply(200)
356360
helper.runInTransaction(agent, (transaction) => {
357-
https.get('https://example.com?foo=bar', (res) => {
361+
http.get('http://www.example.com/path?foo=bar', (res) => {
358362
transaction.end() // prematurely end to truncate
359363

360364
res.resume()

0 commit comments

Comments
 (0)