From 40aea36320d15b201800431268be2c3d4c794a7b Mon Sep 17 00:00:00 2001 From: James Sumners Date: Mon, 18 Nov 2024 07:31:07 -0500 Subject: [PATCH] chore: Updated pg and pg-esm tests to node:test (#2750) --- ...e-native.tap.mjs => force-native.test.mjs} | 0 .../{native.tap.mjs => native.test.mjs} | 0 test/versioned/pg-esm/package.json | 9 +- test/versioned/pg-esm/pg.common.mjs | 469 ++++++++--------- .../pg-esm/{pg.tap.mjs => pg.test.mjs} | 0 ...rce-native.tap.js => force-native.test.js} | 0 .../pg/{native.tap.js => native.test.js} | 0 test/versioned/pg/package.json | 6 +- test/versioned/pg/pg.common.js | 484 ++++++++---------- test/versioned/pg/{pg.tap.js => pg.test.js} | 0 10 files changed, 444 insertions(+), 524 deletions(-) rename test/versioned/pg-esm/{force-native.tap.mjs => force-native.test.mjs} (100%) rename test/versioned/pg-esm/{native.tap.mjs => native.test.mjs} (100%) rename test/versioned/pg-esm/{pg.tap.mjs => pg.test.mjs} (100%) rename test/versioned/pg/{force-native.tap.js => force-native.test.js} (100%) rename test/versioned/pg/{native.tap.js => native.test.js} (100%) rename test/versioned/pg/{pg.tap.js => pg.test.js} (100%) diff --git a/test/versioned/pg-esm/force-native.tap.mjs b/test/versioned/pg-esm/force-native.test.mjs similarity index 100% rename from test/versioned/pg-esm/force-native.tap.mjs rename to test/versioned/pg-esm/force-native.test.mjs diff --git a/test/versioned/pg-esm/native.tap.mjs b/test/versioned/pg-esm/native.test.mjs similarity index 100% rename from test/versioned/pg-esm/native.tap.mjs rename to test/versioned/pg-esm/native.test.mjs diff --git a/test/versioned/pg-esm/package.json b/test/versioned/pg-esm/package.json index cf95a4a11e..2a7fd50f49 100644 --- a/test/versioned/pg-esm/package.json +++ b/test/versioned/pg-esm/package.json @@ -14,11 +14,10 @@ "pg-native": ">=3.0.0" }, "files": [ - "force-native.tap.mjs", - "native.tap.mjs", - "pg.tap.mjs" + "force-native.test.mjs", + "native.test.mjs", + "pg.test.mjs" ] } - ], - "dependencies": {} + ] } diff --git a/test/versioned/pg-esm/pg.common.mjs b/test/versioned/pg-esm/pg.common.mjs index aa52a699ac..aab195c954 100644 --- a/test/versioned/pg-esm/pg.common.mjs +++ b/test/versioned/pg-esm/pg.common.mjs @@ -3,7 +3,10 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { test } from 'tap' +import test from 'node:test' +import assert from 'node:assert' +import tspl from '@matteo.collina/tspl' + import params from '../../lib/params.js' import helper from '../../lib/agent_helper.js' import { findSegment, getMetricHostName } from '../../lib/metrics_helper.js' @@ -52,29 +55,32 @@ export default function runTests(name, clientFactory) { }) }) - await runCommand(setupClient, "set client_min_messages='warning';") // supress PG notices + await runCommand(setupClient, "set client_min_messages='warning';") // suppress PG notices - const tableDrop = 'DROP TABLE IF EXISTS ' + TABLE_PREPARED + const tableDrop = `drop table if exists ${TABLE_PREPARED}` await runCommand(setupClient, tableDrop) - const tableCreate = - 'CREATE TABLE ' + TABLE_PREPARED + ' (' + PK + ' integer PRIMARY KEY, ' + COL + ' text' + ');' + const tableCreate = `create table ${TABLE_PREPARED} (${PK} integer primary key, ${COL} text)` await runCommand(setupClient, tableCreate) setupClient.end() return pg } - function verify(t, segment, selectTable) { - verifyMetrics(t, segment, selectTable) - verifyTrace(t, segment, selectTable) - verifyInstanceParameters(t, segment) + function verify(expect = assert, segment, selectTable) { + verifyMetrics(expect, segment, selectTable) + verifyTrace(expect, segment, selectTable) + verifyInstanceParameters(expect, segment) } - function verifyMetrics(t, segment, selectTable) { + function verifyMetrics(expect = assert, segment, selectTable) { const transaction = segment.transaction const agent = transaction.agent selectTable = selectTable || TABLE - t.equal(Object.keys(transaction.metrics.scoped).length, 0, 'should not have any scoped metrics') + expect.equal( + Object.keys(transaction.metrics.scoped).length, + 0, + 'should not have any scoped metrics' + ) const unscoped = transaction.metrics.unscoped @@ -98,9 +104,9 @@ export default function runTests(name, clientFactory) { const unscopedNames = Object.keys(unscoped) expectedNames.forEach(function (expectedName) { - t.ok(unscoped[expectedName], 'should have unscoped metric ' + expectedName) + expect.ok(unscoped[expectedName], 'should have unscoped metric ' + expectedName) if (unscoped[expectedName]) { - t.equal( + expect.equal( unscoped[expectedName].callCount, expected[expectedName], 'metric ' + expectedName + ' should have correct callCount' @@ -108,20 +114,20 @@ export default function runTests(name, clientFactory) { } }) - t.equal( + expect.equal( unscopedNames.length, expectedNames.length, 'should have correct number of unscoped metrics' ) } - function verifyTrace(t, segment, selectTable) { + function verifyTrace(expect = assert, segment, selectTable) { const transaction = segment.transaction selectTable = selectTable || TABLE const trace = transaction.trace - t.ok(trace, 'trace should exist') - t.ok(trace.root, 'root element should exist') + expect.ok(trace, 'trace should exist') + expect.ok(trace.root, 'root element should exist') const setSegment = findSegment(trace.root, 'Datastore/statement/Postgres/' + TABLE + '/insert') @@ -130,23 +136,23 @@ export default function runTests(name, clientFactory) { 'Datastore/statement/Postgres/' + selectTable + '/select' ) - t.ok(setSegment, 'trace segment for insert should exist') - t.ok(getSegment, 'trace segment for select should exist') + expect.ok(setSegment, 'trace segment for insert should exist') + expect.ok(getSegment, 'trace segment for select should exist') if (!getSegment) { return } - t.equal( + expect.equal( getSegment.name, 'Datastore/statement/Postgres/' + selectTable + '/select', 'should register the query call' ) - t.ok(getSegment.timer.hrDuration, 'trace segment should have ended') + expect.ok(getSegment.timer.hrDuration, 'trace segment should have ended') } - function verifyInstanceParameters(t, segment) { + function verifyInstanceParameters(expect = assert, segment) { const transaction = segment.transaction const agent = transaction.agent const trace = transaction.trace @@ -155,230 +161,215 @@ export default function runTests(name, clientFactory) { const attributes = setSegment.getAttributes() const metricHostName = getMetricHostName(agent, params.postgres_host) - t.equal(attributes.host, metricHostName, 'should add the host parameter') - t.equal( + expect.equal(attributes.host, metricHostName, 'should add the host parameter') + expect.equal( attributes.port_path_or_id, String(params.postgres_port), 'should add the port parameter' ) - t.equal(attributes.database_name, params.postgres_db, 'should add the database name parameter') - t.equal(attributes.product, 'Postgres', 'should add the product attribute') + expect.equal( + attributes.database_name, + params.postgres_db, + 'should add the database name parameter' + ) + expect.equal(attributes.product, 'Postgres', 'should add the product attribute') } - function verifySlowQueries(t, agent) { + function verifySlowQueries(expect = assert, agent) { const metricHostName = getMetricHostName(agent, params.postgres_host) - t.equal(agent.queries.samples.size, 1, 'should have one slow query') + expect.equal(agent.queries.samples.size, 1, 'should have one slow query') for (const sample of agent.queries.samples.values()) { const queryParams = sample.getParams() - t.equal(queryParams.host, metricHostName, 'instance data should show up in slow query params') + expect.equal( + queryParams.host, + metricHostName, + 'instance data should show up in slow query params' + ) - t.equal( + expect.equal( queryParams.port_path_or_id, String(params.postgres_port), 'instance data should show up in slow query params' ) - t.equal( + expect.equal( queryParams.database_name, params.postgres_db, 'database name should show up in slow query params' ) - t.ok(queryParams.backtrace, 'params should contain a backtrace') + expect.ok(queryParams.backtrace, 'params should contain a backtrace') } } - test('Postgres instrumentation: ' + name, function (t) { - t.autoend() - - let agent = null + test('Postgres instrumentation: ' + name, async (t) => { + // This suite has to juggle the side effect-y nature of this setup because + // we don't have any way of re-importing `pg` while bypassing the + // module cache. If we tried to set up this suite properly, we'd instrument + // the library for the first subtest, and then the module would lose its + // instrumentation when the agent is recreated between tests, thus breaking + // the subsequent tests. So, be cautious adding any event handlers; they + // must be unregistered before a test ends, or they will interfere with + // other tests. + const agent = helper.instrumentMockedAgent() let pg = null - t.before(() => { - agent = helper.instrumentMockedAgent() - }) - t.beforeEach(async () => { pg = await postgresSetup() }) t.afterEach(() => { pg = null - // since we don't bootstrap a new agent + // Since we don't bootstrap a new agent // we need to clear the query aggregator as there are timing - // issues with clearing this within a test + // issues with clearing this within a test. agent.queries.clear() }) - t.teardown(() => { + t.after(() => { helper.unloadAgent(agent) }) - t.test('simple query with prepared statement', function (t) { + await t.test('simple query with prepared statement', (t, end) => { const client = new pg.Client(CON_OBJ) - t.teardown(function () { - client.end() - }) + t.after(() => client.end()) - t.notOk(agent.getTransaction(), 'no transaction should be in play') + assert.equal(agent.getTransaction(), undefined, 'no transaction should be in play') helper.runInTransaction(agent, function transactionInScope(tx) { const transaction = agent.getTransaction() - t.ok(transaction, 'transaction should be visible') - t.equal(tx, transaction, 'We got the same transaction') + assert.ok(transaction, 'transaction should be visible') + assert.equal(tx, transaction, 'We got the same transaction') const colVal = 'Hello' const pkVal = 111 - let insQuery = 'INSERT INTO ' + TABLE_PREPARED + ' (' + PK + ',' + COL - insQuery += ') VALUES($1, $2);' + const insQuery = `insert into ${TABLE_PREPARED} (${PK}, ${COL}) values($1, $2)` client.connect(function (error) { - if (!t.error(error)) { - return t.end() - } + assert.ifError(error) client.query(insQuery, [pkVal, colVal], function (error, ok) { - if (!t.error(error)) { - return t.end() - } - - t.ok(agent.getTransaction(), 'transaction should still be visible') - t.ok(ok, 'everything should be peachy after setting') + assert.ifError(error) - let selQuery = 'SELECT * FROM ' + TABLE_PREPARED + ' WHERE ' - selQuery += PK + '=' + pkVal + ';' + assert.ok(agent.getTransaction(), 'transaction should still be visible') + assert.ok(ok, 'everything should be peachy after setting') + const selQuery = `select * from ${TABLE_PREPARED} where ${PK} = ${pkVal}` client.query(selQuery, function (error, value) { - if (!t.error(error)) { - return t.end() - } + assert.ifError(error) - t.ok(agent.getTransaction(), 'transaction should still still be visible') - t.equal(value.rows[0][COL], colVal, 'Postgres client should still work') + assert.ok(agent.getTransaction(), 'transaction should still still be visible') + assert.equal(value.rows[0][COL], colVal, 'Postgres client should still work') transaction.end() - verify(t, agent.tracer.getSegment()) - t.end() + verify(assert, agent.tracer.getSegment()) + end() }) }) }) }) }) - t.test('Promise style query', function (t) { + await t.test('Promise style query', (t, end) => { const client = new pg.Client(CON_OBJ) - t.teardown(function () { - client.end() - }) + t.after(() => client.end()) helper.runInTransaction(agent, async function transactionInScope(tx) { const transaction = agent.getTransaction() - t.ok(transaction, 'transaction should be visible') - t.equal(tx, transaction, 'We got the same transaction') + assert.ok(transaction, 'transaction should be visible') + assert.equal(tx, transaction, 'We got the same transaction') const colVal = 'Hello' const pkVal = 111 - let insQuery = 'INSERT INTO ' + TABLE_PREPARED + ' (' + PK + ',' + COL - insQuery += ') VALUES($1, $2);' + const insQuery = `insert into ${TABLE_PREPARED} (${PK}, ${COL}) values($1, $2)` try { await client.connect() } catch (err) { - t.error(err) + assert.ifError(err) } try { const results = await client.query(insQuery, [pkVal, colVal]) - t.ok(agent.getTransaction(), 'transaction should still be visible') - t.ok(results, 'everything should be peachy after setting') - - let selQuery = 'SELECT * FROM ' + TABLE_PREPARED + ' WHERE ' - selQuery += PK + '=' + pkVal + ';' + assert.ok(agent.getTransaction(), 'transaction should still be visible') + assert.ok(results, 'everything should be peachy after setting') + const selQuery = `select * from ${TABLE_PREPARED} where ${PK} = ${pkVal}` const selectResults = await client.query(selQuery) + assert.ok(agent.getTransaction(), 'transaction should still still be visible') + assert.equal(selectResults.rows[0][COL], colVal, 'Postgres client should still work') - t.ok(agent.getTransaction(), 'transaction should still still be visible') - t.equal(selectResults.rows[0][COL], colVal, 'Postgres client should still work') transaction.end() - verify(t, agent.tracer.getSegment()) - t.end() + verify(assert, agent.tracer.getSegment()) + end() } catch (err) { - t.error(err) - t.end() + assert.ifError(err) + end() } }) }) - t.test('Submittable style Query timings', function (t) { + await t.test('Submittable style Query timings', (t, end) => { // see bottom of this page https://node-postgres.com/guides/upgrading const client = new pg.Client(CON_OBJ) - t.teardown(function () { - client.end() - }) + t.after(() => client.end()) - t.notOk(agent.getTransaction(), 'no transaction should be in play') + assert.equal(agent.getTransaction(), undefined, 'no transaction should be in play') helper.runInTransaction(agent, function transactionInScope(tx) { const transaction = agent.getTransaction() - t.ok(transaction, 'transaction should be visible') - t.equal(tx, transaction, 'We got the same transaction') + assert.ok(transaction, 'transaction should be visible') + assert.equal(tx, transaction, 'We got the same transaction') const selQuery = 'SELECT pg_sleep(2), now() as sleep;' client.connect(function (error) { - if (!t.error(error)) { - return t.end() - } + assert.ifError(error) const pgQuery = client.query(new pg.Query(selQuery)) pgQuery.on('error', () => { - t.error(error) - t.end() + assert.ifError(error) + end() }) pgQuery.on('end', () => { - t.ok(agent.getTransaction(), 'transaction should still be visible') - + assert.ok(agent.getTransaction(), 'transaction should still be visible') transaction.end() const segment = agent.tracer.getSegment() - const finalTx = segment.transaction - const metrics = finalTx.metrics.getMetric('Datastore/operation/Postgres/select') - t.ok( + assert.ok( metrics.total > 2.0, 'Submittable style Query pg_sleep of 2 seconds should result in > 2 sec timing' ) - t.end() + end() }) }) }) }) - t.test('Promise style query timings', function (t) { + await t.test('Promise style query timings', (t, end) => { const client = new pg.Client(CON_OBJ) - t.teardown(function () { - client.end() - }) + t.after(() => client.end()) helper.runInTransaction(agent, async function transactionInScope(tx) { const transaction = agent.getTransaction() - t.ok(transaction, 'transaction should be visible') - t.equal(tx, transaction, 'We got the same transaction') + assert.ok(transaction, 'transaction should be visible') + assert.equal(tx, transaction, 'We got the same transaction') try { await client.connect() } catch (err) { - t.error(err) + assert.ifError(err) } try { @@ -386,152 +377,128 @@ export default function runTests(name, clientFactory) { const selectResults = await client.query(selQuery) - t.ok(agent.getTransaction(), 'transaction should still still be visible') - t.ok(selectResults, 'Postgres client should still work') + assert.ok(agent.getTransaction(), 'transaction should still still be visible') + assert.ok(selectResults, 'Postgres client should still work') transaction.end() - const segment = agent.tracer.getSegment() + const segment = agent.tracer.getSegment() const finalTx = segment.transaction - const metrics = finalTx.metrics.getMetric('Datastore/operation/Postgres/select') - - t.ok( + assert.ok( metrics.total > 2.0, 'Promise style query pg_sleep of 2 seconds should result in > 2 sec timing' ) - t.end() + end() } catch (err) { - t.error(err) - t.end() + assert.ifError(err) + end() } }) }) - t.test('Callback style query timings', function (t) { + await t.test('Callback style query timings', (t, end) => { const client = new pg.Client(CON_OBJ) - t.teardown(function () { - client.end() - }) + t.after(() => client.end()) helper.runInTransaction(agent, async function transactionInScope(tx) { const transaction = agent.getTransaction() - t.ok(transaction, 'transaction should be visible') - t.equal(tx, transaction, 'We got the same transaction') + assert.ok(transaction, 'transaction should be visible') + assert.equal(tx, transaction, 'We got the same transaction') client.connect(function (error) { - if (!t.error(error)) { - return t.end() - } + assert.ifError(error) const selQuery = 'SELECT pg_sleep(2), now() as sleep;' client.query(selQuery, function (error, ok) { - if (!t.error(error)) { - return t.end() - } + assert.ifError(error) - t.ok(agent.getTransaction(), 'transaction should still be visible') - t.ok(ok, 'everything should be peachy after setting') + assert.ok(agent.getTransaction(), 'transaction should still be visible') + assert.ok(ok, 'everything should be peachy after setting') transaction.end() const segment = agent.tracer.getSegment() - const finalTx = segment.transaction - const metrics = finalTx.metrics.getMetric('Datastore/operation/Postgres/select') - - t.ok( + assert.ok( metrics.total > 2.0, 'Callback style query pg_sleep of 2 seconds should result in > 2 sec timing' ) - t.end() + end() }) }) }) }) - t.test('client pooling query', function (t) { - t.plan(39) - t.notOk(agent.getTransaction(), 'no transaction should be in play') + await t.test('client pooling query', async (t) => { + const plan = tspl(t, { plan: 39 }) + + plan.equal(agent.getTransaction(), undefined, 'no transaction should be in play') helper.runInTransaction(agent, function transactionInScope(tx) { const transaction = agent.getTransaction() - t.ok(transaction, 'transaction should be visible') - t.equal(tx, transaction, 'We got the same transaction') + plan.ok(transaction, 'transaction should be visible') + plan.equal(tx, transaction, 'We got the same transaction') const colVal = 'World!' const pkVal = 222 - let insQuery = 'INSERT INTO ' + TABLE_PREPARED + ' (' + PK + ',' + COL - insQuery += ') VALUES(' + pkVal + ",'" + colVal + "');" + const insQuery = `insert into ${TABLE_PREPARED} (${PK}, ${COL}) values('${pkVal}', '${colVal}')` const pool = new pg.Pool(CON_OBJ) pool.query(insQuery, function (error, ok) { - if (!t.error(error)) { - return t.end() - } + plan.ifError(error) - t.ok(agent.getTransaction(), 'transaction should still be visible') - t.ok(ok, 'everything should be peachy after setting') - - let selQuery = 'SELECT * FROM ' + TABLE_PREPARED + ' WHERE ' - selQuery += PK + '=' + pkVal + ';' + plan.ok(agent.getTransaction(), 'transaction should still be visible') + plan.ok(ok, 'everything should be peachy after setting') + const selQuery = `select * from ${TABLE_PREPARED} where ${PK} = ${pkVal}` pool.query(selQuery, function (error, value) { - if (!t.error(error)) { - return t.end() - } + plan.ifError(error) - t.ok(agent.getTransaction(), 'transaction should still still be visible') - t.equal(value.rows[0][COL], colVal, 'Postgres client should still work') + plan.ok(agent.getTransaction(), 'transaction should still still be visible') + plan.equal(value.rows[0][COL], colVal, 'Postgres client should still work') transaction.end() pool.end() - verify(t, agent.tracer.getSegment()) + verify(plan, agent.tracer.getSegment()) }) }) }) + + await plan.completed }) - t.test('using Pool constructor', function (t) { - t.plan(40) + await t.test('using Pool constructor', async (t) => { + const plan = tspl(t, { plan: 40 }) - t.notOk(agent.getTransaction(), 'no transaction should be in play') + plan.equal(agent.getTransaction(), undefined, 'no transaction should be in play') helper.runInTransaction(agent, function transactionInScope(tx) { const transaction = agent.getTransaction() - t.ok(transaction, 'transaction should be visible') - t.equal(tx, transaction, 'We got the same transaction') + plan.ok(transaction, 'transaction should be visible') + plan.equal(tx, transaction, 'We got the same transaction') const colVal = 'World!' const pkVal = 222 - let insQuery = 'INSERT INTO ' + TABLE_PREPARED + ' (' + PK + ',' + COL - insQuery += ') VALUES(' + pkVal + ",'" + colVal + "');" + const insQuery = `insert into ${TABLE_PREPARED} (${PK}, ${COL}) values('${pkVal}', '${colVal}')` const pool = new pg.Pool(CON_OBJ) pool.connect(function (error, client, done) { - if (!t.error(error)) { - return t.end() - } + plan.ifError(error) client.query(insQuery, function (error, ok) { - if (!t.error(error)) { - return t.end() - } - - t.ok(agent.getTransaction(), 'transaction should still be visible') - t.ok(ok, 'everything should be peachy after setting') + plan.ifError(error) - let selQuery = 'SELECT * FROM ' + TABLE_PREPARED + ' WHERE ' - selQuery += PK + '=' + pkVal + ';' + plan.ok(agent.getTransaction(), 'transaction should still be visible') + plan.ok(ok, 'everything should be peachy after setting') + const selQuery = `select * from ${TABLE_PREPARED} where ${PK} = ${pkVal}` client.query(selQuery, function (error, value) { - if (!t.error(error)) { - return t.end() - } + plan.ifError(error) - t.ok(agent.getTransaction(), 'transaction should still still be visible') - t.equal(value.rows[0][COL], colVal, 'Postgres client should still work') + plan.ok(agent.getTransaction(), 'transaction should still still be visible') + plan.equal(value.rows[0][COL], colVal, 'Postgres client should still work') transaction.end() if (pool.end instanceof Function) { @@ -539,100 +506,96 @@ export default function runTests(name, clientFactory) { } done(true) - verify(t, agent.tracer.getSegment()) + verify(plan, agent.tracer.getSegment()) }) }) }) }) + + await plan.completed }) // https://github.com/newrelic/node-newrelic/pull/223 - t.test('query using an config object with `text` getter instead of property', (t) => { - t.plan(3) - const client = new pg.Client(CON_OBJ) + await t.test( + 'query using an config object with `text` getter instead of property', + async (t) => { + const plan = tspl(t, { plan: 3 }) + const client = new pg.Client(CON_OBJ) - t.teardown(function () { - client.end() - }) + t.after(() => client.end()) - helper.runInTransaction(agent, function () { - const transaction = agent.getTransaction() + helper.runInTransaction(agent, function () { + const transaction = agent.getTransaction() - const colVal = 'Sianara' - const pkVal = 444 + const colVal = 'Sianara' + const pkVal = 444 - function CustomConfigClass() { - this._text = 'INSERT INTO ' + TABLE_PREPARED + ' (' + PK + ',' + COL - this._text += ') VALUES($1, $2);' - } - - // "text" is defined as a getter on the prototype, so it will not be - // a property owned by the instance - Object.defineProperty(CustomConfigClass.prototype, 'text', { - get: function () { - return this._text + function CustomConfigClass() { + this._text = `insert into ${TABLE_PREPARED} (${PK}, ${COL}) values($1, $2)` } - }) - // create a config instance - const config = new CustomConfigClass() + // "text" is defined as a getter on the prototype, so it will not be + // a property owned by the instance + Object.defineProperty(CustomConfigClass.prototype, 'text', { + get: function () { + return this._text + } + }) - client.connect(function (error) { - if (!t.error(error)) { - return t.end() - } + // create a config instance + const config = new CustomConfigClass() - client.query(config, [pkVal, colVal], function (error) { - if (!t.error(error)) { - return t.end() - } + client.connect(function (error) { + plan.ifError(error) - const segment = findSegment( - transaction.trace.root, - 'Datastore/statement/Postgres/' + TABLE + '/insert' - ) - t.ok(segment, 'expected segment exists') + client.query(config, [pkVal, colVal], function (error) { + plan.ifError(error) + + const segment = findSegment( + transaction.trace.root, + 'Datastore/statement/Postgres/' + TABLE + '/insert' + ) + plan.ok(segment, 'expected segment exists') + }) }) }) - }) - }) - t.test('should add datastore instance parameters to slow query traces', function (t) { - t.plan(7) + await plan.completed + } + ) + + await t.test('should add datastore instance parameters to slow query traces', async (t) => { + const plan = tspl(t, { plan: 7 }) + // enable slow queries agent.config.transaction_tracer.record_sql = 'raw' agent.config.slow_sql.enabled = true const client = new pg.Client(CON_OBJ) - t.teardown(function () { - client.end() - }) + t.after(() => client.end()) helper.runInTransaction(agent, function () { const transaction = agent.getTransaction() client.connect(function (error) { - if (!t.error(error)) { - return t.end() - } + plan.ifError(error) client.query('SELECT * FROM pg_sleep(1);', function slowQueryCB(error) { - if (!t.error(error)) { - return t.end() - } + plan.ifError(error) transaction.end() - verifySlowQueries(t, agent) - t.end() + verifySlowQueries(plan, agent) }) }) }) + + await plan.completed }) - t.test( + await t.test( 'should not add datastore instance parameters to slow query traces when' + ' disabled', - function (t) { - t.plan(5) + async (t) => { + const plan = tspl(t, { plan: 5 }) // enable slow queries agent.config.transaction_tracer.record_sql = 'raw' @@ -644,30 +607,22 @@ export default function runTests(name, clientFactory) { const client = new pg.Client(CON_OBJ) - t.teardown(function () { - client.end() - }) + t.after(() => client.end()) helper.runInTransaction(agent, function () { const transaction = agent.getTransaction() client.connect(function (error) { - if (!t.error(error)) { - return t.end() - } + plan.ifError(error) client.query('SELECT * FROM pg_sleep(1);', function (error) { - if (!t.error(error)) { - return t.end() - } + plan.ifError(error) transaction.end() const queryParams = agent.queries.samples.values().next().value - t.equal(queryParams.host, undefined, 'should not have host parameter') - - t.equal(queryParams.port_path_or_id, undefined, 'should not have port parameter') - - t.equal( + plan.equal(queryParams.host, undefined, 'should not have host parameter') + plan.equal(queryParams.port_path_or_id, undefined, 'should not have port parameter') + plan.equal( queryParams.database_name, undefined, 'should not have database name parameter' @@ -675,6 +630,8 @@ export default function runTests(name, clientFactory) { }) }) }) + + await plan.completed } ) }) diff --git a/test/versioned/pg-esm/pg.tap.mjs b/test/versioned/pg-esm/pg.test.mjs similarity index 100% rename from test/versioned/pg-esm/pg.tap.mjs rename to test/versioned/pg-esm/pg.test.mjs diff --git a/test/versioned/pg/force-native.tap.js b/test/versioned/pg/force-native.test.js similarity index 100% rename from test/versioned/pg/force-native.tap.js rename to test/versioned/pg/force-native.test.js diff --git a/test/versioned/pg/native.tap.js b/test/versioned/pg/native.test.js similarity index 100% rename from test/versioned/pg/native.tap.js rename to test/versioned/pg/native.test.js diff --git a/test/versioned/pg/package.json b/test/versioned/pg/package.json index 0b28021e6d..b36ec1fcef 100644 --- a/test/versioned/pg/package.json +++ b/test/versioned/pg/package.json @@ -13,9 +13,9 @@ "pg-native": ">=3.0.0" }, "files": [ - "force-native.tap.js", - "native.tap.js", - "pg.tap.js" + "force-native.test.js", + "native.test.js", + "pg.test.js" ] } ], diff --git a/test/versioned/pg/pg.common.js b/test/versioned/pg/pg.common.js index d29dfd301b..79daf1b1f5 100644 --- a/test/versioned/pg/pg.common.js +++ b/test/versioned/pg/pg.common.js @@ -5,11 +5,13 @@ 'use strict' -const tap = require('tap') +const test = require('node:test') +const assert = require('node:assert') +const tspl = require('@matteo.collina/tspl') + const params = require('../../lib/params') const helper = require('../../lib/agent_helper') const findSegment = require('../../lib/metrics_helper').findSegment -const test = tap.test const getMetricHostName = require('../../lib/metrics_helper').getMetricHostName function runCommand(client, cmd) { @@ -42,8 +44,7 @@ module.exports = function runTests(name, clientFactory) { * Deletion of testing table if already exists, * then recreation of a testing table */ - async function postgresSetup() { - const pg = clientFactory() + async function postgresSetup(pg) { const setupClient = new pg.Client(CON_OBJ) await new Promise((resolve, reject) => { @@ -55,28 +56,31 @@ module.exports = function runTests(name, clientFactory) { resolve() }) }) - await runCommand(setupClient, "set client_min_messages='warning';") // supress PG notices + await runCommand(setupClient, "set client_min_messages='warning';") // suppress PG notices - const tableDrop = 'DROP TABLE IF EXISTS ' + TABLE_PREPARED + const tableDrop = `drop table if exists ${TABLE_PREPARED}` await runCommand(setupClient, tableDrop) - const tableCreate = - 'CREATE TABLE ' + TABLE_PREPARED + ' (' + PK + ' integer PRIMARY KEY, ' + COL + ' text' + ');' + const tableCreate = `create table ${TABLE_PREPARED} (${PK} integer primary key, ${COL} text)` await runCommand(setupClient, tableCreate) setupClient.end() } - function verify(t, segment, selectTable) { - verifyMetrics(t, segment, selectTable) - verifyTrace(t, segment, selectTable) - verifyInstanceParameters(t, segment) + function verify(expect = assert, segment, selectTable) { + verifyMetrics(expect, segment, selectTable) + verifyTrace(expect, segment, selectTable) + verifyInstanceParameters(expect, segment) } - function verifyMetrics(t, segment, selectTable) { + function verifyMetrics(expect = assert, segment, selectTable) { const transaction = segment.transaction const agent = transaction.agent selectTable = selectTable || TABLE - t.equal(Object.keys(transaction.metrics.scoped).length, 0, 'should not have any scoped metrics') + expect.equal( + Object.keys(transaction.metrics.scoped).length, + 0, + 'should not have any scoped metrics' + ) const unscoped = transaction.metrics.unscoped @@ -100,9 +104,9 @@ module.exports = function runTests(name, clientFactory) { const unscopedNames = Object.keys(unscoped) expectedNames.forEach(function (expectedName) { - t.ok(unscoped[expectedName], 'should have unscoped metric ' + expectedName) + expect.ok(unscoped[expectedName], 'should have unscoped metric ' + expectedName) if (unscoped[expectedName]) { - t.equal( + expect.equal( unscoped[expectedName].callCount, expected[expectedName], 'metric ' + expectedName + ' should have correct callCount' @@ -110,20 +114,20 @@ module.exports = function runTests(name, clientFactory) { } }) - t.equal( + expect.equal( unscopedNames.length, expectedNames.length, 'should have correct number of unscoped metrics' ) } - function verifyTrace(t, segment, selectTable) { + function verifyTrace(expect = assert, segment, selectTable) { const transaction = segment.transaction selectTable = selectTable || TABLE const trace = transaction.trace - t.ok(trace, 'trace should exist') - t.ok(trace.root, 'root element should exist') + expect.ok(trace, 'trace should exist') + expect.ok(trace.root, 'root element should exist') const setSegment = findSegment(trace.root, 'Datastore/statement/Postgres/' + TABLE + '/insert') @@ -132,23 +136,23 @@ module.exports = function runTests(name, clientFactory) { 'Datastore/statement/Postgres/' + selectTable + '/select' ) - t.ok(setSegment, 'trace segment for insert should exist') - t.ok(getSegment, 'trace segment for select should exist') + expect.ok(setSegment, 'trace segment for insert should exist') + expect.ok(getSegment, 'trace segment for select should exist') if (!getSegment) { return } - t.equal( + expect.equal( getSegment.name, 'Datastore/statement/Postgres/' + selectTable + '/select', 'should register the query call' ) - t.ok(getSegment.timer.hrDuration, 'trace segment should have ended') + expect.ok(getSegment.timer.hrDuration, 'trace segment should have ended') } - function verifyInstanceParameters(t, segment) { + function verifyInstanceParameters(expect = assert, segment) { const transaction = segment.transaction const agent = transaction.agent const trace = transaction.trace @@ -157,127 +161,124 @@ module.exports = function runTests(name, clientFactory) { const attributes = setSegment.getAttributes() const metricHostName = getMetricHostName(agent, params.postgres_host) - t.equal(attributes.host, metricHostName, 'should add the host parameter') - t.equal( + expect.equal(attributes.host, metricHostName, 'should add the host parameter') + expect.equal( attributes.port_path_or_id, String(params.postgres_port), 'should add the port parameter' ) - t.equal(attributes.database_name, params.postgres_db, 'should add the database name parameter') - t.equal(attributes.product, 'Postgres', 'should add the product attribute') + expect.equal( + attributes.database_name, + params.postgres_db, + 'should add the database name parameter' + ) + expect.equal(attributes.product, 'Postgres', 'should add the product attribute') } - function verifySlowQueries(t, agent) { + function verifySlowQueries(expect = assert, agent) { const metricHostName = getMetricHostName(agent, params.postgres_host) - t.equal(agent.queries.samples.size, 1, 'should have one slow query') + expect.equal(agent.queries.samples.size, 1, 'should have one slow query') for (const sample of agent.queries.samples.values()) { const queryParams = sample.getParams() - t.equal(queryParams.host, metricHostName, 'instance data should show up in slow query params') + expect.equal( + queryParams.host, + metricHostName, + 'instance data should show up in slow query params' + ) - t.equal( + expect.equal( queryParams.port_path_or_id, String(params.postgres_port), 'instance data should show up in slow query params' ) - t.equal( + expect.equal( queryParams.database_name, params.postgres_db, 'database name should show up in slow query params' ) - t.ok(queryParams.backtrace, 'params should contain a backtrace') + expect.ok(queryParams.backtrace, 'params should contain a backtrace') } } - test('Postgres instrumentation: ' + name, function (t) { - t.autoend() + test('Postgres instrumentation: ' + name, async (t) => { + t.beforeEach((ctx) => { + ctx.nr = {} + ctx.nr.agent = helper.instrumentMockedAgent() + ctx.nr.pg = clientFactory() - let agent = null - let pg = null + return postgresSetup(ctx.nr.pg) + }) - t.beforeEach(function () { - // the pg module has `native` lazy getter that is removed after first call, - // so in order to re-instrument, we need to remove the pg module from the cache + t.afterEach((ctx) => { + helper.unloadAgent(ctx.nr.agent) + + // The pg module has `native` lazy getter that is removed after first + // call, so in order to re-instrument, we need to remove the pg module + // from the cache. For some unknown reason, we cannot use our utility + // methods for busting the require cache on this module. If we do, + // any require after the first will result in a "Module did not + // self-register" error regarding the native add-on. const pgName = require.resolve('pg') delete require.cache[pgName] - - agent = helper.instrumentMockedAgent() - pg = clientFactory() - - return postgresSetup() - }) - - t.afterEach(function () { - helper.unloadAgent(agent) - agent = null - pg = null }) - t.test('simple query with prepared statement', function (t) { + await t.test('simple query with prepared statement', (t, end) => { + const { agent, pg } = t.nr const client = new pg.Client(CON_OBJ) - t.teardown(function () { - client.end() - }) + t.after(() => client.end()) - t.notOk(agent.getTransaction(), 'no transaction should be in play') + assert.equal(agent.getTransaction(), undefined, 'no transaction should be in play') helper.runInTransaction(agent, function transactionInScope(tx) { const transaction = agent.getTransaction() - t.ok(transaction, 'transaction should be visible') - t.equal(tx, transaction, 'We got the same transaction') + assert.ok(transaction, 'transaction should be visible') + assert.equal(tx, transaction, 'We got the same transaction') const colVal = 'Hello' const pkVal = 111 - let insQuery = 'INSERT INTO ' + TABLE_PREPARED + ' (' + PK + ',' + COL - insQuery += ') VALUES($1, $2);' + const insQuery = `insert into ${TABLE_PREPARED} (${PK}, ${COL}) values($1, $2)` client.connect(function (error) { - if (!t.error(error)) { - return t.end() - } + assert.ifError(error) client.query(insQuery, [pkVal, colVal], function (error, ok) { - if (!t.error(error)) { - return t.end() - } + assert.ifError(error) - t.ok(agent.getTransaction(), 'transaction should still be visible') - t.ok(ok, 'everything should be peachy after setting') + assert.ok(agent.getTransaction(), 'transaction should still be visible') + assert.ok(ok, 'everything should be peachy after setting') let selQuery = 'SELECT * FROM ' + TABLE_PREPARED + ' WHERE ' selQuery += PK + '=' + pkVal + ';' client.query(selQuery, function (error, value) { - if (!t.error(error)) { - return t.end() - } + assert.ifError(error) - t.ok(agent.getTransaction(), 'transaction should still still be visible') - t.equal(value.rows[0][COL], colVal, 'Postgres client should still work') + assert.ok(agent.getTransaction(), 'transaction should still still be visible') + assert.equal(value.rows[0][COL], colVal, 'Postgres client should still work') transaction.end() - verify(t, agent.tracer.getSegment()) - t.end() + verify(assert, agent.tracer.getSegment()) + end() }) }) }) }) }) - t.test('Promise style query', function (t) { + await t.test('Promise style query', (t, end) => { + const { agent, pg } = t.nr const client = new pg.Client(CON_OBJ) - t.teardown(function () { - client.end() - }) + t.after(() => client.end()) helper.runInTransaction(agent, async function transactionInScope(tx) { const transaction = agent.getTransaction() - t.ok(transaction, 'transaction should be visible') - t.equal(tx, transaction, 'We got the same transaction') + assert.ok(transaction, 'transaction should be visible') + assert.equal(tx, transaction, 'We got the same transaction') const colVal = 'Hello' const pkVal = 111 @@ -287,220 +288,195 @@ module.exports = function runTests(name, clientFactory) { try { await client.connect() } catch (err) { - t.error(err) + assert.ifError(err) } try { const results = await client.query(insQuery, [pkVal, colVal]) - t.ok(agent.getTransaction(), 'transaction should still be visible') - t.ok(results, 'everything should be peachy after setting') - - let selQuery = 'SELECT * FROM ' + TABLE_PREPARED + ' WHERE ' - selQuery += PK + '=' + pkVal + ';' + assert.ok(agent.getTransaction(), 'transaction should still be visible') + assert.ok(results, 'everything should be peachy after setting') + const selQuery = `select * from ${TABLE_PREPARED} where ${PK} = ${pkVal}` const selectResults = await client.query(selQuery) - t.ok(agent.getTransaction(), 'transaction should still still be visible') - t.equal(selectResults.rows[0][COL], colVal, 'Postgres client should still work') + assert.ok(agent.getTransaction(), 'transaction should still still be visible') + assert.equal(selectResults.rows[0][COL], colVal, 'Postgres client should still work') transaction.end() - verify(t, agent.tracer.getSegment()) - t.end() + verify(assert, agent.tracer.getSegment()) + end() } catch (err) { - t.error(err) - t.end() + assert.ifError(err) + end() } }) }) - t.test('Submittable style Query timings', function (t) { + await t.test('Submittable style Query timings', (t, end) => { + const { agent, pg } = t.nr // see bottom of this page https://node-postgres.com/guides/upgrading const client = new pg.Client(CON_OBJ) - t.teardown(function () { - client.end() - }) + t.after(() => client.end()) - t.notOk(agent.getTransaction(), 'no transaction should be in play') + assert.equal(agent.getTransaction(), undefined, 'no transaction should be in play') helper.runInTransaction(agent, function transactionInScope(tx) { const transaction = agent.getTransaction() - t.ok(transaction, 'transaction should be visible') - t.equal(tx, transaction, 'We got the same transaction') + assert.ok(transaction, 'transaction should be visible') + assert.equal(tx, transaction, 'We got the same transaction') const selQuery = 'SELECT pg_sleep(2), now() as sleep;' client.connect(function (error) { - if (!t.error(error)) { - return t.end() - } - + assert.ifError(error) const pgQuery = client.query(new pg.Query(selQuery)) - pgQuery.on('error', () => { - t.error(error) - t.end() + pgQuery.on('error', (error) => { + assert.ifError(error) + end() }) pgQuery.on('end', () => { - t.ok(agent.getTransaction(), 'transaction should still be visible') - + assert.ok(agent.getTransaction(), 'transaction should still be visible') transaction.end() const segment = agent.tracer.getSegment() - const finalTx = segment.transaction - const metrics = finalTx.metrics.getMetric('Datastore/operation/Postgres/select') - - t.ok( + assert.ok( metrics.total > 2.0, - 'Submittable style Query pg_sleep of 2 seconds should result in > 2 sec timing' + 'Submittable style Query pg_sleep of 2 seconds should result in > 2 seßc timing' ) - t.end() + end() }) }) }) }) - t.test('Promise style query timings', function (t) { + await t.test('Promise style query timings', (t, end) => { + const { agent, pg } = t.nr const client = new pg.Client(CON_OBJ) - t.teardown(function () { - client.end() - }) + t.after(() => client.end()) helper.runInTransaction(agent, async function transactionInScope(tx) { const transaction = agent.getTransaction() - t.ok(transaction, 'transaction should be visible') - t.equal(tx, transaction, 'We got the same transaction') + assert.ok(transaction, 'transaction should be visible') + assert.equal(tx, transaction, 'We got the same transaction') try { await client.connect() } catch (err) { - t.error(err) + assert.ifError(err) } try { const selQuery = 'SELECT pg_sleep(2), now() as sleep;' const selectResults = await client.query(selQuery) - - t.ok(agent.getTransaction(), 'transaction should still still be visible') - t.ok(selectResults, 'Postgres client should still work') + assert.ok(agent.getTransaction(), 'transaction should still still be visible') + assert.ok(selectResults, 'Postgres client should still work') transaction.end() - const segment = agent.tracer.getSegment() + const segment = agent.tracer.getSegment() const finalTx = segment.transaction - const metrics = finalTx.metrics.getMetric('Datastore/operation/Postgres/select') - - t.ok( + assert.ok( metrics.total > 2.0, 'Promise style query pg_sleep of 2 seconds should result in > 2 sec timing' ) - t.end() + end() } catch (err) { - t.error(err) - t.end() + assert.ifError(err) + end() } }) }) - t.test('Callback style query timings', function (t) { + await t.test('Callback style query timings', (t, end) => { + const { agent, pg } = t.nr const client = new pg.Client(CON_OBJ) - t.teardown(function () { - client.end() - }) + t.after(() => client.end()) helper.runInTransaction(agent, async function transactionInScope(tx) { const transaction = agent.getTransaction() - t.ok(transaction, 'transaction should be visible') - t.equal(tx, transaction, 'We got the same transaction') + assert.ok(transaction, 'transaction should be visible') + assert.equal(tx, transaction, 'We got the same transaction') client.connect(function (error) { - if (!t.error(error)) { - return t.end() - } + assert.ifError(error) const selQuery = 'SELECT pg_sleep(2), now() as sleep;' client.query(selQuery, function (error, ok) { - if (!t.error(error)) { - return t.end() - } - - t.ok(agent.getTransaction(), 'transaction should still be visible') - t.ok(ok, 'everything should be peachy after setting') + assert.ifError(error) + assert.ok(agent.getTransaction(), 'transaction should still be visible') + assert.ok(ok, 'everything should be peachy after setting') transaction.end() const segment = agent.tracer.getSegment() - const finalTx = segment.transaction - const metrics = finalTx.metrics.getMetric('Datastore/operation/Postgres/select') - - t.ok( + assert.ok( metrics.total > 2.0, 'Callback style query pg_sleep of 2 seconds should result in > 2 sec timing' ) - t.end() + end() }) }) }) }) - t.test('client pooling query', function (t) { - t.plan(39) - t.notOk(agent.getTransaction(), 'no transaction should be in play') + await t.test('client pooling query', async (t) => { + const plan = tspl(t, { plan: 39 }) + const { agent, pg } = t.nr + + plan.equal(agent.getTransaction(), undefined, 'no transaction should be in play') helper.runInTransaction(agent, function transactionInScope(tx) { const transaction = agent.getTransaction() - t.ok(transaction, 'transaction should be visible') - t.equal(tx, transaction, 'We got the same transaction') + plan.ok(transaction, 'transaction should be visible') + plan.equal(tx, transaction, 'We got the same transaction') const colVal = 'World!' const pkVal = 222 - let insQuery = 'INSERT INTO ' + TABLE_PREPARED + ' (' + PK + ',' + COL - insQuery += ') VALUES(' + pkVal + ",'" + colVal + "');" + const insQuery = `insert into ${TABLE_PREPARED} (${PK}, ${COL}) values('${pkVal}', '${colVal}')` const pool = new pg.Pool(CON_OBJ) pool.query(insQuery, function (error, ok) { - if (!t.error(error)) { - return t.end() - } - - t.ok(agent.getTransaction(), 'transaction should still be visible') - t.ok(ok, 'everything should be peachy after setting') + plan.ifError(error) - let selQuery = 'SELECT * FROM ' + TABLE_PREPARED + ' WHERE ' - selQuery += PK + '=' + pkVal + ';' + plan.ok(agent.getTransaction(), 'transaction should still be visible') + plan.ok(ok, 'everything should be peachy after setting') + const selQuery = `select * from ${TABLE_PREPARED} where ${PK} = ${pkVal}` pool.query(selQuery, function (error, value) { - if (!t.error(error)) { - return t.end() - } + plan.ifError(error) - t.ok(agent.getTransaction(), 'transaction should still still be visible') - t.equal(value.rows[0][COL], colVal, 'Postgres client should still work') + plan.ok(agent.getTransaction(), 'transaction should still still be visible') + plan.equal(value.rows[0][COL], colVal, 'Postgres client should still work') transaction.end() pool.end() - verify(t, agent.tracer.getSegment()) + verify(plan, agent.tracer.getSegment()) }) }) }) + + await plan.completed }) - t.test('using Pool constructor', function (t) { - t.plan(40) + await t.test('using Pool constructor', async (t) => { + const plan = tspl(t, { plan: 40 }) + const { agent, pg } = t.nr - t.notOk(agent.getTransaction(), 'no transaction should be in play') + plan.equal(agent.getTransaction(), undefined, 'no transaction should be in play') helper.runInTransaction(agent, function transactionInScope(tx) { const transaction = agent.getTransaction() - t.ok(transaction, 'transaction should be visible') - t.equal(tx, transaction, 'We got the same transaction') + plan.ok(transaction, 'transaction should be visible') + plan.equal(tx, transaction, 'We got the same transaction') const colVal = 'World!' const pkVal = 222 @@ -510,28 +486,22 @@ module.exports = function runTests(name, clientFactory) { const pool = new pg.Pool(CON_OBJ) pool.connect(function (error, client, done) { - if (!t.error(error)) { - return t.end() - } + plan.ifError(error) client.query(insQuery, function (error, ok) { - if (!t.error(error)) { - return t.end() - } + plan.ifError(error) - t.ok(agent.getTransaction(), 'transaction should still be visible') - t.ok(ok, 'everything should be peachy after setting') + plan.ok(agent.getTransaction(), 'transaction should still be visible') + plan.ok(ok, 'everything should be peachy after setting') let selQuery = 'SELECT * FROM ' + TABLE_PREPARED + ' WHERE ' selQuery += PK + '=' + pkVal + ';' client.query(selQuery, function (error, value) { - if (!t.error(error)) { - return t.end() - } + plan.ifError(error) - t.ok(agent.getTransaction(), 'transaction should still still be visible') - t.equal(value.rows[0][COL], colVal, 'Postgres client should still work') + plan.ok(agent.getTransaction(), 'transaction should still still be visible') + plan.equal(value.rows[0][COL], colVal, 'Postgres client should still work') transaction.end() if (pool.end instanceof Function) { @@ -539,100 +509,98 @@ module.exports = function runTests(name, clientFactory) { } done(true) - verify(t, agent.tracer.getSegment()) + verify(plan, agent.tracer.getSegment()) }) }) }) }) + + await plan.completed }) // https://github.com/newrelic/node-newrelic/pull/223 - t.test('query using an config object with `text` getter instead of property', (t) => { - t.plan(3) - const client = new pg.Client(CON_OBJ) - - t.teardown(function () { - client.end() - }) + await t.test( + 'query using an config object with `text` getter instead of property', + async (t) => { + const plan = tspl(t, { plan: 3 }) + const { agent, pg } = t.nr + const client = new pg.Client(CON_OBJ) - helper.runInTransaction(agent, function () { - const transaction = agent.getTransaction() + t.after(() => client.end()) - const colVal = 'Sianara' - const pkVal = 444 + helper.runInTransaction(agent, function () { + const transaction = agent.getTransaction() - function CustomConfigClass() { - this._text = 'INSERT INTO ' + TABLE_PREPARED + ' (' + PK + ',' + COL - this._text += ') VALUES($1, $2);' - } + const colVal = 'Sianara' + const pkVal = 444 - // "text" is defined as a getter on the prototype, so it will not be - // a property owned by the instance - Object.defineProperty(CustomConfigClass.prototype, 'text', { - get: function () { - return this._text + function CustomConfigClass() { + this._text = `insert into ${TABLE_PREPARED} (${PK}, ${COL}) values($1, $2)` } - }) - // create a config instance - const config = new CustomConfigClass() + // "text" is defined as a getter on the prototype, so it will not be + // a property owned by the instance + Object.defineProperty(CustomConfigClass.prototype, 'text', { + get: function () { + return this._text + } + }) - client.connect(function (error) { - if (!t.error(error)) { - return t.end() - } + // create a config instance + const config = new CustomConfigClass() - client.query(config, [pkVal, colVal], function (error) { - if (!t.error(error)) { - return t.end() - } + client.connect(function (error) { + plan.ifError(error) - const segment = findSegment( - transaction.trace.root, - 'Datastore/statement/Postgres/' + TABLE + '/insert' - ) - t.ok(segment, 'expected segment exists') + client.query(config, [pkVal, colVal], function (error) { + plan.ifError(error) + const segment = findSegment( + transaction.trace.root, + 'Datastore/statement/Postgres/' + TABLE + '/insert' + ) + plan.ok(segment, 'expected segment exists') + }) }) }) - }) - }) - t.test('should add datastore instance parameters to slow query traces', function (t) { - t.plan(7) + await plan.completed + } + ) + + await t.test('should add datastore instance parameters to slow query traces', async (t) => { + const plan = tspl(t, { plan: 7 }) + const { agent, pg } = t.nr + // enable slow queries agent.config.transaction_tracer.record_sql = 'raw' agent.config.slow_sql.enabled = true const client = new pg.Client(CON_OBJ) - t.teardown(function () { - client.end() - }) + t.after(() => client.end()) helper.runInTransaction(agent, function () { const transaction = agent.getTransaction() client.connect(function (error) { - if (!t.error(error)) { - return t.end() - } + plan.ifError(error) client.query('SELECT * FROM pg_sleep(1);', function slowQueryCB(error) { - if (!t.error(error)) { - return t.end() - } + plan.ifError(error) transaction.end() - verifySlowQueries(t, agent) - t.end() + verifySlowQueries(plan, agent) }) }) }) + + await plan.completed }) - t.test( + await t.test( 'should not add datastore instance parameters to slow query traces when' + ' disabled', - function (t) { - t.plan(5) + async (t) => { + const plan = tspl(t, { plan: 5 }) + const { agent, pg } = t.nr // enable slow queries agent.config.transaction_tracer.record_sql = 'raw' @@ -644,30 +612,24 @@ module.exports = function runTests(name, clientFactory) { const client = new pg.Client(CON_OBJ) - t.teardown(function () { - client.end() - }) + t.after(() => client.end()) helper.runInTransaction(agent, function () { const transaction = agent.getTransaction() client.connect(function (error) { - if (!t.error(error)) { - return t.end() - } + plan.ifError(error) client.query('SELECT * FROM pg_sleep(1);', function (error) { - if (!t.error(error)) { - return t.end() - } + plan.ifError(error) transaction.end() const queryParams = agent.queries.samples.values().next().value - t.equal(queryParams.host, undefined, 'should not have host parameter') + plan.equal(queryParams.host, undefined, 'should not have host parameter') - t.equal(queryParams.port_path_or_id, undefined, 'should not have port parameter') + plan.equal(queryParams.port_path_or_id, undefined, 'should not have port parameter') - t.equal( + plan.equal( queryParams.database_name, undefined, 'should not have database name parameter' @@ -675,6 +637,8 @@ module.exports = function runTests(name, clientFactory) { }) }) }) + + await plan.completed } ) }) diff --git a/test/versioned/pg/pg.tap.js b/test/versioned/pg/pg.test.js similarity index 100% rename from test/versioned/pg/pg.tap.js rename to test/versioned/pg/pg.test.js