github.com/adoriasoft/tendermint@v0.34.0-dev1.0.20200722151356-96d84601a75a/rpc/client/helpers_test.go (about)

     1  package client_test
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/tendermint/tendermint/rpc/client"
    12  	"github.com/tendermint/tendermint/rpc/client/mock"
    13  	ctypes "github.com/tendermint/tendermint/rpc/core/types"
    14  )
    15  
    16  func TestWaitForHeight(t *testing.T) {
    17  	assert, require := assert.New(t), require.New(t)
    18  
    19  	// test with error result - immediate failure
    20  	m := &mock.StatusMock{
    21  		Call: mock.Call{
    22  			Error: errors.New("bye"),
    23  		},
    24  	}
    25  	r := mock.NewStatusRecorder(m)
    26  
    27  	// connection failure always leads to error
    28  	err := client.WaitForHeight(r, 8, nil)
    29  	require.NotNil(err)
    30  	require.Equal("bye", err.Error())
    31  	// we called status once to check
    32  	require.Equal(1, len(r.Calls))
    33  
    34  	// now set current block height to 10
    35  	m.Call = mock.Call{
    36  		Response: &ctypes.ResultStatus{SyncInfo: ctypes.SyncInfo{LatestBlockHeight: 10}},
    37  	}
    38  
    39  	// we will not wait for more than 10 blocks
    40  	err = client.WaitForHeight(r, 40, nil)
    41  	require.NotNil(err)
    42  	require.True(strings.Contains(err.Error(), "aborting"))
    43  	// we called status once more to check
    44  	require.Equal(2, len(r.Calls))
    45  
    46  	// waiting for the past returns immediately
    47  	err = client.WaitForHeight(r, 5, nil)
    48  	require.Nil(err)
    49  	// we called status once more to check
    50  	require.Equal(3, len(r.Calls))
    51  
    52  	// since we can't update in a background goroutine (test --race)
    53  	// we use the callback to update the status height
    54  	myWaiter := func(delta int64) error {
    55  		// update the height for the next call
    56  		m.Call.Response = &ctypes.ResultStatus{SyncInfo: ctypes.SyncInfo{LatestBlockHeight: 15}}
    57  		return client.DefaultWaitStrategy(delta)
    58  	}
    59  
    60  	// we wait for a few blocks
    61  	err = client.WaitForHeight(r, 12, myWaiter)
    62  	require.Nil(err)
    63  	// we called status once to check
    64  	require.Equal(5, len(r.Calls))
    65  
    66  	pre := r.Calls[3]
    67  	require.Nil(pre.Error)
    68  	prer, ok := pre.Response.(*ctypes.ResultStatus)
    69  	require.True(ok)
    70  	assert.Equal(int64(10), prer.SyncInfo.LatestBlockHeight)
    71  
    72  	post := r.Calls[4]
    73  	require.Nil(post.Error)
    74  	postr, ok := post.Response.(*ctypes.ResultStatus)
    75  	require.True(ok)
    76  	assert.Equal(int64(15), postr.SyncInfo.LatestBlockHeight)
    77  }