Skip to content

Commit

Permalink
Fix race in leader election test
Browse files Browse the repository at this point in the history
  • Loading branch information
agouin committed Nov 9, 2023
1 parent 5c9a04e commit 833ade9
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions signer/threshold_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,25 @@ func testThresholdValidatorLeaderElection(t *testing.T, threshold, total uint8)
require.NoError(t, err)
}

electChan := make(chan struct{})
var done bool
var mu sync.Mutex
isDone := func() bool {
mu.Lock()
defer mu.Unlock()
return done
}
setDone := func() {
mu.Lock()
defer mu.Unlock()
done = true
}

go func() {
for i := 0; true; i++ {
if isDone() {
break
}
// simulate leader election
for _, l := range leaders {
l.SetLeader(nil)
Expand All @@ -387,6 +404,7 @@ func testThresholdValidatorLeaderElection(t *testing.T, threshold, total uint8)
// time with new leader
time.Sleep(time.Duration(mrand.Intn(50)+100) * time.Millisecond) //nolint:gosec
}
electChan <- struct{}{}
}()

// sign 20 blocks (proposal, prevote, precommit)
Expand Down Expand Up @@ -503,8 +521,12 @@ func testThresholdValidatorLeaderElection(t *testing.T, threshold, total uint8)
}

wg.Wait()

require.True(t, success) // at least one should succeed so that the block is not missed.
}

setDone()
<-electChan
}

func TestThresholdValidatorLeaderElection2of3(t *testing.T) {
Expand Down

0 comments on commit 833ade9

Please sign in to comment.