@dads-framework/postgres 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +21 -0
- package/README.md +97 -2
- package/dist/drizzle.d.ts +9 -0
- package/dist/drizzle.d.ts.map +1 -0
- package/dist/drizzle.js +2 -0
- package/dist/drizzle.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/attributed-pool.d.ts +95 -0
- package/dist/lib/attributed-pool.d.ts.map +1 -0
- package/dist/lib/attributed-pool.js +128 -0
- package/dist/lib/attributed-pool.js.map +1 -0
- package/dist/lib/bytea-column.d.ts +37 -0
- package/dist/lib/bytea-column.d.ts.map +1 -0
- package/dist/lib/bytea-column.js +20 -0
- package/dist/lib/bytea-column.js.map +1 -0
- package/dist/lib/checked-out-client-errors.d.ts +22 -0
- package/dist/lib/checked-out-client-errors.d.ts.map +1 -0
- package/dist/lib/checked-out-client-errors.js +28 -0
- package/dist/lib/checked-out-client-errors.js.map +1 -0
- package/dist/lib/connect-postgres-client.d.ts +16 -0
- package/dist/lib/connect-postgres-client.d.ts.map +1 -0
- package/dist/lib/connect-postgres-client.js +14 -0
- package/dist/lib/connect-postgres-client.js.map +1 -0
- package/dist/lib/counting-client.d.ts +39 -0
- package/dist/lib/counting-client.d.ts.map +1 -0
- package/dist/lib/counting-client.js +102 -0
- package/dist/lib/counting-client.js.map +1 -0
- package/dist/lib/create-postgres-connection.d.ts +65 -0
- package/dist/lib/create-postgres-connection.d.ts.map +1 -0
- package/dist/lib/create-postgres-connection.js +68 -0
- package/dist/lib/create-postgres-connection.js.map +1 -0
- package/dist/lib/event-actor-columns.d.ts +49 -0
- package/dist/lib/event-actor-columns.d.ts.map +1 -0
- package/dist/lib/event-actor-columns.js +37 -0
- package/dist/lib/event-actor-columns.js.map +1 -0
- package/dist/lib/inet-column.d.ts +37 -0
- package/dist/lib/inet-column.d.ts.map +1 -0
- package/dist/lib/inet-column.js +17 -0
- package/dist/lib/inet-column.js.map +1 -0
- package/dist/lib/load-page-and-total.d.ts +16 -0
- package/dist/lib/load-page-and-total.d.ts.map +1 -0
- package/dist/lib/load-page-and-total.js +16 -0
- package/dist/lib/load-page-and-total.js.map +1 -0
- package/dist/lib/postgres-pool-laws.d.ts +6 -0
- package/dist/lib/postgres-pool-laws.d.ts.map +1 -0
- package/dist/lib/postgres-pool-laws.js +58 -0
- package/dist/lib/postgres-pool-laws.js.map +1 -0
- package/dist/lib/synced-rows.d.ts +97 -0
- package/dist/lib/synced-rows.d.ts.map +1 -0
- package/dist/lib/synced-rows.js +158 -0
- package/dist/lib/synced-rows.js.map +1 -0
- package/dist/lib/unique-violation.d.ts +10 -0
- package/dist/lib/unique-violation.d.ts.map +1 -0
- package/dist/lib/unique-violation.js +24 -0
- package/dist/lib/unique-violation.js.map +1 -0
- package/dist/lib/update-at-row-version.d.ts +31 -0
- package/dist/lib/update-at-row-version.d.ts.map +1 -0
- package/dist/lib/update-at-row-version.js +14 -0
- package/dist/lib/update-at-row-version.js.map +1 -0
- package/dist/lib/xid8-column.d.ts +38 -0
- package/dist/lib/xid8-column.d.ts.map +1 -0
- package/dist/lib/xid8-column.js +18 -0
- package/dist/lib/xid8-column.js.map +1 -0
- package/dist/postgres.tsbuildinfo +1 -0
- package/dist/testing/create-test-pool.d.ts +35 -0
- package/dist/testing/create-test-pool.d.ts.map +1 -0
- package/dist/testing/create-test-pool.js +47 -0
- package/dist/testing/create-test-pool.js.map +1 -0
- package/dist/testing/deterministic-uuid.d.ts +2 -0
- package/dist/testing/deterministic-uuid.d.ts.map +1 -0
- package/dist/testing/deterministic-uuid.js +20 -0
- package/dist/testing/deterministic-uuid.js.map +1 -0
- package/dist/testing/global-setup/container-tier-semaphore.d.ts +51 -0
- package/dist/testing/global-setup/container-tier-semaphore.d.ts.map +1 -0
- package/dist/testing/global-setup/container-tier-semaphore.js +129 -0
- package/dist/testing/global-setup/container-tier-semaphore.js.map +1 -0
- package/dist/testing/global-setup/decide-shared-test-postgres.d.ts +51 -0
- package/dist/testing/global-setup/decide-shared-test-postgres.d.ts.map +1 -0
- package/dist/testing/global-setup/decide-shared-test-postgres.js +109 -0
- package/dist/testing/global-setup/decide-shared-test-postgres.js.map +1 -0
- package/dist/testing/global-setup/index.d.ts +16 -0
- package/dist/testing/global-setup/index.d.ts.map +1 -0
- package/dist/testing/global-setup/index.js +151 -0
- package/dist/testing/global-setup/index.js.map +1 -0
- package/dist/testing/global-setup/refuse-disabled-container-cleanup.d.ts +34 -0
- package/dist/testing/global-setup/refuse-disabled-container-cleanup.d.ts.map +1 -0
- package/dist/testing/global-setup/refuse-disabled-container-cleanup.js +45 -0
- package/dist/testing/global-setup/refuse-disabled-container-cleanup.js.map +1 -0
- package/dist/testing/global-setup/refuse-low-memory-runs.d.ts +176 -0
- package/dist/testing/global-setup/refuse-low-memory-runs.d.ts.map +1 -0
- package/dist/testing/global-setup/refuse-low-memory-runs.js +388 -0
- package/dist/testing/global-setup/refuse-low-memory-runs.js.map +1 -0
- package/dist/testing/global-setup/refuse-slow-local-test-mode.d.ts +5 -0
- package/dist/testing/global-setup/refuse-slow-local-test-mode.d.ts.map +1 -0
- package/dist/testing/global-setup/refuse-slow-local-test-mode.js +18 -0
- package/dist/testing/global-setup/refuse-slow-local-test-mode.js.map +1 -0
- package/dist/testing/global-setup/refuse-stacked-container-runs.d.ts +106 -0
- package/dist/testing/global-setup/refuse-stacked-container-runs.d.ts.map +1 -0
- package/dist/testing/global-setup/refuse-stacked-container-runs.js +284 -0
- package/dist/testing/global-setup/refuse-stacked-container-runs.js.map +1 -0
- package/dist/testing/global-setup/remove-stale-testcontainers.d.ts +33 -0
- package/dist/testing/global-setup/remove-stale-testcontainers.d.ts.map +1 -0
- package/dist/testing/global-setup/remove-stale-testcontainers.js +165 -0
- package/dist/testing/global-setup/remove-stale-testcontainers.js.map +1 -0
- package/dist/testing/global-setup/retired-database-touches.d.ts +16 -0
- package/dist/testing/global-setup/retired-database-touches.d.ts.map +1 -0
- package/dist/testing/global-setup/retired-database-touches.js +73 -0
- package/dist/testing/global-setup/retired-database-touches.js.map +1 -0
- package/dist/testing/global-setup/schema-keyed-coverage.d.ts +56 -0
- package/dist/testing/global-setup/schema-keyed-coverage.d.ts.map +1 -0
- package/dist/testing/global-setup/schema-keyed-coverage.js +84 -0
- package/dist/testing/global-setup/schema-keyed-coverage.js.map +1 -0
- package/dist/testing/global-setup/skip-unless-container-tests.d.ts +31 -0
- package/dist/testing/global-setup/skip-unless-container-tests.d.ts.map +1 -0
- package/dist/testing/global-setup/skip-unless-container-tests.js +33 -0
- package/dist/testing/global-setup/skip-unless-container-tests.js.map +1 -0
- package/dist/testing/global-setup/start-migrated-template-postgres.d.ts +20 -0
- package/dist/testing/global-setup/start-migrated-template-postgres.d.ts.map +1 -0
- package/dist/testing/global-setup/start-migrated-template-postgres.js +53 -0
- package/dist/testing/global-setup/start-migrated-template-postgres.js.map +1 -0
- package/dist/testing/global-setup/verify-schema-keyed-suite.d.ts +43 -0
- package/dist/testing/global-setup/verify-schema-keyed-suite.d.ts.map +1 -0
- package/dist/testing/global-setup/verify-schema-keyed-suite.js +201 -0
- package/dist/testing/global-setup/verify-schema-keyed-suite.js.map +1 -0
- package/dist/testing/global-setup/worktree-identity.d.ts +21 -0
- package/dist/testing/global-setup/worktree-identity.d.ts.map +1 -0
- package/dist/testing/global-setup/worktree-identity.js +30 -0
- package/dist/testing/global-setup/worktree-identity.js.map +1 -0
- package/dist/testing/index.d.ts +11 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +11 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/rehearsal-postgres.d.ts +20 -0
- package/dist/testing/rehearsal-postgres.d.ts.map +1 -0
- package/dist/testing/rehearsal-postgres.js +61 -0
- package/dist/testing/rehearsal-postgres.js.map +1 -0
- package/dist/testing/start-test-postgres.d.ts +59 -0
- package/dist/testing/start-test-postgres.d.ts.map +1 -0
- package/dist/testing/start-test-postgres.js +241 -0
- package/dist/testing/start-test-postgres.js.map +1 -0
- package/dist/testing/test-postgres-storage.d.ts +44 -0
- package/dist/testing/test-postgres-storage.d.ts.map +1 -0
- package/dist/testing/test-postgres-storage.js +88 -0
- package/dist/testing/test-postgres-storage.js.map +1 -0
- package/dist/testing/truncate-tables.d.ts +31 -0
- package/dist/testing/truncate-tables.d.ts.map +1 -0
- package/dist/testing/truncate-tables.js +39 -0
- package/dist/testing/truncate-tables.js.map +1 -0
- package/dist/testing/violated-foreign-key.d.ts +14 -0
- package/dist/testing/violated-foreign-key.d.ts.map +1 -0
- package/dist/testing/violated-foreign-key.js +78 -0
- package/dist/testing/violated-foreign-key.js.map +1 -0
- package/dist/testing/with-database.d.ts +2 -0
- package/dist/testing/with-database.d.ts.map +1 -0
- package/dist/testing/with-database.js +6 -0
- package/dist/testing/with-database.js.map +1 -0
- package/package.json +88 -4
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Refuse a local run that switched off Testcontainers cleanup (Ryuk).
|
|
3
|
+
*
|
|
4
|
+
* Testcontainers ships Ryuk: a sidecar container that holds a socket open to
|
|
5
|
+
* this process and removes the run's containers when that socket dies. It is
|
|
6
|
+
* the ONLY cleanup that survives `kill -9`, because a killed process runs no
|
|
7
|
+
* `afterAll` and no exit handler. It is on by default, and
|
|
8
|
+
* `TESTCONTAINERS_RYUK_DISABLED=true` is the single switch that turns it off —
|
|
9
|
+
* which is exactly the switch a developer reaches for when Docker feels slow,
|
|
10
|
+
* trading a second of reaper boot for containers that outlive every run on the
|
|
11
|
+
* machine.
|
|
12
|
+
*
|
|
13
|
+
* So the switch is refused for local runs. CI may set it: a runner is a fresh
|
|
14
|
+
* VM destroyed when the job ends, so its containers die with the box whatever
|
|
15
|
+
* the reaper does.
|
|
16
|
+
*
|
|
17
|
+
* Ryuk being on is necessary and NOT sufficient — measured on 2026-08-02, a
|
|
18
|
+
* SIGKILLed client's container was still running 60s later with a healthy
|
|
19
|
+
* reaper attached, because `getReaper()` REUSES the newest running reaper and
|
|
20
|
+
* adopts its session id, so every test process on the box shares one reaper
|
|
21
|
+
* and one death pact. That pact fires when the LAST client disconnects, not
|
|
22
|
+
* when one dies. `remove-stale-testcontainers.ts` is the half that does not
|
|
23
|
+
* depend on anyone else's process exiting.
|
|
24
|
+
*/
|
|
25
|
+
/** Testcontainers' own switch for the Ryuk reaper. */
|
|
26
|
+
export declare const TESTCONTAINERS_RYUK_DISABLED = "TESTCONTAINERS_RYUK_DISABLED";
|
|
27
|
+
/**
|
|
28
|
+
* Refuse a local run whose container cleanup was switched off.
|
|
29
|
+
*
|
|
30
|
+
* Reads the switch exactly as testcontainers reads it, so this wall and the
|
|
31
|
+
* library can never disagree about whether the reaper is running.
|
|
32
|
+
*/
|
|
33
|
+
export declare function refuseDisabledContainerCleanup(env: Readonly<Record<string, string | undefined>>): void;
|
|
34
|
+
//# sourceMappingURL=refuse-disabled-container-cleanup.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"refuse-disabled-container-cleanup.d.ts","sourceRoot":"","sources":["../../../src/testing/global-setup/refuse-disabled-container-cleanup.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,sDAAsD;AACtD,eAAO,MAAM,4BAA4B,iCAAiC,CAAC;AAK3E;;;;;GAKG;AACH,wBAAgB,8BAA8B,CAC5C,GAAG,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,GAChD,IAAI,CAUN"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Refuse a local run that switched off Testcontainers cleanup (Ryuk).
|
|
3
|
+
*
|
|
4
|
+
* Testcontainers ships Ryuk: a sidecar container that holds a socket open to
|
|
5
|
+
* this process and removes the run's containers when that socket dies. It is
|
|
6
|
+
* the ONLY cleanup that survives `kill -9`, because a killed process runs no
|
|
7
|
+
* `afterAll` and no exit handler. It is on by default, and
|
|
8
|
+
* `TESTCONTAINERS_RYUK_DISABLED=true` is the single switch that turns it off —
|
|
9
|
+
* which is exactly the switch a developer reaches for when Docker feels slow,
|
|
10
|
+
* trading a second of reaper boot for containers that outlive every run on the
|
|
11
|
+
* machine.
|
|
12
|
+
*
|
|
13
|
+
* So the switch is refused for local runs. CI may set it: a runner is a fresh
|
|
14
|
+
* VM destroyed when the job ends, so its containers die with the box whatever
|
|
15
|
+
* the reaper does.
|
|
16
|
+
*
|
|
17
|
+
* Ryuk being on is necessary and NOT sufficient — measured on 2026-08-02, a
|
|
18
|
+
* SIGKILLed client's container was still running 60s later with a healthy
|
|
19
|
+
* reaper attached, because `getReaper()` REUSES the newest running reaper and
|
|
20
|
+
* adopts its session id, so every test process on the box shares one reaper
|
|
21
|
+
* and one death pact. That pact fires when the LAST client disconnects, not
|
|
22
|
+
* when one dies. `remove-stale-testcontainers.ts` is the half that does not
|
|
23
|
+
* depend on anyone else's process exiting.
|
|
24
|
+
*/
|
|
25
|
+
/** Testcontainers' own switch for the Ryuk reaper. */
|
|
26
|
+
export const TESTCONTAINERS_RYUK_DISABLED = "TESTCONTAINERS_RYUK_DISABLED";
|
|
27
|
+
/** The value testcontainers compares against — an exact string, not truthiness. */
|
|
28
|
+
const DISABLED = "true";
|
|
29
|
+
/**
|
|
30
|
+
* Refuse a local run whose container cleanup was switched off.
|
|
31
|
+
*
|
|
32
|
+
* Reads the switch exactly as testcontainers reads it, so this wall and the
|
|
33
|
+
* library can never disagree about whether the reaper is running.
|
|
34
|
+
*/
|
|
35
|
+
export function refuseDisabledContainerCleanup(env) {
|
|
36
|
+
if (env[TESTCONTAINERS_RYUK_DISABLED] !== DISABLED)
|
|
37
|
+
return;
|
|
38
|
+
if (env["CI"] !== undefined)
|
|
39
|
+
return;
|
|
40
|
+
throw new Error(`${TESTCONTAINERS_RYUK_DISABLED}=${DISABLED} is refused for local runs. Ryuk is the ` +
|
|
41
|
+
`only cleanup that survives a killed test process; without it this run's ` +
|
|
42
|
+
`containers outlive it and stay on the machine forever. CI sets this ` +
|
|
43
|
+
`variable freely — its runners are destroyed with their containers.`);
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=refuse-disabled-container-cleanup.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"refuse-disabled-container-cleanup.js","sourceRoot":"","sources":["../../../src/testing/global-setup/refuse-disabled-container-cleanup.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,sDAAsD;AACtD,MAAM,CAAC,MAAM,4BAA4B,GAAG,8BAA8B,CAAC;AAE3E,mFAAmF;AACnF,MAAM,QAAQ,GAAG,MAAM,CAAC;AAExB;;;;;GAKG;AACH,MAAM,UAAU,8BAA8B,CAC5C,GAAiD;IAEjD,IAAI,GAAG,CAAC,4BAA4B,CAAC,KAAK,QAAQ;QAAE,OAAO;IAC3D,IAAI,GAAG,CAAC,IAAI,CAAC,KAAK,SAAS;QAAE,OAAO;IAEpC,MAAM,IAAI,KAAK,CACb,GAAG,4BAA4B,IAAI,QAAQ,0CAA0C;QACnF,0EAA0E;QAC1E,sEAAsE;QACtE,oEAAoE,CACvE,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { type TestPostgresStorage } from "../test-postgres-storage.js";
|
|
2
|
+
/** Free host memory a run needs before it starts, in GB. `0` skips the check. */
|
|
3
|
+
export declare const MIN_FREE_MEMORY_GB = "DADS_TEST_MIN_FREE_MEMORY_GB";
|
|
4
|
+
/** Free memory inside Docker's VM a run needs, in GB. `0` skips the check. */
|
|
5
|
+
export declare const MIN_FREE_DOCKER_MEMORY_GB = "DADS_TEST_MIN_FREE_DOCKER_MEMORY_GB";
|
|
6
|
+
/** How long a run waits for memory before refusing. `0` refuses at once. */
|
|
7
|
+
export declare const MEMORY_WAIT_MS = "DADS_TEST_MEMORY_WAIT_MS";
|
|
8
|
+
/**
|
|
9
|
+
* `TEST_POSTGRES_STORAGE=ram` puts a 4 GB tmpfs inside Docker. It needs that
|
|
10
|
+
* much plus room for Postgres, SpiceDB and the rest of what Docker runs.
|
|
11
|
+
*/
|
|
12
|
+
export declare const RAM_STORAGE_MIN_DOCKER_MEMORY_GB = 8;
|
|
13
|
+
export type DockerMemoryReading = {
|
|
14
|
+
/** All the memory Docker's containers can use: the VM's size on a Mac. */
|
|
15
|
+
readonly totalBytes: number;
|
|
16
|
+
/**
|
|
17
|
+
* True when Docker runs in its own VM (Docker Desktop, OrbStack, colima), so
|
|
18
|
+
* the host's free memory says nothing about room inside Docker.
|
|
19
|
+
*/
|
|
20
|
+
readonly inVm: boolean;
|
|
21
|
+
/** VM size minus what running containers hold; `null` when not in a VM. */
|
|
22
|
+
readonly freeBytes: number | null;
|
|
23
|
+
};
|
|
24
|
+
export type MemoryReading = {
|
|
25
|
+
readonly hostAvailableBytes: number;
|
|
26
|
+
/** Which measurement `hostAvailableBytes` came from, for the message. */
|
|
27
|
+
readonly hostSource: "MemAvailable" | "vm_stat" | "os.freemem";
|
|
28
|
+
/** `null` when Docker could not be reached in time. */
|
|
29
|
+
readonly docker: DockerMemoryReading | null;
|
|
30
|
+
};
|
|
31
|
+
export type MemoryThresholds = {
|
|
32
|
+
readonly hostMinFreeBytes: number;
|
|
33
|
+
readonly dockerMinFreeBytes: number;
|
|
34
|
+
readonly waitBudgetMs: number;
|
|
35
|
+
};
|
|
36
|
+
/** Linux: `MemAvailable` — free memory plus cache the kernel can reclaim. */
|
|
37
|
+
export declare function parseLinuxMemAvailable(meminfo: string): number | null;
|
|
38
|
+
/**
|
|
39
|
+
* macOS: free + inactive + speculative + purgeable pages, times the page size
|
|
40
|
+
* `vm_stat` prints in its header.
|
|
41
|
+
*
|
|
42
|
+
* `os.freemem()` is only the "Pages free" line — on a 48 GB Mac it read under
|
|
43
|
+
* about 1 GB while 14 GB was reclaimable cache. `memory_pressure`'s "free
|
|
44
|
+
* percentage" errs the other way: it counts compressible memory as free and
|
|
45
|
+
* read 27 GB on the same Mac while 18 GB sat in the compressor. Inactive pages
|
|
46
|
+
* are the ones the kernel reclaims first, which is the honest middle and what
|
|
47
|
+
* Activity Monitor's own figures are built from.
|
|
48
|
+
*/
|
|
49
|
+
export declare function parseMacAvailableMemory(vmStat: string): number | null;
|
|
50
|
+
/**
|
|
51
|
+
* What one container holds that the VM cannot simply take back: its usage
|
|
52
|
+
* minus inactive page cache, the same figure `docker stats` shows.
|
|
53
|
+
*/
|
|
54
|
+
export declare function containerWorkingSetBytes(memory: {
|
|
55
|
+
readonly usage?: number;
|
|
56
|
+
readonly inactiveFile?: number;
|
|
57
|
+
}): number;
|
|
58
|
+
/**
|
|
59
|
+
* Docker runs in a VM everywhere but Linux, and on Linux under Docker Desktop.
|
|
60
|
+
* Natively on Linux its containers share the host's memory, which the host
|
|
61
|
+
* reading already covers.
|
|
62
|
+
*/
|
|
63
|
+
export declare function dockerRunsInVm(hostPlatform: string, operatingSystem: string): boolean;
|
|
64
|
+
export declare function memoryThresholds(env: Readonly<Record<string, string | undefined>>): MemoryThresholds;
|
|
65
|
+
/** `5.8 GB` — one decimal is the precision anyone acts on. */
|
|
66
|
+
export declare function describeGb(bytes: number): string;
|
|
67
|
+
export type MemoryPreflightDecision = {
|
|
68
|
+
readonly action: "go";
|
|
69
|
+
readonly message: null;
|
|
70
|
+
} | {
|
|
71
|
+
readonly action: "wait";
|
|
72
|
+
readonly message: string;
|
|
73
|
+
} | {
|
|
74
|
+
readonly action: "abort";
|
|
75
|
+
readonly message: string;
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* The whole decision, with no measuring in it: go when every floor is met,
|
|
79
|
+
* wait while the budget lasts, then refuse with what is short and how to
|
|
80
|
+
* override it.
|
|
81
|
+
*/
|
|
82
|
+
export declare function decideMemoryPreflight(input: {
|
|
83
|
+
readonly reading: MemoryReading;
|
|
84
|
+
readonly thresholds: MemoryThresholds;
|
|
85
|
+
readonly tier: string;
|
|
86
|
+
readonly waitedMs: number;
|
|
87
|
+
}): MemoryPreflightDecision;
|
|
88
|
+
export type RamStorageDecision = {
|
|
89
|
+
readonly storage: TestPostgresStorage;
|
|
90
|
+
readonly message: string | null;
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* `ram` needs Docker to have room for the 4 GB tmpfs plus headroom. When it
|
|
94
|
+
* does not — a Mac's default 6 GB Docker VM — the run uses disk and says why,
|
|
95
|
+
* instead of letting the tmpfs push the VM's other containers out of memory.
|
|
96
|
+
*/
|
|
97
|
+
export declare function decideRamStorageFallback(input: {
|
|
98
|
+
readonly storage: TestPostgresStorage;
|
|
99
|
+
readonly docker: DockerMemoryReading | null;
|
|
100
|
+
}): RamStorageDecision;
|
|
101
|
+
/**
|
|
102
|
+
* How many workers fit in what is free: everything above `reserveBytes`,
|
|
103
|
+
* divided by what one worker was measured to need. Never below one.
|
|
104
|
+
*/
|
|
105
|
+
export declare function memoryWorkerLimit(input: {
|
|
106
|
+
readonly availableBytes: number;
|
|
107
|
+
readonly reserveBytes: number;
|
|
108
|
+
readonly perWorkerBytes: number;
|
|
109
|
+
}): number;
|
|
110
|
+
/** One reading of host and Docker memory; the Docker half may be `null`. */
|
|
111
|
+
export declare function readTestMemory(): Promise<MemoryReading>;
|
|
112
|
+
/** Thrown instead of booting, so the refusal is loud and immediate. */
|
|
113
|
+
export declare class NotEnoughTestMemoryError extends Error {
|
|
114
|
+
constructor(message: string);
|
|
115
|
+
}
|
|
116
|
+
export type TestMemoryDeps = {
|
|
117
|
+
readonly read: () => Promise<MemoryReading>;
|
|
118
|
+
readonly sleep: (ms: number) => Promise<void>;
|
|
119
|
+
readonly log: (line: string) => void;
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* Waits until the machine has the memory a `tier` run needs, and throws
|
|
123
|
+
* `NotEnoughTestMemoryError` once the wait budget is spent. Returns the
|
|
124
|
+
* reading that let it through.
|
|
125
|
+
*/
|
|
126
|
+
export declare function awaitEnoughTestMemory(tier: string, options?: {
|
|
127
|
+
readonly thresholds?: MemoryThresholds;
|
|
128
|
+
readonly deps?: Partial<TestMemoryDeps>;
|
|
129
|
+
}): Promise<MemoryReading>;
|
|
130
|
+
/**
|
|
131
|
+
* Turns `TEST_POSTGRES_STORAGE=ram` into `disk` in `env` when Docker is too
|
|
132
|
+
* small for it, before any container starts, so every child and worker that
|
|
133
|
+
* inherits `env` makes the same choice. Reads Docker only when `ram` is asked
|
|
134
|
+
* for. Returns the storage the run will use.
|
|
135
|
+
*/
|
|
136
|
+
export declare function applyRamStorageFallback(env: Record<string, string | undefined>, log?: (line: string) => void): Promise<TestPostgresStorage>;
|
|
137
|
+
/**
|
|
138
|
+
* Peak memory one Vitest worker thread of an integration project adds,
|
|
139
|
+
* measured 2026-11-18 on the test VM: `domain-facilities` (39 files) peaked at
|
|
140
|
+
* 0.9, 1.2, 1.7 and 2.7 GB with 1, 2, 4 and 8 workers — about 0.25 GB a
|
|
141
|
+
* worker, rounded up.
|
|
142
|
+
*/
|
|
143
|
+
export declare const INTEGRATION_WORKER_MEMORY_BYTES: number;
|
|
144
|
+
export type VitestWorkerCap = {
|
|
145
|
+
/** The worker count to use, or `null` to leave Vitest's own. */
|
|
146
|
+
readonly workers: number | null;
|
|
147
|
+
readonly message: string | null;
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
150
|
+
* Vitest's default is one worker per core but one. On the 32-core test VM
|
|
151
|
+
* that is 31 workers per project, and three projects at once (Nx's default
|
|
152
|
+
* parallelism) outgrow 25 GB. This lowers — never raises — a container-tier
|
|
153
|
+
* project's workers to what fits in free memory above the floor. It leaves
|
|
154
|
+
* alone a count someone chose (`VITEST_MAX_WORKERS`, which the integration
|
|
155
|
+
* gate always sets after its own memory-aware plan) and CI.
|
|
156
|
+
*/
|
|
157
|
+
export declare function decideVitestWorkerCap(input: {
|
|
158
|
+
readonly env: Readonly<Record<string, string | undefined>>;
|
|
159
|
+
readonly configuredWorkers: number | undefined;
|
|
160
|
+
readonly cores: number;
|
|
161
|
+
readonly availableBytes: number;
|
|
162
|
+
readonly reserveBytes: number;
|
|
163
|
+
}): VitestWorkerCap;
|
|
164
|
+
/** The slice of Vitest's `TestProject` the cap reads and writes. */
|
|
165
|
+
export type VitestProjectWorkers = {
|
|
166
|
+
readonly config: {
|
|
167
|
+
maxWorkers?: number;
|
|
168
|
+
};
|
|
169
|
+
};
|
|
170
|
+
/**
|
|
171
|
+
* Applies `decideVitestWorkerCap` to the project a `globalSetup` was handed.
|
|
172
|
+
* Vitest reads `config.maxWorkers` when it schedules files, after every
|
|
173
|
+
* `globalSetup` has run, so the lowered count takes effect for this run.
|
|
174
|
+
*/
|
|
175
|
+
export declare function capVitestWorkersToFreeMemory(project: VitestProjectWorkers, cores: number, log?: (line: string) => void): Promise<void>;
|
|
176
|
+
//# sourceMappingURL=refuse-low-memory-runs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"refuse-low-memory-runs.d.ts","sourceRoot":"","sources":["../../../src/testing/global-setup/refuse-low-memory-runs.ts"],"names":[],"mappings":"AAKA,OAAO,EAGL,KAAK,mBAAmB,EACzB,MAAM,6BAA6B,CAAC;AAwBrC,iFAAiF;AACjF,eAAO,MAAM,kBAAkB,iCAAiC,CAAC;AACjE,8EAA8E;AAC9E,eAAO,MAAM,yBAAyB,wCAAwC,CAAC;AAC/E,4EAA4E;AAC5E,eAAO,MAAM,cAAc,6BAA6B,CAAC;AAezD;;;GAGG;AACH,eAAO,MAAM,gCAAgC,IAAI,CAAC;AAElD,MAAM,MAAM,mBAAmB,GAAG;IAChC,0EAA0E;IAC1E,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B;;;OAGG;IACH,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IACvB,2EAA2E;IAC3E,QAAQ,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAA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AY,EAAE,MAAM,CAAC;CAC/B,GAAG,eAAe,CAsBlB;AAED,oEAAoE;AACpE,MAAM,MAAM,oBAAoB,GAAG;IACjC,QAAQ,CAAC,MAAM,EAAE;QAAE,UAAU,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CAC1C,CAAC;AAEF;;;;GAIG;AACH,wBAAsB,4BAA4B,CAChD,OAAO,EAAE,oBAAoB,EAC7B,KAAK,EAAE,MAAM,EACb,GAAG,GAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAmB,GACzC,OAAO,CAAC,IAAI,CAAC,CAYf"}
|
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { freemem, platform } from "node:os";
|
|
4
|
+
import { promisify } from "node:util";
|
|
5
|
+
import { getContainerRuntimeClient } from "testcontainers";
|
|
6
|
+
import { TEST_POSTGRES_STORAGE, testPostgresStorage, } from "../test-postgres-storage.js";
|
|
7
|
+
/**
|
|
8
|
+
* A memory floor under container-tier runs. The wall against a test run
|
|
9
|
+
* taking the developer's machine down.
|
|
10
|
+
*
|
|
11
|
+
* WHY THIS EXISTS. The slot semaphore (refuse-stacked-container-runs.ts)
|
|
12
|
+
* bounds HOW MANY runs share a machine, and the gate sizes workers from
|
|
13
|
+
* cores — but nothing looked at memory. On the 25 GB test VM one
|
|
14
|
+
* `nx run-many` of two integration projects at Vitest's default worker count
|
|
15
|
+
* took `MemAvailable` down to 1.4 GB (2026-11-18), and on a Mac the Docker VM
|
|
16
|
+
* (6 GB by default) is smaller still. Out of memory, Linux kills whatever is
|
|
17
|
+
* biggest — often the editor or the agent, not the test.
|
|
18
|
+
*
|
|
19
|
+
* WHAT IT DOES. Before a run takes its slot it measures what is free, on the
|
|
20
|
+
* host and (when Docker runs in a VM) inside Docker's VM. Below the floor it
|
|
21
|
+
* WAITS like the slot queue does, then refuses with what is free, what is
|
|
22
|
+
* needed and how to override. It is advisory: a reading it cannot take is
|
|
23
|
+
* skipped, never fatal, and the wait is bounded.
|
|
24
|
+
*/
|
|
25
|
+
const GIB = 1024 ** 3;
|
|
26
|
+
const INTEGRATION_WORKER_GB = 0.3;
|
|
27
|
+
/** Free host memory a run needs before it starts, in GB. `0` skips the check. */
|
|
28
|
+
export const MIN_FREE_MEMORY_GB = "DADS_TEST_MIN_FREE_MEMORY_GB";
|
|
29
|
+
/** Free memory inside Docker's VM a run needs, in GB. `0` skips the check. */
|
|
30
|
+
export const MIN_FREE_DOCKER_MEMORY_GB = "DADS_TEST_MIN_FREE_DOCKER_MEMORY_GB";
|
|
31
|
+
/** How long a run waits for memory before refusing. `0` refuses at once. */
|
|
32
|
+
export const MEMORY_WAIT_MS = "DADS_TEST_MEMORY_WAIT_MS";
|
|
33
|
+
const DEFAULT_MIN_FREE_GB = 4;
|
|
34
|
+
const DEFAULT_MIN_FREE_DOCKER_GB = 3;
|
|
35
|
+
/**
|
|
36
|
+
* Shorter than the slot queue's ten minutes: a busy slot frees itself when its
|
|
37
|
+
* run ends, but memory held by something else usually does not, and this wait
|
|
38
|
+
* stacks on top of the slot wait.
|
|
39
|
+
*/
|
|
40
|
+
const DEFAULT_MEMORY_WAIT_MS = 120_000;
|
|
41
|
+
const POLL_INTERVAL_MS = 2_000;
|
|
42
|
+
const ANNOUNCE_INTERVAL_MS = 15_000;
|
|
43
|
+
/** Each reading gives up after this; a slow Docker daemon skips the Docker half. */
|
|
44
|
+
const READING_TIMEOUT_MS = 1_500;
|
|
45
|
+
/**
|
|
46
|
+
* `TEST_POSTGRES_STORAGE=ram` puts a 4 GB tmpfs inside Docker. It needs that
|
|
47
|
+
* much plus room for Postgres, SpiceDB and the rest of what Docker runs.
|
|
48
|
+
*/
|
|
49
|
+
export const RAM_STORAGE_MIN_DOCKER_MEMORY_GB = 8;
|
|
50
|
+
/** Linux: `MemAvailable` — free memory plus cache the kernel can reclaim. */
|
|
51
|
+
export function parseLinuxMemAvailable(meminfo) {
|
|
52
|
+
const match = /^MemAvailable:\s+(\d+)\s+kB$/m.exec(meminfo);
|
|
53
|
+
return match === null ? null : Number(match[1]) * 1024;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* macOS: free + inactive + speculative + purgeable pages, times the page size
|
|
57
|
+
* `vm_stat` prints in its header.
|
|
58
|
+
*
|
|
59
|
+
* `os.freemem()` is only the "Pages free" line — on a 48 GB Mac it read under
|
|
60
|
+
* about 1 GB while 14 GB was reclaimable cache. `memory_pressure`'s "free
|
|
61
|
+
* percentage" errs the other way: it counts compressible memory as free and
|
|
62
|
+
* read 27 GB on the same Mac while 18 GB sat in the compressor. Inactive pages
|
|
63
|
+
* are the ones the kernel reclaims first, which is the honest middle and what
|
|
64
|
+
* Activity Monitor's own figures are built from.
|
|
65
|
+
*/
|
|
66
|
+
export function parseMacAvailableMemory(vmStat) {
|
|
67
|
+
const pageSize = /page size of (\d+) bytes/.exec(vmStat);
|
|
68
|
+
if (pageSize === null)
|
|
69
|
+
return null;
|
|
70
|
+
const pages = (label) => {
|
|
71
|
+
const match = new RegExp(`^${label}:\\s+(\\d+)\\.$`, "m").exec(vmStat);
|
|
72
|
+
return match === null ? null : Number(match[1]);
|
|
73
|
+
};
|
|
74
|
+
const counts = [
|
|
75
|
+
pages("Pages free"),
|
|
76
|
+
pages("Pages inactive"),
|
|
77
|
+
pages("Pages speculative"),
|
|
78
|
+
pages("Pages purgeable"),
|
|
79
|
+
];
|
|
80
|
+
let total = 0;
|
|
81
|
+
for (const count of counts) {
|
|
82
|
+
if (count === null)
|
|
83
|
+
return null;
|
|
84
|
+
total += count;
|
|
85
|
+
}
|
|
86
|
+
return total * Number(pageSize[1]);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* What one container holds that the VM cannot simply take back: its usage
|
|
90
|
+
* minus inactive page cache, the same figure `docker stats` shows.
|
|
91
|
+
*/
|
|
92
|
+
export function containerWorkingSetBytes(memory) {
|
|
93
|
+
return Math.max(0, (memory.usage ?? 0) - (memory.inactiveFile ?? 0));
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Docker runs in a VM everywhere but Linux, and on Linux under Docker Desktop.
|
|
97
|
+
* Natively on Linux its containers share the host's memory, which the host
|
|
98
|
+
* reading already covers.
|
|
99
|
+
*/
|
|
100
|
+
export function dockerRunsInVm(hostPlatform, operatingSystem) {
|
|
101
|
+
return hostPlatform !== "linux" || /docker desktop/i.test(operatingSystem);
|
|
102
|
+
}
|
|
103
|
+
/** A knob set to nonsense is a mistake worth saying out loud. */
|
|
104
|
+
function readNonNegative(env, name, fallback) {
|
|
105
|
+
const raw = env[name];
|
|
106
|
+
if (raw === undefined || raw.trim().length === 0)
|
|
107
|
+
return fallback;
|
|
108
|
+
const value = Number(raw);
|
|
109
|
+
if (!Number.isFinite(value) || value < 0) {
|
|
110
|
+
throw new Error(`${name} must be a number of at least 0 — got "${raw}"`);
|
|
111
|
+
}
|
|
112
|
+
return value;
|
|
113
|
+
}
|
|
114
|
+
export function memoryThresholds(env) {
|
|
115
|
+
return {
|
|
116
|
+
hostMinFreeBytes: readNonNegative(env, MIN_FREE_MEMORY_GB, DEFAULT_MIN_FREE_GB) * GIB,
|
|
117
|
+
dockerMinFreeBytes: readNonNegative(env, MIN_FREE_DOCKER_MEMORY_GB, DEFAULT_MIN_FREE_DOCKER_GB) * GIB,
|
|
118
|
+
waitBudgetMs: Math.round(readNonNegative(env, MEMORY_WAIT_MS, DEFAULT_MEMORY_WAIT_MS)),
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/** `5.8 GB` — one decimal is the precision anyone acts on. */
|
|
122
|
+
export function describeGb(bytes) {
|
|
123
|
+
return `${(bytes / GIB).toFixed(1)} GB`;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* The whole decision, with no measuring in it: go when every floor is met,
|
|
127
|
+
* wait while the budget lasts, then refuse with what is short and how to
|
|
128
|
+
* override it.
|
|
129
|
+
*/
|
|
130
|
+
export function decideMemoryPreflight(input) {
|
|
131
|
+
const { reading, thresholds, tier } = input;
|
|
132
|
+
const short = [];
|
|
133
|
+
if (thresholds.hostMinFreeBytes > 0 &&
|
|
134
|
+
reading.hostAvailableBytes < thresholds.hostMinFreeBytes) {
|
|
135
|
+
short.push(`host: ${describeGb(reading.hostAvailableBytes)} free (${reading.hostSource}), ` +
|
|
136
|
+
`needs ${describeGb(thresholds.hostMinFreeBytes)} — ${MIN_FREE_MEMORY_GB}`);
|
|
137
|
+
}
|
|
138
|
+
const docker = reading.docker;
|
|
139
|
+
if (docker !== null &&
|
|
140
|
+
docker.freeBytes !== null &&
|
|
141
|
+
thresholds.dockerMinFreeBytes > 0 &&
|
|
142
|
+
docker.freeBytes < thresholds.dockerMinFreeBytes) {
|
|
143
|
+
short.push(`Docker VM: ${describeGb(docker.freeBytes)} free of ${describeGb(docker.totalBytes)}, ` +
|
|
144
|
+
`needs ${describeGb(thresholds.dockerMinFreeBytes)} — ${MIN_FREE_DOCKER_MEMORY_GB}`);
|
|
145
|
+
}
|
|
146
|
+
if (short.length === 0)
|
|
147
|
+
return { action: "go", message: null };
|
|
148
|
+
if (input.waitedMs < thresholds.waitBudgetMs) {
|
|
149
|
+
return {
|
|
150
|
+
action: "wait",
|
|
151
|
+
message: `waiting for memory before the ${tier} run — ${short.join("; ")}`,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
action: "abort",
|
|
156
|
+
message: `not enough free memory for the ${tier} run after waiting ${String(Math.round(input.waitedMs / 1000))}s — refusing to start.\n` +
|
|
157
|
+
short.map((line) => ` ${line}\n`).join("") +
|
|
158
|
+
` A run that starts anyway can push the machine out of memory, and the kernel\n` +
|
|
159
|
+
` then kills whatever is biggest — often your editor, not the test.\n` +
|
|
160
|
+
` npm run doctor # other test runs or stale containers holding memory?\n` +
|
|
161
|
+
` To run anyway, lower the floor (0 skips it):\n` +
|
|
162
|
+
` ${MIN_FREE_MEMORY_GB}=2 ${MIN_FREE_DOCKER_MEMORY_GB}=2 <your command>`,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* `ram` needs Docker to have room for the 4 GB tmpfs plus headroom. When it
|
|
167
|
+
* does not — a Mac's default 6 GB Docker VM — the run uses disk and says why,
|
|
168
|
+
* instead of letting the tmpfs push the VM's other containers out of memory.
|
|
169
|
+
*/
|
|
170
|
+
export function decideRamStorageFallback(input) {
|
|
171
|
+
if (input.storage !== "ram" || input.docker === null) {
|
|
172
|
+
return { storage: input.storage, message: null };
|
|
173
|
+
}
|
|
174
|
+
const needed = RAM_STORAGE_MIN_DOCKER_MEMORY_GB * GIB;
|
|
175
|
+
if (input.docker.totalBytes >= needed) {
|
|
176
|
+
return { storage: "ram", message: null };
|
|
177
|
+
}
|
|
178
|
+
const where = input.docker.inVm ? "Docker's VM has" : "this machine has";
|
|
179
|
+
return {
|
|
180
|
+
storage: "disk",
|
|
181
|
+
message: `${TEST_POSTGRES_STORAGE}=ram ignored: ${where} ${describeGb(input.docker.totalBytes)} of memory, ` +
|
|
182
|
+
`and ram needs ${String(RAM_STORAGE_MIN_DOCKER_MEMORY_GB)} GB (a 4 GB tmpfs plus headroom) — using disk. ` +
|
|
183
|
+
`Give Docker more memory to use ram.`,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* How many workers fit in what is free: everything above `reserveBytes`,
|
|
188
|
+
* divided by what one worker was measured to need. Never below one.
|
|
189
|
+
*/
|
|
190
|
+
export function memoryWorkerLimit(input) {
|
|
191
|
+
const spare = input.availableBytes - input.reserveBytes;
|
|
192
|
+
return Math.max(1, Math.floor(spare / input.perWorkerBytes));
|
|
193
|
+
}
|
|
194
|
+
const run = promisify(execFile);
|
|
195
|
+
/** Resolves `null` instead of hanging when a reading is slow. */
|
|
196
|
+
function withinTimeout(work) {
|
|
197
|
+
return Promise.race([
|
|
198
|
+
work.catch(() => null),
|
|
199
|
+
new Promise((resolve) => {
|
|
200
|
+
setTimeout(() => {
|
|
201
|
+
resolve(null);
|
|
202
|
+
}, READING_TIMEOUT_MS).unref();
|
|
203
|
+
}),
|
|
204
|
+
]);
|
|
205
|
+
}
|
|
206
|
+
async function readHostAvailableMemory() {
|
|
207
|
+
if (platform() === "linux") {
|
|
208
|
+
const meminfo = await withinTimeout(readFile("/proc/meminfo", "utf8"));
|
|
209
|
+
const bytes = meminfo === null ? null : parseLinuxMemAvailable(meminfo);
|
|
210
|
+
if (bytes !== null) {
|
|
211
|
+
return { hostAvailableBytes: bytes, hostSource: "MemAvailable" };
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if (platform() === "darwin") {
|
|
215
|
+
const output = await withinTimeout(run("vm_stat", [], { encoding: "utf8", timeout: READING_TIMEOUT_MS }));
|
|
216
|
+
const bytes = output === null ? null : parseMacAvailableMemory(output.stdout);
|
|
217
|
+
if (bytes !== null) {
|
|
218
|
+
return { hostAvailableBytes: bytes, hostSource: "vm_stat" };
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return { hostAvailableBytes: freemem(), hostSource: "os.freemem" };
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Docker's memory through the same client testcontainers starts containers
|
|
225
|
+
* with, so it measures the daemon the run will actually use. Inside a VM it
|
|
226
|
+
* also sums what running containers hold: one `one-shot` stats call each, in
|
|
227
|
+
* parallel — without `one-shot` Docker waits a second for a CPU sample.
|
|
228
|
+
*/
|
|
229
|
+
async function readDockerMemory() {
|
|
230
|
+
const client = await getContainerRuntimeClient();
|
|
231
|
+
const runtime = client.info.containerRuntime;
|
|
232
|
+
const inVm = dockerRunsInVm(platform(), runtime.operatingSystem);
|
|
233
|
+
if (!inVm) {
|
|
234
|
+
return { totalBytes: runtime.memory, inVm, freeBytes: null };
|
|
235
|
+
}
|
|
236
|
+
const running = (await client.container.list()).filter((container) => container.State === "running");
|
|
237
|
+
// A container that is restarting or just exited reports empty stats; it
|
|
238
|
+
// counts as holding nothing rather than dropping the whole reading.
|
|
239
|
+
const held = await Promise.all(running.map(async (container) => {
|
|
240
|
+
try {
|
|
241
|
+
const stats = await client.container
|
|
242
|
+
.getById(container.Id)
|
|
243
|
+
.stats({ stream: false, "one-shot": true });
|
|
244
|
+
return containerWorkingSetBytes({
|
|
245
|
+
usage: stats.memory_stats.usage,
|
|
246
|
+
inactiveFile: stats.memory_stats.stats.inactive_file,
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
catch {
|
|
250
|
+
return 0;
|
|
251
|
+
}
|
|
252
|
+
}));
|
|
253
|
+
const used = held.reduce((sum, bytes) => sum + bytes, 0);
|
|
254
|
+
return {
|
|
255
|
+
totalBytes: runtime.memory,
|
|
256
|
+
inVm,
|
|
257
|
+
freeBytes: Math.max(0, runtime.memory - used),
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
/** One reading of host and Docker memory; the Docker half may be `null`. */
|
|
261
|
+
export async function readTestMemory() {
|
|
262
|
+
const [host, docker] = await Promise.all([
|
|
263
|
+
readHostAvailableMemory(),
|
|
264
|
+
withinTimeout(readDockerMemory()),
|
|
265
|
+
]);
|
|
266
|
+
return { ...host, docker };
|
|
267
|
+
}
|
|
268
|
+
/** Thrown instead of booting, so the refusal is loud and immediate. */
|
|
269
|
+
export class NotEnoughTestMemoryError extends Error {
|
|
270
|
+
constructor(message) {
|
|
271
|
+
super(message);
|
|
272
|
+
this.name = "NotEnoughTestMemoryError";
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
const realDeps = {
|
|
276
|
+
read: readTestMemory,
|
|
277
|
+
sleep: (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
|
|
278
|
+
log: (line) => process.stdout.write(`${line}\n`),
|
|
279
|
+
};
|
|
280
|
+
/**
|
|
281
|
+
* Waits until the machine has the memory a `tier` run needs, and throws
|
|
282
|
+
* `NotEnoughTestMemoryError` once the wait budget is spent. Returns the
|
|
283
|
+
* reading that let it through.
|
|
284
|
+
*/
|
|
285
|
+
export async function awaitEnoughTestMemory(tier, options = {}) {
|
|
286
|
+
const deps = { ...realDeps, ...options.deps };
|
|
287
|
+
const thresholds = options.thresholds ?? memoryThresholds(process.env);
|
|
288
|
+
let waitedMs = 0;
|
|
289
|
+
let announcedAtMs = null;
|
|
290
|
+
for (;;) {
|
|
291
|
+
const reading = await deps.read();
|
|
292
|
+
const decision = decideMemoryPreflight({
|
|
293
|
+
reading,
|
|
294
|
+
thresholds,
|
|
295
|
+
tier,
|
|
296
|
+
waitedMs,
|
|
297
|
+
});
|
|
298
|
+
if (decision.action === "go")
|
|
299
|
+
return reading;
|
|
300
|
+
if (decision.action === "abort") {
|
|
301
|
+
throw new NotEnoughTestMemoryError(decision.message);
|
|
302
|
+
}
|
|
303
|
+
if (announcedAtMs === null ||
|
|
304
|
+
waitedMs - announcedAtMs >= ANNOUNCE_INTERVAL_MS) {
|
|
305
|
+
deps.log(decision.message);
|
|
306
|
+
announcedAtMs = waitedMs;
|
|
307
|
+
}
|
|
308
|
+
await deps.sleep(POLL_INTERVAL_MS);
|
|
309
|
+
waitedMs += POLL_INTERVAL_MS;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Turns `TEST_POSTGRES_STORAGE=ram` into `disk` in `env` when Docker is too
|
|
314
|
+
* small for it, before any container starts, so every child and worker that
|
|
315
|
+
* inherits `env` makes the same choice. Reads Docker only when `ram` is asked
|
|
316
|
+
* for. Returns the storage the run will use.
|
|
317
|
+
*/
|
|
318
|
+
export async function applyRamStorageFallback(env, log = realDeps.log) {
|
|
319
|
+
const storage = testPostgresStorage(env);
|
|
320
|
+
if (storage !== "ram")
|
|
321
|
+
return storage;
|
|
322
|
+
const decision = decideRamStorageFallback({
|
|
323
|
+
storage,
|
|
324
|
+
docker: await withinTimeout(readDockerMemory()),
|
|
325
|
+
});
|
|
326
|
+
if (decision.message !== null) {
|
|
327
|
+
env[TEST_POSTGRES_STORAGE] = decision.storage;
|
|
328
|
+
log(decision.message);
|
|
329
|
+
}
|
|
330
|
+
return decision.storage;
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Peak memory one Vitest worker thread of an integration project adds,
|
|
334
|
+
* measured 2026-11-18 on the test VM: `domain-facilities` (39 files) peaked at
|
|
335
|
+
* 0.9, 1.2, 1.7 and 2.7 GB with 1, 2, 4 and 8 workers — about 0.25 GB a
|
|
336
|
+
* worker, rounded up.
|
|
337
|
+
*/
|
|
338
|
+
export const INTEGRATION_WORKER_MEMORY_BYTES = INTEGRATION_WORKER_GB * GIB;
|
|
339
|
+
/**
|
|
340
|
+
* Vitest's default is one worker per core but one. On the 32-core test VM
|
|
341
|
+
* that is 31 workers per project, and three projects at once (Nx's default
|
|
342
|
+
* parallelism) outgrow 25 GB. This lowers — never raises — a container-tier
|
|
343
|
+
* project's workers to what fits in free memory above the floor. It leaves
|
|
344
|
+
* alone a count someone chose (`VITEST_MAX_WORKERS`, which the integration
|
|
345
|
+
* gate always sets after its own memory-aware plan) and CI.
|
|
346
|
+
*/
|
|
347
|
+
export function decideVitestWorkerCap(input) {
|
|
348
|
+
const chosen = input.env["VITEST_MAX_WORKERS"];
|
|
349
|
+
if (input.env["CI"] !== undefined ||
|
|
350
|
+
(chosen !== undefined && chosen !== "")) {
|
|
351
|
+
return { workers: null, message: null };
|
|
352
|
+
}
|
|
353
|
+
const planned = input.configuredWorkers ?? Math.max(1, input.cores - 1);
|
|
354
|
+
const fit = memoryWorkerLimit({
|
|
355
|
+
availableBytes: input.availableBytes,
|
|
356
|
+
reserveBytes: input.reserveBytes,
|
|
357
|
+
perWorkerBytes: INTEGRATION_WORKER_MEMORY_BYTES,
|
|
358
|
+
});
|
|
359
|
+
if (fit >= planned)
|
|
360
|
+
return { workers: null, message: null };
|
|
361
|
+
return {
|
|
362
|
+
workers: fit,
|
|
363
|
+
message: `vitest workers: ${String(fit)} instead of ${String(planned)} — ` +
|
|
364
|
+
`${describeGb(input.availableBytes)} free, ${describeGb(input.reserveBytes)} kept free, ` +
|
|
365
|
+
`~${describeGb(INTEGRATION_WORKER_MEMORY_BYTES)} per worker. Set VITEST_MAX_WORKERS to choose.`,
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Applies `decideVitestWorkerCap` to the project a `globalSetup` was handed.
|
|
370
|
+
* Vitest reads `config.maxWorkers` when it schedules files, after every
|
|
371
|
+
* `globalSetup` has run, so the lowered count takes effect for this run.
|
|
372
|
+
*/
|
|
373
|
+
export async function capVitestWorkersToFreeMemory(project, cores, log = realDeps.log) {
|
|
374
|
+
const host = await readHostAvailableMemory();
|
|
375
|
+
const cap = decideVitestWorkerCap({
|
|
376
|
+
env: process.env,
|
|
377
|
+
configuredWorkers: project.config.maxWorkers,
|
|
378
|
+
cores,
|
|
379
|
+
availableBytes: host.hostAvailableBytes,
|
|
380
|
+
reserveBytes: memoryThresholds(process.env).hostMinFreeBytes,
|
|
381
|
+
});
|
|
382
|
+
if (cap.workers === null)
|
|
383
|
+
return;
|
|
384
|
+
project.config.maxWorkers = cap.workers;
|
|
385
|
+
if (cap.message !== null)
|
|
386
|
+
log(cap.message);
|
|
387
|
+
}
|
|
388
|
+
//# sourceMappingURL=refuse-low-memory-runs.js.map
|