From 5cd3b7c72e154aa6a236a6c19c1f3642d5033ba2 Mon Sep 17 00:00:00 2001 From: Onur Temizkan Date: Thu, 5 Dec 2024 19:10:25 +0000 Subject: [PATCH] Update tests --- .../test/fixtures/use-pg.mjs | 37 ++++++++---- .../test/pg-pool.test.ts | 6 +- .../test/pg.esm.test.ts | 58 +++++++++++++++++++ .../test/pg.test.ts | 25 ++------ 4 files changed, 93 insertions(+), 33 deletions(-) create mode 100644 plugins/node/opentelemetry-instrumentation-pg/test/pg.esm.test.ts diff --git a/plugins/node/opentelemetry-instrumentation-pg/test/fixtures/use-pg.mjs b/plugins/node/opentelemetry-instrumentation-pg/test/fixtures/use-pg.mjs index aa8ee5ce0b..b571f3f68a 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/test/fixtures/use-pg.mjs +++ b/plugins/node/opentelemetry-instrumentation-pg/test/fixtures/use-pg.mjs @@ -15,30 +15,45 @@ */ // Use postgres from an ES module: -// node --experimental-loader=@opentelemetry/instrumentation/hook.mjs pg-esm.mjs +// node --experimental-loader=@opentelemetry/instrumentation/hook.mjs use-pg.mjs +import { trace } from '@opentelemetry/api'; import { createTestNodeSdk } from '@opentelemetry/contrib-test-utils'; +import assert from 'assert'; import { PgInstrumentation } from '../../build/src/index.js'; +const CONFIG = { + user: process.env.POSTGRES_USER || 'postgres', + password: process.env.POSTGRES_PASSWORD || 'postgres', + database: process.env.POSTGRES_DB || 'postgres', + host: process.env.POSTGRES_HOST || 'localhost', + port: process.env.POSTGRES_PORT + ? parseInt(process.env.POSTGRES_PORT, 10) + : 54320, +}; + const sdk = createTestNodeSdk({ serviceName: 'use-pg', - instrumentations: [ - new PgInstrumentation() - ] -}) + instrumentations: [new PgInstrumentation()], +}); sdk.start(); import pg from 'pg'; -const client = new pg.Client(); +const client = new pg.Client(CONFIG); -client.connect(); +await new Promise(resolve => setTimeout(resolve, 4000)); +await client.connect(); -client.query('SELECT NOW()', (err, res) => { - console.log(err, res); - client.end(); -}); +const tracer = trace.getTracer(); +await tracer.startActiveSpan('test-span', async (span) => { + const res = await client.query('SELECT NOW()'); + assert.ok(res); + span.end(); +}); +await client.end(); +await sdk.shutdown(); diff --git a/plugins/node/opentelemetry-instrumentation-pg/test/pg-pool.test.ts b/plugins/node/opentelemetry-instrumentation-pg/test/pg-pool.test.ts index f0ac1508d9..4f56e4e0f0 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/test/pg-pool.test.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/test/pg-pool.test.ts @@ -68,7 +68,8 @@ const CONFIG = { ? parseInt(process.env.POSTGRES_PORT, 10) : 54320, maxClient: 1, - idleTimeoutMillis: 10000, + idleTimeoutMillis: 30000, + }; const DEFAULT_PGPOOL_ATTRIBUTES = { @@ -125,7 +126,7 @@ describe('pg-pool', () => { const testPostgresLocally = process.env.RUN_POSTGRES_TESTS_LOCAL; // For local: spins up local postgres db via docker const shouldTest = testPostgres || testPostgresLocally; // Skips these tests if false (default) - before(function () { + before(function (done) { const skip = () => { // this.skip() workaround // https://github.com/mochajs/mocha/issues/2683#issuecomment-375629901 @@ -150,6 +151,7 @@ describe('pg-pool', () => { const pgPool = require('pg-pool'); pool = new pgPool(CONFIG); + setTimeout(done, 3000); }); after(done => { diff --git a/plugins/node/opentelemetry-instrumentation-pg/test/pg.esm.test.ts b/plugins/node/opentelemetry-instrumentation-pg/test/pg.esm.test.ts new file mode 100644 index 0000000000..931a08ac88 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-pg/test/pg.esm.test.ts @@ -0,0 +1,58 @@ +import * as assert from 'assert'; +import * as testUtils from '@opentelemetry/contrib-test-utils'; + +describe('pg ESM usage', () => { + const testPostgres = process.env.RUN_POSTGRES_TESTS; // For CI: assumes local postgres db is already available + const testPostgresLocally = process.env.RUN_POSTGRES_TESTS_LOCAL; // For local: spins up local postgres db via docker + const shouldTest = testPostgres || testPostgresLocally; // Skips these tests if false (default) + + before(async function () { + const skip = () => { + // this.skip() workaround + // https://github.com/mochajs/mocha/issues/2683#issuecomment-375629901 + this.test!.parent!.pending = true; + this.skip(); + }; + + if (!shouldTest) { + skip(); + } + + if (testPostgresLocally) { + testUtils.startDocker('postgres'); + } + }); + + after(async () => { + if (testPostgresLocally) { + testUtils.cleanUpDocker('postgres'); + } + }); + + it('should work with ESM usage', async () => { + await testUtils.runTestFixture({ + cwd: __dirname, + argv: ['fixtures/use-pg.mjs'], + env: { + NODE_OPTIONS: + '--experimental-loader=@opentelemetry/instrumentation/hook.mjs', + NODE_NO_WARNINGS: '1', + }, + checkResult: (err) => { + assert.ifError(err); + }, + checkCollector: (collector: testUtils.TestCollector) => { + const spans = collector.sortedSpans; + + assert.strictEqual(spans.length, 3); + + assert.strictEqual(spans[0].name, 'pg.connect'); + assert.strictEqual(spans[0].kind, 3); + assert.strictEqual(spans[1].name, 'test-span'); + assert.strictEqual(spans[1].kind, 1); + assert.strictEqual(spans[2].name, 'pg.query:SELECT postgres'); + assert.strictEqual(spans[2].kind, 3); + }, + }); + }); +}); diff --git a/plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts b/plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts index 7d7024af06..e68ffd0946 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts @@ -152,10 +152,15 @@ describe('pg', () => { postgres = require('pg'); client = new postgres.Client(CONFIG); + + await new Promise(resolve => setTimeout(resolve, 3000)); + await client.connect(); }); after(async () => { + await new Promise(resolve => setTimeout(resolve, 3000)); + if (testPostgresLocally) { testUtils.cleanUpDocker('postgres'); } @@ -1086,24 +1091,4 @@ describe('pg', () => { }); }); }); - - it('should work with ESM usage', async () => { - await testUtils.runTestFixture({ - cwd: __dirname, - argv: ['fixtures/use-pg.mjs'], - env: { - NODE_OPTIONS: - '--experimental-loader=@opentelemetry/instrumentation/hook.mjs', - NODE_NO_WARNINGS: '1', - }, - checkResult: (err, stdout, stderr) => { - assert.ifError(err); - }, - checkCollector: (collector: testUtils.TestCollector) => { - const spans = collector.sortedSpans; - - assert.strictEqual(spans.length, 2); - }, - }); - }); });