Clean up tests, remove panics

This commit is contained in:
Ethan Frey 2017-02-21 19:57:10 +01:00
parent d971416d12
commit 2c75c9daf9
4 changed files with 67 additions and 127 deletions

View File

@ -8,6 +8,8 @@ import (
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/abci/types" abci "github.com/tendermint/abci/types"
. "github.com/tendermint/go-common" . "github.com/tendermint/go-common"
ctypes "github.com/tendermint/tendermint/rpc/core/types" ctypes "github.com/tendermint/tendermint/rpc/core/types"
@ -25,18 +27,14 @@ import (
func TestURIStatus(t *testing.T) { func TestURIStatus(t *testing.T) {
tmResult := new(ctypes.TMResult) tmResult := new(ctypes.TMResult)
_, err := GetURIClient().Call("status", map[string]interface{}{}, tmResult) _, err := GetURIClient().Call("status", map[string]interface{}{}, tmResult)
if err != nil { require.Nil(t, err)
panic(err)
}
testStatus(t, tmResult) testStatus(t, tmResult)
} }
func TestJSONStatus(t *testing.T) { func TestJSONStatus(t *testing.T) {
tmResult := new(ctypes.TMResult) tmResult := new(ctypes.TMResult)
_, err := GetJSONClient().Call("status", []interface{}{}, tmResult) _, err := GetJSONClient().Call("status", []interface{}{}, tmResult)
if err != nil { require.Nil(t, err)
panic(err)
}
testStatus(t, tmResult) testStatus(t, tmResult)
} }
@ -45,23 +43,18 @@ func testStatus(t *testing.T, statusI interface{}) {
tmRes := statusI.(*ctypes.TMResult) tmRes := statusI.(*ctypes.TMResult)
status := (*tmRes).(*ctypes.ResultStatus) status := (*tmRes).(*ctypes.ResultStatus)
if status.NodeInfo.Network != chainID { assert.Equal(t, chainID, status.NodeInfo.Network)
panic(Fmt("ChainID mismatch: got %s expected %s",
status.NodeInfo.Network, chainID))
}
} }
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
// broadcast tx sync // broadcast tx sync
// random bytes (excluding byte('=')) // random bytes (excluding byte('='))
func randBytes() []byte { func randBytes(t *testing.T) []byte {
n := rand.Intn(10) + 2 n := rand.Intn(10) + 2
buf := make([]byte, n) buf := make([]byte, n)
_, err := crand.Read(buf) _, err := crand.Read(buf)
if err != nil { require.Nil(t, err)
panic(err)
}
return bytes.Replace(buf, []byte("="), []byte{100}, -1) return bytes.Replace(buf, []byte("="), []byte{100}, -1)
} }
@ -69,11 +62,9 @@ func TestURIBroadcastTxSync(t *testing.T) {
config.Set("block_size", 0) config.Set("block_size", 0)
defer config.Set("block_size", -1) defer config.Set("block_size", -1)
tmResult := new(ctypes.TMResult) tmResult := new(ctypes.TMResult)
tx := randBytes() tx := randBytes(t)
_, err := GetURIClient().Call("broadcast_tx_sync", map[string]interface{}{"tx": tx}, tmResult) _, err := GetURIClient().Call("broadcast_tx_sync", map[string]interface{}{"tx": tx}, tmResult)
if err != nil { require.Nil(t, err)
panic(err)
}
testBroadcastTxSync(t, tmResult, tx) testBroadcastTxSync(t, tmResult, tx)
} }
@ -81,84 +72,64 @@ func TestJSONBroadcastTxSync(t *testing.T) {
config.Set("block_size", 0) config.Set("block_size", 0)
defer config.Set("block_size", -1) defer config.Set("block_size", -1)
tmResult := new(ctypes.TMResult) tmResult := new(ctypes.TMResult)
tx := randBytes() tx := randBytes(t)
_, err := GetJSONClient().Call("broadcast_tx_sync", []interface{}{tx}, tmResult) _, err := GetJSONClient().Call("broadcast_tx_sync", []interface{}{tx}, tmResult)
if err != nil { require.Nil(t, err)
panic(err)
}
testBroadcastTxSync(t, tmResult, tx) testBroadcastTxSync(t, tmResult, tx)
} }
func testBroadcastTxSync(t *testing.T, resI interface{}, tx []byte) { func testBroadcastTxSync(t *testing.T, resI interface{}, tx []byte) {
tmRes := resI.(*ctypes.TMResult) tmRes := resI.(*ctypes.TMResult)
res := (*tmRes).(*ctypes.ResultBroadcastTx) res := (*tmRes).(*ctypes.ResultBroadcastTx)
if res.Code != abci.CodeType_OK { require.Equal(t, abci.CodeType_OK, res.Code)
panic(Fmt("BroadcastTxSync got non-zero exit code: %v. %X; %s", res.Code, res.Data, res.Log))
}
mem := node.MempoolReactor().Mempool mem := node.MempoolReactor().Mempool
if mem.Size() != 1 { require.Equal(t, 1, mem.Size())
panic(Fmt("Mempool size should have been 1. Got %d", mem.Size()))
}
txs := mem.Reap(1) txs := mem.Reap(1)
if !bytes.Equal(txs[0], tx) { require.EqualValues(t, tx, txs[0])
panic(Fmt("Tx in mempool does not match test tx. Got %X, expected %X", txs[0], tx))
}
mem.Flush() mem.Flush()
} }
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
// query // query
func testTxKV() ([]byte, []byte, []byte) { func testTxKV(t *testing.T) ([]byte, []byte, []byte) {
k := randBytes() k := randBytes(t)
v := randBytes() v := randBytes(t)
return k, v, []byte(Fmt("%s=%s", k, v)) return k, v, []byte(Fmt("%s=%s", k, v))
} }
func sendTx() ([]byte, []byte) { func sendTx(t *testing.T) ([]byte, []byte) {
tmResult := new(ctypes.TMResult) tmResult := new(ctypes.TMResult)
k, v, tx := testTxKV() k, v, tx := testTxKV(t)
_, err := GetJSONClient().Call("broadcast_tx_commit", []interface{}{tx}, tmResult) _, err := GetJSONClient().Call("broadcast_tx_commit", []interface{}{tx}, tmResult)
if err != nil { require.Nil(t, err)
panic(err)
}
return k, v return k, v
} }
func TestURIABCIQuery(t *testing.T) { func TestURIABCIQuery(t *testing.T) {
k, v := sendTx() k, v := sendTx(t)
time.Sleep(time.Second) time.Sleep(time.Second)
tmResult := new(ctypes.TMResult) tmResult := new(ctypes.TMResult)
_, err := GetURIClient().Call("abci_query", map[string]interface{}{"path": "", "data": k, "prove": false}, tmResult) _, err := GetURIClient().Call("abci_query", map[string]interface{}{"path": "", "data": k, "prove": false}, tmResult)
if err != nil { require.Nil(t, err)
panic(err)
}
testABCIQuery(t, tmResult, v) testABCIQuery(t, tmResult, v)
} }
func TestJSONABCIQuery(t *testing.T) { func TestJSONABCIQuery(t *testing.T) {
k, v := sendTx() k, v := sendTx(t)
tmResult := new(ctypes.TMResult) tmResult := new(ctypes.TMResult)
_, err := GetJSONClient().Call("abci_query", []interface{}{"", k, false}, tmResult) _, err := GetJSONClient().Call("abci_query", []interface{}{"", k, false}, tmResult)
if err != nil { require.Nil(t, err)
panic(err)
}
testABCIQuery(t, tmResult, v) testABCIQuery(t, tmResult, v)
} }
func testABCIQuery(t *testing.T, statusI interface{}, value []byte) { func testABCIQuery(t *testing.T, statusI interface{}, value []byte) {
tmRes := statusI.(*ctypes.TMResult) tmRes := statusI.(*ctypes.TMResult)
resQuery := (*tmRes).(*ctypes.ResultABCIQuery) resQuery := (*tmRes).(*ctypes.ResultABCIQuery)
if !resQuery.Response.Code.IsOK() { require.EqualValues(t, 0, resQuery.Response.Code)
panic(Fmt("Query returned an err: %v", resQuery))
}
// XXX: specific to value returned by the dummy // XXX: specific to value returned by the dummy
if len(resQuery.Response.Value) == 0 { require.NotEqual(t, 0, len(resQuery.Response.Value))
panic(Fmt("Query error. Found no value"))
}
} }
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
@ -166,40 +137,30 @@ func testABCIQuery(t *testing.T, statusI interface{}, value []byte) {
func TestURIBroadcastTxCommit(t *testing.T) { func TestURIBroadcastTxCommit(t *testing.T) {
tmResult := new(ctypes.TMResult) tmResult := new(ctypes.TMResult)
tx := randBytes() tx := randBytes(t)
_, err := GetURIClient().Call("broadcast_tx_commit", map[string]interface{}{"tx": tx}, tmResult) _, err := GetURIClient().Call("broadcast_tx_commit", map[string]interface{}{"tx": tx}, tmResult)
if err != nil { require.Nil(t, err)
panic(err)
}
testBroadcastTxCommit(t, tmResult, tx) testBroadcastTxCommit(t, tmResult, tx)
} }
func TestJSONBroadcastTxCommit(t *testing.T) { func TestJSONBroadcastTxCommit(t *testing.T) {
tmResult := new(ctypes.TMResult) tmResult := new(ctypes.TMResult)
tx := randBytes() tx := randBytes(t)
_, err := GetJSONClient().Call("broadcast_tx_commit", []interface{}{tx}, tmResult) _, err := GetJSONClient().Call("broadcast_tx_commit", []interface{}{tx}, tmResult)
if err != nil { require.Nil(t, err)
panic(err)
}
testBroadcastTxCommit(t, tmResult, tx) testBroadcastTxCommit(t, tmResult, tx)
} }
func testBroadcastTxCommit(t *testing.T, resI interface{}, tx []byte) { func testBroadcastTxCommit(t *testing.T, resI interface{}, tx []byte) {
require := require.New(t)
tmRes := resI.(*ctypes.TMResult) tmRes := resI.(*ctypes.TMResult)
res := (*tmRes).(*ctypes.ResultBroadcastTxCommit) res := (*tmRes).(*ctypes.ResultBroadcastTxCommit)
checkTx := res.CheckTx checkTx := res.CheckTx
if checkTx.Code != abci.CodeType_OK { require.Equal(abci.CodeType_OK, checkTx.Code)
panic(Fmt("BroadcastTxCommit got non-zero exit code from CheckTx: %v. %X; %s", checkTx.Code, checkTx.Data, checkTx.Log))
}
deliverTx := res.DeliverTx deliverTx := res.DeliverTx
if deliverTx.Code != abci.CodeType_OK { require.Equal(abci.CodeType_OK, deliverTx.Code)
panic(Fmt("BroadcastTxCommit got non-zero exit code from CheckTx: %v. %X; %s", deliverTx.Code, deliverTx.Data, deliverTx.Log))
}
mem := node.MempoolReactor().Mempool mem := node.MempoolReactor().Mempool
if mem.Size() != 0 { require.Equal(0, mem.Size())
panic(Fmt("Mempool size should have been 0. Got %d", mem.Size()))
}
// TODO: find tx in block // TODO: find tx in block
} }
@ -218,9 +179,10 @@ func TestWSConnect(t *testing.T) {
func TestWSNewBlock(t *testing.T) { func TestWSNewBlock(t *testing.T) {
wsc := GetWSClient() wsc := GetWSClient()
eid := types.EventStringNewBlock() eid := types.EventStringNewBlock()
subscribe(t, wsc, eid) require.Nil(t, wsc.Subscribe(eid))
defer func() { defer func() {
unsubscribe(t, wsc, eid) require.Nil(t, wsc.Unsubscribe(eid))
wsc.Stop() wsc.Stop()
}() }()
waitForEvent(t, wsc, eid, true, func() {}, func(eid string, b interface{}) error { waitForEvent(t, wsc, eid, true, func() {}, func(eid string, b interface{}) error {
@ -236,9 +198,10 @@ func TestWSBlockchainGrowth(t *testing.T) {
} }
wsc := GetWSClient() wsc := GetWSClient()
eid := types.EventStringNewBlock() eid := types.EventStringNewBlock()
subscribe(t, wsc, eid) require.Nil(t, wsc.Subscribe(eid))
defer func() { defer func() {
unsubscribe(t, wsc, eid) require.Nil(t, wsc.Unsubscribe(eid))
wsc.Stop() wsc.Stop()
}() }()
@ -262,35 +225,29 @@ func TestWSBlockchainGrowth(t *testing.T) {
} }
func TestWSTxEvent(t *testing.T) { func TestWSTxEvent(t *testing.T) {
require := require.New(t)
wsc := GetWSClient() wsc := GetWSClient()
tx := randBytes() tx := randBytes(t)
// listen for the tx I am about to submit // listen for the tx I am about to submit
eid := types.EventStringTx(types.Tx(tx)) eid := types.EventStringTx(types.Tx(tx))
subscribe(t, wsc, eid) require.Nil(wsc.Subscribe(eid))
defer func() { defer func() {
unsubscribe(t, wsc, eid) require.Nil(wsc.Unsubscribe(eid))
wsc.Stop() wsc.Stop()
}() }()
// send an tx // send an tx
tmResult := new(ctypes.TMResult) tmResult := new(ctypes.TMResult)
_, err := GetJSONClient().Call("broadcast_tx_sync", []interface{}{tx}, tmResult) _, err := GetJSONClient().Call("broadcast_tx_sync", []interface{}{tx}, tmResult)
if err != nil { require.Nil(err)
t.Fatal("Error submitting event")
}
waitForEvent(t, wsc, eid, true, func() {}, func(eid string, b interface{}) error { waitForEvent(t, wsc, eid, true, func() {}, func(eid string, b interface{}) error {
evt, ok := b.(types.EventDataTx) evt, ok := b.(types.EventDataTx)
if !ok { require.True(ok, "Got wrong event type: %#v", b)
t.Fatal("Got wrong event type", b) require.Equal(tx, []byte(evt.Tx), "Returned different tx")
} require.Equal(abci.CodeType_OK, evt.Code)
if bytes.Compare([]byte(evt.Tx), tx) != 0 {
t.Error("Event returned different tx")
}
if evt.Code != abci.CodeType_OK {
t.Error("Event returned tx error code", evt.Code)
}
return nil return nil
}) })
} }
@ -345,9 +302,7 @@ func TestURIUnsafeSetConfig(t *testing.T) {
"key": testCase[1], "key": testCase[1],
"value": testCase[2], "value": testCase[2],
}, tmResult) }, tmResult)
if err != nil { require.Nil(t, err)
panic(err)
}
} }
testUnsafeSetConfig(t) testUnsafeSetConfig(t)
} }
@ -356,26 +311,19 @@ func TestJSONUnsafeSetConfig(t *testing.T) {
for _, testCase := range testCasesUnsafeSetConfig { for _, testCase := range testCasesUnsafeSetConfig {
tmResult := new(ctypes.TMResult) tmResult := new(ctypes.TMResult)
_, err := GetJSONClient().Call("unsafe_set_config", []interface{}{testCase[0], testCase[1], testCase[2]}, tmResult) _, err := GetJSONClient().Call("unsafe_set_config", []interface{}{testCase[0], testCase[1], testCase[2]}, tmResult)
if err != nil { require.Nil(t, err)
panic(err)
}
} }
testUnsafeSetConfig(t) testUnsafeSetConfig(t)
} }
func testUnsafeSetConfig(t *testing.T) { func testUnsafeSetConfig(t *testing.T) {
require := require.New(t)
s := config.GetString("key1") s := config.GetString("key1")
if s != stringVal { require.Equal(stringVal, s)
panic(Fmt("got %v, expected %v", s, stringVal))
}
i := config.GetInt("key2") i := config.GetInt("key2")
if i != intVal { require.Equal(intVal, i)
panic(Fmt("got %v, expected %v", i, intVal))
}
b := config.GetBool("key3") b := config.GetBool("key3")
if b != boolVal { require.Equal(boolVal, b)
panic(Fmt("got %v, expected %v", b, boolVal))
}
} }

View File

@ -5,20 +5,16 @@ import (
"golang.org/x/net/context" "golang.org/x/net/context"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/rpc/grpc" "github.com/tendermint/tendermint/rpc/grpc"
) )
//------------------------------------------- //-------------------------------------------
func TestBroadcastTx(t *testing.T) { func TestBroadcastTx(t *testing.T) {
require := require.New(t)
res, err := GetGRPCClient().BroadcastTx(context.Background(), &core_grpc.RequestBroadcastTx{[]byte("this is a tx")}) res, err := GetGRPCClient().BroadcastTx(context.Background(), &core_grpc.RequestBroadcastTx{[]byte("this is a tx")})
if err != nil { require.Nil(err)
t.Fatal(err) require.EqualValues(0, res.CheckTx.Code)
} require.EqualValues(0, res.DeliverTx.Code)
if res.CheckTx.Code != 0 {
t.Fatalf("Non-zero check tx code: %d", res.CheckTx.Code)
}
if res.DeliverTx.Code != 0 {
t.Fatalf("Non-zero append tx code: %d", res.DeliverTx.Code)
}
} }

View File

@ -4,6 +4,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/require"
. "github.com/tendermint/go-common" . "github.com/tendermint/go-common"
"github.com/tendermint/go-wire" "github.com/tendermint/go-wire"
@ -76,18 +77,15 @@ func newWSClient(t *testing.T) *client.WSClient {
return wsc return wsc
} }
*/ */
// subscribe to an event // subscribe to an event
func subscribe(t *testing.T, wsc *client.WSClient, eventid string) { func subscribe(t *testing.T, wsc *client.WSClient, eventid string) {
if err := wsc.Subscribe(eventid); err != nil { require.Nil(t, wsc.Subscribe(eventid))
panic(err)
}
} }
// unsubscribe from an event // unsubscribe from an event
func unsubscribe(t *testing.T, wsc *client.WSClient, eventid string) { func unsubscribe(t *testing.T, wsc *client.WSClient, eventid string) {
if err := wsc.Unsubscribe(eventid); err != nil { require.Nil(t, wsc.Unsubscribe(eventid))
panic(err)
}
} }
// wait for an event; do things that might trigger events, and check them when they are received // wait for an event; do things that might trigger events, and check them when they are received
@ -141,16 +139,13 @@ func waitForEvent(t *testing.T, wsc *client.WSClient, eventid string, dieOnTimeo
if dieOnTimeout { if dieOnTimeout {
// message was received and expected // message was received and expected
// run the check // run the check
if err := check(eventid, eventData); err != nil { require.Nil(t, check(eventid, eventData))
panic(err) // Show the stack trace.
}
} else { } else {
wsc.Stop() wsc.Stop()
panic(Fmt("%s event was not expected", eventid)) panic(Fmt("%s event was not expected", eventid))
} }
case err := <-errCh: case err := <-errCh:
panic(err) // Show the stack trace. panic(err) // Show the stack trace.
} }
} }

View File

@ -12,9 +12,10 @@ import (
// Make sure status is correct (we connect properly) // Make sure status is correct (we connect properly)
func TestStatus(t *testing.T) { func TestStatus(t *testing.T) {
c := GetClient() c := GetClient()
chainID := GetConfig().GetString("chain_id")
status, err := c.Status() status, err := c.Status()
if assert.Nil(t, err) { if assert.Nil(t, err) {
assert.Equal(t, GetConfig().GetString("chain_id"), status.NodeInfo.Network) assert.Equal(t, chainID, status.NodeInfo.Network)
} }
} }