Skip to content

Commit

Permalink
Adding instrumentation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
uurien committed Aug 2, 2024
1 parent daefc81 commit b7cb2c9
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/datadog-instrumentations/src/pg.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function wrapQuery (query) {
if (abortController.signal.aborted) {
const error = abortController.signal.reason || new Error('Aborted')
const reusingQuery = typeof pgQuery.submit === 'function'
const callback = (reusingQuery && pgQuery.callback) || typeof arguments[arguments.length - 1]
const callback = (reusingQuery && pgQuery.callback) || arguments[arguments.length - 1]

finish(error)

Expand Down
112 changes: 112 additions & 0 deletions packages/datadog-instrumentations/test/pg.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
const agent = require('../../dd-trace/test/plugins/agent')
const dc = require('dc-polyfill')

const clients = {
pg: pg => pg.Client
}

if (process.env.PG_TEST_NATIVE === 'true') {
clients['pg.native'] = pg => pg.native.Client
}

describe('pg instrumentation', () => {
withVersions('pg', 'pg', version => {
const queryStartChannel = dc.channel('apm:pg:query:start')

let pg
let client

function abortQuery({ abortController }) {

Check failure on line 19 in packages/datadog-instrumentations/test/pg.spec.js

View workflow job for this annotation

GitHub Actions / lint

Missing space before function parentheses
const error = new Error('Test')
abortController.abort(error)

if (!abortController.signal.reason) {
abortController.signal.reason = error
}
}

before(() => {
return agent.load(['pg'])
})

Object.keys(clients).forEach(implementation => {
beforeEach(done => {
pg = require(`../../../versions/pg@${version}`).get()
const Client = clients[implementation](pg)

client = new Client({
host: '127.0.0.1',
user: 'postgres',
password: 'postgres',
database: 'postgres',
application_name: 'test'
})

client.connect(err => done(err))
})

// afterEach((done) => {
// client.end((err) => {
// done(err)
// })
// })

describe('pg.Client', () => {
describe('abortController', () => {
afterEach(() => {
if (queryStartChannel.hasSubscribers) {
queryStartChannel.unsubscribe(abortQuery)
}
})

describe('using callback', () => {
it('Should not fail if it is not aborted', (done) => {
client.query('SELECT 1', (err) => {
client.end()
done(err)
})
})

it('Should abort query', (done) => {
queryStartChannel.subscribe(abortQuery)

client.query('SELECT 1', (err) => {
try {
client.end()
} catch (e) {
console.error(e)

Check failure on line 77 in packages/datadog-instrumentations/test/pg.spec.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
}
if (err && err.message === 'Test') {
return done()
}

done(new Error('Query was not aborted'))
})
})
})

describe('using promise', () => {
it('Should not fail if it is not aborted', async () => {
await client.query('SELECT 1')
client.end()
})

it('Should abort query', async () => {
queryStartChannel.subscribe(abortQuery)

try {
await client.query('SELECT 1')

throw new Error('Query was not aborted')
} catch (err) {
if (!err || err.message !== 'Test') {

Check failure on line 102 in packages/datadog-instrumentations/test/pg.spec.js

View workflow job for this annotation

GitHub Actions / lint

Empty block statement
}
throw err

Check failure on line 104 in packages/datadog-instrumentations/test/pg.spec.js

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 16 spaces but found 18
}
})
})
})
})
})
})
})

0 comments on commit b7cb2c9

Please sign in to comment.