mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-20 15:04:22 +00:00
Add dependencies, pull out HTTPClient test code
This commit is contained in:
@@ -2,8 +2,12 @@ package rpctest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
logger "github.com/tendermint/go-logger"
|
||||
wire "github.com/tendermint/go-wire"
|
||||
|
||||
abci "github.com/tendermint/abci/types"
|
||||
cfg "github.com/tendermint/go-config"
|
||||
@@ -12,6 +16,7 @@ import (
|
||||
nm "github.com/tendermint/tendermint/node"
|
||||
"github.com/tendermint/tendermint/proxy"
|
||||
rpcclient "github.com/tendermint/tendermint/rpc/client"
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
core_grpc "github.com/tendermint/tendermint/rpc/grpc"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
)
|
||||
@@ -89,3 +94,69 @@ func NewTendermint(app abci.Application) *nm.Node {
|
||||
node.Start()
|
||||
return node
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
// Utilities for testing the websocket service
|
||||
|
||||
// wait for an event; do things that might trigger events, and check them when they are received
|
||||
// the check function takes an event id and the byte slice read off the ws
|
||||
func waitForEvent(t *testing.T, wsc *client.WSClient, eventid string, dieOnTimeout bool, f func(), check func(string, interface{}) error) {
|
||||
// go routine to wait for webscoket msg
|
||||
goodCh := make(chan interface{})
|
||||
errCh := make(chan error)
|
||||
|
||||
// Read message
|
||||
go func() {
|
||||
var err error
|
||||
LOOP:
|
||||
for {
|
||||
select {
|
||||
case r := <-wsc.ResultsCh:
|
||||
result := new(ctypes.TMResult)
|
||||
wire.ReadJSONPtr(result, r, &err)
|
||||
if err != nil {
|
||||
errCh <- err
|
||||
break LOOP
|
||||
}
|
||||
event, ok := (*result).(*ctypes.ResultEvent)
|
||||
if ok && event.Name == eventid {
|
||||
goodCh <- event.Data
|
||||
break LOOP
|
||||
}
|
||||
case err := <-wsc.ErrorsCh:
|
||||
errCh <- err
|
||||
break LOOP
|
||||
case <-wsc.Quit:
|
||||
break LOOP
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// do stuff (transactions)
|
||||
f()
|
||||
|
||||
// wait for an event or timeout
|
||||
timeout := time.NewTimer(10 * time.Second)
|
||||
select {
|
||||
case <-timeout.C:
|
||||
if dieOnTimeout {
|
||||
wsc.Stop()
|
||||
require.True(t, false, "%s event was not received in time", eventid)
|
||||
}
|
||||
// else that's great, we didn't hear the event
|
||||
// and we shouldn't have
|
||||
case eventData := <-goodCh:
|
||||
if dieOnTimeout {
|
||||
// message was received and expected
|
||||
// run the check
|
||||
require.Nil(t, check(eventid, eventData))
|
||||
} else {
|
||||
wsc.Stop()
|
||||
require.True(t, false, "%s event was not expected", eventid)
|
||||
}
|
||||
case err := <-errCh:
|
||||
panic(err) // Show the stack trace.
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
|
||||
@@ -1,152 +0,0 @@
|
||||
package rpctest
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
. "github.com/tendermint/go-common"
|
||||
"github.com/tendermint/go-wire"
|
||||
|
||||
client "github.com/tendermint/go-rpc/client"
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
)
|
||||
|
||||
/*
|
||||
// global variables for use across all tests
|
||||
var (
|
||||
config cfg.Config
|
||||
node *nm.Node
|
||||
chainID string
|
||||
rpcAddr string
|
||||
requestAddr string
|
||||
websocketAddr string
|
||||
websocketEndpoint string
|
||||
grpcAddr string
|
||||
clientURI *client.ClientURI
|
||||
clientJSON *client.ClientJSONRPC
|
||||
clientGRPC core_grpc.BroadcastAPIClient
|
||||
)
|
||||
|
||||
// initialize config and create new node
|
||||
func init() {
|
||||
config = tendermint_test.ResetConfig("rpc_test_client_test")
|
||||
chainID = config.GetString("chain_id")
|
||||
rpcAddr = config.GetString("rpc_laddr")
|
||||
grpcAddr = config.GetString("grpc_laddr")
|
||||
requestAddr = rpcAddr
|
||||
websocketAddr = rpcAddr
|
||||
websocketEndpoint = "/websocket"
|
||||
|
||||
clientURI = client.NewClientURI(requestAddr)
|
||||
clientJSON = client.NewClientJSONRPC(requestAddr)
|
||||
clientGRPC = core_grpc.StartGRPCClient(grpcAddr)
|
||||
|
||||
// TODO: change consensus/state.go timeouts to be shorter
|
||||
|
||||
// start a node
|
||||
ready := make(chan struct{})
|
||||
go newNode(ready)
|
||||
<-ready
|
||||
}
|
||||
|
||||
// create a new node and sleep forever
|
||||
func newNode(ready chan struct{}) {
|
||||
// Create & start node
|
||||
node = nm.NewNodeDefault(config)
|
||||
node.Start()
|
||||
|
||||
time.Sleep(time.Second)
|
||||
|
||||
ready <- struct{}{}
|
||||
|
||||
// Sleep forever
|
||||
ch := make(chan struct{})
|
||||
<-ch
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
// Utilities for testing the websocket service
|
||||
|
||||
// create a new connection
|
||||
func newWSClient(t *testing.T) *client.WSClient {
|
||||
wsc := client.NewWSClient(websocketAddr, websocketEndpoint)
|
||||
if _, err := wsc.Start(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return wsc
|
||||
}
|
||||
*/
|
||||
|
||||
// subscribe to an event
|
||||
func subscribe(t *testing.T, wsc *client.WSClient, eventid string) {
|
||||
require.Nil(t, wsc.Subscribe(eventid))
|
||||
}
|
||||
|
||||
// unsubscribe from an event
|
||||
func unsubscribe(t *testing.T, wsc *client.WSClient, eventid string) {
|
||||
require.Nil(t, wsc.Unsubscribe(eventid))
|
||||
}
|
||||
|
||||
// wait for an event; do things that might trigger events, and check them when they are received
|
||||
// the check function takes an event id and the byte slice read off the ws
|
||||
func waitForEvent(t *testing.T, wsc *client.WSClient, eventid string, dieOnTimeout bool, f func(), check func(string, interface{}) error) {
|
||||
// go routine to wait for webscoket msg
|
||||
goodCh := make(chan interface{})
|
||||
errCh := make(chan error)
|
||||
|
||||
// Read message
|
||||
go func() {
|
||||
var err error
|
||||
LOOP:
|
||||
for {
|
||||
select {
|
||||
case r := <-wsc.ResultsCh:
|
||||
result := new(ctypes.TMResult)
|
||||
wire.ReadJSONPtr(result, r, &err)
|
||||
if err != nil {
|
||||
errCh <- err
|
||||
break LOOP
|
||||
}
|
||||
event, ok := (*result).(*ctypes.ResultEvent)
|
||||
if ok && event.Name == eventid {
|
||||
goodCh <- event.Data
|
||||
break LOOP
|
||||
}
|
||||
case err := <-wsc.ErrorsCh:
|
||||
errCh <- err
|
||||
break LOOP
|
||||
case <-wsc.Quit:
|
||||
break LOOP
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// do stuff (transactions)
|
||||
f()
|
||||
|
||||
// wait for an event or timeout
|
||||
timeout := time.NewTimer(10 * time.Second)
|
||||
select {
|
||||
case <-timeout.C:
|
||||
if dieOnTimeout {
|
||||
wsc.Stop()
|
||||
panic(Fmt("%s event was not received in time", eventid))
|
||||
}
|
||||
// else that's great, we didn't hear the event
|
||||
// and we shouldn't have
|
||||
case eventData := <-goodCh:
|
||||
if dieOnTimeout {
|
||||
// message was received and expected
|
||||
// run the check
|
||||
require.Nil(t, check(eventid, eventData))
|
||||
} else {
|
||||
wsc.Stop()
|
||||
panic(Fmt("%s event was not expected", eventid))
|
||||
}
|
||||
case err := <-errCh:
|
||||
panic(err) // Show the stack trace.
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
@@ -1,131 +0,0 @@
|
||||
package rpctest
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
// "github.com/stretchr/testify/require"
|
||||
// merkle "github.com/tendermint/go-merkle"
|
||||
// "github.com/tendermint/tendermint/types"
|
||||
)
|
||||
|
||||
// Make sure status is correct (we connect properly)
|
||||
func TestStatus(t *testing.T) {
|
||||
c := GetClient()
|
||||
chainID := GetConfig().GetString("chain_id")
|
||||
status, err := c.Status()
|
||||
if assert.Nil(t, err) {
|
||||
assert.Equal(t, chainID, status.NodeInfo.Network)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
// Make some app checks
|
||||
func TestAppCalls(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
c := GetClient()
|
||||
_, err := c.Block(1)
|
||||
assert.NotNil(err) // no block yet
|
||||
k, v, tx := TestTxKV()
|
||||
_, err = c.BroadcastTxCommit(tx)
|
||||
require.Nil(err)
|
||||
// wait before querying
|
||||
time.Sleep(time.Second * 2)
|
||||
qres, err := c.ABCIQuery("/key", k, false)
|
||||
if assert.Nil(err) && assert.True(qres.Response.Code.IsOK()) {
|
||||
data := qres.Response
|
||||
// assert.Equal(k, data.GetKey()) // only returned for proofs
|
||||
assert.Equal(v, data.GetValue())
|
||||
}
|
||||
// and we can even check the block is added
|
||||
block, err := c.Block(3)
|
||||
assert.Nil(err) // now it's good :)
|
||||
appHash := block.BlockMeta.Header.AppHash
|
||||
assert.True(len(appHash) > 0)
|
||||
|
||||
// and get the corresponding commit with the same apphash
|
||||
commit, err := c.Commit(3)
|
||||
assert.Nil(err) // now it's good :)
|
||||
cappHash := commit.Header.AppHash
|
||||
assert.Equal(appHash, cappHash)
|
||||
assert.NotNil(commit.Commit)
|
||||
|
||||
// compare the commits (note Commit(2) has commit from Block(3))
|
||||
commit2, err := c.Commit(2)
|
||||
assert.Nil(err) // now it's good :)
|
||||
assert.Equal(block.Block.LastCommit, commit2.Commit)
|
||||
|
||||
// and we got a proof that works!
|
||||
pres, err := c.ABCIQuery("/key", k, true)
|
||||
if assert.Nil(err) && assert.True(pres.Response.Code.IsOK()) {
|
||||
proof, err := merkle.ReadProof(pres.Response.GetProof())
|
||||
if assert.Nil(err) {
|
||||
key := pres.Response.GetKey()
|
||||
value := pres.Response.GetValue()
|
||||
assert.Equal(appHash, proof.RootHash)
|
||||
valid := proof.Verify(key, value, appHash)
|
||||
assert.True(valid)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// run most calls just to make sure no syntax errors
|
||||
func TestNoErrors(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
c := GetClient()
|
||||
_, err := c.NetInfo()
|
||||
assert.Nil(err)
|
||||
_, err = c.BlockchainInfo(0, 4)
|
||||
assert.Nil(err)
|
||||
// TODO: check with a valid height
|
||||
_, err = c.Block(1000)
|
||||
assert.NotNil(err)
|
||||
// maybe this is an error???
|
||||
// _, err = c.DialSeeds([]string{"one", "two"})
|
||||
// assert.Nil(err)
|
||||
gen, err := c.Genesis()
|
||||
if assert.Nil(err) {
|
||||
assert.Equal(GetConfig().GetString("chain_id"), gen.Genesis.ChainID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubscriptions(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
c := GetClient()
|
||||
err := c.StartWebsocket()
|
||||
require.Nil(err)
|
||||
defer c.StopWebsocket()
|
||||
|
||||
// subscribe to a transaction event
|
||||
_, _, tx := TestTxKV()
|
||||
// this causes a panic in tendermint core!!!
|
||||
eventType := types.EventStringTx(types.Tx(tx))
|
||||
c.Subscribe(eventType)
|
||||
read := 0
|
||||
|
||||
// set up a listener
|
||||
r, e := c.GetEventChannels()
|
||||
go func() {
|
||||
// read one event in the background
|
||||
select {
|
||||
case <-r:
|
||||
// TODO: actually parse this or something
|
||||
read += 1
|
||||
case err := <-e:
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
|
||||
// make sure nothing has happened yet.
|
||||
assert.Equal(0, read)
|
||||
|
||||
// send a tx and wait for it to propogate
|
||||
_, err = c.BroadcastTxCommit(tx)
|
||||
assert.Nil(err, string(tx))
|
||||
// wait before querying
|
||||
time.Sleep(time.Second)
|
||||
|
||||
// now make sure the event arrived
|
||||
assert.Equal(1, read)
|
||||
}
|
||||
*/
|
||||
Reference in New Issue
Block a user