tendermint/rpc/lib/rpc_test.go

377 lines
8.7 KiB
Go
Raw Normal View History

2016-02-18 13:07:49 -08:00
package rpc
import (
"bytes"
"context"
crand "crypto/rand"
"encoding/json"
2017-03-10 00:52:40 -08:00
"fmt"
"math/rand"
2016-02-18 13:07:49 -08:00
"net/http"
"os"
"os/exec"
2016-02-18 13:07:49 -08:00
"testing"
"time"
"github.com/go-kit/kit/log/term"
2017-03-10 00:03:16 -08:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2017-04-28 11:36:38 -07:00
"github.com/tendermint/go-wire/data"
client "github.com/tendermint/tendermint/rpc/lib/client"
server "github.com/tendermint/tendermint/rpc/lib/server"
types "github.com/tendermint/tendermint/rpc/lib/types"
2017-05-02 00:53:32 -07:00
"github.com/tendermint/tmlibs/log"
2016-02-18 13:07:49 -08:00
)
// Client and Server should work over tcp or unix sockets
const (
tcpAddr = "tcp://0.0.0.0:47768"
unixSocket = "/tmp/rpc_test.sock"
unixAddr = "unix://" + unixSocket
2016-02-18 18:05:24 -08:00
websocketEndpoint = "/websocket/endpoint"
testPongWait = 2 * time.Second
2016-02-18 13:07:49 -08:00
)
type ResultEcho struct {
2017-04-28 19:04:14 -07:00
Value string `json:"value"`
2016-02-18 13:07:49 -08:00
}
2017-04-28 11:36:38 -07:00
type ResultEchoInt struct {
2017-04-28 19:04:14 -07:00
Value int `json:"value"`
2017-04-28 11:36:38 -07:00
}
type ResultEchoBytes struct {
2017-04-28 19:04:14 -07:00
Value []byte `json:"value"`
}
2017-04-28 11:36:38 -07:00
type ResultEchoDataBytes struct {
2017-04-28 19:04:14 -07:00
Value data.Bytes `json:"value"`
2017-04-28 11:36:38 -07:00
}
2016-02-18 13:07:49 -08:00
// Define some routes
var Routes = map[string]*server.RPCFunc{
2017-04-28 11:36:38 -07:00
"echo": server.NewRPCFunc(EchoResult, "arg"),
"echo_ws": server.NewWSRPCFunc(EchoWSResult, "arg"),
"echo_bytes": server.NewRPCFunc(EchoBytesResult, "arg"),
"echo_data_bytes": server.NewRPCFunc(EchoDataBytesResult, "arg"),
"echo_int": server.NewRPCFunc(EchoIntResult, "arg"),
2016-02-18 13:07:49 -08:00
}
2017-04-28 19:04:14 -07:00
func EchoResult(v string) (*ResultEcho, error) {
return &ResultEcho{v}, nil
2016-02-18 13:07:49 -08:00
}
2017-04-28 19:04:14 -07:00
func EchoWSResult(wsCtx types.WSRPCContext, v string) (*ResultEcho, error) {
return &ResultEcho{v}, nil
}
2017-04-28 19:04:14 -07:00
func EchoIntResult(v int) (*ResultEchoInt, error) {
return &ResultEchoInt{v}, nil
2017-04-28 11:36:38 -07:00
}
2017-04-28 19:04:14 -07:00
func EchoBytesResult(v []byte) (*ResultEchoBytes, error) {
return &ResultEchoBytes{v}, nil
}
2017-04-28 19:04:14 -07:00
func EchoDataBytesResult(v data.Bytes) (*ResultEchoDataBytes, error) {
return &ResultEchoDataBytes{v}, nil
2017-04-28 11:36:38 -07:00
}
func TestMain(m *testing.M) {
setup()
code := m.Run()
os.Exit(code)
}
var colorFn = func(keyvals ...interface{}) term.FgBgColor {
for i := 0; i < len(keyvals)-1; i += 2 {
if keyvals[i] == "socket" {
if keyvals[i+1] == "tcp" {
return term.FgBgColor{Fg: term.DarkBlue}
} else if keyvals[i+1] == "unix" {
return term.FgBgColor{Fg: term.DarkCyan}
}
}
}
return term.FgBgColor{}
}
2016-02-18 13:07:49 -08:00
// launch unix and tcp servers
func setup() {
logger := log.NewTMLoggerWithColorFn(log.NewSyncWriter(os.Stdout), colorFn)
cmd := exec.Command("rm", "-f", unixSocket)
err := cmd.Start()
if err != nil {
panic(err)
}
2017-04-12 16:30:05 -07:00
if err = cmd.Wait(); err != nil {
panic(err)
}
tcpLogger := logger.With("socket", "tcp")
2016-02-18 13:07:49 -08:00
mux := http.NewServeMux()
server.RegisterRPCFuncs(mux, Routes, tcpLogger)
wm := server.NewWebsocketManager(Routes, nil, server.PingPong((testPongWait*9)/10, testPongWait))
wm.SetLogger(tcpLogger)
2016-02-18 18:05:24 -08:00
mux.HandleFunc(websocketEndpoint, wm.WebsocketHandler)
2016-02-18 13:07:49 -08:00
go func() {
_, err := server.StartHTTPServer(tcpAddr, mux, tcpLogger)
2016-02-18 13:07:49 -08:00
if err != nil {
panic(err)
}
}()
unixLogger := logger.With("socket", "unix")
2016-06-21 12:19:56 -07:00
mux2 := http.NewServeMux()
server.RegisterRPCFuncs(mux2, Routes, unixLogger)
wm = server.NewWebsocketManager(Routes, nil)
wm.SetLogger(unixLogger)
2016-06-21 12:19:56 -07:00
mux2.HandleFunc(websocketEndpoint, wm.WebsocketHandler)
2016-02-18 13:07:49 -08:00
go func() {
_, err := server.StartHTTPServer(unixAddr, mux2, unixLogger)
2016-02-18 13:07:49 -08:00
if err != nil {
panic(err)
}
}()
// wait for servers to start
time.Sleep(time.Second * 2)
}
func echoViaHTTP(cl client.HTTPClient, val string) (string, error) {
2016-02-18 13:07:49 -08:00
params := map[string]interface{}{
"arg": val,
}
2017-04-28 19:04:14 -07:00
result := new(ResultEcho)
if _, err := cl.Call("echo", params, result); err != nil {
2017-03-10 00:52:40 -08:00
return "", err
}
2017-04-28 19:04:14 -07:00
return result.Value, nil
2016-02-18 13:07:49 -08:00
}
2017-04-28 11:36:38 -07:00
func echoIntViaHTTP(cl client.HTTPClient, val int) (int, error) {
params := map[string]interface{}{
"arg": val,
}
2017-04-28 19:04:14 -07:00
result := new(ResultEchoInt)
if _, err := cl.Call("echo_int", params, result); err != nil {
2017-04-28 11:36:38 -07:00
return 0, err
}
2017-04-28 19:04:14 -07:00
return result.Value, nil
2017-04-28 11:36:38 -07:00
}
func echoBytesViaHTTP(cl client.HTTPClient, bytes []byte) ([]byte, error) {
params := map[string]interface{}{
"arg": bytes,
}
2017-04-28 19:04:14 -07:00
result := new(ResultEchoBytes)
if _, err := cl.Call("echo_bytes", params, result); err != nil {
return []byte{}, err
}
2017-04-28 19:04:14 -07:00
return result.Value, nil
}
2017-04-28 11:36:38 -07:00
func echoDataBytesViaHTTP(cl client.HTTPClient, bytes data.Bytes) (data.Bytes, error) {
params := map[string]interface{}{
"arg": bytes,
}
2017-04-28 19:04:14 -07:00
result := new(ResultEchoDataBytes)
if _, err := cl.Call("echo_data_bytes", params, result); err != nil {
2017-04-28 11:36:38 -07:00
return []byte{}, err
}
2017-04-28 19:04:14 -07:00
return result.Value, nil
2017-04-28 11:36:38 -07:00
}
2017-03-10 00:52:40 -08:00
func testWithHTTPClient(t *testing.T, cl client.HTTPClient) {
2016-02-18 13:07:49 -08:00
val := "acbd"
got, err := echoViaHTTP(cl, val)
2017-03-10 00:03:16 -08:00
require.Nil(t, err)
assert.Equal(t, got, val)
val2 := randBytes(t)
got2, err := echoBytesViaHTTP(cl, val2)
require.Nil(t, err)
assert.Equal(t, got2, val2)
2017-04-28 11:36:38 -07:00
val3 := data.Bytes(randBytes(t))
got3, err := echoDataBytesViaHTTP(cl, val3)
require.Nil(t, err)
assert.Equal(t, got3, val3)
2017-04-28 14:57:06 -07:00
val4 := rand.Intn(10000)
got4, err := echoIntViaHTTP(cl, val4)
require.Nil(t, err)
assert.Equal(t, got4, val4)
2016-02-18 13:07:49 -08:00
}
func echoViaWS(cl *client.WSClient, val string) (string, error) {
params := map[string]interface{}{
"arg": val,
}
err := cl.Call(context.Background(), "echo", params)
if err != nil {
return "", err
}
2016-02-18 18:05:24 -08:00
2017-03-09 05:44:00 -08:00
select {
case msg := <-cl.ResultsCh:
2017-04-28 19:04:14 -07:00
result := new(ResultEcho)
err = json.Unmarshal(msg, result)
if err != nil {
return "", nil
}
2017-04-28 19:04:14 -07:00
return result.Value, nil
2017-03-09 05:44:00 -08:00
case err := <-cl.ErrorsCh:
return "", err
2016-02-18 18:05:24 -08:00
}
}
func echoBytesViaWS(cl *client.WSClient, bytes []byte) ([]byte, error) {
params := map[string]interface{}{
"arg": bytes,
}
err := cl.Call(context.Background(), "echo_bytes", params)
if err != nil {
return []byte{}, err
}
select {
case msg := <-cl.ResultsCh:
2017-04-28 19:04:14 -07:00
result := new(ResultEchoBytes)
err = json.Unmarshal(msg, result)
if err != nil {
return []byte{}, nil
}
2017-04-28 19:04:14 -07:00
return result.Value, nil
case err := <-cl.ErrorsCh:
return []byte{}, err
}
}
func testWithWSClient(t *testing.T, cl *client.WSClient) {
val := "acbd"
got, err := echoViaWS(cl, val)
require.Nil(t, err)
assert.Equal(t, got, val)
val2 := randBytes(t)
got2, err := echoBytesViaWS(cl, val2)
require.Nil(t, err)
assert.Equal(t, got2, val2)
}
2016-02-18 18:05:24 -08:00
//-------------
2017-03-10 00:52:40 -08:00
func TestServersAndClientsBasic(t *testing.T) {
serverAddrs := [...]string{tcpAddr, unixAddr}
for _, addr := range serverAddrs {
cl1 := client.NewURIClient(addr)
fmt.Printf("=== testing server on %s using %v client", addr, cl1)
testWithHTTPClient(t, cl1)
2016-02-18 13:07:49 -08:00
2017-06-24 18:27:16 -07:00
cl2 := client.NewJSONRPCClient(addr)
2017-03-10 00:52:40 -08:00
fmt.Printf("=== testing server on %s using %v client", addr, cl2)
testWithHTTPClient(t, cl2)
2016-02-18 18:05:24 -08:00
2017-06-24 18:27:16 -07:00
cl3 := client.NewWSClient(addr, websocketEndpoint)
cl3.SetLogger(log.TestingLogger())
2017-03-10 00:52:40 -08:00
_, err := cl3.Start()
require.Nil(t, err)
fmt.Printf("=== testing server on %s using %v client", addr, cl3)
testWithWSClient(t, cl3)
cl3.Stop()
}
2016-02-18 18:05:24 -08:00
}
func TestHexStringArg(t *testing.T) {
cl := client.NewURIClient(tcpAddr)
// should NOT be handled as hex
val := "0xabc"
got, err := echoViaHTTP(cl, val)
2017-03-10 00:03:16 -08:00
require.Nil(t, err)
assert.Equal(t, got, val)
}
func TestQuotedStringArg(t *testing.T) {
cl := client.NewURIClient(tcpAddr)
// should NOT be unquoted
val := "\"abc\""
got, err := echoViaHTTP(cl, val)
2017-03-10 00:03:16 -08:00
require.Nil(t, err)
assert.Equal(t, got, val)
}
func TestWSNewWSRPCFunc(t *testing.T) {
cl := client.NewWSClient(tcpAddr, websocketEndpoint)
cl.SetLogger(log.TestingLogger())
_, err := cl.Start()
2017-03-10 00:03:16 -08:00
require.Nil(t, err)
defer cl.Stop()
val := "acbd"
params := map[string]interface{}{
"arg": val,
}
err = cl.Call(context.Background(), "echo_ws", params)
2017-03-10 00:03:16 -08:00
require.Nil(t, err)
select {
case msg := <-cl.ResultsCh:
2017-04-28 19:04:14 -07:00
result := new(ResultEcho)
err = json.Unmarshal(msg, result)
2017-03-10 00:03:16 -08:00
require.Nil(t, err)
2017-04-28 19:04:14 -07:00
got := result.Value
2017-03-10 00:03:16 -08:00
assert.Equal(t, got, val)
case err := <-cl.ErrorsCh:
t.Fatal(err)
}
}
2017-04-21 08:30:22 -07:00
func TestWSHandlesArrayParams(t *testing.T) {
cl := client.NewWSClient(tcpAddr, websocketEndpoint)
cl.SetLogger(log.TestingLogger())
2017-04-21 08:30:22 -07:00
_, err := cl.Start()
require.Nil(t, err)
defer cl.Stop()
val := "acbd"
params := []interface{}{val}
err = cl.CallWithArrayParams(context.Background(), "echo_ws", params)
2017-04-21 08:30:22 -07:00
require.Nil(t, err)
select {
case msg := <-cl.ResultsCh:
2017-04-28 19:04:14 -07:00
result := new(ResultEcho)
err = json.Unmarshal(msg, result)
2017-04-21 08:30:22 -07:00
require.Nil(t, err)
2017-04-28 19:04:14 -07:00
got := result.Value
2017-04-21 08:30:22 -07:00
assert.Equal(t, got, val)
case err := <-cl.ErrorsCh:
t.Fatalf("%+v", err)
}
}
// TestWSClientPingPong checks that a client & server exchange pings
// & pongs so connection stays alive.
func TestWSClientPingPong(t *testing.T) {
cl := client.NewWSClient(tcpAddr, websocketEndpoint)
cl.SetLogger(log.TestingLogger())
_, err := cl.Start()
require.Nil(t, err)
defer cl.Stop()
time.Sleep((testPongWait * 11) / 10)
}
func randBytes(t *testing.T) []byte {
n := rand.Intn(10) + 2
buf := make([]byte, n)
_, err := crand.Read(buf)
require.Nil(t, err)
return bytes.Replace(buf, []byte("="), []byte{100}, -1)
}