Skip to content

tests: raise dataexchange coverage from 69% to 80% (package 95%)#5

Merged
TeoSlayer merged 1 commit into
mainfrom
add-test-coverage-round2
May 28, 2026
Merged

tests: raise dataexchange coverage from 69% to 80% (package 95%)#5
TeoSlayer merged 1 commit into
mainfrom
add-test-coverage-round2

Conversation

@TeoSlayer
Copy link
Copy Markdown
Contributor

Summary

Round-2 coverage push following PR #1.

  • Total (./...): 69.2% → 79.9%
  • Package only (github.com/pilot-protocol/dataexchange): 82.5% → 95.3%

The remaining gap to 100% in the total is entirely examples/main.go (3 funcs at 0%) — a daemon-dialling CLI program that isn't worth wrapping in tests.

What's new

Two new test files, no production code touched:

  • zz_client_test.go — adds a fakeDaemon (unix-socket IPC peer that mimics enough of the Pilot daemon's wire protocol to satisfy driver.Driver) and covers every previously-zero function in client.go:
    • Dial, SendText, SendJSON, SendBinary, SendFile, SendTrace, Recv, Close — all 0% → 100%
    • Bonus: server.go:ListenAndServe 0% → 90% via cmdAccept/cmdRecv push from the daemon side
  • zz_service_errors_test.go — drills the still-uncovered error paths in service.go:
    • saveReceivedFile EACCES (chmod 0500, skip-as-root) and file.received event branch
    • saveInboxMessage message.received event branch
    • WriteFrame header-write + payload-write error branches
    • handleConn ACK-write-failure exit branch
    • Stop() ctx-cancelled branch (hanging listener forces <-ctx.Done())
    • handleConn with IncludeBase64=true so the b64 branch in saveInboxMessage is exercised end-to-end
    • PILOT-183 regression pins — two tests for evictInboxOverflow's subdir-miscount behaviour, with comments marked REMOVE WHEN PILOT-183 LANDS

Per-function deltas

Function Before After
client.Dial 0% 100%¹
client.SendText/SendJSON/SendBinary/SendFile/SendTrace 0% 100%
client.Recv/Close 0% 100%
server.ListenAndServe 0% 90%
service.handleConn 93.9% 97.0%
service.Stop 88.9% 100%
service.evictInboxOverflow 92.3% 96.2%
service.saveReceivedFile 72.7% 90.9%
service.saveInboxMessage 82.6% 87.0%

¹ Dial shows 75% because one error branch (d.DialAddr failing) requires either a cmdError reply path (not covered by the happy-path daemon) or driver internals we can't reach from a test module — leaving it as the one stub.

Flake hardening

Two stress-only failure modes surfaced under -race -count=20, both fixed in the test harness (no production change):

  1. Daemon write race — daemon-side replies (cmdBindOK/cmdDialOK) interleaved with test-thread pushes (cmdAccept/cmdRecv) on the same unix socket, corrupting the wire. Fixed by routing all daemon→driver writes through safeWrite under writeMu.
  2. Send fragmentation racedriver.Conn.Write splits WriteFrame's 8-byte header and the payload into separate cmdSend IPC messages. The first version of the tests waited for "at least one cmdSend" before decoding, sometimes capturing only the header. Fixed by waitForCompleteFrame polling until the byte stream decodes as a complete data-exchange Frame.

Test plan

  • go test -race -count=1 -timeout 180s ./... passes (~12s)
  • go test -race -count=20 -timeout 180s -run 'TestClient_|TestServer_' . passes (no flake)
  • go vet ./... clean
  • gofmt -l clean on new files

Round 2 follows the round-1 PR (#1) and lifts the test surface from the
69% post-round-1 baseline (./...) to 80%, with the dataexchange package
itself moving from 82.5% to 95.3% of statements.

New files:

- zz_client_test.go — fake daemon (unix socket + IPC framer) plus
  end-to-end coverage for every Client method (Dial, SendText, SendJSON,
  SendBinary, SendFile, SendTrace, Recv, Close — all 0% → 100%). Also
  covers server.go ListenAndServe (0% → 90%) by pushing cmdAccept +
  cmdRecv at the driver and asserting the handler fires.

- zz_service_errors_test.go — error-path drilling for service.go:
  saveReceivedFile EACCES + Events.Publish branches, saveInboxMessage
  Events.Publish branch, WriteFrame header/payload write errors,
  handleConn ACK-write-failure exit, Stop() ctx-cancelled branch,
  handleConn IncludeBase64=true branch. Includes two regression tests
  for PILOT-183 (evictInboxOverflow miscounts when subdirs are mixed
  with regular files) that pin the current behaviour and document the
  comment to remove once the fix lands.

Per-function notable jumps:

- client.go: 0% → 100% across all eight functions
- server.go: ListenAndServe 0% → 90%
- service.go: handleConn 93.9% → 97.0%, Stop 88.9% → 100%,
  evictInboxOverflow 92.3% → 96.2%, saveReceivedFile 72.7% → 90.9%,
  saveInboxMessage 82.6% → 87.0%

Test fixtures:

- fakeDaemon serialises all writes to the unix socket via writeMu —
  without it, daemon replies (cmdBindOK/cmdDialOK) interleave with
  test thread pushes (cmdAccept/cmdRecv) and corrupt the wire,
  surfacing as flaky "daemon disconnected" errors under -race.
- waitForCompleteFrame polls the daemon's captured cmdSend stream until
  a full data-exchange Frame can be decoded — needed because
  driver.Conn.Write splits WriteFrame's header + payload writes into
  separate cmdSend IPC messages.

Run: `go test -race -count=1 -timeout 180s ./...` — passes (12s, 0 flake
across 20-iter stress).
@codecov
Copy link
Copy Markdown

codecov Bot commented May 28, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@TeoSlayer TeoSlayer merged commit ec1b3c7 into main May 28, 2026
2 checks passed
@TeoSlayer TeoSlayer deleted the add-test-coverage-round2 branch May 28, 2026 05:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants