Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oblitirate test flakiness #756

Merged
merged 3 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions core/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,41 @@ package core
import (
"context"
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/types"
)

func TestRemoteClient_Status(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
t.Cleanup(cancel)

_, client := StartTestClient(ctx, t)

status, err := client.Status(ctx)
require.NoError(t, err)
require.NotNil(t, status)
}

func TestRemoteClient_StartBlockSubscription_And_GetBlock(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
t.Cleanup(cancel)

_, client := StartTestClient(ctx, t)

eventChan, err := client.Subscribe(ctx, newBlockSubscriber, newBlockEventQuery)
require.NoError(t, err)

for i := 1; i <= 3; i++ {
<-eventChan
// check that `Block` works as intended (passing nil to get block at latest height)
block, err := client.Block(ctx, nil)
require.NoError(t, err)
require.Equal(t, int64(i), block.Block.Height)
select {
case evt := <-eventChan:
h := evt.Data.(types.EventDataNewBlock).Block.Height
block, err := client.Block(ctx, &h)
require.NoError(t, err)
require.GreaterOrEqual(t, block.Block.Height, int64(i))
case <-ctx.Done():
require.NoError(t, ctx.Err())
}
}

// unsubscribe to event channel
require.NoError(t, client.Unsubscribe(ctx, newBlockSubscriber, newBlockEventQuery))
}
43 changes: 29 additions & 14 deletions core/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ package core
import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/types"

"github.com/tendermint/tendermint/libs/bytes"
)

func TestBlockFetcher_GetBlock_and_SubscribeNewBlockEvent(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
t.Cleanup(cancel)

_, client := StartTestClient(ctx, t)
Expand All @@ -22,21 +24,24 @@ func TestBlockFetcher_GetBlock_and_SubscribeNewBlockEvent(t *testing.T) {
require.NoError(t, err)

for i := 1; i < 3; i++ {
newBlockFromChan := <-newBlockChan

block, err := fetcher.GetBlock(ctx, nil)
require.NoError(t, err)

assert.Equal(t, newBlockFromChan, block)
select {
case newBlockFromChan := <-newBlockChan:
h := newBlockFromChan.Height
block, err := fetcher.GetBlock(ctx, &h)
require.NoError(t, err)
assert.Equal(t, newBlockFromChan, block)
require.GreaterOrEqual(t, block.Height, int64(i))
case <-ctx.Done():
require.NoError(t, ctx.Err())
}
}

require.NoError(t, fetcher.UnsubscribeNewBlockEvent(ctx))
}

// TestBlockFetcherHeaderValues tests that both the Commit and ValidatorSet
// endpoints are working as intended.
func TestBlockFetcherHeaderValues(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
t.Cleanup(cancel)

_, client := StartTestClient(ctx, t)
Expand All @@ -46,15 +51,26 @@ func TestBlockFetcherHeaderValues(t *testing.T) {
newBlockChan, err := fetcher.SubscribeNewBlockEvent(ctx)
require.NoError(t, err)
// read once from channel to generate next block
<-newBlockChan
var h int64
select {
case evt := <-newBlockChan:
h = evt.Header.Height
case <-ctx.Done():
require.NoError(t, ctx.Err())
}
// get Commit from current height
commit, err := fetcher.Commit(ctx, nil)
commit, err := fetcher.Commit(ctx, &h)
require.NoError(t, err)
// get ValidatorSet from current height
valSet, err := fetcher.ValidatorSet(ctx, nil)
valSet, err := fetcher.ValidatorSet(ctx, &h)
require.NoError(t, err)
// get next block
nextBlock := <-newBlockChan
var nextBlock *types.Block
select {
case nextBlock = <-newBlockChan:
case <-ctx.Done():
require.NoError(t, ctx.Err())
}
// compare LastCommit from next block to Commit from first block height
assert.Equal(t, nextBlock.LastCommit.Hash(), commit.Hash())
assert.Equal(t, nextBlock.LastCommit.Height, commit.Height)
Expand All @@ -64,6 +80,5 @@ func TestBlockFetcherHeaderValues(t *testing.T) {
err = hexBytes.Unmarshal(valSet.Hash())
require.NoError(t, err)
assert.Equal(t, nextBlock.ValidatorsHash, hexBytes)

require.NoError(t, fetcher.UnsubscribeNewBlockEvent(ctx))
}
3 changes: 2 additions & 1 deletion core/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (
tmtypes "github.com/tendermint/tendermint/types"
)

const defaultRetainBlocks int64 = 50
// so that we never hit an issue where we request blocks that are removed
const defaultRetainBlocks int64 = 10000

// StartTestNode starts a mock Core node background process and returns it.
func StartTestNode(ctx context.Context, t *testing.T, app types.Application, cfg *config.Config) tmservice.Service {
Expand Down
17 changes: 9 additions & 8 deletions header/core/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ func createMocknetWithTwoPubsubEndpoints(ctx context.Context, t *testing.T) (*pu
require.NoError(t, err)
host0, host1 := net.Hosts()[0], net.Hosts()[1]

// create pubsub for host
ps0, err := pubsub.NewGossipSub(context.Background(), host0,
pubsub.WithMessageSignaturePolicy(pubsub.StrictNoSign))
require.NoError(t, err)
// create pubsub for peer-side (to test broadcast comes through network)
ps1, err := pubsub.NewGossipSub(context.Background(), host1,
pubsub.WithMessageSignaturePolicy(pubsub.StrictNoSign))
require.NoError(t, err)

sub0, err := host0.EventBus().Subscribe(&event.EvtPeerIdentificationCompleted{})
require.NoError(t, err)
sub1, err := host1.EventBus().Subscribe(&event.EvtPeerIdentificationCompleted{})
Expand All @@ -76,14 +85,6 @@ func createMocknetWithTwoPubsubEndpoints(ctx context.Context, t *testing.T) (*pu
}
}

// create pubsub for host
ps0, err := pubsub.NewGossipSub(context.Background(), host0,
pubsub.WithMessageSignaturePolicy(pubsub.StrictNoSign))
require.NoError(t, err)
// create pubsub for peer-side (to test broadcast comes through network)
ps1, err := pubsub.NewGossipSub(context.Background(), host1,
pubsub.WithMessageSignaturePolicy(pubsub.StrictNoSign))
require.NoError(t, err)
return ps0, ps1
}

Expand Down
5 changes: 4 additions & 1 deletion header/store/heightsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ func TestHeightSub(t *testing.T) {
// assert actual subscription works
{
go func() {
// fixes flakiness on CI
time.Sleep(time.Millisecond)

h1 := header.RandExtendedHeader(t)
h1.Height = 101
h2 := header.RandExtendedHeader(t)
h1.Height = 101
h2.Height = 102
hs.Pub(h1, h2)
}()

Expand Down