Phase 1 (throughput pipeline) is now fully landed.
4.5 KiB
Task 1.10 — Integration test (testcontainers Redis + Postgres)
Phase: 1 — Throughput pipeline Status: 🟩 Done Depends on: 1.5, 1.7, 1.8, 1.9 Wiki refs: —
Goal
End-to-end pipeline test: spin up Redis 7 and TimescaleDB via testcontainers, boot the Processor against them, publish a synthetic Position to telemetry:t, verify the row appears in positions with byte-equivalent attribute decoding (bigint, Buffer included).
This is the integration test that proves the upstream contract from tcp-ingestion flows through end-to-end. Mirror tcp-ingestion/test/publish.integration.test.ts's structure and skip-on-no-Docker pattern.
Deliverables
-
test/pipeline.integration.test.ts:beforeAll: start Redis container, start TimescaleDB container, run migrations, build a Processor instance pointed at both. If Docker is unavailable, log a clear skip message and set a flag so allitblocks early-return without failing.afterAll: stop the Processor, stop containers.- Test 1: publish a Position with
bigintandBufferattributes viaXADD; wait for the row inpositions(poll, timeout 10s); assertdevice_id,ts, GPS fields, and a JSON round-trip ofattributesmatches the original (bigint as string, Buffer as base64). - Test 2: publish two records with the same
(device_id, ts); verify only one row inpositions(idempotency check). - Test 3: publish a malformed payload (broken JSON) on the stream; verify
processor_decode_errors_totalincrements and the bad entry stays in PEL (not ACKed). - Test 4: simulate the writer failing once (e.g. by temporarily shutting Postgres mid-test, then bringing it back); verify the record gets retried and eventually lands.
-
Use the TimescaleDB image, not stock
postgres:7-alpine. Suggested:timescale/timescaledb:latest-pg16. Confirm the migration'sCREATE EXTENSION IF NOT EXISTS timescaledbno-ops (extension already loaded). -
Use the same Vitest config split as
tcp-ingestion:vitest.integration.config.tswithhookTimeout: 120_000,testTimeout: 60_000. Defaultpnpm testexcludes*.integration.test.ts; opt-in viapnpm test:integration.
Specification
Skip-on-no-Docker pattern
Copy tcp-ingestion/test/publish.integration.test.ts's pattern verbatim:
- Try to start the first container in
beforeAll. On error, setdockerAvailable = false, log a warning, and return. - Each
itblock early-returns with aconsole.warnif!dockerAvailable. - This pattern was the fix for the CI test failure on the runner without Docker — keep it.
Synthetic Position publishing
Reuse serializePosition from tcp-ingestion's publish.ts if it can be imported (likely not — separate repos). Otherwise inline the encoding: a Position object → JSON.stringify with the bigint/Buffer replacer → XADD telemetry:t * ts <iso> device_id <imei> codec 8E payload <json>.
Why test 4 (writer failure → retry)
This validates the core ACK semantics: if a write fails, the record stays pending, and re-delivery brings it back. Without this test, we have unit tests showing each piece behaves correctly, but no proof the pieces compose right. Skip-conditions: if simulating Postgres failure mid-test is too flaky in testcontainers, weaken to: stop Postgres before publishing, publish, start Postgres, verify row appears.
Acceptance criteria
pnpm test:integrationruns all four scenarios green when Docker is available.- Without Docker, the suite logs skip messages and exits 0 (does not fail).
- CI (
pnpm test, unit only) does not run these — they are opt-in. - First-run container pull is reasonable; subsequent runs are fast (testcontainers caches the image).
Risks / open questions
- Image pull on first CI run. The TimescaleDB image is large (~700MB). If we ever wire integration tests into CI (separate job with Docker), pre-pulling may be required. Document but defer.
- Test flakiness from polling. Polling for "row appears in
positions" uses a 10s timeout. If CI is slow, raise it. Don't replace polling withawait sleep(2000)— that's reliably wrong.
Done
test/pipeline.integration.test.ts: four scenarios (happy path with bigint+Buffer, idempotency, malformed payload stays pending, writer failure → retry after Postgres restart). Uses timescale/timescaledb:latest-pg16; skip-on-no-Docker pattern verified (exits 0 without Docker). pnpm test:integration runs 4 tests green with Docker, 4 skips without. Landed in 9791620.