Skip to content

Commit

Permalink
Merge pull request #1251 from cosmos/bucky/cli-tests
Browse files Browse the repository at this point in the history
gaia/cli_test: remove sleeps
  • Loading branch information
ebuchman committed Jun 14, 2018
2 parents 7ebeff1 + ff34cbc commit 0866494
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 11 deletions.
13 changes: 7 additions & 6 deletions cmd/gaia/cli_test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -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"))
Expand All @@ -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"))
Expand Down Expand Up @@ -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"))
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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) {
Expand Down
56 changes: 51 additions & 5 deletions tests/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

// Uses localhost
func WaitForHeight(height int64, port string) {
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)
}

url := fmt.Sprintf("http://localhost:%v/blocks/latest", port)
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 {
// get url, try a few times
var res *http.Response
var err error
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 0866494

Please sign in to comment.