Skip to content

Commit

Permalink
tests(core): rewrite tests in core pkg to remove some of the falkeyness
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan committed May 24, 2022
1 parent fed15d9 commit e8f44bd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
24 changes: 14 additions & 10 deletions core/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,42 @@ 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:
// check that `Block` works as intended (passing nil to get block at latest height)
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

0 comments on commit e8f44bd

Please sign in to comment.