Fixed data race in TestSchedule test (#4647)

This commit is contained in:
João Pereira 2025-08-11 23:25:37 +01:00 committed by GitHub
commit 7f2dad4420
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,13 +2,15 @@ package scheduler
import ( import (
"encoding/json" "encoding/json"
"fmt"
"sync" "sync"
"testing" "testing"
"time" "time"
"github.com/distribution/reference"
"github.com/distribution/distribution/v3/internal/dcontext" "github.com/distribution/distribution/v3/internal/dcontext"
"github.com/distribution/distribution/v3/registry/storage/driver/inmemory" "github.com/distribution/distribution/v3/registry/storage/driver/inmemory"
"github.com/distribution/reference"
) )
func testRefs(t *testing.T) (reference.Reference, reference.Reference, reference.Reference) { func testRefs(t *testing.T) (reference.Reference, reference.Reference, reference.Reference) {
@ -30,13 +32,34 @@ func testRefs(t *testing.T) (reference.Reference, reference.Reference, reference
return ref1, ref2, ref3 return ref1, ref2, ref3
} }
func testRefsN(t *testing.T, n int) []reference.Canonical {
refs := make([]reference.Canonical, 0, n)
for i := range n {
name := "testrepo@sha256:" + fmt.Sprintf("%064d", i)
ref, err := reference.Parse(name)
if err != nil {
t.Fatalf("could not parse reference: %v", err)
}
canonical, ok := ref.(reference.Canonical)
if !ok {
t.Fatalf("not canonical reference: %v", ref)
}
refs = append(refs, canonical)
}
return refs
}
func TestSchedule(t *testing.T) { func TestSchedule(t *testing.T) {
ref1, ref2, ref3 := testRefs(t) refs := testRefsN(t, 20)
timeUnit := time.Millisecond timeUnit := time.Millisecond
remainingRepos := map[string]bool{
ref1.String(): true, remainingRepos := map[string]bool{}
ref2.String(): true, for _, ref := range refs {
ref3.String(): true, remainingRepos[ref.String()] = true
} }
var mu sync.Mutex var mu sync.Mutex
@ -62,14 +85,9 @@ func TestSchedule(t *testing.T) {
t.Fatalf("Error starting ttlExpirationScheduler: %s", err) t.Fatalf("Error starting ttlExpirationScheduler: %s", err)
} }
s.add(ref1, 3*timeUnit, entryTypeBlob) for i, ref := range refs {
s.add(ref2, 1*timeUnit, entryTypeBlob) _ = s.AddBlob(ref, time.Duration(i)*timeUnit)
}
func() {
s.Lock()
s.add(ref3, 1*timeUnit, entryTypeBlob)
s.Unlock()
}()
// Ensure all repos are deleted // Ensure all repos are deleted
<-time.After(50 * timeUnit) <-time.After(50 * timeUnit)