refactor(database): increase Prisma connection pool limits for improved test performance

This commit is contained in:
Dries Augustyns
2026-05-22 20:43:49 +02:00
parent 079e1879b3
commit 71e2277643
2 changed files with 26 additions and 30 deletions
@@ -50,27 +50,20 @@ describe('SecurityService', () => {
const complainedCount = opts?.complainedCount ?? 0; const complainedCount = opts?.complainedCount ?? 0;
const createdAt = opts?.createdAt ?? new Date(); const createdAt = opts?.createdAt ?? new Date();
const emails = []; const data = Array.from({length: count}, (_, i) => ({
for (let i = 0; i < count; i++) { projectId,
emails.push( contactId,
prisma.email.create({ subject: `Test ${i}`,
data: { body: '<p>test</p>',
projectId, from: '[email protected]',
contactId, status: EmailStatus.SENT,
subject: `Test ${i}`, sourceType: EmailSourceType.TRANSACTIONAL,
body: '<p>test</p>', sentAt: createdAt,
from: '[email protected]', createdAt,
status: EmailStatus.SENT, bouncedAt: i < bouncedCount ? createdAt : null,
sourceType: EmailSourceType.TRANSACTIONAL, complainedAt: i >= bouncedCount && i < bouncedCount + complainedCount ? createdAt : null,
sentAt: createdAt, }));
createdAt, await prisma.email.createMany({data});
bouncedAt: i < bouncedCount ? createdAt : null,
complainedAt: i >= bouncedCount && i < bouncedCount + complainedCount ? createdAt : null,
},
}),
);
}
await Promise.all(emails);
} }
describe('Rate-based checks (existing behavior)', () => { describe('Rate-based checks (existing behavior)', () => {
+12 -9
View File
@@ -16,18 +16,21 @@ class TestDatabase {
throw new Error('DATABASE_URL or TEST_DATABASE_URL must be set for testing'); 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({ this.prisma = new PrismaClient({
datasources: { datasources: {
db: { db: {
url: databaseUrl, url: url.toString(),
},
},
// 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,
}, },
}, },
}); });