move waitForXxx funcs from lcd to tests.WaitForXxx
This commit is contained in:
parent
4049c5d24f
commit
d1402f4e92
|
@ -7,33 +7,14 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
cmn "github.com/tendermint/tmlibs/common"
|
cmn "github.com/tendermint/tmlibs/common"
|
||||||
|
|
||||||
cfg "github.com/tendermint/tendermint/config"
|
cfg "github.com/tendermint/tendermint/config"
|
||||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
|
||||||
rpcclient "github.com/tendermint/tendermint/rpc/lib/client"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var globalConfig *cfg.Config
|
var globalConfig *cfg.Config
|
||||||
|
|
||||||
func waitForRPC() {
|
|
||||||
laddr := GetConfig().RPC.ListenAddress
|
|
||||||
fmt.Println("LADDR", laddr)
|
|
||||||
client := rpcclient.NewJSONRPCClient(laddr)
|
|
||||||
ctypes.RegisterAmino(client.Codec())
|
|
||||||
result := new(ctypes.ResultStatus)
|
|
||||||
for {
|
|
||||||
_, err := client.Call("status", map[string]interface{}{}, result)
|
|
||||||
if err == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Println("error", err)
|
|
||||||
time.Sleep(time.Millisecond)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// f**ing long, but unique for each test
|
// f**ing long, but unique for each test
|
||||||
func makePathname() string {
|
func makePathname() string {
|
||||||
// get path
|
// get path
|
||||||
|
|
|
@ -10,7 +10,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
@ -34,6 +33,7 @@ import (
|
||||||
keys "github.com/cosmos/cosmos-sdk/client/keys"
|
keys "github.com/cosmos/cosmos-sdk/client/keys"
|
||||||
bapp "github.com/cosmos/cosmos-sdk/examples/basecoin/app"
|
bapp "github.com/cosmos/cosmos-sdk/examples/basecoin/app"
|
||||||
btypes "github.com/cosmos/cosmos-sdk/examples/basecoin/types"
|
btypes "github.com/cosmos/cosmos-sdk/examples/basecoin/types"
|
||||||
|
tests "github.com/cosmos/cosmos-sdk/tests"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -157,7 +157,7 @@ func TestNodeStatus(t *testing.T) {
|
||||||
|
|
||||||
func TestBlock(t *testing.T) {
|
func TestBlock(t *testing.T) {
|
||||||
|
|
||||||
waitForHeight(2)
|
tests.WaitForHeight(2, port)
|
||||||
|
|
||||||
var resultBlock ctypes.ResultBlock
|
var resultBlock ctypes.ResultBlock
|
||||||
|
|
||||||
|
@ -224,7 +224,7 @@ func TestCoinSend(t *testing.T) {
|
||||||
|
|
||||||
// create TX
|
// create TX
|
||||||
receiveAddr, resultTx := doSend(t, port, seed)
|
receiveAddr, resultTx := doSend(t, port, seed)
|
||||||
waitForHeight(resultTx.Height + 1)
|
tests.WaitForHeight(resultTx.Height+1, port)
|
||||||
|
|
||||||
// check if tx was commited
|
// check if tx was commited
|
||||||
assert.Equal(t, uint32(0), resultTx.CheckTx.Code)
|
assert.Equal(t, uint32(0), resultTx.CheckTx.Code)
|
||||||
|
@ -253,7 +253,7 @@ func TestIBCTransfer(t *testing.T) {
|
||||||
// create TX
|
// create TX
|
||||||
resultTx := doIBCTransfer(t, port, seed)
|
resultTx := doIBCTransfer(t, port, seed)
|
||||||
|
|
||||||
waitForHeight(resultTx.Height + 1)
|
tests.WaitForHeight(resultTx.Height+1, port)
|
||||||
|
|
||||||
// check if tx was commited
|
// check if tx was commited
|
||||||
assert.Equal(t, uint32(0), resultTx.CheckTx.Code)
|
assert.Equal(t, uint32(0), resultTx.CheckTx.Code)
|
||||||
|
@ -286,7 +286,7 @@ func TestTxs(t *testing.T) {
|
||||||
// create TX
|
// create TX
|
||||||
_, resultTx := doSend(t, port, seed)
|
_, resultTx := doSend(t, port, seed)
|
||||||
|
|
||||||
waitForHeight(resultTx.Height + 1)
|
tests.WaitForHeight(resultTx.Height+1, port)
|
||||||
|
|
||||||
// check if tx is findable
|
// check if tx is findable
|
||||||
res, body := request(t, port, "GET", fmt.Sprintf("/txs/%s", resultTx.Hash), nil)
|
res, body := request(t, port, "GET", fmt.Sprintf("/txs/%s", resultTx.Hash), nil)
|
||||||
|
@ -380,7 +380,7 @@ func startTMAndLCD() (*nm.Node, net.Listener, error) {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
waitForStart()
|
tests.WaitForStart(port)
|
||||||
|
|
||||||
return node, lcd, nil
|
return node, lcd, nil
|
||||||
}
|
}
|
||||||
|
@ -407,7 +407,7 @@ func startTM(cfg *tmcfg.Config, logger log.Logger, genDoc *tmtypes.GenesisDoc, p
|
||||||
}
|
}
|
||||||
|
|
||||||
// wait for rpc
|
// wait for rpc
|
||||||
waitForRPC()
|
tests.WaitForRPC(GetConfig().RPC.ListenAddress)
|
||||||
|
|
||||||
logger.Info("Tendermint running!")
|
logger.Info("Tendermint running!")
|
||||||
return n, err
|
return n, err
|
||||||
|
@ -490,71 +490,3 @@ func doIBCTransfer(t *testing.T, port, seed string) (resultTx ctypes.ResultBroad
|
||||||
|
|
||||||
return resultTx
|
return resultTx
|
||||||
}
|
}
|
||||||
|
|
||||||
func waitForHeight(height int64) {
|
|
||||||
for {
|
|
||||||
var resultBlock ctypes.ResultBlock
|
|
||||||
|
|
||||||
url := fmt.Sprintf("http://localhost:%v%v", port, "/blocks/latest")
|
|
||||||
res, err := http.Get(url)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(res.Body)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
res.Body.Close()
|
|
||||||
|
|
||||||
err = cdc.UnmarshalJSON([]byte(body), &resultBlock)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("RES", res)
|
|
||||||
fmt.Println("BODY", string(body))
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if resultBlock.Block.Height >= height {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
time.Sleep(time.Millisecond * 100)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// wait for 2 blocks
|
|
||||||
func waitForStart() {
|
|
||||||
waitHeight := int64(2)
|
|
||||||
for {
|
|
||||||
time.Sleep(time.Second)
|
|
||||||
|
|
||||||
url := fmt.Sprintf("http://localhost:%v%v", port, "/blocks/latest")
|
|
||||||
res, err := http.Get(url)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// waiting for server to start ...
|
|
||||||
if res.StatusCode != http.StatusOK {
|
|
||||||
res.Body.Close()
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(res.Body)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
res.Body.Close()
|
|
||||||
|
|
||||||
resultBlock := new(ctypes.ResultBlock)
|
|
||||||
err = cdc.UnmarshalJSON([]byte(body), &resultBlock)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("RES", res)
|
|
||||||
fmt.Println("BODY", string(body))
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if resultBlock.Block.Height >= waitHeight {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1,107 @@
|
||||||
|
package tests
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
amino "github.com/tendermint/go-amino"
|
||||||
|
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||||
|
rpcclient "github.com/tendermint/tendermint/rpc/lib/client"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TODO: these functions just print to Stdout.
|
||||||
|
// consider using the logger.
|
||||||
|
|
||||||
|
// Uses localhost
|
||||||
|
func WaitForHeight(height int64, port string) {
|
||||||
|
for {
|
||||||
|
var resultBlock ctypes.ResultBlock
|
||||||
|
|
||||||
|
url := fmt.Sprintf("http://localhost:%v%v", port, "/blocks/latest")
|
||||||
|
res, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := ioutil.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
res.Body.Close()
|
||||||
|
|
||||||
|
err = cdc.UnmarshalJSON([]byte(body), &resultBlock)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("RES", res)
|
||||||
|
fmt.Println("BODY", string(body))
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if resultBlock.Block.Height >= height {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
time.Sleep(time.Millisecond * 100)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// wait for 2 blocks.
|
||||||
|
// uses localhost
|
||||||
|
func WaitForStart(port string) {
|
||||||
|
waitHeight := int64(2)
|
||||||
|
for {
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
|
url := fmt.Sprintf("http://localhost:%v%v", port, "/blocks/latest")
|
||||||
|
res, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// waiting for server to start ...
|
||||||
|
if res.StatusCode != http.StatusOK {
|
||||||
|
res.Body.Close()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := ioutil.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
res.Body.Close()
|
||||||
|
|
||||||
|
resultBlock := new(ctypes.ResultBlock)
|
||||||
|
err = cdc.UnmarshalJSON([]byte(body), &resultBlock)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("RES", res)
|
||||||
|
fmt.Println("BODY", string(body))
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if resultBlock.Block.Height >= waitHeight {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for the RPC server to respond to /status
|
||||||
|
func WaitForRPC(laddr string) {
|
||||||
|
fmt.Println("LADDR", laddr)
|
||||||
|
client := rpcclient.NewJSONRPCClient(laddr)
|
||||||
|
ctypes.RegisterAmino(client.Codec())
|
||||||
|
result := new(ctypes.ResultStatus)
|
||||||
|
for {
|
||||||
|
_, err := client.Call("status", map[string]interface{}{}, result)
|
||||||
|
if err == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Printf("Waiting for RPC server to start on %s:%v\n", laddr, err)
|
||||||
|
time.Sleep(time.Millisecond)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var cdc = amino.NewCodec()
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
ctypes.RegisterAmino(cdc)
|
||||||
|
}
|
Loading…
Reference in New Issue