Skip to content

Add coverage for health-server guards, db readiness deadlines, and lag histogram buckets - #1445

Merged
Mimah97 merged 5 commits into
Vatix-Protocol:devfrom
Atisan12:drips/1310-1315-1316-1317
Oct 26, 2026
Merged

Mimah97 merged 5 commits into
Vatix-Protocol:devfrom
Atisan12:drips/1310-1315-1316-1317

Conversation

@Atisan12

Copy link
Copy Markdown
Contributor

Summary

Add coverage for health-server guards, db readiness deadlines, and lag histogram buckets

What was solved

#1310 — Workers health server: validate the port before Fastify is constructed and cover the failure path

Extend the workers health-server test suite to explicitly cover the invalid-port guard (negative and non-finite ports throwing before Fastify is constructed), the port-0 ephemeral-listen path with a GET /live 200 assertion, and the listen-failure path asserting a structured error log containing the port and a rethrow. Only test code is added; the production module is left unchanged unless a small adjustment is needed to make the failure path observable.

Addressed:

  • Changed: apps/workers/src/health-server.test.ts, apps/workers/src/health-server.test.ts, apps/workers/src/health-server.test.ts
  • Add tests asserting negative and non-finite ports (NaN, Infinity) throw before any Fastify instance is constructed, with the error message including the offending port value.
  • Add a test asserting port 0 starts the server on an ephemeral port and GET /live returns 200.
  • Add a test asserting the listen-failure path logs an error entry containing the port field and rethrows.

#1315 — dbReadiness: add a timeout and a stable error code so a hung Postgres cannot stall /ready

Add a bounded deadline and stable error codes to checkDbReadiness in src/services/dbReadiness.ts: race $queryRaw against a 2000ms (overridable) timer, reject with 'db: DEPENDENCY_TIMEOUT' (code DEPENDENCY_TIMEOUT) on timeout and 'db: DEPENDENCY_UNAVAILABLE' (code DEPENDENCY_UNAVAILABLE, cause preserved) on other failures, clearing the timer in finally. Extend src/services/dbReadiness.test.ts with a never-resolving fake prisma (timeout code + bounded settle) and a rejecting fake prisma (unavailable code, no driver message leak).

Addressed:

  • Changed: src/services/dbReadiness.ts, src/services/dbReadiness.ts, src/services/dbReadiness.test.ts, src/services/dbReadiness.ts
  • checkDbReadiness must accept an optional timeoutMs parameter defaulting to 2000 ms
  • Wrap $queryRaw in Promise.race against a timer; on timeout reject with message 'db: DEPENDENCY_TIMEOUT' and code 'DEPENDENCY_TIMEOUT'
  • On any other rejection, rethrow as message 'db: DEPENDENCY_UNAVAILABLE' with code 'DEPENDENCY_UNAVAILABLE', preserving original error as cause

#1316 — lag-metrics: expose the lag histogram bucket boundaries and assert the shed-threshold straddle

Export DEFAULT_SHED_THRESHOLD = 500 from src/services/lag-metrics.ts, reference it in the JSDoc, and add tests in src/services/lag-metrics.test.ts asserting LAG_HISTOGRAM_BUCKETS is strictly ascending, contains DEFAULT_SHED_THRESHOLD, and that the registered vatix_settlement_lag histogram exposes a le="500" bucket with non-zero count after setCurrentLag(DEFAULT_SHED_THRESHOLD), using resetMetrics() in beforeEach.

Addressed:

  • Changed: src/services/lag-metrics.ts, src/services/lag-metrics.test.ts
  • Export const DEFAULT_SHED_THRESHOLD = 500 next to LAG_HISTOGRAM_BUCKETS in src/services/lag-metrics.ts and reference it in the JSDoc that currently hardcodes '500'.
  • Add test asserting every consecutive pair in LAG_HISTOGRAM_BUCKETS is strictly increasing.
  • Add test asserting LAG_HISTOGRAM_BUCKETS includes DEFAULT_SHED_THRESHOLD (no literal 500 in the test).

#1317 — dbReadiness: make the readiness check injectable so /ready tests do not need a live Postgres

Make checkDbReadiness injectable by accepting an optional client (Pick<PrismaClient, '$queryRaw'>) and timeoutMs, defaulting to getPrismaClient(), so unit tests can simulate resolve/reject/never-settle without a live Postgres while production callers in ready-deps.ts remain unchanged.

Addressed:

  • Changed: src/services/dbReadiness.ts, src/services/dbReadiness.test.ts, src/api/deps/ready-deps.ts
  • Change checkDbReadiness signature to checkDbReadiness(options?: { client?: Pick<PrismaClient, '$queryRaw'>; timeoutMs?: number }) and resolve client as options?.client ?? getPrismaClient()
  • Ensure getPrismaClient is never called when an injected client is supplied
  • Keep exported function name and Promise return type so src/api/deps/ready-deps.ts needs no change beyond optionally threading options

Changes

  • apps/workers/src/health-server.test.ts (modify)
  • src/services/dbReadiness.ts (modify)
  • src/services/dbReadiness.test.ts (modify)
  • src/services/lag-metrics.ts (modify)
  • src/services/lag-metrics.test.ts (modify)
  • src/api/deps/ready-deps.ts (modify)

Approach

  1. Workers health server: validate the port before Fastify is constructed and cover the failure path #1310 — Workers health server: validate the port before Fastify is constructed and cover the failure path (Changed: apps/workers/src/health-server.test.ts, apps/workers/src/health-server.test.ts, apps/workers/src/health-server.test.ts)
  2. dbReadiness: add a timeout and a stable error code so a hung Postgres cannot stall /ready #1315 — dbReadiness: add a timeout and a stable error code so a hung Postgres cannot stall /ready (Changed: src/services/dbReadiness.ts, src/services/dbReadiness.ts, src/services/dbReadiness.test.ts, src/services/dbReadiness.ts)
  3. lag-metrics: expose the lag histogram bucket boundaries and assert the shed-threshold straddle #1316 — lag-metrics: expose the lag histogram bucket boundaries and assert the shed-threshold straddle (Changed: src/services/lag-metrics.ts, src/services/lag-metrics.test.ts)
  4. dbReadiness: make the readiness check injectable so /ready tests do not need a live Postgres #1317 — dbReadiness: make the readiness check injectable so /ready tests do not need a live Postgres (Changed: src/services/dbReadiness.ts, src/services/dbReadiness.test.ts, src/api/deps/ready-deps.ts)

Issues

Closes #1310
Closes #1315
Closes #1316
Closes #1317

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment