Add hash map locking to proxy tests

Signed-off-by: Derek McGowan <derek@mcgstyle.net> (github: dmcgowan)
This commit is contained in:
Derek McGowan 2016-08-29 13:39:24 -07:00
parent a50ce1ab93
commit b3e276ff93
2 changed files with 26 additions and 0 deletions

View file

@ -370,15 +370,20 @@ func testProxyStoreServe(t *testing.T, te *testEnv, numClients int) {
wg.Wait() wg.Wait()
remoteBlobCount := len(te.inRemote) remoteBlobCount := len(te.inRemote)
sbsMu.Lock()
if (*localStats)["stat"] != remoteBlobCount*numClients && (*localStats)["create"] != te.numUnique { if (*localStats)["stat"] != remoteBlobCount*numClients && (*localStats)["create"] != te.numUnique {
sbsMu.Unlock()
t.Fatal("Expected: stat:", remoteBlobCount*numClients, "create:", remoteBlobCount) t.Fatal("Expected: stat:", remoteBlobCount*numClients, "create:", remoteBlobCount)
} }
sbsMu.Unlock()
// Wait for any async storage goroutines to finish // Wait for any async storage goroutines to finish
time.Sleep(3 * time.Second) time.Sleep(3 * time.Second)
sbsMu.Lock()
remoteStatCount := (*remoteStats)["stat"] remoteStatCount := (*remoteStats)["stat"]
remoteOpenCount := (*remoteStats)["open"] remoteOpenCount := (*remoteStats)["open"]
sbsMu.Unlock()
// Serveblob - blobs come from local // Serveblob - blobs come from local
for _, dr := range te.inRemote { for _, dr := range te.inRemote {
@ -403,6 +408,8 @@ func testProxyStoreServe(t *testing.T, te *testEnv, numClients int) {
remoteStats = te.RemoteStats() remoteStats = te.RemoteStats()
// Ensure remote unchanged // Ensure remote unchanged
sbsMu.Lock()
defer sbsMu.Unlock()
if (*remoteStats)["stat"] != remoteStatCount && (*remoteStats)["open"] != remoteOpenCount { if (*remoteStats)["stat"] != remoteStatCount && (*remoteStats)["open"] != remoteOpenCount {
t.Fatalf("unexpected remote stats: %#v", remoteStats) t.Fatalf("unexpected remote stats: %#v", remoteStats)
} }

View file

@ -2,6 +2,7 @@ package scheduler
import ( import (
"encoding/json" "encoding/json"
"sync"
"testing" "testing"
"time" "time"
@ -38,6 +39,7 @@ func TestSchedule(t *testing.T) {
ref3.String(): true, ref3.String(): true,
} }
var mu sync.Mutex
s := New(context.Background(), inmemory.New(), "/ttl") s := New(context.Background(), inmemory.New(), "/ttl")
deleteFunc := func(repoName reference.Reference) error { deleteFunc := func(repoName reference.Reference) error {
if len(remainingRepos) == 0 { if len(remainingRepos) == 0 {
@ -48,7 +50,9 @@ func TestSchedule(t *testing.T) {
t.Fatalf("Trying to remove nonexistent repo: %s", repoName) t.Fatalf("Trying to remove nonexistent repo: %s", repoName)
} }
t.Log("removing", repoName) t.Log("removing", repoName)
mu.Lock()
delete(remainingRepos, repoName.String()) delete(remainingRepos, repoName.String())
mu.Unlock()
return nil return nil
} }
@ -62,12 +66,17 @@ func TestSchedule(t *testing.T) {
s.add(ref2, 1*timeUnit, entryTypeBlob) s.add(ref2, 1*timeUnit, entryTypeBlob)
func() { func() {
s.Lock()
s.add(ref3, 1*timeUnit, entryTypeBlob) 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)
mu.Lock()
defer mu.Unlock()
if len(remainingRepos) != 0 { if len(remainingRepos) != 0 {
t.Fatalf("Repositories remaining: %#v", remainingRepos) t.Fatalf("Repositories remaining: %#v", remainingRepos)
} }
@ -80,6 +89,7 @@ func TestRestoreOld(t *testing.T) {
ref2.String(): true, ref2.String(): true,
} }
var mu sync.Mutex
deleteFunc := func(r reference.Reference) error { deleteFunc := func(r reference.Reference) error {
if r.String() == ref1.String() && len(remainingRepos) == 2 { if r.String() == ref1.String() && len(remainingRepos) == 2 {
t.Errorf("ref1 should be removed first") t.Errorf("ref1 should be removed first")
@ -88,7 +98,9 @@ func TestRestoreOld(t *testing.T) {
if !ok { if !ok {
t.Fatalf("Trying to remove nonexistent repo: %s", r) t.Fatalf("Trying to remove nonexistent repo: %s", r)
} }
mu.Lock()
delete(remainingRepos, r.String()) delete(remainingRepos, r.String())
mu.Unlock()
return nil return nil
} }
@ -124,6 +136,8 @@ func TestRestoreOld(t *testing.T) {
} }
<-time.After(50 * timeUnit) <-time.After(50 * timeUnit)
mu.Lock()
defer mu.Unlock()
if len(remainingRepos) != 0 { if len(remainingRepos) != 0 {
t.Fatalf("Repositories remaining: %#v", remainingRepos) t.Fatalf("Repositories remaining: %#v", remainingRepos)
} }
@ -138,8 +152,11 @@ func TestStopRestore(t *testing.T) {
ref2.String(): true, ref2.String(): true,
} }
var mu sync.Mutex
deleteFunc := func(r reference.Reference) error { deleteFunc := func(r reference.Reference) error {
mu.Lock()
delete(remainingRepos, r.String()) delete(remainingRepos, r.String())
mu.Unlock()
return nil return nil
} }
@ -169,6 +186,8 @@ func TestStopRestore(t *testing.T) {
} }
<-time.After(500 * timeUnit) <-time.After(500 * timeUnit)
mu.Lock()
defer mu.Unlock()
if len(remainingRepos) != 0 { if len(remainingRepos) != 0 {
t.Fatalf("Repositories remaining: %#v", remainingRepos) t.Fatalf("Repositories remaining: %#v", remainingRepos)
} }