Skip to content

Commit 1b90154

Browse files
committed
test(node): Unflake postgres tests
Closes #20553 The clanker identified this as possible problem here: > docker compose up --wait waits on the container healthcheck (pg_isready inside the DB). On busy CI, localhost:5444 can still refuse connections briefly afterward, so the scenario could hit Postgres before it was reachable from the host and fail or behave inconsistently → flaky CJS (and the same class of failure for the other scenarios).
1 parent c0005cd commit 1b90154

9 files changed

Lines changed: 50 additions & 0 deletions

File tree

dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-requestHook.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const Sentry = require('@sentry/node');
22
const postgres = require('postgres');
3+
const { waitForPostgres } = require('./wait-for-postgres.js');
34

45
// Stop the process from exiting before the transaction is sent
56
setInterval(() => {}, 1000);
@@ -14,6 +15,7 @@ async function run() {
1415
},
1516
async () => {
1617
try {
18+
await waitForPostgres(sql);
1719
await sql`
1820
CREATE TABLE "User" ("id" SERIAL NOT NULL,"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,"email" TEXT NOT NULL,"name" TEXT,CONSTRAINT "User_pkey" PRIMARY KEY ("id"));
1921
`;

dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-requestHook.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import * as Sentry from '@sentry/node';
2+
import { createRequire } from 'node:module';
23
import postgres from 'postgres';
34

5+
const require = createRequire(import.meta.url);
6+
const { waitForPostgres } = require('./wait-for-postgres.js');
7+
48
// Stop the process from exiting before the transaction is sent
59
setInterval(() => {}, 1000);
610

@@ -14,6 +18,7 @@ async function run() {
1418
},
1519
async () => {
1620
try {
21+
await waitForPostgres(sql);
1722
await sql`
1823
CREATE TABLE "User" ("id" SERIAL NOT NULL,"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,"email" TEXT NOT NULL,"name" TEXT,CONSTRAINT "User_pkey" PRIMARY KEY ("id"));
1924
`;

dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-unsafe.cjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Sentry.init({
1010

1111
// Import postgres AFTER Sentry.init() so instrumentation is set up
1212
const postgres = require('postgres');
13+
const { waitForPostgres } = require('./wait-for-postgres.js');
1314

1415
// Stop the process from exiting before the transaction is sent
1516
setInterval(() => {}, 1000);
@@ -25,6 +26,7 @@ async function run() {
2526
},
2627
async () => {
2728
try {
29+
await waitForPostgres(sql);
2830
// Test sql.unsafe() - this was not being instrumented before the fix
2931
await sql.unsafe('CREATE TABLE "User" ("id" SERIAL NOT NULL, "email" TEXT NOT NULL, PRIMARY KEY ("id"))');
3032

dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-unsafe.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import * as Sentry from '@sentry/node';
2+
import { createRequire } from 'node:module';
23
import postgres from 'postgres';
34

5+
const require = createRequire(import.meta.url);
6+
const { waitForPostgres } = require('./wait-for-postgres.js');
7+
48
// Stop the process from exiting before the transaction is sent
59
setInterval(() => {}, 1000);
610

@@ -15,6 +19,7 @@ async function run() {
1519
},
1620
async () => {
1721
try {
22+
await waitForPostgres(sql);
1823
// Test sql.unsafe() - this was not being instrumented before the fix
1924
await sql.unsafe('CREATE TABLE "User" ("id" SERIAL NOT NULL, "email" TEXT NOT NULL, PRIMARY KEY ("id"))');
2025

dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-url.cjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Sentry.init({
1010

1111
// Import postgres AFTER Sentry.init() so instrumentation is set up
1212
const postgres = require('postgres');
13+
const { waitForPostgres } = require('./wait-for-postgres.js');
1314

1415
// Stop the process from exiting before the transaction is sent
1516
setInterval(() => {}, 1000);
@@ -25,6 +26,7 @@ async function run() {
2526
},
2627
async () => {
2728
try {
29+
await waitForPostgres(sql);
2830
await sql`
2931
CREATE TABLE "User" ("id" SERIAL NOT NULL,"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,"email" TEXT NOT NULL,"name" TEXT,CONSTRAINT "User_pkey" PRIMARY KEY ("id"));
3032
`;

dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-url.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import * as Sentry from '@sentry/node';
2+
import { createRequire } from 'node:module';
23
import postgres from 'postgres';
34

5+
const require = createRequire(import.meta.url);
6+
const { waitForPostgres } = require('./wait-for-postgres.js');
7+
48
// Stop the process from exiting before the transaction is sent
59
setInterval(() => {}, 1000);
610

@@ -15,6 +19,7 @@ async function run() {
1519
},
1620
async () => {
1721
try {
22+
await waitForPostgres(sql);
1823
await sql`
1924
CREATE TABLE "User" ("id" SERIAL NOT NULL,"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,"email" TEXT NOT NULL,"name" TEXT,CONSTRAINT "User_pkey" PRIMARY KEY ("id"));
2025
`;

dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { loggingTransport } = require('@sentry-internal/node-integration-tests');
22
const Sentry = require('@sentry/node');
3+
const { waitForPostgres } = require('./wait-for-postgres.js');
34

45
Sentry.init({
56
dsn: 'https://public@dsn.ingest.sentry.io/1337',
@@ -23,6 +24,7 @@ async function run() {
2324
},
2425
async () => {
2526
try {
27+
await waitForPostgres(sql);
2628
await sql`
2729
CREATE TABLE "User" ("id" SERIAL NOT NULL,"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,"email" TEXT NOT NULL,"name" TEXT,CONSTRAINT "User_pkey" PRIMARY KEY ("id"));
2830
`;

dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import * as Sentry from '@sentry/node';
2+
import { createRequire } from 'node:module';
23
import postgres from 'postgres';
34

5+
const require = createRequire(import.meta.url);
6+
const { waitForPostgres } = require('./wait-for-postgres.js');
7+
48
// Stop the process from exiting before the transaction is sent
59
setInterval(() => {}, 1000);
610

@@ -14,6 +18,7 @@ async function run() {
1418
},
1519
async () => {
1620
try {
21+
await waitForPostgres(sql);
1722
await sql`
1823
CREATE TABLE "User" ("id" SERIAL NOT NULL,"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,"email" TEXT NOT NULL,"name" TEXT,CONSTRAINT "User_pkey" PRIMARY KEY ("id"));
1924
`;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
/**
4+
* Retries until Postgres accepts connections. `docker compose up --wait` can report healthy
5+
* before the port forward on the host is ready (flaky on busy CI).
6+
*/
7+
async function waitForPostgres(sql, maxWaitMs = 60_000) {
8+
const deadline = Date.now() + maxWaitMs;
9+
for (;;) {
10+
try {
11+
await sql`SELECT 1`;
12+
return;
13+
} catch {
14+
if (Date.now() > deadline) {
15+
throw new Error('Timed out waiting for Postgres to accept connections');
16+
}
17+
await new Promise(r => setTimeout(r, 250));
18+
}
19+
}
20+
}
21+
22+
module.exports = { waitForPostgres };

0 commit comments

Comments
 (0)