Skip to content

Commit 5cd3b7c

Browse files
authored
Update tests
1 parent bd89bcc commit 5cd3b7c

File tree

4 files changed

+93
-33
lines changed

4 files changed

+93
-33
lines changed

plugins/node/opentelemetry-instrumentation-pg/test/fixtures/use-pg.mjs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,45 @@
1515
*/
1616

1717
// Use postgres from an ES module:
18-
// node --experimental-loader=@opentelemetry/instrumentation/hook.mjs pg-esm.mjs
18+
// node --experimental-loader=@opentelemetry/instrumentation/hook.mjs use-pg.mjs
1919

20+
import { trace } from '@opentelemetry/api';
2021
import { createTestNodeSdk } from '@opentelemetry/contrib-test-utils';
22+
import assert from 'assert';
2123

2224
import { PgInstrumentation } from '../../build/src/index.js';
2325

26+
const CONFIG = {
27+
user: process.env.POSTGRES_USER || 'postgres',
28+
password: process.env.POSTGRES_PASSWORD || 'postgres',
29+
database: process.env.POSTGRES_DB || 'postgres',
30+
host: process.env.POSTGRES_HOST || 'localhost',
31+
port: process.env.POSTGRES_PORT
32+
? parseInt(process.env.POSTGRES_PORT, 10)
33+
: 54320,
34+
};
35+
2436
const sdk = createTestNodeSdk({
2537
serviceName: 'use-pg',
26-
instrumentations: [
27-
new PgInstrumentation()
28-
]
29-
})
38+
instrumentations: [new PgInstrumentation()],
39+
});
3040
sdk.start();
3141

3242
import pg from 'pg';
3343

34-
const client = new pg.Client();
44+
const client = new pg.Client(CONFIG);
3545

36-
client.connect();
46+
await new Promise(resolve => setTimeout(resolve, 4000));
47+
await client.connect();
3748

38-
client.query('SELECT NOW()', (err, res) => {
39-
console.log(err, res);
40-
client.end();
41-
});
49+
const tracer = trace.getTracer();
4250

51+
await tracer.startActiveSpan('test-span', async (span) => {
52+
const res = await client.query('SELECT NOW()');
4353

54+
assert.ok(res);
55+
span.end();
56+
});
4457

58+
await client.end();
59+
await sdk.shutdown();

plugins/node/opentelemetry-instrumentation-pg/test/pg-pool.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ const CONFIG = {
6868
? parseInt(process.env.POSTGRES_PORT, 10)
6969
: 54320,
7070
maxClient: 1,
71-
idleTimeoutMillis: 10000,
71+
idleTimeoutMillis: 30000,
72+
7273
};
7374

7475
const DEFAULT_PGPOOL_ATTRIBUTES = {
@@ -125,7 +126,7 @@ describe('pg-pool', () => {
125126
const testPostgresLocally = process.env.RUN_POSTGRES_TESTS_LOCAL; // For local: spins up local postgres db via docker
126127
const shouldTest = testPostgres || testPostgresLocally; // Skips these tests if false (default)
127128

128-
before(function () {
129+
before(function (done) {
129130
const skip = () => {
130131
// this.skip() workaround
131132
// https://github.com/mochajs/mocha/issues/2683#issuecomment-375629901
@@ -150,6 +151,7 @@ describe('pg-pool', () => {
150151

151152
const pgPool = require('pg-pool');
152153
pool = new pgPool(CONFIG);
154+
setTimeout(done, 3000);
153155
});
154156

155157
after(done => {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import * as assert from 'assert';
2+
import * as testUtils from '@opentelemetry/contrib-test-utils';
3+
4+
describe('pg ESM usage', () => {
5+
const testPostgres = process.env.RUN_POSTGRES_TESTS; // For CI: assumes local postgres db is already available
6+
const testPostgresLocally = process.env.RUN_POSTGRES_TESTS_LOCAL; // For local: spins up local postgres db via docker
7+
const shouldTest = testPostgres || testPostgresLocally; // Skips these tests if false (default)
8+
9+
before(async function () {
10+
const skip = () => {
11+
// this.skip() workaround
12+
// https://github.com/mochajs/mocha/issues/2683#issuecomment-375629901
13+
this.test!.parent!.pending = true;
14+
this.skip();
15+
};
16+
17+
if (!shouldTest) {
18+
skip();
19+
}
20+
21+
if (testPostgresLocally) {
22+
testUtils.startDocker('postgres');
23+
}
24+
});
25+
26+
after(async () => {
27+
if (testPostgresLocally) {
28+
testUtils.cleanUpDocker('postgres');
29+
}
30+
});
31+
32+
it('should work with ESM usage', async () => {
33+
await testUtils.runTestFixture({
34+
cwd: __dirname,
35+
argv: ['fixtures/use-pg.mjs'],
36+
env: {
37+
NODE_OPTIONS:
38+
'--experimental-loader=@opentelemetry/instrumentation/hook.mjs',
39+
NODE_NO_WARNINGS: '1',
40+
},
41+
checkResult: (err) => {
42+
assert.ifError(err);
43+
},
44+
checkCollector: (collector: testUtils.TestCollector) => {
45+
const spans = collector.sortedSpans;
46+
47+
assert.strictEqual(spans.length, 3);
48+
49+
assert.strictEqual(spans[0].name, 'pg.connect');
50+
assert.strictEqual(spans[0].kind, 3);
51+
assert.strictEqual(spans[1].name, 'test-span');
52+
assert.strictEqual(spans[1].kind, 1);
53+
assert.strictEqual(spans[2].name, 'pg.query:SELECT postgres');
54+
assert.strictEqual(spans[2].kind, 3);
55+
},
56+
});
57+
});
58+
});

plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,15 @@ describe('pg', () => {
152152

153153
postgres = require('pg');
154154
client = new postgres.Client(CONFIG);
155+
156+
await new Promise<void>(resolve => setTimeout(resolve, 3000));
157+
155158
await client.connect();
156159
});
157160

158161
after(async () => {
162+
await new Promise<void>(resolve => setTimeout(resolve, 3000));
163+
159164
if (testPostgresLocally) {
160165
testUtils.cleanUpDocker('postgres');
161166
}
@@ -1086,24 +1091,4 @@ describe('pg', () => {
10861091
});
10871092
});
10881093
});
1089-
1090-
it('should work with ESM usage', async () => {
1091-
await testUtils.runTestFixture({
1092-
cwd: __dirname,
1093-
argv: ['fixtures/use-pg.mjs'],
1094-
env: {
1095-
NODE_OPTIONS:
1096-
'--experimental-loader=@opentelemetry/instrumentation/hook.mjs',
1097-
NODE_NO_WARNINGS: '1',
1098-
},
1099-
checkResult: (err, stdout, stderr) => {
1100-
assert.ifError(err);
1101-
},
1102-
checkCollector: (collector: testUtils.TestCollector) => {
1103-
const spans = collector.sortedSpans;
1104-
1105-
assert.strictEqual(spans.length, 2);
1106-
},
1107-
});
1108-
});
11091094
});

0 commit comments

Comments
 (0)