@@ -23,6 +23,7 @@ import (
23
23
mrand "math/rand"
24
24
"net"
25
25
"sync"
26
+ "sync/atomic"
26
27
"testing"
27
28
"time"
28
29
@@ -71,7 +72,7 @@ var keys = []string{
71
72
}
72
73
73
74
type TestData struct {
74
- started int
75
+ started int64
75
76
counter [NumNodes ]int
76
77
mutex sync.RWMutex
77
78
}
@@ -240,9 +241,7 @@ func startServer(t *testing.T, s *p2p.Server) {
240
241
t .Fatalf ("failed to start the fisrt server." )
241
242
}
242
243
243
- result .mutex .Lock ()
244
- defer result .mutex .Unlock ()
245
- result .started ++
244
+ atomic .AddInt64 (& result .started , 1 )
246
245
}
247
246
248
247
func stopServers () {
@@ -472,7 +471,10 @@ func checkPowExchange(t *testing.T) {
472
471
func checkBloomFilterExchangeOnce (t * testing.T , mustPass bool ) bool {
473
472
for i , node := range nodes {
474
473
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 {
476
478
if mustPass {
477
479
t .Fatalf ("node %d: failed to exchange bloom filter requirement in round %d. \n %x expected \n %x got" ,
478
480
i , round , masterBloomFilter , peer .bloomFilter )
@@ -500,11 +502,13 @@ func checkBloomFilterExchange(t *testing.T) {
500
502
501
503
func waitForServersToStart (t * testing.T ) {
502
504
const iterations = 200
505
+ var started int64
503
506
for j := 0 ; j < iterations ; j ++ {
504
507
time .Sleep (50 * time .Millisecond )
505
- if result .started == NumNodes {
508
+ started = atomic .LoadInt64 (& result .started )
509
+ if started == NumNodes {
506
510
return
507
511
}
508
512
}
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 )
510
514
}
0 commit comments