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