Skip to content

Commit 52bb0a1

Browse files
authored
Merge pull request #16214 from b00ris/whisperv6_datarace
whisper: fixed dataraces in peer unit tests
2 parents 1843615 + 62c239f commit 52bb0a1

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

whisper/whisperv6/peer_test.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
mrand "math/rand"
2424
"net"
2525
"sync"
26+
"sync/atomic"
2627
"testing"
2728
"time"
2829

@@ -71,7 +72,7 @@ var keys = []string{
7172
}
7273

7374
type TestData struct {
74-
started int
75+
started int64
7576
counter [NumNodes]int
7677
mutex sync.RWMutex
7778
}
@@ -240,9 +241,7 @@ func startServer(t *testing.T, s *p2p.Server) {
240241
t.Fatalf("failed to start the fisrt server.")
241242
}
242243

243-
result.mutex.Lock()
244-
defer result.mutex.Unlock()
245-
result.started++
244+
atomic.AddInt64(&result.started, 1)
246245
}
247246

248247
func stopServers() {
@@ -472,7 +471,10 @@ func checkPowExchange(t *testing.T) {
472471
func checkBloomFilterExchangeOnce(t *testing.T, mustPass bool) bool {
473472
for i, node := range nodes {
474473
for peer := range node.shh.peers {
475-
if !bytes.Equal(peer.bloomFilter, masterBloomFilter) {
474+
peer.bloomMu.Lock()
475+
equals := bytes.Equal(peer.bloomFilter, masterBloomFilter)
476+
peer.bloomMu.Unlock()
477+
if !equals {
476478
if mustPass {
477479
t.Fatalf("node %d: failed to exchange bloom filter requirement in round %d. \n%x expected \n%x got",
478480
i, round, masterBloomFilter, peer.bloomFilter)
@@ -500,11 +502,13 @@ func checkBloomFilterExchange(t *testing.T) {
500502

501503
func waitForServersToStart(t *testing.T) {
502504
const iterations = 200
505+
var started int64
503506
for j := 0; j < iterations; j++ {
504507
time.Sleep(50 * time.Millisecond)
505-
if result.started == NumNodes {
508+
started = atomic.LoadInt64(&result.started)
509+
if started == NumNodes {
506510
return
507511
}
508512
}
509-
t.Fatalf("Failed to start all the servers, running: %d", result.started)
513+
t.Fatalf("Failed to start all the servers, running: %d", started)
510514
}

0 commit comments

Comments
 (0)