From 71e227764319e6fc732aefdebd11f3e844257ef6 Mon Sep 17 00:00:00 2001 From: Dries Augustyns Date: Fri, 22 May 2026 20:43:49 +0200 Subject: [PATCH] refactor(database): increase Prisma connection pool limits for improved test performance --- .../__tests__/SecurityService.test.ts | 35 ++++++++----------- test/helpers/database.ts | 21 ++++++----- 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/apps/api/src/services/__tests__/SecurityService.test.ts b/apps/api/src/services/__tests__/SecurityService.test.ts index 5a9b99b..559fcbe 100644 --- a/apps/api/src/services/__tests__/SecurityService.test.ts +++ b/apps/api/src/services/__tests__/SecurityService.test.ts @@ -50,27 +50,20 @@ describe('SecurityService', () => { const complainedCount = opts?.complainedCount ?? 0; const createdAt = opts?.createdAt ?? new Date(); - const emails = []; - for (let i = 0; i < count; i++) { - emails.push( - prisma.email.create({ - data: { - projectId, - contactId, - subject: `Test ${i}`, - body: '

test

', - from: 'test@example.com', - status: EmailStatus.SENT, - sourceType: EmailSourceType.TRANSACTIONAL, - sentAt: createdAt, - createdAt, - bouncedAt: i < bouncedCount ? createdAt : null, - complainedAt: i >= bouncedCount && i < bouncedCount + complainedCount ? createdAt : null, - }, - }), - ); - } - await Promise.all(emails); + const data = Array.from({length: count}, (_, i) => ({ + projectId, + contactId, + subject: `Test ${i}`, + body: '

test

', + from: 'test@example.com', + status: EmailStatus.SENT, + sourceType: EmailSourceType.TRANSACTIONAL, + sentAt: createdAt, + createdAt, + bouncedAt: i < bouncedCount ? createdAt : null, + complainedAt: i >= bouncedCount && i < bouncedCount + complainedCount ? createdAt : null, + })); + await prisma.email.createMany({data}); } describe('Rate-based checks (existing behavior)', () => { diff --git a/test/helpers/database.ts b/test/helpers/database.ts index 7f933cd..2360769 100644 --- a/test/helpers/database.ts +++ b/test/helpers/database.ts @@ -16,18 +16,21 @@ class TestDatabase { throw new Error('DATABASE_URL or TEST_DATABASE_URL must be set for testing'); } - // Create Prisma client with connection pool limits + // Raise Prisma's connection pool above its default (num_cpus * 2 + 1, ~5 on CI). + // Bulk inserts in some tests (e.g. SecurityService) saturate the default pool + // and time out. Test Postgres has max_connections=100, so 20 is well under budget. + const url = new URL(databaseUrl); + if (!url.searchParams.has('connection_limit')) { + url.searchParams.set('connection_limit', '20'); + } + if (!url.searchParams.has('pool_timeout')) { + url.searchParams.set('pool_timeout', '20'); + } + this.prisma = new PrismaClient({ datasources: { db: { - url: databaseUrl, - }, - }, - // Limit connection pool to prevent memory issues in tests - // @ts-ignore - These options exist but may not be in types - __internal: { - engine: { - connection_limit: 5, + url: url.toString(), }, }, });