Skip to content

test: add direct unit coverage for closeDbPair/closeDbPairDeferred/closeDbDeferred#1862

Closed
carlos-alm wants to merge 1 commit into
fix/issue-1750-finddbpath-swallowed-errorsfrom
fix/issue-1751-closedbpair-test-coverage
Closed

test: add direct unit coverage for closeDbPair/closeDbPairDeferred/closeDbDeferred#1862
carlos-alm wants to merge 1 commit into
fix/issue-1750-finddbpath-swallowed-errorsfrom
fix/issue-1751-closedbpair-test-coverage

Conversation

@carlos-alm

Copy link
Copy Markdown
Contributor

Summary

  • closeDbPair, closeDbPairDeferred, and closeDbDeferred had zero direct test coverage despite being core resource-lifecycle primitives in a fanIn-55 file — exactly the kind of gap an earlier phase's real resource-leak bug slipped through.
  • Added 10 tests to tests/unit/db.test.ts (existing home for this file's connection tests): close ordering (native before better-sqlite3, survives native-close failure), deferred-close timing (native immediate, better-sqlite3 deferred to next tick), and advisory-lock release/flush semantics (immediate lock release, flushDeferredClose() closes synchronously with no double-close).
  • No production code changes — everything needed was already exported.

Closes #1751

Test plan

  • npm test — full suite green (3500 passed, 0 failed), all 10 new tests confirmed running and passing (not skipped)
  • npm run lint / npm run typecheck — clean
  • Non-vacuousness confirmed via 3 temporary mutation checks (swapped close order, removed double-close guard, moved lock release inside the deferred callback) — each correctly caught by the corresponding test, then reverted

Stacked on #1861 (base branch fix/issue-1750-finddbpath-swallowed-errors) — only the test diff is this issue's change.

…oseDbDeferred

closeDbPair, closeDbPairDeferred, and closeDbDeferred in src/db/connection.ts
had zero direct test coverage despite being core resource-lifecycle
primitives in a fanIn-55 file — the same category of gap where the
openReadonlyWithNative leak went undetected (see
openReadonlyWithNative-leak.test.ts).

Adds three describe blocks to tests/unit/db.test.ts, alongside the existing
openDb/closeDb coverage:
- closeDbPair: native handle closes before the better-sqlite3 handle; a
  native close failure doesn't prevent the better-sqlite3 close; works with
  no native handle present.
- closeDbPairDeferred: native closes synchronously within the call, while
  the better-sqlite3 close is deferred via closeDbDeferred (including when
  the native close throws).
- closeDbDeferred: the advisory lock releases synchronously (verified via a
  real lock file), the handle itself closes on the next tick, and
  flushDeferredClose() closes it synchronously when called first — with the
  originally scheduled callback correctly skipping a second close.

Fixes #1751
@greptile-apps

greptile-apps Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds 10 unit tests covering three previously untested resource-lifecycle helpers — closeDbPair, closeDbPairDeferred, and closeDbDeferred — all in the existing tests/unit/db.test.ts file. No production code is changed.

  • closeDbPair (3 tests): verifies native-before-better-sqlite3 close ordering, error resilience when the native handle throws, and graceful handling of a missing native handle.
  • closeDbPairDeferred (3 tests): confirms the native close is synchronous while the better-sqlite3 WAL checkpoint is deferred to the next tick via setImmediate, including the throw-resilient path.
  • closeDbDeferred (4 tests): covers advisory-lock release timing (synchronous), deferred handle close (next tick), flushDeferredClose() short-circuit with no double-close, and the no-lock-path path.

Confidence Score: 5/5

Pure test addition with no production code changes; all new tests correctly model the source functions they exercise.

Every new test was traced against the corresponding implementation in connection.ts — close ordering, try/catch resilience, setImmediate deferral, the double-close guard (indexOf check), and advisory-lock release timing all match the production code exactly. The afterEach cleanup pattern correctly drains the module-level _deferredDbs singleton between tests. No production code was touched.

No files require special attention.

Important Files Changed

Filename Overview
tests/unit/db.test.ts Adds 10 focused unit tests for closeDbPair/closeDbPairDeferred/closeDbDeferred using lightweight mock handles; correct cleanup via afterEach, accurate tick() implementation, and proper double-close guard verified.

Reviews (1): Last reviewed commit: "test: add direct unit coverage for close..." | Re-trigger Greptile

@carlos-alm carlos-alm deleted the branch fix/issue-1750-finddbpath-swallowed-errors July 6, 2026 17:43
@carlos-alm carlos-alm closed this Jul 6, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 6, 2026
@carlos-alm

Copy link
Copy Markdown
Contributor Author

Recreated as #1910 — this PR was inadvertently closed when its stacked base branch was deleted after #1903 merged. Continuing there.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant