Skip to content

Commit

Permalink
fix: Fix data race test coverage. (cosmos#20156)
Browse files Browse the repository at this point in the history
  • Loading branch information
tnv1 authored Apr 23, 2024
1 parent 0e3bce7 commit 8a98c7c
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"path/filepath"
"strings"
"sync"
"sync/atomic"
"syscall"
"testing"
"time"
Expand Down Expand Up @@ -647,29 +648,29 @@ func (n *Network) LatestHeight() (int64, error) {
timeout := time.NewTimer(time.Second * 5)
defer timeout.Stop()

var latestHeight int64
var latestHeight atomic.Int64
val := n.Validators[0]
queryClient := cmtservice.NewServiceClient(val.clientCtx)

for {
select {
case <-timeout.C:
return latestHeight, errors.New("timeout exceeded waiting for block")
return latestHeight.Load(), errors.New("timeout exceeded waiting for block")
case <-ticker.C:
done := make(chan struct{})
go func() {
res, err := queryClient.GetLatestBlock(context.Background(), &cmtservice.GetLatestBlockRequest{})
if err == nil && res != nil {
latestHeight = res.SdkBlock.Header.Height
latestHeight.Store(res.SdkBlock.Header.Height)
}
done <- struct{}{}
}()
select {
case <-timeout.C:
return latestHeight, errors.New("timeout exceeded waiting for block")
return latestHeight.Load(), errors.New("timeout exceeded waiting for block")
case <-done:
if latestHeight != 0 {
return latestHeight, nil
if latestHeight.Load() != 0 {
return latestHeight.Load(), nil
}
}
}
Expand Down

0 comments on commit 8a98c7c

Please sign in to comment.