mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-30 02:58:11 +00:00
test(integration): boot freight API in-process across parallel shards
The suite drove a containerized freight API, so every code change needed an image rebuild before a test could see it, and there was no way to attach a debugger. Files also ran strictly in sequence against one shared database, which is the root of the warm-stack gotchas the README documents: stowaway paid bookings climbing back aboard, a short consist on the fifth file. The freight app now boots inside each vitest worker from dist/, and each worker owns a whole shard of the topology - its own database, payment API, gateway mock and broker vhost - so nothing mutable is shared and files run in parallel. Full suite drops from roughly 20 minutes to 196s at 4 shards. - main.ts exports createFreightApp() so the harness applies the same prefix, pipes, filters and interceptors as production instead of replaying them by hand; self-start is guarded by require.main so the Dockerfile CMD still boots - booking-window tick cadence is env-driven (BOOKING_WINDOW_TICK_CRON), */1 in the suite, */10 unchanged in production - prepare-shards.mjs seeds a template database (boot seeders, then the SQL fixtures that depend on them) and clones it per shard; it.mjs re-clones on every run, so each run is hermetic - gateway mock and payment API are generated per shard: the mock keeps modes and orders process-global and 20 of 25 specs reset it in beforeAll, and the inbound CBE bill query has to reach one specific shard's app - poll() samples every 250ms instead of 2000ms, keeping the caller's deadline - authz.it.ts seeds its own invoice; it previously read another spec's leftover and returned early, which silently passed on a pristine database Known: an unlocked MAX(sequence_no)+1 in train-scheduling.service.ts races under concurrent allocation and leaves a short consist, so 1-3 specs fail intermittently. Pre-existing and reproduces at the production tick cadence.
This commit is contained in:
@@ -1,41 +1,17 @@
|
||||
/**
|
||||
* Runs once before any spec: wait for both APIs, then seed.
|
||||
* Runs once, in its own process, before any spec: confirm every shard's
|
||||
* containerized half is answering, then clear its gateway mock.
|
||||
*
|
||||
* Seeds are the Cypress suite's fixtures, reused verbatim (they are idempotent
|
||||
* `insert … where not exists`), plus one of our own for the second tenant:
|
||||
* seed-users.sql → seed-company.sql (order matters; company needs the users)
|
||||
* seed-import-corridor.sql (yards, locos, wagons, rates, distances)
|
||||
* seed-bulk-items.sql (PER_ITEM break-bulk cargo types)
|
||||
* seed-g1-train.sql (the 53-wagon BUILT container train)
|
||||
* seed-g2-weight.sql (the two 3 500 T weight-bound trains)
|
||||
* seed-government.sql (the kind='government' company)
|
||||
* seed-company-b.sql (user2@gmail.com's company — this suite)
|
||||
* seed-customs-service-type.sql (a service type that bundles customs)
|
||||
* Seeding is NOT here any more. The order is load-bearing —
|
||||
* `seed-users.sql` names its prerequisites as "created by the API's always-on
|
||||
* boot seeders" (iam.organizations / units / positions) — and the freight app
|
||||
* now boots inside the vitest *workers*, which this process cannot reach. So the
|
||||
* template database is seeded once by `scripts/prepare-shards.mjs` (boot the app
|
||||
* → boot seeders → SQL fixtures) and each shard is a clone of it.
|
||||
*/
|
||||
import { readFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { Client } from "pg";
|
||||
|
||||
const API = process.env.IT_API_URL ?? "http://localhost:3111";
|
||||
const PAYMENT_API = process.env.IT_PAYMENT_URL ?? "http://localhost:3113";
|
||||
const GATEWAY = process.env.IT_GATEWAY_URL ?? "http://localhost:4600";
|
||||
const DB_URL =
|
||||
process.env.IT_DB_URL ?? "postgres://edr_e2e:edr_e2e@localhost:5543/edr_freight_e2e";
|
||||
|
||||
const CYPRESS_FIXTURES = join(process.cwd(), "..", "e2e", "freight", "cypress", "fixtures");
|
||||
const OWN_FIXTURES = join(process.cwd(), "sql");
|
||||
|
||||
const SEEDS: Array<[dir: string, file: string]> = [
|
||||
[CYPRESS_FIXTURES, "seed-users.sql"],
|
||||
[CYPRESS_FIXTURES, "seed-company.sql"],
|
||||
[CYPRESS_FIXTURES, "seed-import-corridor.sql"],
|
||||
[CYPRESS_FIXTURES, "seed-bulk-items.sql"],
|
||||
[CYPRESS_FIXTURES, "seed-g1-train.sql"],
|
||||
[CYPRESS_FIXTURES, "seed-g2-weight.sql"],
|
||||
[CYPRESS_FIXTURES, "seed-government.sql"],
|
||||
[OWN_FIXTURES, "seed-company-b.sql"],
|
||||
[OWN_FIXTURES, "seed-customs-service-type.sql"],
|
||||
];
|
||||
const SHARDS = Number(process.env.IT_SHARDS ?? 1);
|
||||
const PAYMENT_BASE = Number(process.env.IT_PAYMENT_PORT_BASE ?? 3131);
|
||||
const GATEWAY_BASE = Number(process.env.IT_GATEWAY_PORT_BASE ?? 4600);
|
||||
|
||||
async function waitFor(label: string, url: string, attempts = 60): Promise<void> {
|
||||
for (let i = 0; i < attempts; i++) {
|
||||
@@ -51,22 +27,20 @@ async function waitFor(label: string, url: string, attempts = 60): Promise<void>
|
||||
}
|
||||
|
||||
export async function setup(): Promise<void> {
|
||||
await Promise.all([
|
||||
waitFor("freight-api", `${API}/api/health`),
|
||||
waitFor("payment-api", `${PAYMENT_API}/health`),
|
||||
waitFor("gateway-mock", `${GATEWAY}/__control/health`),
|
||||
]);
|
||||
const shards = Array.from({ length: SHARDS }, (_, i) => i);
|
||||
|
||||
const client = new Client({ connectionString: DB_URL });
|
||||
await client.connect();
|
||||
try {
|
||||
for (const [dir, file] of SEEDS) {
|
||||
await client.query(readFileSync(join(dir, file), "utf8"));
|
||||
console.log(`it: seeded ${file}`);
|
||||
}
|
||||
} finally {
|
||||
await client.end();
|
||||
}
|
||||
await Promise.all(
|
||||
shards.flatMap((i) => [
|
||||
waitFor(`payment-api (shard ${i})`, `http://localhost:${PAYMENT_BASE + i}/health`),
|
||||
waitFor(`gateway-mock (shard ${i})`, `http://localhost:${GATEWAY_BASE + i}/__control/health`),
|
||||
]),
|
||||
);
|
||||
|
||||
await fetch(`${GATEWAY}/__control/reset`, { method: "POST" });
|
||||
await Promise.all(
|
||||
shards.map((i) =>
|
||||
fetch(`http://localhost:${GATEWAY_BASE + i}/__control/reset`, { method: "POST" }),
|
||||
),
|
||||
);
|
||||
|
||||
console.log(`it: ${SHARDS} shard(s) ready`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user