Skip to content

Commit

Permalink
tests: cleanup unused functions (#6627)
Browse files Browse the repository at this point in the history
Gut unnecessary test helpers.
  • Loading branch information
Alessio Treglia committed Jul 7, 2020
1 parent 0fd559e commit ebf1583
Showing 1 changed file with 2 additions and 104 deletions.
106 changes: 2 additions & 104 deletions tests/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,16 @@ import (
"io/ioutil"
"net/http"
"os"
"strings"
"testing"
"time"

"github.com/stretchr/testify/require"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
tmjsonrpc "github.com/tendermint/tendermint/rpc/jsonrpc/client"

"github.com/cosmos/cosmos-sdk/codec"
)

// Wait for the next tendermint block from the Tendermint RPC
// on localhost
func WaitForNextHeightTM(port string) {
WaitForNextNBlocksTM(1, port)
}

// Wait for N tendermint blocks to pass using the Tendermint RPC
// on localhost
func WaitForNextNBlocksTM(n int64, port string) {
Expand Down Expand Up @@ -83,62 +76,6 @@ func waitForHeightTM(height int64, url string) {
}
}

// Wait for height from the LCD API on localhost
func WaitForHeight(height int64, port string) {
url := fmt.Sprintf("http://localhost:%v/blocks/latest", port)
waitForHeight(height, url)
}

// Whether or not an HTTP status code was "successful"
func StatusOK(statusCode int) bool {
switch statusCode {
case http.StatusOK:
case http.StatusCreated:
case http.StatusNoContent:
return true
}
return false
}

func waitForHeight(height int64, url string) {
var res *http.Response
var err error

for {
// Since this is in a testing file we are accepting nolint to be passed
res, err = http.Get(url) // nolint:gosec
if err != nil {
panic(err)
}

body, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}

if err = res.Body.Close(); err != nil {
panic(err)
}

var resultBlock ctypes.ResultBlock
if err = cdc.UnmarshalJSON(body, &resultBlock); err != nil {
panic(err)
}

if resultBlock.Block != nil && resultBlock.Block.Height >= height {
return
}

time.Sleep(time.Millisecond * 100)
}
}

// wait for tendermint to start by querying the LCD
func WaitForLCDStart(port string) {
url := fmt.Sprintf("http://localhost:%v/blocks/latest", port)
WaitForStart(url)
}

// wait for tendermint to start by querying tendermint
func WaitForTMStart(port string) {
url := fmt.Sprintf("http://localhost:%v/block", port)
Expand Down Expand Up @@ -178,49 +115,10 @@ func WaitForStart(url string) {
panic(err)
}

// TODO: these functions just print to Stdout.
// consider using the logger.

// Wait for the RPC server to respond to /status
func WaitForRPC(laddr string) {
fmt.Println("LADDR", laddr)
client, err := tmjsonrpc.New(laddr)
if err != nil {
panic(fmt.Sprintf("failed to create Tendermint RPC client: %s", err))
}

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

// ExtractPortFromAddress extract port from listenAddress
// The listenAddress must be some strings like tcp://0.0.0.0:12345
func ExtractPortFromAddress(listenAddress string) string {
stringList := strings.Split(listenAddress, ":")
length := len(stringList)
if length != 3 {
panic(fmt.Errorf("expected listen address: tcp://0.0.0.0:12345, got %s", listenAddress))
}
return stringList[2]
}

type NamedTestingT interface {
require.TestingT
Name() string
}

// NewTestCaseDir creates a new temporary directory for a test case.
// Returns the directory path and a cleanup function.
// nolint: errcheck
func NewTestCaseDir(t NamedTestingT) (string, func()) {
func NewTestCaseDir(t testing.TB) (string, func()) {
dir, err := ioutil.TempDir("", t.Name()+"_")
require.NoError(t, err)
return dir, func() { os.RemoveAll(dir) }
Expand Down

0 comments on commit ebf1583

Please sign in to comment.