From 87bbd617bddf828c3735c45b0bb2c3709289359e Mon Sep 17 00:00:00 2001 From: Janos Guljas Date: Mon, 14 Jan 2019 11:22:12 +0100 Subject: [PATCH] swarm/storage/localstore: protect slices in push subs test --- swarm/storage/localstore/subscription_push_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/swarm/storage/localstore/subscription_push_test.go b/swarm/storage/localstore/subscription_push_test.go index 1c4b1465d2..34742017c9 100644 --- a/swarm/storage/localstore/subscription_push_test.go +++ b/swarm/storage/localstore/subscription_push_test.go @@ -20,6 +20,7 @@ import ( "bytes" "context" "fmt" + "sync" "testing" "time" @@ -38,6 +39,7 @@ func TestDB_SubscribePush(t *testing.T) { uploader := db.NewPutter(ModePutUpload) chunks := make([]storage.Chunk, 0) + var chunksMu sync.Mutex uploadRandomChunks := func(count int) { for i := 0; i < count; i++ { @@ -48,7 +50,9 @@ func TestDB_SubscribePush(t *testing.T) { t.Fatal(err) } + chunksMu.Lock() chunks = append(chunks, chunk) + chunksMu.Unlock() } } @@ -76,7 +80,9 @@ func TestDB_SubscribePush(t *testing.T) { if !ok { return } + chunksMu.Lock() want := chunks[i] + chunksMu.Unlock() var err error if !bytes.Equal(got.Data(), want.Data()) { err = fmt.Errorf("got chunk %v data %x, want %x", i, got.Data(), want.Data()) @@ -118,6 +124,7 @@ func TestDB_SubscribePush_multiple(t *testing.T) { uploader := db.NewPutter(ModePutUpload) addrs := make([]storage.Address, 0) + var addrsMu sync.Mutex uploadRandomChunks := func(count int) { for i := 0; i < count; i++ { @@ -128,7 +135,9 @@ func TestDB_SubscribePush_multiple(t *testing.T) { t.Fatal(err) } + addrsMu.Lock() addrs = append(addrs, chunk.Address()) + addrsMu.Unlock() } } @@ -161,7 +170,9 @@ func TestDB_SubscribePush_multiple(t *testing.T) { if !ok { return } + addrsMu.Lock() want := addrs[i] + addrsMu.Unlock() var err error if !bytes.Equal(got.Address(), want) { err = fmt.Errorf("got chunk %v address on subscription %v %s, want %s", i, j, got, want)