Each Payload transaction holds a pool connection; code inside it that runs without its req needs a second one (Payload's own forgotPassword, the mail adapter's settings read and status write, payload.kv). With the default pool of 10, ten form submissions or ten "forgot password" requests at once held every connection and waited for good. - connectionReserve (default on): at most pool size − reserve transactions open at once; a waiter older than a second may use all but the last connection. Building blocks in modules/database/transactions.ts. - Form e-mails go out right after the submission's transaction commits, in the same request (forms.emailsInTransaction: true = the old way). - recordIntegrationStatus merges identical writes and does not hold its caller while the pool is busy. - SMTP time-outs: 10 s connect, 30 s greeting, 60 s silence (smtp.timeouts). Tests on Postgres 17: 30 submissions, 30 forgot-password, and both against an SMTP server that never answers, finish while a page answers. Co-Authored-By: Claude Opus 5.5 <[email protected]>
90 lines
3.5 KiB
TypeScript
90 lines
3.5 KiB
TypeScript
/** The site of test/transactionPool*.test.ts: a contact form, accounts, mailAdapter with short SMTP time-outs. */
|
|
import { lexicalEditor } from '@payloadcms/richtext-lexical'
|
|
import net from 'node:net'
|
|
import os from 'node:os'
|
|
import path from 'node:path'
|
|
import { getPayload } from 'payload'
|
|
|
|
import { mailAdapter } from '../src/modules/email/mailAdapter.js'
|
|
import { iapl } from '../src/plugin.js'
|
|
import { buildTestConfig } from './helpers.js'
|
|
import { testDatabase } from './http/db.js'
|
|
|
|
const text = (value: string) => ({
|
|
root: { type: 'root', children: [{ type: 'paragraph', children: [{ type: 'text', text: value, version: 1 }], direction: null, format: '', indent: 0, version: 1 }], direction: null, format: '', indent: 0, version: 1 },
|
|
})
|
|
|
|
/** An SMTP server that accepts the connection and never says a word. */
|
|
export async function silentServer(): Promise<{ close: () => Promise<void>; port: number }> {
|
|
const sockets = new Set<net.Socket>()
|
|
const server = net.createServer((socket) => {
|
|
sockets.add(socket)
|
|
socket.on('close', () => sockets.delete(socket))
|
|
})
|
|
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve))
|
|
return {
|
|
close: () => {
|
|
for (const s of sockets) {s.destroy()}
|
|
return new Promise((resolve) => server.close(() => resolve()))
|
|
},
|
|
port: (server.address() as net.AddressInfo).port,
|
|
}
|
|
}
|
|
|
|
export async function site(name: string, { emailsInTransaction = false } = {}) {
|
|
const payload = await getPayload({
|
|
config: buildTestConfig({
|
|
admin: { user: 'users' },
|
|
collections: [
|
|
{ slug: 'users', auth: true, fields: [] },
|
|
{ slug: 'media', fields: [], upload: { staticDir: path.join(os.tmpdir(), 'iapl-test-pool-media') } },
|
|
{ slug: 'pages', access: { read: () => true }, fields: [{ name: 'title', type: 'text' }] },
|
|
],
|
|
db: testDatabase(name),
|
|
editor: lexicalEditor(),
|
|
// Short SMTP time-outs: a server that hangs costs two seconds a send.
|
|
email: mailAdapter({ smtp: { timeouts: { connectionMs: 2000, greetingMs: 2000, socketMs: 2000 } } }),
|
|
plugins: [
|
|
iapl({
|
|
access: { authCollection: 'users' },
|
|
forms: {
|
|
emailsInTransaction,
|
|
formSubmissionOverrides: {
|
|
hooks: {
|
|
// a later hook that fails: the submission is rolled back
|
|
afterChange: [({ doc }) => {
|
|
if ((doc as { submissionData?: { value?: string }[] }).submissionData?.some((row) => row.value === 'roll-me-back')) {throw new Error('rolled back on purpose')}
|
|
}],
|
|
},
|
|
},
|
|
},
|
|
i18n: { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }] },
|
|
mail: { relay: false },
|
|
twoFactor: false,
|
|
}),
|
|
],
|
|
secret: 'transaction-pool-test-secret-0123456789',
|
|
serverURL: 'https://example.com',
|
|
telemetry: false,
|
|
}),
|
|
})
|
|
const form = await payload.create({
|
|
collection: 'forms' as never,
|
|
data: {
|
|
confirmationMessage: text('ok'),
|
|
confirmationType: 'message',
|
|
emails: [
|
|
{ emailTo: '[email protected]', message: text('{{message}}'), subject: 'New message' },
|
|
{ emailTo: '{{email}}', message: text('Thank you'), subject: 'Thank you' },
|
|
],
|
|
fields: [
|
|
{ name: 'email', blockType: 'email', label: 'E-mail', required: true },
|
|
{ name: 'message', blockType: 'text', label: 'Message', required: true },
|
|
],
|
|
title: 'Contact',
|
|
} as never,
|
|
})
|
|
return { formId: String(form.id), payload }
|
|
}
|
|
|