testing/cross_backend/spawn_backend.ts

Spawn a test backend binary, wait for it to come up, and return a handle the test harness drives.

Lifecycle:

  1. Refuse when config.base_url's port already accepts a TCP connection — another cross run or a stale backend holds it, and the health probe would otherwise drive that process instead of the one spawned here.
  2. Write the bootstrap token (config.bootstrap.token) to config.bootstrap.token_path so the binary picks it up at startup, and delete any config.bootstrap.daemon_token_path a previous run left, so step 5 can only read the token this binary writes.
  3. child_process.spawn(...) the binary with detached: true — creates a new process group so a SIGTERM to the negative PID tears down any descendants the binary spawned (PTYs, child workers). vitest worker death + SIGINT / SIGTERM / SIGHUP handlers also fire the group teardown so ports never strand.
  4. Poll {base_url}{health_path} until it returns 2xx or startup_timeout_ms elapses — each probe bounded by its own timeout, so a listener that accepts but never answers can't outlast the deadline.
  5. Read config.bootstrap.daemon_token_path to load the binary's deterministic daemon token; thread it onto BackendHandle so _testing_reset and other keeper-credential calls can authenticate.
  6. Log one line with the time from spawn to step 5 done, so a run shows what each backend costs to bring up.

The child's stderr is kept as a bounded tail (STDERR_TAIL_MAX_BYTES): a startup failure reports it with the exit code or signal, and an exit after startup that teardown didn't cause is logged with it.

Bootstrapping (POST /api/account/bootstrap) is a separate concern — the caller composes bootstrap() from testing/transports/bootstrap.ts against a FetchTransport built around handle.config.base_url. Splitting the two keeps spawn_backend consumer-agnostic — fuz_app knows nothing about specific binary contents.

view source

Declarations
#

6 declarations

BackendHandle
#

testing/cross_backend/spawn_backend.ts view source

BackendHandle import type {BackendHandle} from '@fuzdev/fuz_app/testing/cross_backend/spawn_backend.js';

Handle returned by spawn_backend — passed to per-test setup helpers.

config

The config used to spawn this backend. Carried for diagnostic + downstream access.

type BackendConfig

readonly

child

Child process reference — exposed for diagnostic logging only.

type ChildProcess

readonly

daemon_token

Deterministic daemon token captured from config.bootstrap.daemon_token_path after the binary booted. default_cross_process_setup builds keeper-daemon-token headers from this for _testing_reset calls.

type string

readonly

teardown

SIGTERM the child's process group — even when the child itself has exited, so no descendant outlives it — and await the group's exit, escalating to SIGKILL after a grace window. Idempotent — later calls await the same exit. A descendant that left the group — spawned detached, or one that called setsid — is out of reach and survives teardown.

type (): Promise<void>

readonly
returns Promise<void>

create_output_tail
#

testing/cross_backend/spawn_backend.ts view source

(max_bytes: number): OutputTail import {create_output_tail} from '@fuzdev/fuz_app/testing/cross_backend/spawn_backend.js';

Create an OutputTail keeping at most max_bytes.

max_bytes

how many trailing bytes to keep

type number

returns

OutputTail

is_port_accepting
#

testing/cross_backend/spawn_backend.ts view source

(base_url: string): Promise<boolean> import {is_port_accepting} from '@fuzdev/fuz_app/testing/cross_backend/spawn_backend.js';

Whether something accepts TCP connections at base_url's host and port. A refused connection, an unresolvable host or no answer within PORT_PROBE_TIMEOUT_MS all read as free.

base_url

an http:// or https:// URL; a missing port is the scheme's default

type string

returns

Promise<boolean>

OutputTail
#

testing/cross_backend/spawn_backend.ts view source

OutputTail import type {OutputTail} from '@fuzdev/fuz_app/testing/cross_backend/spawn_backend.js';

A byte-bounded tail of a stream's output: keeps the last max_bytes written, dropping the oldest.

push

type (chunk: Buffer<ArrayBufferLike>): void

readonly

chunk

type Buffer<ArrayBufferLike>
returns void

text

The kept bytes as UTF-8, prefixed with a marker when older output was dropped.

type (): string

readonly
returns string

spawn_backend
#

testing/cross_backend/spawn_backend.ts view source

(config: BackendConfig): Promise<BackendHandle> import {spawn_backend} from '@fuzdev/fuz_app/testing/cross_backend/spawn_backend.js';

Spawn config.start_command and return a handle once the binary is health-probe-ready and the daemon-token file is readable.

Errors at any stage SIGTERM the child group before rethrowing — the caller never sees a half-started backend.

config

returns

Promise<BackendHandle>

throws

  • Error - when `config.base_url`'s port already accepts connections, before anything is spawned; when the child exits before it is healthy (with its exit code or signal and stderr tail); or when the health probe or the daemon-token read times out

STDERR_TAIL_MAX_BYTES
#

testing/cross_backend/spawn_backend.ts view source

number import {STDERR_TAIL_MAX_BYTES} from '@fuzdev/fuz_app/testing/cross_backend/spawn_backend.js';

How much of the child's stderr is kept (bytes) — the tail a startup failure or an unexpected exit reports. Bounded so a backend that logs to stderr for a whole suite doesn't grow the runner's memory with it.

Depends on
#

Imported by
#