all: fix staticcheck warning SA2002: must call T.Fatalf in same goroutine

This commit is contained in:
Daniel Liu 2024-10-29 12:55:45 +08:00
parent 52dd69ecdb
commit 11285be830
3 changed files with 16 additions and 36 deletions

View file

@ -986,14 +986,17 @@ func TestCanonicalBlockRetrieval(t *testing.T) {
continue // busy wait for canonical hash to be written continue // busy wait for canonical hash to be written
} }
if ch != block.Hash() { if ch != block.Hash() {
t.Fatalf("unknown canonical hash, want %s, got %s", block.Hash().Hex(), ch.Hex()) t.Errorf("unknown canonical hash, want %s, got %s", block.Hash().Hex(), ch.Hex())
return
} }
fb := GetBlock(blockchain.db, ch, block.NumberU64()) fb := GetBlock(blockchain.db, ch, block.NumberU64())
if fb == nil { if fb == nil {
t.Fatalf("unable to retrieve block %d for canonical hash: %s", block.NumberU64(), ch.Hex()) t.Errorf("unable to retrieve block %d for canonical hash: %s", block.NumberU64(), ch.Hex())
return
} }
if fb.Hash() != block.Hash() { if fb.Hash() != block.Hash() {
t.Fatalf("invalid block hash for block %d, want %s, got %s", block.NumberU64(), block.Hash().Hex(), fb.Hash().Hex()) t.Errorf("invalid block hash for block %d, want %s, got %s", block.NumberU64(), block.Hash().Hex(), fb.Hash().Hex())
return
} }
return return
} }

View file

@ -80,14 +80,17 @@ func TestMocker(t *testing.T) {
var opts SubscribeOpts var opts SubscribeOpts
sub, err := client.SubscribeNetwork(events, opts) sub, err := client.SubscribeNetwork(events, opts)
defer sub.Unsubscribe() defer sub.Unsubscribe()
//wait until all nodes are started and connected
//store every node up event in a map (value is irrelevant, mimic Set datatype) // wait until all nodes are started and connected
// store every node up event in a map (value is irrelevant, mimic Set datatype)
nodemap := make(map[discover.NodeID]bool) nodemap := make(map[discover.NodeID]bool)
wg.Add(1)
nodesComplete := false nodesComplete := false
connCount := 0 connCount := 0
wg.Add(1)
go func() { go func() {
for { defer wg.Done()
for connCount < (nodeCount-1)*2 {
select { select {
case event := <-events: case event := <-events:
//if the event is a node Up event only //if the event is a node Up event only
@ -102,14 +105,10 @@ func TestMocker(t *testing.T) {
} }
} else if event.Conn != nil && nodesComplete { } else if event.Conn != nil && nodesComplete {
connCount += 1 connCount += 1
if connCount == (nodeCount-1)*2 {
wg.Done()
return
}
} }
case <-time.After(30 * time.Second): case <-time.After(30 * time.Second):
wg.Done() t.Errorf("Timeout waiting for nodes being started up!")
t.Fatalf("Timeout waiting for nodes being started up!") return
} }
} }
}() }()

View file

@ -23,7 +23,6 @@ import (
mrand "math/rand" mrand "math/rand"
"net" "net"
"sync" "sync"
"sync/atomic"
"testing" "testing"
"time" "time"
@ -72,7 +71,6 @@ var keys = []string{
} }
type TestData struct { type TestData struct {
started int64
counter [NumNodes]int counter [NumNodes]int
mutex sync.RWMutex mutex sync.RWMutex
} }
@ -226,14 +224,9 @@ func initialize(t *testing.T) {
}, },
} }
startServer(t, node.server)
nodes[i] = &node nodes[i] = &node
} }
for i := 0; i < NumNodes; i++ {
go startServer(t, nodes[i].server)
}
waitForServersToStart(t)
} }
func startServer(t *testing.T, s *p2p.Server) { func startServer(t *testing.T, s *p2p.Server) {
@ -241,8 +234,6 @@ func startServer(t *testing.T, s *p2p.Server) {
if err != nil { if err != nil {
t.Fatalf("failed to start the fisrt server.") t.Fatalf("failed to start the fisrt server.")
} }
atomic.AddInt64(&result.started, 1)
} }
func stopServers() { func stopServers() {
@ -500,16 +491,3 @@ func checkBloomFilterExchange(t *testing.T) {
time.Sleep(50 * time.Millisecond) time.Sleep(50 * time.Millisecond)
} }
} }
func waitForServersToStart(t *testing.T) {
const iterations = 200
var started int64
for j := 0; j < iterations; j++ {
time.Sleep(50 * time.Millisecond)
started = atomic.LoadInt64(&result.started)
if started == NumNodes {
return
}
}
t.Fatalf("Failed to start all the servers, running: %d", started)
}