Skip to content

Commit

Permalink
lcd: waitForHeight instead of sleep in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ebuchman committed Apr 2, 2018
1 parent f40e01a commit e7e98a0
Showing 1 changed file with 75 additions and 10 deletions.
85 changes: 75 additions & 10 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func TestNodeStatus(t *testing.T) {

func TestBlock(t *testing.T) {

time.Sleep(time.Second * 2) // TODO: LOL -> wait for blocks
waitForHeight(2)

var resultBlock ctypes.ResultBlock

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

// create TX
receiveAddr, resultTx := doSend(t, port, seed)

time.Sleep(time.Second * 2) // T
waitForHeight(resultTx.Height + 1)

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

time.Sleep(time.Second * 2) // T
waitForHeight(resultTx.Height + 1)

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

time.Sleep(time.Second * 2) // TO
waitForHeight(resultTx.Height + 1)

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

time.Sleep(time.Second * 2)
waitForStart()

return node, lcd, nil
}
Expand Down Expand Up @@ -442,6 +441,7 @@ func request(t *testing.T, port, method, path string, payload []byte) (*http.Res
require.Nil(t, err)

output, err := ioutil.ReadAll(res.Body)
res.Body.Close()
require.Nil(t, err)

return res, string(output)
Expand All @@ -461,8 +461,6 @@ func doSend(t *testing.T, port, seed string) (receiveAddr string, resultTx ctype
acc := auth.BaseAccount{}
err = json.Unmarshal([]byte(body), &acc)
require.Nil(t, err)
fmt.Println("BODY", body)
fmt.Println("ACC", acc)
sequence := acc.Sequence

// send
Expand Down Expand Up @@ -490,8 +488,6 @@ func doIBCTransfer(t *testing.T, port, seed string) (resultTx ctypes.ResultBroad
acc := auth.BaseAccount{}
err = json.Unmarshal([]byte(body), &acc)
require.Nil(t, err)
fmt.Println("BODY", body)
fmt.Println("ACC", acc)
sequence := acc.Sequence

// send
Expand All @@ -504,3 +500,72 @@ 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 = json.Unmarshal([]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)

var resultBlock ctypes.ResultBlock

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()

err = json.Unmarshal([]byte(body), &resultBlock)
if err != nil {
fmt.Println("RES", res)
fmt.Println("BODY", string(body))
panic(err)
}

if resultBlock.Block.Height >= waitHeight {
return
}
}
}

0 comments on commit e7e98a0

Please sign in to comment.