swarm/storage/localstore: protect chunk put with addr lock in tests

This commit is contained in:
Janos Guljas 2019-02-21 17:06:27 +01:00
parent 7e7e5ef8dc
commit 9f5e9d6d94
2 changed files with 18 additions and 12 deletions

View file

@ -151,6 +151,9 @@ func TestDB_SubscribePull_since(t *testing.T) {
})() })()
uploadRandomChunks := func(count int, wanted bool) (last map[uint8]ChunkDescriptor) { uploadRandomChunks := func(count int, wanted bool) (last map[uint8]ChunkDescriptor) {
addrsMu.Lock()
defer addrsMu.Unlock()
last = make(map[uint8]ChunkDescriptor) last = make(map[uint8]ChunkDescriptor)
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
chunk := generateRandomChunk() chunk := generateRandomChunk()
@ -162,7 +165,6 @@ func TestDB_SubscribePull_since(t *testing.T) {
bin := db.po(chunk.Address()) bin := db.po(chunk.Address())
addrsMu.Lock()
if _, ok := addrs[bin]; !ok { if _, ok := addrs[bin]; !ok {
addrs[bin] = make([]storage.Address, 0) addrs[bin] = make([]storage.Address, 0)
} }
@ -170,7 +172,6 @@ func TestDB_SubscribePull_since(t *testing.T) {
addrs[bin] = append(addrs[bin], chunk.Address()) addrs[bin] = append(addrs[bin], chunk.Address())
wantedChunksCount++ wantedChunksCount++
} }
addrsMu.Unlock()
lastTimestampMu.RLock() lastTimestampMu.RLock()
storeTimestamp := lastTimestamp storeTimestamp := lastTimestamp
@ -241,6 +242,9 @@ func TestDB_SubscribePull_until(t *testing.T) {
})() })()
uploadRandomChunks := func(count int, wanted bool) (last map[uint8]ChunkDescriptor) { uploadRandomChunks := func(count int, wanted bool) (last map[uint8]ChunkDescriptor) {
addrsMu.Lock()
defer addrsMu.Unlock()
last = make(map[uint8]ChunkDescriptor) last = make(map[uint8]ChunkDescriptor)
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
chunk := generateRandomChunk() chunk := generateRandomChunk()
@ -252,7 +256,6 @@ func TestDB_SubscribePull_until(t *testing.T) {
bin := db.po(chunk.Address()) bin := db.po(chunk.Address())
addrsMu.Lock()
if _, ok := addrs[bin]; !ok { if _, ok := addrs[bin]; !ok {
addrs[bin] = make([]storage.Address, 0) addrs[bin] = make([]storage.Address, 0)
} }
@ -260,7 +263,6 @@ func TestDB_SubscribePull_until(t *testing.T) {
addrs[bin] = append(addrs[bin], chunk.Address()) addrs[bin] = append(addrs[bin], chunk.Address())
wantedChunksCount++ wantedChunksCount++
} }
addrsMu.Unlock()
lastTimestampMu.RLock() lastTimestampMu.RLock()
storeTimestamp := lastTimestamp storeTimestamp := lastTimestamp
@ -330,6 +332,9 @@ func TestDB_SubscribePull_sinceAndUntil(t *testing.T) {
})() })()
uploadRandomChunks := func(count int, wanted bool) (last map[uint8]ChunkDescriptor) { uploadRandomChunks := func(count int, wanted bool) (last map[uint8]ChunkDescriptor) {
addrsMu.Lock()
defer addrsMu.Unlock()
last = make(map[uint8]ChunkDescriptor) last = make(map[uint8]ChunkDescriptor)
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
chunk := generateRandomChunk() chunk := generateRandomChunk()
@ -341,7 +346,6 @@ func TestDB_SubscribePull_sinceAndUntil(t *testing.T) {
bin := db.po(chunk.Address()) bin := db.po(chunk.Address())
addrsMu.Lock()
if _, ok := addrs[bin]; !ok { if _, ok := addrs[bin]; !ok {
addrs[bin] = make([]storage.Address, 0) addrs[bin] = make([]storage.Address, 0)
} }
@ -349,7 +353,6 @@ func TestDB_SubscribePull_sinceAndUntil(t *testing.T) {
addrs[bin] = append(addrs[bin], chunk.Address()) addrs[bin] = append(addrs[bin], chunk.Address())
wantedChunksCount++ wantedChunksCount++
} }
addrsMu.Unlock()
lastTimestampMu.RLock() lastTimestampMu.RLock()
storeTimestamp := lastTimestamp storeTimestamp := lastTimestamp
@ -410,6 +413,9 @@ func TestDB_SubscribePull_sinceAndUntil(t *testing.T) {
// uploadRandomChunksBin uploads random chunks to database and adds them to // uploadRandomChunksBin uploads random chunks to database and adds them to
// the map of addresses ber bin. // the map of addresses ber bin.
func uploadRandomChunksBin(t *testing.T, db *DB, uploader *Putter, addrs map[uint8][]storage.Address, addrsMu *sync.Mutex, wantedChunksCount *int, count int) { func uploadRandomChunksBin(t *testing.T, db *DB, uploader *Putter, addrs map[uint8][]storage.Address, addrsMu *sync.Mutex, wantedChunksCount *int, count int) {
addrsMu.Lock()
defer addrsMu.Unlock()
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
chunk := generateRandomChunk() chunk := generateRandomChunk()
@ -418,13 +424,11 @@ func uploadRandomChunksBin(t *testing.T, db *DB, uploader *Putter, addrs map[uin
t.Fatal(err) t.Fatal(err)
} }
addrsMu.Lock()
bin := db.po(chunk.Address()) bin := db.po(chunk.Address())
if _, ok := addrs[bin]; !ok { if _, ok := addrs[bin]; !ok {
addrs[bin] = make([]storage.Address, 0) addrs[bin] = make([]storage.Address, 0)
} }
addrs[bin] = append(addrs[bin], chunk.Address()) addrs[bin] = append(addrs[bin], chunk.Address())
addrsMu.Unlock()
*wantedChunksCount++ *wantedChunksCount++
} }

View file

@ -40,6 +40,9 @@ func TestDB_SubscribePush(t *testing.T) {
var chunksMu sync.Mutex var chunksMu sync.Mutex
uploadRandomChunks := func(count int) { uploadRandomChunks := func(count int) {
chunksMu.Lock()
defer chunksMu.Unlock()
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
chunk := generateRandomChunk() chunk := generateRandomChunk()
@ -48,9 +51,7 @@ func TestDB_SubscribePush(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
chunksMu.Lock()
chunks = append(chunks, chunk) chunks = append(chunks, chunk)
chunksMu.Unlock()
} }
} }
@ -127,6 +128,9 @@ func TestDB_SubscribePush_multiple(t *testing.T) {
var addrsMu sync.Mutex var addrsMu sync.Mutex
uploadRandomChunks := func(count int) { uploadRandomChunks := func(count int) {
addrsMu.Lock()
defer addrsMu.Unlock()
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
chunk := generateRandomChunk() chunk := generateRandomChunk()
@ -135,9 +139,7 @@ func TestDB_SubscribePush_multiple(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
addrsMu.Lock()
addrs = append(addrs, chunk.Address()) addrs = append(addrs, chunk.Address())
addrsMu.Unlock()
} }
} }