diff --git a/seaweed-block/core/frontend/durable/provider_restore_test.go b/seaweed-block/core/frontend/durable/provider_restore_test.go index 63cc0ab70f..f52f111b48 100644 --- a/seaweed-block/core/frontend/durable/provider_restore_test.go +++ b/seaweed-block/core/frontend/durable/provider_restore_test.go @@ -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) } diff --git a/seaweed-block/core/host/volume/chunk_restore_test.go b/seaweed-block/core/host/volume/chunk_restore_test.go index 17b264e38a..d70420b8b4 100644 --- a/seaweed-block/core/host/volume/chunk_restore_test.go +++ b/seaweed-block/core/host/volume/chunk_restore_test.go @@ -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) diff --git a/seaweed-block/core/storage/chunk_hydrate_all_test.go b/seaweed-block/core/storage/chunk_hydrate_all_test.go index bed6e2297b..9efd0c2d57 100644 --- a/seaweed-block/core/storage/chunk_hydrate_all_test.go +++ b/seaweed-block/core/storage/chunk_hydrate_all_test.go @@ -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)) } } }