Skip to content

Commit

Permalink
fix: make TestValidatorSetHandling stable (cosmos#313)
Browse files Browse the repository at this point in the history
Instead of `time.Sleep` a channel is used for reliable synchronization
  • Loading branch information
tzdybal authored Mar 7, 2022
1 parent 8b9271f commit 989fb16
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ Month, DD, YYYY
- [conv/abci] [Map LastBlockID.Hash to LastHeaderHash in conversion #303](https://github.com/celestiaorg/optimint/pull/303) [@tzdybal](https://github.com/tzdybal/)
- [chore] [Fix multiple bugs for Ethermint #305](https://github.com/celestiaorg/optimint/pull/305) [@tzdybal](https://github.com/tzdybal/)
- [lint] [Fix linter on main #308](https://github.com/celestiaorg/optimint/pull/308) [@tzdybal](https://github.com/tzdybal/)
- [rpc/client] [Make TestValidatorSetHandling stable](https://github.com/celestiaorg/optimint/pull/313) [@tzdybal](https://github.com/tzdybal/)

- [go package] (Link to PR) Description @username
9 changes: 7 additions & 2 deletions rpc/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,11 +645,15 @@ func TestValidatorSetHandling(t *testing.T) {
pbValKey, err := encoding.PubKeyToProto(vKeys[0].PubKey())
require.NoError(err)

waitCh := make(chan interface{})

app.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{}).Times(5)
app.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{ValidatorUpdates: []abci.ValidatorUpdate{{PubKey: pbValKey, Power: 0}}}).Once()
app.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{}).Once()
app.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{ValidatorUpdates: []abci.ValidatorUpdate{{PubKey: pbValKey, Power: 100}}}).Once()
app.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{})
app.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{}).Run(func(args mock.Arguments) {
waitCh <- nil
})

node, err := node.NewNode(context.Background(), config.NodeConfig{DALayer: "mock", Aggregator: true, BlockManagerConfig: config.BlockManagerConfig{BlockTime: 10 * time.Millisecond}}, key, proxy.NewLocalClientCreator(app), &tmtypes.GenesisDoc{ChainID: "test", Validators: genesisValidators}, log.TestingLogger())
require.NoError(err)
Expand All @@ -673,7 +677,7 @@ func TestValidatorSetHandling(t *testing.T) {
assert.Greater(vals.BlockHeight, lastHeight)
lastHeight = vals.BlockHeight
}
time.Sleep(100 * time.Millisecond)
<-waitCh

// 6th EndBlock removes first validator from the list
for h := int64(7); h <= 8; h++ {
Expand All @@ -687,6 +691,7 @@ func TestValidatorSetHandling(t *testing.T) {

// 8th EndBlock adds validator back
for h := int64(9); h < 12; h++ {
<-waitCh
vals, err := rpc.Validators(context.Background(), &h, nil, nil)
assert.NoError(err)
assert.NotNil(vals)
Expand Down

0 comments on commit 989fb16

Please sign in to comment.