Skip to content

Commit d834ec2

Browse files
author
Andrei Smirnov
authored
core: flakey tests fixes (#4283)
Better flakey test (qbft) fix: changed the test to explicitly establish connections between all hosts before creating consensus components. Verified with `go test -v -run 'TestQBFTConsensus$' ./core/consensus/qbft/ -count=100` category: fixbuild ticket: none
1 parent e1f4b2f commit d834ec2

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

‎core/consensus/qbft/qbft_test.go‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
libp2pcrypto "github.com/libp2p/go-libp2p/core/crypto"
1414
"github.com/libp2p/go-libp2p/core/host"
1515
"github.com/libp2p/go-libp2p/core/peer"
16-
"github.com/libp2p/go-libp2p/core/peerstore"
1716
"github.com/multiformats/go-multiaddr"
1817
"github.com/stretchr/testify/mock"
1918
"github.com/stretchr/testify/require"
@@ -114,16 +113,20 @@ func testQBFTConsensus(t *testing.T, threshold, nodes int) {
114113
hosts = append(hosts, h)
115114
}
116115

117-
// Connect each host with its peers
116+
// Connect each host with its peers (establish actual connections, not just add addresses)
118117
for i := range threshold {
119118
for j := range threshold {
120119
if i == j {
121120
continue
122121
}
123122

124-
hosts[i].Peerstore().AddAddrs(hostsInfo[j].ID, hostsInfo[j].Addrs, peerstore.PermanentAddrTTL)
123+
err := hosts[i].Connect(ctx, hostsInfo[j])
124+
require.NoError(t, err)
125125
}
126+
}
126127

128+
// Create consensus components
129+
for i := range threshold {
127130
sniffer := func(msgs *pbv1.SniffedConsensusInstance) {
128131
sniffed <- len(msgs.GetMsgs())
129132
}

‎core/scheduler/scheduler_test.go‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ func TestSchedulerWait(t *testing.T) {
146146
var t0 time.Time
147147

148148
clock := newTestClock(t0)
149+
150+
// Set a deadline to prevent the slot ticker goroutine from advancing
151+
// the clock past the expected wait time. This avoids race conditions
152+
// where the goroutine advances time before we can measure it.
153+
expectedEndTime := t0.Add(time.Duration(test.WaitSecs) * time.Second)
154+
clock.SetDeadline(expectedEndTime)
155+
149156
eth2Cl, err := beaconmock.New(t.Context())
150157
require.NoError(t, err)
151158

@@ -667,10 +674,21 @@ func newTestClock(now time.Time) *testClock {
667674
type testClock struct {
668675
nowMutex sync.Mutex
669676
now time.Time
677+
deadline time.Time // If set, clock won't advance past this time
670678
callbacks map[time.Time]func()
671679
paused atomic.Bool
672680
}
673681

682+
// SetDeadline sets a deadline time that the clock cannot advance past.
683+
// Any Sleep that would advance past the deadline will instead advance to the deadline and pause.
684+
// This is useful for tests that need to prevent background goroutines from advancing time.
685+
func (c *testClock) SetDeadline(deadline time.Time) {
686+
c.nowMutex.Lock()
687+
defer c.nowMutex.Unlock()
688+
689+
c.deadline = deadline
690+
}
691+
674692
// CallbackAfter sets a callback function that is called once
675693
// before Sleep returns at or after the time has been reached.
676694
// It is useful to trigger logic "when a certain time has been reached".
@@ -695,7 +713,15 @@ func (c *testClock) Sleep(d time.Duration) {
695713
c.nowMutex.Lock()
696714
defer c.nowMutex.Unlock()
697715

698-
c.now = c.now.Add(d)
716+
newTime := c.now.Add(d)
717+
718+
// If deadline is set and we would advance past it, cap at deadline and pause
719+
if !c.deadline.IsZero() && newTime.After(c.deadline) {
720+
c.now = c.deadline
721+
c.paused.Store(true)
722+
} else {
723+
c.now = newTime
724+
}
699725

700726
for after, callback := range c.callbacks {
701727
if c.now.Before(after) {

0 commit comments

Comments
 (0)