P-1: a transaction begun inside another one (a hook calling the Local API without req) no longer queues behind its caller. An AsyncLocalStorage mark set when a transaction begins lets the inner one through at once, without a place, but never into the last connection (a second level may take that one). If the mark is lost, a queue that has not moved for a second admits its newest waiter. connectionReserve: false now admits everything, as before. Measured on Postgres 17: 9, 12 and 30 such submissions finish (before: stuck from 9). P-2: Payload's "forgot password" and "verify your e-mail" through mailAdapter() go out right after their transaction commits (mail.accountMailAfterCommit, default on). With the relay hanging, 30 resets + 30 submissions finish in about 11 s instead of 43.5 s. P-3: Postgres tests for the "not confirmed" file result after commit, a caller's outer transaction rolled back or committed, the relay hanging, and the nested rows; deployment.md has the measured table. Co-Authored-By: Claude Opus 5.5 <[email protected]>
101 lines
4.3 KiB
TypeScript
101 lines
4.3 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' }] },
|
|
{ slug: 'log', fields: [{ name: 'note', 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')}
|
|
},
|
|
// a project's hook that writes through the Local API WITHOUT `req`: a transaction of its own,
|
|
// begun while the submission's is still open
|
|
async ({ doc, req }) => {
|
|
const rows = (doc as { id: number | string; submissionData?: { value?: string }[] }).submissionData ?? []
|
|
if (!rows.some((row) => row.value?.startsWith('nested-'))) {return}
|
|
await new Promise((resolve) => setTimeout(resolve, 50))
|
|
await req.payload.create({ collection: 'log' as never, data: { note: `submission ${doc.id}` } as never })
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
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 }
|
|
}
|
|
|