Skip to content

Add SessionBuilder.Cache - #24

Merged
winebarrel merged 1 commit into
mainfrom
session-cache
Nov 8, 2026
Merged

winebarrel merged 1 commit into
mainfrom
session-cache

Conversation

@winebarrel

Copy link
Copy Markdown
Owner

Summary

  • Add SessionBuilder.Cache, which makes Session return the session cached for t if there is one, and otherwise open one and cache it until t and its subtests finish. Helpers can get the session from t alone.
  • The cache belongs to the Sandpit, so separate With().Cache() calls on one Sandpit share it.
  • The options of the call that opened the session apply, and those of later calls are ignored.
  • Sessions built without Cache are neither read from nor added to the cache, so sandpit.Session(t) still opens a new session each time.
  • A subtest has its own t, so it gets its own session rather than its parent's.
  • Concurrent calls with the same t open one session. If opening it fails the test, the other calls fail the test too instead of returning nil.
  • Update docs, README and tests.
var cached = sandpit.With().Cache()

func insertOrder(t *testing.T) {
	t.Helper()
	db := cached.Session(t).DB()
	// ...
}

func TestOrder(t *testing.T) {
	insertOrder(t)
	s := cached.Session(t) // the session insertOrder used
	// ...
}

Test plan

  • make vet
  • go test -race -count=1 ./...
  • go test -race -count=20 -run 'TestCache' ./...
  • golangci-lint run

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.08%. Comparing base (4a4435d) to head (898f691).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #24      +/-   ##
==========================================
+ Coverage   92.75%   93.08%   +0.32%     
==========================================
  Files           7        7              
  Lines         359      376      +17     
==========================================
+ Hits          333      350      +17     
  Misses         14       14              
  Partials       12       12              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@winebarrel
winebarrel merged commit 24bf93b into main Nov 8, 2026
4 checks passed
@winebarrel
winebarrel deleted the session-cache branch November 8, 2026 08:37
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.

1 participant