diff --git a/cmd/gaia/cli_test/cli_test.go b/cmd/gaia/cli_test/cli_test.go index 1868452a64..3eee20c855 100644 --- a/cmd/gaia/cli_test/cli_test.go +++ b/cmd/gaia/cli_test/cli_test.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "testing" - "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -50,7 +49,7 @@ func TestGaiaCLISend(t *testing.T) { assert.Equal(t, int64(50), fooAcc.GetCoins().AmountOf("steak")) executeWrite(t, fmt.Sprintf("gaiacli send %v --amount=10steak --to=%v --name=foo", flags, barCech), pass) - time.Sleep(time.Second * 2) // waiting for some blocks to pass + tests.WaitForNextHeightTM(port) barAcc := executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", barCech, flags)) assert.Equal(t, int64(10), barAcc.GetCoins().AmountOf("steak")) @@ -59,7 +58,7 @@ func TestGaiaCLISend(t *testing.T) { // test autosequencing executeWrite(t, fmt.Sprintf("gaiacli send %v --amount=10steak --to=%v --name=foo", flags, barCech), pass) - time.Sleep(time.Second * 2) // waiting for some blocks to pass + tests.WaitForNextHeightTM(port) barAcc = executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", barCech, flags)) assert.Equal(t, int64(20), barAcc.GetCoins().AmountOf("steak")) @@ -96,7 +95,7 @@ func TestGaiaCLICreateValidator(t *testing.T) { require.NoError(t, err) executeWrite(t, fmt.Sprintf("gaiacli send %v --amount=10steak --to=%v --name=foo", flags, barCech), pass) - time.Sleep(time.Second * 3) // waiting for some blocks to pass + tests.WaitForNextHeightTM(port) barAcc := executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", barCech, flags)) assert.Equal(t, int64(10), barAcc.GetCoins().AmountOf("steak")) @@ -112,7 +111,7 @@ func TestGaiaCLICreateValidator(t *testing.T) { cvStr += fmt.Sprintf(" --moniker=%v", "bar-vally") executeWrite(t, cvStr, pass) - time.Sleep(time.Second * 3) // waiting for some blocks to pass + tests.WaitForNextHeightTM(port) barAcc = executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", barCech, flags)) require.Equal(t, int64(8), barAcc.GetCoins().AmountOf("steak"), "%v", barAcc) @@ -131,7 +130,7 @@ func TestGaiaCLICreateValidator(t *testing.T) { t.Log(fmt.Sprintf("debug unbondStr: %v\n", unbondStr)) executeWrite(t, unbondStr, pass) - time.Sleep(time.Second * 3) // waiting for some blocks to pass + tests.WaitForNextHeightTM(port) barAcc = executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", barCech, flags)) require.Equal(t, int64(9), barAcc.GetCoins().AmountOf("steak"), "%v", barAcc) @@ -150,6 +149,8 @@ func executeWrite(t *testing.T, cmdStr string, writes ...string) { require.NoError(t, err) } proc.Wait() + // bz := proc.StdoutBuffer.Bytes() + // fmt.Println("EXEC WRITE", string(bz)) } func executeInit(t *testing.T, cmdStr string) (chainID string) { diff --git a/tests/util.go b/tests/util.go index 292cbab060..c47d9907f7 100644 --- a/tests/util.go +++ b/tests/util.go @@ -7,16 +7,61 @@ import ( "time" amino "github.com/tendermint/go-amino" + tmclient "github.com/tendermint/tendermint/rpc/client" ctypes "github.com/tendermint/tendermint/rpc/core/types" rpcclient "github.com/tendermint/tendermint/rpc/lib/client" ) +func WaitForNextHeightTM(port string) { + url := fmt.Sprintf("http://localhost:%v", port) + cl := tmclient.NewHTTP(url, "/websocket") + resBlock, err := cl.Block(nil) + if err != nil { + panic(err) + } + waitForHeightTM(resBlock.Block.Height+1, url) +} + +func WaitForHeightTM(height int64, port string) { + url := fmt.Sprintf("http://localhost:%v", port) + waitForHeightTM(height, url) +} + +func waitForHeightTM(height int64, url string) { + cl := tmclient.NewHTTP(url, "/websocket") + for { + // get url, try a few times + var resBlock *ctypes.ResultBlock + var err error + INNER: + for i := 0; i < 5; i++ { + resBlock, err = cl.Block(nil) + if err == nil { + break INNER + } + time.Sleep(time.Millisecond * 200) + } + if err != nil { + panic(err) + } + + if resBlock.Block != nil && + resBlock.Block.Height >= height { + fmt.Println("HEIGHT", resBlock.Block.Height) + return + } + time.Sleep(time.Millisecond * 100) + } +} + // Uses localhost func WaitForHeight(height int64, port string) { + url := fmt.Sprintf("http://localhost:%v/blocks/latest", port) + waitForHeight(height, url) +} + +func waitForHeight(height int64, url string) { for { - - url := fmt.Sprintf("http://localhost:%v/blocks/latest", port) - // get url, try a few times var res *http.Response var err error @@ -25,7 +70,7 @@ func WaitForHeight(height int64, port string) { if err == nil { break } - time.Sleep(time.Second) + time.Sleep(time.Millisecond * 200) } if err != nil { panic(err) @@ -45,7 +90,8 @@ func WaitForHeight(height int64, port string) { panic(err) } - if resultBlock.Block.Height >= height { + if resultBlock.Block != nil && + resultBlock.Block.Height >= height { return } time.Sleep(time.Millisecond * 100)