Skip to content

Commit

Permalink
chore: Fixed superagent versioned tests (#2190)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners-nr committed May 10, 2024
1 parent 6b733c0 commit 297bc01
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 18 deletions.
22 changes: 15 additions & 7 deletions test/versioned/superagent/async-await.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,34 @@

const tap = require('tap')
const helper = require('../../lib/agent_helper')
const testServer = require('./test-server')
const { removeModules } = require('../../lib/cache-buster')
const EXTERNAL_NAME = /External\/newrelic.com(:443)*\//
const EXTERNAL_NAME = /External\/127.0.0.1:\d+\//

tap.test('SuperAgent instrumentation with async/await', (t) => {
t.beforeEach((t) => {
t.beforeEach(async (t) => {
const { address, server, stopServer } = await testServer()
t.context.address = address
t.context.server = server
t.context.stopServer = stopServer

t.context.agent = helper.instrumentMockedAgent()
t.context.request = require('superagent')
})
t.afterEach((t) => {
t.afterEach(async (t) => {
helper.unloadAgent(t.context.agent)
removeModules(['superagent'])

await t.context.stopServer()
})

t.test('should maintain transaction context with promises', (t) => {
const { agent } = t.context
const { address, agent } = t.context
helper.runInTransaction(agent, async function (tx) {
t.ok(tx)

const { request } = t.context
await request.get('https://newrelic.com')
await request.get(address)

const mainSegment = tx.trace.root.children[0]
t.ok(mainSegment)
Expand All @@ -42,8 +50,8 @@ tap.test('SuperAgent instrumentation with async/await', (t) => {
})

t.test('should not create segment if not in a transaction', async (t) => {
const { agent, request } = t.context
await request.get('https://newrelic.com')
const { address, agent, request } = t.context
await request.get(address)
t.notOk(agent.getTransaction(), 'should not have a transaction')
t.end()
})
Expand Down
30 changes: 19 additions & 11 deletions test/versioned/superagent/superagent.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,32 @@

const tap = require('tap')
const helper = require('../../lib/agent_helper')
const testServer = require('./test-server')
const { removeModules } = require('../../lib/cache-buster')
const EXTERNAL_NAME = /External\/newrelic.com(:443)*\//
const EXTERNAL_NAME = /External\/127.0.0.1:\d+\//

tap.test('SuperAgent instrumentation', (t) => {
t.beforeEach((t) => {
t.beforeEach(async (t) => {
const { address, server, stopServer } = await testServer()
t.context.address = address
t.context.server = server
t.context.stopServer = stopServer

t.context.agent = helper.instrumentMockedAgent()
t.context.request = require('superagent')
})
t.afterEach((t) => {
t.afterEach(async (t) => {
helper.unloadAgent(t.context.agent)
removeModules(['superagent'])

await t.context.stopServer()
})

t.test('should maintain transaction context with callbacks', (t) => {
const { agent, request } = t.context
const { address, agent, request } = t.context

helper.runInTransaction(agent, (tx) => {
request.get('https://newrelic.com', function testCallback() {
request.get(address, function testCallback() {
t.ok(tx)

const mainSegment = tx.trace.root.children[0]
Expand All @@ -42,17 +50,17 @@ tap.test('SuperAgent instrumentation', (t) => {
})

t.test('should not create a segment for callback if not in transaction', (t) => {
const { agent, request } = t.context
request.get('https://newrelic.com', function testCallback() {
const { address, agent, request } = t.context
request.get(address, function testCallback() {
t.notOk(agent.getTransaction(), 'should not have a transaction')
t.end()
})
})

t.test('should maintain transaction context with promises', (t) => {
const { agent, request } = t.context
const { address, agent, request } = t.context
helper.runInTransaction(agent, (tx) => {
request.get('https://newrelic.com').then(function testThen() {
request.get(address).then(function testThen() {
t.ok(tx)

const mainSegment = tx.trace.root.children[0]
Expand All @@ -70,8 +78,8 @@ tap.test('SuperAgent instrumentation', (t) => {
})

t.test('should not create segment for a promise if not in a transaction', (t) => {
const { agent, request } = t.context
request.get('https://newrelic.com').then(function testThen() {
const { address, agent, request } = t.context
request.get(address).then(function testThen() {
t.notOk(agent.getTransaction(), 'should not have a transaction')
t.end()
})
Expand Down
45 changes: 45 additions & 0 deletions test/versioned/superagent/test-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2024 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'

const http = require('http')

module.exports = async function testServer() {
const server = http.createServer()

server.on('request', (req, res) => {
res.writeHead(200, { 'content-type': 'application/json' })
res.end('"ok"')
})

let address = await new Promise((resolve, reject) => {
server.listen(0, '127.0.0.1', (error) => {
if (error) {
return reject(error)
}
return resolve(server.address())
})
})

address = `http://${address.address}:${address.port}`

return { address, server, stopServer }

async function stopServer() {
await new Promise((resolve, reject) => {
if (server.closeAllConnections) {
// Node.js 16 does not support this method.
server.closeAllConnections()
}
server.close((error) => {
if (error) {
return reject(error)
}
return resolve()
})
})
}
}

0 comments on commit 297bc01

Please sign in to comment.