Skip to content

Commit

Permalink
move waitForXxx funcs from lcd to tests.WaitForXxx
Browse files Browse the repository at this point in the history
  • Loading branch information
ebuchman authored and rigelrozanski committed Apr 26, 2018
1 parent 4049c5d commit d1402f4
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 94 deletions.
19 changes: 0 additions & 19 deletions client/lcd/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,14 @@ import (
"os"
"path/filepath"
"strings"
"time"

cmn "github.com/tendermint/tmlibs/common"

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

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
func makePathname() string {
// get path
Expand Down
82 changes: 7 additions & 75 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"os"
"regexp"
"testing"
"time"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
Expand All @@ -34,6 +33,7 @@ import (
keys "github.com/cosmos/cosmos-sdk/client/keys"
bapp "github.com/cosmos/cosmos-sdk/examples/basecoin/app"
btypes "github.com/cosmos/cosmos-sdk/examples/basecoin/types"
tests "github.com/cosmos/cosmos-sdk/tests"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand Down Expand Up @@ -157,7 +157,7 @@ func TestNodeStatus(t *testing.T) {

func TestBlock(t *testing.T) {

waitForHeight(2)
tests.WaitForHeight(2, port)

var resultBlock ctypes.ResultBlock

Expand Down Expand Up @@ -224,7 +224,7 @@ func TestCoinSend(t *testing.T) {

// create TX
receiveAddr, resultTx := doSend(t, port, seed)
waitForHeight(resultTx.Height + 1)
tests.WaitForHeight(resultTx.Height+1, port)

// check if tx was commited
assert.Equal(t, uint32(0), resultTx.CheckTx.Code)
Expand Down Expand Up @@ -253,7 +253,7 @@ func TestIBCTransfer(t *testing.T) {
// create TX
resultTx := doIBCTransfer(t, port, seed)

waitForHeight(resultTx.Height + 1)
tests.WaitForHeight(resultTx.Height+1, port)

// check if tx was commited
assert.Equal(t, uint32(0), resultTx.CheckTx.Code)
Expand Down Expand Up @@ -286,7 +286,7 @@ func TestTxs(t *testing.T) {
// create TX
_, resultTx := doSend(t, port, seed)

waitForHeight(resultTx.Height + 1)
tests.WaitForHeight(resultTx.Height+1, port)

// check if tx is findable
res, body := request(t, port, "GET", fmt.Sprintf("/txs/%s", resultTx.Hash), nil)
Expand Down Expand Up @@ -380,7 +380,7 @@ func startTMAndLCD() (*nm.Node, net.Listener, error) {
return nil, nil, err
}

waitForStart()
tests.WaitForStart(port)

return node, lcd, nil
}
Expand All @@ -407,7 +407,7 @@ func startTM(cfg *tmcfg.Config, logger log.Logger, genDoc *tmtypes.GenesisDoc, p
}

// wait for rpc
waitForRPC()
tests.WaitForRPC(GetConfig().RPC.ListenAddress)

logger.Info("Tendermint running!")
return n, err
Expand Down Expand Up @@ -490,71 +490,3 @@ func doIBCTransfer(t *testing.T, port, seed string) (resultTx ctypes.ResultBroad

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
}
}
}
107 changes: 107 additions & 0 deletions tests/util.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit d1402f4

Please sign in to comment.