Merge pull request #16214 from b00ris/whisperv6_datarace

whisper: fixed dataraces in peer unit tests
This commit is contained in:
Guillaume Ballet 2018-02-28 14:31:19 +01:00 committed by GitHub
commit 52bb0a1ec7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 7 deletions

View File

@ -23,6 +23,7 @@ import (
mrand "math/rand" mrand "math/rand"
"net" "net"
"sync" "sync"
"sync/atomic"
"testing" "testing"
"time" "time"
@ -71,7 +72,7 @@ var keys = []string{
} }
type TestData struct { type TestData struct {
started int started int64
counter [NumNodes]int counter [NumNodes]int
mutex sync.RWMutex mutex sync.RWMutex
} }
@ -240,9 +241,7 @@ func startServer(t *testing.T, s *p2p.Server) {
t.Fatalf("failed to start the fisrt server.") t.Fatalf("failed to start the fisrt server.")
} }
result.mutex.Lock() atomic.AddInt64(&result.started, 1)
defer result.mutex.Unlock()
result.started++
} }
func stopServers() { func stopServers() {
@ -472,7 +471,10 @@ func checkPowExchange(t *testing.T) {
func checkBloomFilterExchangeOnce(t *testing.T, mustPass bool) bool { func checkBloomFilterExchangeOnce(t *testing.T, mustPass bool) bool {
for i, node := range nodes { for i, node := range nodes {
for peer := range node.shh.peers { for peer := range node.shh.peers {
if !bytes.Equal(peer.bloomFilter, masterBloomFilter) { peer.bloomMu.Lock()
equals := bytes.Equal(peer.bloomFilter, masterBloomFilter)
peer.bloomMu.Unlock()
if !equals {
if mustPass { if mustPass {
t.Fatalf("node %d: failed to exchange bloom filter requirement in round %d. \n%x expected \n%x got", t.Fatalf("node %d: failed to exchange bloom filter requirement in round %d. \n%x expected \n%x got",
i, round, masterBloomFilter, peer.bloomFilter) i, round, masterBloomFilter, peer.bloomFilter)
@ -500,11 +502,13 @@ func checkBloomFilterExchange(t *testing.T) {
func waitForServersToStart(t *testing.T) { func waitForServersToStart(t *testing.T) {
const iterations = 200 const iterations = 200
var started int64
for j := 0; j < iterations; j++ { for j := 0; j < iterations; j++ {
time.Sleep(50 * time.Millisecond) time.Sleep(50 * time.Millisecond)
if result.started == NumNodes { started = atomic.LoadInt64(&result.started)
if started == NumNodes {
return return
} }
} }
t.Fatalf("Failed to start all the servers, running: %d", result.started) t.Fatalf("Failed to start all the servers, running: %d", started)
} }