Skip to content

Git designed for AI agents.

syncer: TestSyncerRoundBoundedBySyncTimeout waits for the round, not for a read

Federico Elia0fb82b1a520f
diff --git a/internal/graft/corestore/syncer/export_test.go b/internal/graft/corestore/syncer/export_test.go
index 9ab2d4a..9efd31e 100644
--- a/internal/graft/corestore/syncer/export_test.go
+++ b/internal/graft/corestore/syncer/export_test.go
@@ -18,9 +18,10 @@ func LockBuild(dir string, afterOpen func(path string)) (*os.File, error) {
}
// SetTestHook installs a function called at the named points of a catch-up
-// ("fetched", "checked") and of a seed ("seed" before the pack is indexed,
-// "seeded" after, "installed" after the install). Only for tests; set it
-// before the first EnsureFresh.
+// ("fetched", "checked"), of a seed ("seed" before the pack is indexed,
+// "seeded" after, "installed" after the install) and of a sync round
+// ("synced" once it ended). Only for tests; set it before the first
+// EnsureFresh.
func (s *Syncer) SetTestHook(hook func(point string)) {
s.testHook = hook
}
diff --git a/internal/graft/corestore/syncer/syncer.go b/internal/graft/corestore/syncer/syncer.go
index 2711f9a..6f3a924 100644
--- a/internal/graft/corestore/syncer/syncer.go
+++ b/internal/graft/corestore/syncer/syncer.go
@@ -264,6 +264,8 @@ func (s *Syncer) EnsureFresh(ctx context.Context, repo storage.Repository) error
ticket := s.headReads.Add(1)
err := s.sync(syncCtx, key)
+ // The round has ended, and with it every read it started.
+ s.hook("synced")
if err != nil && syncCtx.Err() != nil {
// The budget ran out (lock contention, a slow database or bucket).
return ticket, structerr.NewUnavailable("storage core: sync of %s did not finish within %s: %v", key, timeout, err)
diff --git a/internal/graft/corestore/syncer/syncer_checkpoint_test.go b/internal/graft/corestore/syncer/syncer_checkpoint_test.go
index be05aa7..93b8f36 100644
--- a/internal/graft/corestore/syncer/syncer_checkpoint_test.go
+++ b/internal/graft/corestore/syncer/syncer_checkpoint_test.go
@@ -180,13 +180,24 @@ type blockingBlobs struct {
func (b blockingBlobs) Get(ctx context.Context, key string) (io.ReadCloser, error) {
<-ctx.Done()
- b.cancelled <- ctx.Err()
+ // The first read's cause is kept; a later read must not block the round.
+ select {
+ case b.cancelled <- ctx.Err():
+ default:
+ }
return nil, ctx.Err()
}
// TestSyncerRoundBoundedBySyncTimeout: a sync round is bounded by sync_timeout,
// which cancels its bucket reads: a hydration whose download outlives it fails
// as Unavailable and its read is cancelled (GC BOUNDARY rule, enforcement).
+//
+// On a loaded machine the 200 ms budget can run out before the round starts a
+// pack read (the round stops starting reads near its deadline, or a database
+// call meets it first): the outcome is then the same, without a read. The
+// round can also outlive the call, whose own deadline is set just before it.
+// So the test waits for the round's end ("synced"), and checks the read's
+// cancellation whenever a read started.
func TestSyncerRoundBoundedBySyncTimeout(t *testing.T) {
a := newEnv(t, envOptions{})
src := a.newSource(t)
@@ -197,10 +208,25 @@ func TestSyncerRoundBoundedBySyncTimeout(t *testing.T) {
cancelled := make(chan error, 1)
b.core.Blobs = blockingBlobs{BlobStore: b.core.Blobs, cancelled: cancelled}
b.core.SyncTimeout = 200 * time.Millisecond
+ synced := make(chan struct{})
+ b.sy.SetTestHook(once("synced", func() { close(synced) }))
err := b.sy.EnsureFresh(b.ctx, b.pbRepo(localRelPath))
require.Equal(t, codes.Unavailable, status.Code(err), "%v", err)
- require.ErrorIs(t, <-cancelled, context.DeadlineExceeded, "the bucket read was cancelled by the round's deadline")
+
+ // A deadlock detector, not a timing assumption: the round ends at its
+ // 200 ms deadline.
+ select {
+ case <-synced:
+ case <-time.After(time.Minute):
+ t.Fatal("the sync round did not end")
+ }
+ select {
+ case readErr := <-cancelled:
+ require.ErrorIs(t, readErr, context.DeadlineExceeded, "the bucket read was cancelled by the round's deadline")
+ default:
+ t.Log("the round's budget ran out before it started a pack read")
+ }
_, ok, err := b.sy.Applied(b.ctx, b.pbRepo(localRelPath))
require.NoError(t, err)