github.com/number571/tendermint@v0.34.11-gost/light/provider/http/http_test.go (about) 1 package http_test 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 "time" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 12 "github.com/number571/tendermint/abci/example/kvstore" 13 "github.com/number571/tendermint/light/provider" 14 lighthttp "github.com/number571/tendermint/light/provider/http" 15 rpcclient "github.com/number571/tendermint/rpc/client" 16 rpchttp "github.com/number571/tendermint/rpc/client/http" 17 rpctest "github.com/number571/tendermint/rpc/test" 18 "github.com/number571/tendermint/types" 19 ) 20 21 func TestNewProvider(t *testing.T) { 22 c, err := lighthttp.New("chain-test", "192.168.0.1:26657") 23 require.NoError(t, err) 24 require.Equal(t, fmt.Sprintf("%s", c), "http{http://192.168.0.1:26657}") 25 26 c, err = lighthttp.New("chain-test", "http://153.200.0.1:26657") 27 require.NoError(t, err) 28 require.Equal(t, fmt.Sprintf("%s", c), "http{http://153.200.0.1:26657}") 29 30 c, err = lighthttp.New("chain-test", "153.200.0.1") 31 require.NoError(t, err) 32 require.Equal(t, fmt.Sprintf("%s", c), "http{http://153.200.0.1}") 33 } 34 35 func TestProvider(t *testing.T) { 36 ctx, cancel := context.WithCancel(context.Background()) 37 defer cancel() 38 cfg := rpctest.CreateConfig(t.Name()) 39 40 // start a tendermint node in the background to test against 41 app := kvstore.NewApplication() 42 app.RetainBlocks = 9 43 _, closer, err := rpctest.StartTendermint(ctx, cfg, app) 44 require.NoError(t, err) 45 46 rpcAddr := cfg.RPC.ListenAddress 47 genDoc, err := types.GenesisDocFromFile(cfg.GenesisFile()) 48 require.NoError(t, err) 49 50 chainID := genDoc.ChainID 51 t.Log("chainID:", chainID) 52 53 c, err := rpchttp.New(rpcAddr) 54 require.Nil(t, err) 55 56 p := lighthttp.NewWithClient(chainID, c) 57 require.NoError(t, err) 58 require.NotNil(t, p) 59 60 // let it produce some blocks 61 err = rpcclient.WaitForHeight(c, 10, nil) 62 require.NoError(t, err) 63 64 // let's get the highest block 65 lb, err := p.LightBlock(context.Background(), 0) 66 require.NoError(t, err) 67 assert.True(t, lb.Height < 9001, "height=%d", lb.Height) 68 69 // let's check this is valid somehow 70 assert.Nil(t, lb.ValidateBasic(chainID)) 71 72 // historical queries now work :) 73 lower := lb.Height - 3 74 lb, err = p.LightBlock(context.Background(), lower) 75 require.NoError(t, err) 76 assert.Equal(t, lower, lb.Height) 77 78 // fetching missing heights (both future and pruned) should return appropriate errors 79 lb, err = p.LightBlock(context.Background(), 9001) 80 require.Error(t, err) 81 require.Nil(t, lb) 82 assert.Equal(t, provider.ErrHeightTooHigh, err) 83 84 lb, err = p.LightBlock(context.Background(), 1) 85 require.Error(t, err) 86 require.Nil(t, lb) 87 assert.Equal(t, provider.ErrLightBlockNotFound, err) 88 89 // if the provider is unable to provide four more blocks then we should return 90 // an unreliable peer error 91 for i := 0; i < 4; i++ { 92 _, err = p.LightBlock(context.Background(), 1) 93 } 94 assert.IsType(t, provider.ErrUnreliableProvider{}, err) 95 96 // shut down tendermint node 97 require.NoError(t, closer(ctx)) 98 cancel() 99 100 time.Sleep(10 * time.Second) 101 lb, err = p.LightBlock(context.Background(), lower+2) 102 // we should see a connection refused 103 require.Error(t, err) 104 require.Nil(t, lb) 105 assert.Equal(t, provider.ErrConnectionClosed, err) 106 }