test fold: pace origin before the call, GoOwned held settlement, owner-close teardown order

- The pacing test measures each admission from a time taken before the call,
  a lower bound of the walker's own start, keeping the exact 8 fills, Gets
  and admissions.
- The GoOwned test bounds every wait, pre-registers release and a bounded
  join of Close, and witnesses that Close does not return while the owned
  goroutine is held after its ctx ended (no sleep).
- The owner-close driver test releases the held provider call before the
  owner's join at teardown, so its red case cannot leave a worker behind.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-21 03:08:35 -08:00
1 parent ef9155e660
commit 7fa772b975
3 files changed
+42 -18

No files matched your search

@@ -10,34 +10,53 @@ import (
)
// A goroutine started with GoOwned sees its ctx end when Close starts, and
// Close returns only after it has returned; after Close nothing is admitted.
// Close does not return while it is still settling; after Close nothing is
// admitted.
func TestDurableProvider_GoOwnedIsCancelledAndJoinedByClose(t *testing.T) {
p, err := durable.NewDurableProvider(durable.ProviderConfig{Impl: durable.ImplWALStore, StorageRoot: t.TempDir(), BlockSize: 4096, NumBlocks: 64}, v1View())
if err != nil {
t.Fatal(err)
}
started, finished := make(chan struct{}), make(chan struct{})
started, cancelled, release := make(chan struct{}), make(chan struct{}), make(chan struct{})
closed := make(chan struct{})
released := false
t.Cleanup(func() { // registered before launch: release, then a bounded join of Close
if !released {
close(release)
}
select {
case <-closed:
case <-time.After(5 * time.Second):
t.Error("cleanup: Close did not return")
}
})
if err := p.GoOwned(func(ctx context.Context) {
close(started)
<-ctx.Done()
time.Sleep(20 * time.Millisecond) // still settling when Close is called
close(finished)
close(cancelled)
<-release // held settlement: Close must wait for this
}); err != nil {
t.Fatal(err)
}
<-started
closed := make(chan struct{})
wait := func(ch <-chan struct{}, what string) {
t.Helper()
select {
case <-ch:
case <-time.After(5 * time.Second):
t.Fatalf("timed out waiting for %s", what)
}
}
wait(started, "the owned goroutine to start")
go func() { _ = p.Close(); close(closed) }()
wait(cancelled, "Close to cancel the owned goroutine's ctx")
select {
case <-closed:
select {
case <-finished:
default:
t.Fatal("Close returned before the owned goroutine finished")
}
case <-time.After(5 * time.Second):
t.Fatal("Close did not return")
t.Fatal("Close returned while the owned goroutine was still settling")
default:
}
released = true
close(release)
wait(closed, "Close to return after the owned goroutine returned")
if err := p.GoOwned(func(context.Context) {}); !errors.Is(err, durable.ErrProviderClosed) {
t.Fatalf("GoOwned after Close: %v, want ErrProviderClosed", err)
}
@@ -368,7 +368,10 @@ func TestRestoreDriver_ChunkControlStartsOneRunAndReportsIt(t *testing.T) {
func TestRestoreDriver_OwnerCloseCancelsAndJoinsTheRun(t *testing.T) {
_, cat, p := restoreFixtureHost(t, 3)
owner := newTestOwner(t)
p.entered, p.gate = make(chan struct{}, 1), make(chan struct{}) // gate never opens
p.entered, p.gate = make(chan struct{}, 1), make(chan struct{}) // opened only at teardown
// Registered after the owner: it runs first, so a run that ignored the
// owner's cancel (the red case) is released before the owner's join.
t.Cleanup(func() { close(p.gate) })
ctl := &chunkControl{p: restoreOnlyProvider{p, owner}, cat: cat}
if _, err := ctl.Restore(context.Background(), "v2", true); err != nil {
t.Fatal(err)
@@ -415,6 +415,7 @@ func TestHydrateAll_PacedByByteRate(t *testing.T) {
budget := &timedBudget{}
layer.cfg.Budget = budget // admission times witness the pace
rate := int64(4 * ctChunkBytes)
origin := time.Now() // a lower bound of the walker's own pace start
p, err := layer.HydrateAllAt(boundedCtx(t, context.Background()), rate)
want := ctNumBlocks / ctChunkBlocks
if err != nil || p.Remaining != 0 || p.Filled != want {
@@ -433,10 +434,11 @@ func TestHydrateAll_PacedByByteRate(t *testing.T) {
t.Fatalf("%d admissions, want %d", len(budget.at), want)
}
for i := 1; i < len(budget.at); i++ {
// fill i is admitted no earlier than i chunks at the rate from the start
due := budget.at[0].Add(time.Duration(i) * time.Second / 4)
if budget.at[i].Before(due.Add(-10 * time.Millisecond)) {
t.Fatalf("fill %d admitted %v after the first, before its pace time %v", i, budget.at[i].Sub(budget.at[0]), due.Sub(budget.at[0]))
// fill i is admitted no earlier than i chunks at the rate after the
// walker started, and the walker started after origin
due := origin.Add(time.Duration(i) * time.Second / 4)
if budget.at[i].Before(due) {
t.Fatalf("fill %d admitted %v after the call, before its pace time %v", i, budget.at[i].Sub(origin), due.Sub(origin))
}
}
}