mirror of
https://github.com/distribution/distribution.git
synced 2025-08-19 23:38:13 +00:00
Fixed data race in TestSchedule test (#4647)
This commit is contained in:
commit
7f2dad4420
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user