Skip to content

test(cache): de-flake TestGetNarInfoOpaqueUpstreamURL size assert - #1349

Merged
kalbasit merged 2 commits into
mainfrom
fix-flaky-opaque-upstream-nar-size
Jun 7, 2026
Merged

kalbasit merged 2 commits into
mainfrom
fix-flaky-opaque-upstream-nar-size

Conversation

@kalbasit

@kalbasit kalbasit commented Jun 7, 2026

Copy link
Copy Markdown
Owner

Summary

TestGetNarInfoOpaqueUpstreamURL flaked in CI with cache_test.go:529 expected: 50308, actual: -1. The first GetNar asserted the exact NAR size, but GetNar legitimately returns -1 (size unknown) when it serves bytes via the live-streaming path while the upstream download is still in flight (cache.go:1449). Which path it takes is timing-dependent, so under CPU contention (parallel integration cohorts) the assertion failed even though the streamed bytes were always correct.

  • Fix (pkg/cache/cache_test.go): accept either the concrete file size or -1, keeping the authoritative byte-equality check — mirroring the post-eviction re-fetch just below that already handled this.
  • Spec (nar-concurrent-streaming): document the GetNar -1 streaming size contract so it's explicit and testable.
  • Spec (api-surface): correct the stale Cache.GetNar / NarStore / NarInfoStore signatures to match the real code (the block also omitted StatNar/WalkNars and listed a phantom GetNarSize). Docs only — no behavior change.

OpenSpec change archived as 2026-06-07-fix-flaky-opaque-upstream-nar-size; canonical specs synced.

Test plan

  • Reproduced the flake deterministically: go test ./pkg/cache -run '^TestGetNarInfoOpaqueUpstreamURL$' -count=60 -race under full CPU saturation → expected 50308, actual -1.
  • After the fix, 80× -race runs under full CPU load → ok.
  • task fmt (0 changed), task lint (0 issues), task test (pass).
  • openspec validate --specs → 33 passed, 0 failed.

kalbasit and others added 2 commits June 7, 2026 00:20
The first GetNar in TestGetNarInfoOpaqueUpstreamURL asserted the exact
NAR size, but GetNar legitimately returns -1 (size unknown) when it
serves the bytes via the live-streaming path while the upstream download
is still in flight (cache.go:1449). Which path it takes is timing-
dependent, so under CPU contention (parallel integration cohorts) the
assertion failed with 'expected 50308, actual -1' even though the
streamed bytes were always correct.

Accept either the concrete file size or -1 and keep the authoritative
byte-equality check, mirroring the post-eviction re-fetch a few lines
below that already handled this. Reproduced deterministically under load
before; 80x -race runs under full CPU load pass after.

Also fold in OpenSpec change fix-flaky-opaque-upstream-nar-size:
- nar-concurrent-streaming: document the GetNar -1 size contract.
- api-surface: correct the stale Cache.GetNar / NarStore / NarInfoStore
  signatures to match the real code (no behavior change).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Sync the change's delta specs into the canonical specs and move the
change to openspec/changes/archive/2026-06-07-fix-flaky-opaque-upstream-nar-size:
- nar-concurrent-streaming: documents the GetNar -1 (size unknown)
  streaming contract.
- api-surface: corrects the stale Cache.GetNar / NarStore / NarInfoStore
  signatures to match the real code.

Satisfies the openspec-guard (no active changes remain).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@kalbasit

kalbasit commented Jun 7, 2026 •

Copy link
Copy Markdown
Owner Author

This change is part of the following stack:

Change managed by git-spice.

@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. bug Something isn't working go Pull requests that update go code labels Jun 7, 2026
@coderabbitai

coderabbitai Bot commented Jun 7, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ace3ff72-2a7d-491f-ad56-bc7a8ccceda0

📥 Commits

Reviewing files that changed from the base of the PR and between bf18644 and 895d0a6.

📒 Files selected for processing (9)
  • openspec/changes/archive/2026-06-07-fix-flaky-opaque-upstream-nar-size/.openspec.yaml
  • openspec/changes/archive/2026-06-07-fix-flaky-opaque-upstream-nar-size/design.md
  • openspec/changes/archive/2026-06-07-fix-flaky-opaque-upstream-nar-size/proposal.md
  • openspec/changes/archive/2026-06-07-fix-flaky-opaque-upstream-nar-size/specs/api-surface/spec.md
  • openspec/changes/archive/2026-06-07-fix-flaky-opaque-upstream-nar-size/specs/nar-concurrent-streaming/spec.md
  • openspec/changes/archive/2026-06-07-fix-flaky-opaque-upstream-nar-size/tasks.md
  • openspec/specs/api-surface/spec.md
  • openspec/specs/nar-concurrent-streaming/spec.md
  • pkg/cache/cache_test.go

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Fixed flaky test for opaque upstream NAR downloads by accommodating both concrete file sizes and unknown-size indicators during streaming.
  • Documentation

    • Added specifications for NAR caching API contracts, storage backend interfaces, and concurrent streaming behavior.
    • Defined requirements for NAR methods and storage sentinel errors.
  • Tests

    • Updated NAR size assertions to accept multiple valid outcomes.

Walkthrough

This PR documents and fixes a flaky test for NAR (Nix Archive) cache operations. The test TestGetNarInfoOpaqueUpstreamURL fails nondeterministically because Cache.GetNar returns different sizes depending on whether the NAR is served from completed storage (concrete size) or streamed in-flight (unknown size -1). The fix relaxes the test assertion to accept either outcome while preserving byte correctness, and codifies the dual-path behavior in canonical specifications.

Changes

NAR GetNar Size Flakiness and Specification

Layer / File(s) Summary
Issue documentation and proposed resolution
openspec/changes/archive/2026-06-07-fix-flaky-opaque-upstream-nar-size/{.openspec.yaml,design.md,proposal.md,specs/api-surface/spec.md,specs/nar-concurrent-streaming/spec.md,tasks.md}
Archive entry files explaining why TestGetNarInfoOpaqueUpstreamURL flakes under scheduling contention, proposing to relax the test size assertion while preserving byte verification, and documenting spec synchronization tasks.
API surface specification updates
openspec/specs/api-surface/spec.md
Canonical spec updated with revised pkg/cache.Cache GetNar method signature (returns nar.URL, int64, io.ReadCloser, error) and updated pkg/storage backend interfaces (NarStore and NarInfoStore methods for size reporting and iteration).
Concurrent streaming size behavior specification
openspec/specs/nar-concurrent-streaming/spec.md
Spec documents that Cache.GetNar returns -1 ("unknown size") during in-flight upstream streaming while still delivering complete correct bytes, and concrete nar_files.file_size when fully available; consumers must validate content via streamed bytes.
Test assertion relaxation
pkg/cache/cache_test.go
Test now accepts either the expected NAR byte length or -1 from GetNar on the first fetch, eliminating the nondeterministic failure.

🎯 2 (Simple) | ⏱️ ~12 minutes

Suggested labels

go, documentation, size:XS

Poem

🐰 A test flaked with timing's cruel dance,
Sometimes size returned, sometimes unknown—
We let both paths take their chance,
And specs now sing what we've always known.
Bytes flow true, regardless of the size we see. ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the primary change: de-flaking a test by relaxing its size assertion from exact to tolerant of either concrete or -1 sentinel values.
Description check ✅ Passed The description clearly relates to the changeset, detailing the flaky test issue, the fix approach, spec updates, and test plan results.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses a flaky test (TestGetNarInfoOpaqueUpstreamURL) in pkg/cache/cache_test.go by relaxing the exact size assertion to accept either the concrete NAR file size or the -1 sentinel value, which is returned when streaming an in-flight upstream download. It also documents this behavior in the nar-concurrent-streaming specification and updates stale signatures for Cache.GetNar, NarStore, and NarInfoStore in the api-surface specification. No review comments were provided, so there is no feedback to address.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@kalbasit
kalbasit merged commit 9f0b3e1 into main Jun 7, 2026
41 checks passed
@kalbasit
kalbasit deleted the fix-flaky-opaque-upstream-nar-size branch June 7, 2026 16:02
@dosubot

dosubot Bot commented Jun 7, 2026

Copy link
Copy Markdown

Documentation Updates

1 document(s) were updated by changes in this PR:

NAR File Download and Streaming
View Changes
@@ -18,7 +18,7 @@
 func (c *Cache) GetNar(ctx context.Context, narURL nar.URL) (nar.URL, int64, io.ReadCloser, error)
 ```
 
-The returned `nar.URL` contains the `TransparentZstd` flag indicating whether the server was able to serve compressed data directly.
+The returned `nar.URL` contains the `TransparentZstd` flag indicating whether the server was able to serve compressed data directly. The `int64` size can be `-1` (size unknown) when live-streaming an in-flight download, or the concrete `nar_files.file_size` when serving from local storage or completed chunks. Consumers must handle both cases; the streamed bytes are always correct regardless of the size value.
 
 **CDC Chunks and Transparent Compression:**
 

How did I do? Any feedback?  Join Discord

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

Labels

bug Something isn't working go Pull requests that update go code size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant