2016-02-18 21:07:49 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2017-03-09 13:46:48 +04:00
|
|
|
"bytes"
|
2017-07-31 18:44:46 -04:00
|
|
|
"context"
|
2017-03-09 13:46:48 +04:00
|
|
|
crand "crypto/rand"
|
2017-04-28 16:24:06 +02:00
|
|
|
"encoding/json"
|
2017-03-10 12:52:40 +04:00
|
|
|
"fmt"
|
2017-03-09 13:46:48 +04:00
|
|
|
"math/rand"
|
2016-02-18 21:07:49 +00:00
|
|
|
"net/http"
|
2017-07-31 18:44:46 -04:00
|
|
|
"os"
|
2017-03-07 19:09:58 +04:00
|
|
|
"os/exec"
|
2016-02-18 21:07:49 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-07-31 18:44:46 -04:00
|
|
|
"github.com/go-kit/kit/log/term"
|
2017-03-10 12:03:16 +04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2017-10-04 16:40:45 -04:00
|
|
|
|
2017-04-26 19:57:33 -04:00
|
|
|
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 11:53:32 +04:00
|
|
|
"github.com/tendermint/tmlibs/log"
|
2016-02-18 21:07:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Client and Server should work over tcp or unix sockets
|
2017-03-07 19:04:24 +04:00
|
|
|
const (
|
2017-04-28 14:03:38 +02:00
|
|
|
tcpAddr = "tcp://0.0.0.0:47768"
|
2017-03-07 19:09:58 +04:00
|
|
|
|
2017-04-28 14:03:38 +02:00
|
|
|
unixSocket = "/tmp/rpc_test.sock"
|
|
|
|
unixAddr = "unix://" + unixSocket
|
2016-02-19 02:05:24 +00:00
|
|
|
|
|
|
|
websocketEndpoint = "/websocket/endpoint"
|
2016-02-18 21:07:49 +00:00
|
|
|
)
|
|
|
|
|
2017-03-10 12:57:14 +04:00
|
|
|
type ResultEcho struct {
|
2017-04-28 22:04:14 -04:00
|
|
|
Value string `json:"value"`
|
2016-02-18 21:07:49 +00:00
|
|
|
}
|
|
|
|
|
2017-04-28 14:36:38 -04:00
|
|
|
type ResultEchoInt struct {
|
2017-04-28 22:04:14 -04:00
|
|
|
Value int `json:"value"`
|
2017-04-28 14:36:38 -04:00
|
|
|
}
|
|
|
|
|
2017-03-10 12:57:14 +04:00
|
|
|
type ResultEchoBytes struct {
|
2017-04-28 22:04:14 -04:00
|
|
|
Value []byte `json:"value"`
|
2017-03-09 13:46:48 +04:00
|
|
|
}
|
|
|
|
|
2017-04-28 14:36:38 -04:00
|
|
|
type ResultEchoDataBytes struct {
|
2018-02-03 03:42:59 -05:00
|
|
|
Value cmn.HexBytes `json:"value"`
|
2017-04-28 14:36:38 -04:00
|
|
|
}
|
|
|
|
|
2016-02-18 21:07:49 +00:00
|
|
|
// Define some routes
|
2017-03-07 19:04:24 +04:00
|
|
|
var Routes = map[string]*server.RPCFunc{
|
2017-04-28 14:36:38 -04: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 21:07:49 +00:00
|
|
|
}
|
|
|
|
|
2017-04-28 22:04:14 -04:00
|
|
|
func EchoResult(v string) (*ResultEcho, error) {
|
|
|
|
return &ResultEcho{v}, nil
|
2016-02-18 21:07:49 +00:00
|
|
|
}
|
|
|
|
|
2017-04-28 22:04:14 -04:00
|
|
|
func EchoWSResult(wsCtx types.WSRPCContext, v string) (*ResultEcho, error) {
|
|
|
|
return &ResultEcho{v}, nil
|
2017-03-09 18:30:55 +04:00
|
|
|
}
|
|
|
|
|
2017-04-28 22:04:14 -04:00
|
|
|
func EchoIntResult(v int) (*ResultEchoInt, error) {
|
|
|
|
return &ResultEchoInt{v}, nil
|
2017-04-28 14:36:38 -04:00
|
|
|
}
|
|
|
|
|
2017-04-28 22:04:14 -04:00
|
|
|
func EchoBytesResult(v []byte) (*ResultEchoBytes, error) {
|
|
|
|
return &ResultEchoBytes{v}, nil
|
2017-03-09 13:46:48 +04:00
|
|
|
}
|
|
|
|
|
2018-02-03 03:42:59 -05:00
|
|
|
func EchoDataBytesResult(v cmn.HexBytes) (*ResultEchoDataBytes, error) {
|
2017-04-28 22:04:14 -04:00
|
|
|
return &ResultEchoDataBytes{v}, nil
|
2017-04-28 14:36:38 -04:00
|
|
|
}
|
|
|
|
|
2017-07-31 18:44:46 -04: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 21:07:49 +00:00
|
|
|
// launch unix and tcp servers
|
2017-07-31 18:44:46 -04:00
|
|
|
func setup() {
|
|
|
|
logger := log.NewTMLoggerWithColorFn(log.NewSyncWriter(os.Stdout), colorFn)
|
|
|
|
|
2017-03-07 19:09:58 +04:00
|
|
|
cmd := exec.Command("rm", "-f", unixSocket)
|
|
|
|
err := cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-04-12 19:30:05 -04:00
|
|
|
if err = cmd.Wait(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-03-07 19:04:24 +04:00
|
|
|
|
2017-07-31 18:44:46 -04:00
|
|
|
tcpLogger := logger.With("socket", "tcp")
|
2016-02-18 21:07:49 +00:00
|
|
|
mux := http.NewServeMux()
|
2017-07-31 18:44:46 -04:00
|
|
|
server.RegisterRPCFuncs(mux, Routes, tcpLogger)
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
wm := server.NewWebsocketManager(Routes, server.ReadWait(5*time.Second), server.PingPeriod(1*time.Second))
|
2017-07-31 18:44:46 -04:00
|
|
|
wm.SetLogger(tcpLogger)
|
2016-02-19 02:05:24 +00:00
|
|
|
mux.HandleFunc(websocketEndpoint, wm.WebsocketHandler)
|
2016-02-18 21:07:49 +00:00
|
|
|
go func() {
|
2017-07-31 18:44:46 -04:00
|
|
|
_, err := server.StartHTTPServer(tcpAddr, mux, tcpLogger)
|
2016-02-18 21:07:49 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-07-31 18:44:46 -04:00
|
|
|
unixLogger := logger.With("socket", "unix")
|
2016-06-21 15:19:56 -04:00
|
|
|
mux2 := http.NewServeMux()
|
2017-07-31 18:44:46 -04:00
|
|
|
server.RegisterRPCFuncs(mux2, Routes, unixLogger)
|
new pubsub package
comment out failing consensus tests for now
rewrite rpc httpclient to use new pubsub package
import pubsub as tmpubsub, query as tmquery
make event IDs constants
EventKey -> EventTypeKey
rename EventsPubsub to PubSub
mempool does not use pubsub
rename eventsSub to pubsub
new subscribe API
fix channel size issues and consensus tests bugs
refactor rpc client
add missing discardFromChan method
add mutex
rename pubsub to eventBus
remove IsRunning from WSRPCConnection interface (not needed)
add a comment in broadcastNewRoundStepsAndVotes
rename registerEventCallbacks to broadcastNewRoundStepsAndVotes
See https://dave.cheney.net/2014/03/19/channel-axioms
stop eventBuses after reactor tests
remove unnecessary Unsubscribe
return subscribe helper function
move discardFromChan to where it is used
subscribe now returns an err
this gives us ability to refuse to subscribe if pubsub is at its max
capacity.
use context for control overflow
cache queries
handle err when subscribing in replay_test
rename testClientID to testSubscriber
extract var
set channel buffer capacity to 1 in replay_file
fix byzantine_test
unsubscribe from single event, not all events
refactor httpclient to return events to appropriate channels
return failing testReplayCrashBeforeWriteVote test
fix TestValidatorSetChanges
refactor code a bit
fix testReplayCrashBeforeWriteVote
add comment
fix TestValidatorSetChanges
fixes from Bucky's review
update comment [ci skip]
test TxEventBuffer
update changelog
fix TestValidatorSetChanges (2nd attempt)
only do wg.Done when no errors
benchmark event bus
create pubsub server inside NewEventBus
only expose config params (later if needed)
set buffer capacity to 0 so we are not testing cache
new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ}
This should allow to subscribe to all transactions! or a specific one
using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'"
use TimeoutCommit instead of afterPublishEventNewBlockTimeout
TimeoutCommit is the time a node waits after committing a block, before
it goes into the next height. So it will finish everything from the last
block, but then wait a bit. The idea is this gives it time to hear more
votes from other validators, to strengthen the commit it includes in the
next block. But it also gives it time to hear about new transactions.
waitForBlockWithUpdatedVals
rewrite WAL crash tests
Task:
test that we can recover from any WAL crash.
Solution:
the old tests were relying on event hub being run in the same thread (we
were injecting the private validator's last signature).
when considering a rewrite, we considered two possible solutions: write
a "fuzzy" testing system where WAL is crashing upon receiving a new
message, or inject failures and trigger them in tests using something
like https://github.com/coreos/gofail.
remove sleep
no cs.Lock around wal.Save
test different cases (empty block, non-empty block, ...)
comments
add comments
test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks
fixes as per Bucky's last review
reset subscriptions on UnsubscribeAll
use a simple counter to track message for which we panicked
also, set a smaller part size for all test cases
2017-06-26 19:00:30 +04:00
|
|
|
wm = server.NewWebsocketManager(Routes)
|
2017-07-31 18:44:46 -04:00
|
|
|
wm.SetLogger(unixLogger)
|
2016-06-21 15:19:56 -04:00
|
|
|
mux2.HandleFunc(websocketEndpoint, wm.WebsocketHandler)
|
2016-02-18 21:07:49 +00:00
|
|
|
go func() {
|
2017-07-31 18:44:46 -04:00
|
|
|
_, err := server.StartHTTPServer(unixAddr, mux2, unixLogger)
|
2016-02-18 21:07:49 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// wait for servers to start
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
}
|
|
|
|
|
2017-03-10 14:56:04 +04:00
|
|
|
func echoViaHTTP(cl client.HTTPClient, val string) (string, error) {
|
2016-02-18 21:07:49 +00:00
|
|
|
params := map[string]interface{}{
|
|
|
|
"arg": val,
|
|
|
|
}
|
2017-04-28 22:04:14 -04:00
|
|
|
result := new(ResultEcho)
|
|
|
|
if _, err := cl.Call("echo", params, result); err != nil {
|
2017-03-10 12:52:40 +04:00
|
|
|
return "", err
|
|
|
|
}
|
2017-04-28 22:04:14 -04:00
|
|
|
return result.Value, nil
|
2016-02-18 21:07:49 +00:00
|
|
|
}
|
|
|
|
|
2017-04-28 14:36:38 -04:00
|
|
|
func echoIntViaHTTP(cl client.HTTPClient, val int) (int, error) {
|
|
|
|
params := map[string]interface{}{
|
|
|
|
"arg": val,
|
|
|
|
}
|
2017-04-28 22:04:14 -04:00
|
|
|
result := new(ResultEchoInt)
|
|
|
|
if _, err := cl.Call("echo_int", params, result); err != nil {
|
2017-04-28 14:36:38 -04:00
|
|
|
return 0, err
|
|
|
|
}
|
2017-04-28 22:04:14 -04:00
|
|
|
return result.Value, nil
|
2017-04-28 14:36:38 -04:00
|
|
|
}
|
|
|
|
|
2017-03-10 14:56:04 +04:00
|
|
|
func echoBytesViaHTTP(cl client.HTTPClient, bytes []byte) ([]byte, error) {
|
|
|
|
params := map[string]interface{}{
|
|
|
|
"arg": bytes,
|
|
|
|
}
|
2017-04-28 22:04:14 -04:00
|
|
|
result := new(ResultEchoBytes)
|
|
|
|
if _, err := cl.Call("echo_bytes", params, result); err != nil {
|
2017-03-10 14:56:04 +04:00
|
|
|
return []byte{}, err
|
|
|
|
}
|
2017-04-28 22:04:14 -04:00
|
|
|
return result.Value, nil
|
2017-03-10 14:56:04 +04:00
|
|
|
}
|
|
|
|
|
2018-02-03 03:42:59 -05:00
|
|
|
func echoDataBytesViaHTTP(cl client.HTTPClient, bytes cmn.HexBytes) (cmn.HexBytes, error) {
|
2017-04-28 14:36:38 -04:00
|
|
|
params := map[string]interface{}{
|
|
|
|
"arg": bytes,
|
|
|
|
}
|
2017-04-28 22:04:14 -04:00
|
|
|
result := new(ResultEchoDataBytes)
|
|
|
|
if _, err := cl.Call("echo_data_bytes", params, result); err != nil {
|
2017-04-28 14:36:38 -04:00
|
|
|
return []byte{}, err
|
|
|
|
}
|
2017-04-28 22:04:14 -04:00
|
|
|
return result.Value, nil
|
2017-04-28 14:36:38 -04:00
|
|
|
}
|
|
|
|
|
2017-03-10 12:52:40 +04:00
|
|
|
func testWithHTTPClient(t *testing.T, cl client.HTTPClient) {
|
2016-02-18 21:07:49 +00:00
|
|
|
val := "acbd"
|
2017-03-10 14:56:04 +04:00
|
|
|
got, err := echoViaHTTP(cl, val)
|
2017-03-10 12:03:16 +04:00
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, got, val)
|
2017-03-10 14:56:04 +04:00
|
|
|
|
|
|
|
val2 := randBytes(t)
|
|
|
|
got2, err := echoBytesViaHTTP(cl, val2)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, got2, val2)
|
2017-04-28 14:36:38 -04:00
|
|
|
|
2018-02-03 03:42:59 -05:00
|
|
|
val3 := cmn.HexBytes(randBytes(t))
|
2017-04-28 14:36:38 -04:00
|
|
|
got3, err := echoDataBytesViaHTTP(cl, val3)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, got3, val3)
|
|
|
|
|
2017-04-28 17:57:06 -04:00
|
|
|
val4 := rand.Intn(10000)
|
|
|
|
got4, err := echoIntViaHTTP(cl, val4)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, got4, val4)
|
2016-02-18 21:07:49 +00:00
|
|
|
}
|
|
|
|
|
2017-03-10 14:56:04 +04:00
|
|
|
func echoViaWS(cl *client.WSClient, val string) (string, error) {
|
2017-03-07 18:34:54 +04:00
|
|
|
params := map[string]interface{}{
|
|
|
|
"arg": val,
|
|
|
|
}
|
2017-07-31 18:44:46 -04:00
|
|
|
err := cl.Call(context.Background(), "echo", params)
|
2017-03-10 14:56:04 +04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-02-19 02:05:24 +00:00
|
|
|
|
2017-10-26 19:24:18 -04:00
|
|
|
msg := <-cl.ResponsesCh
|
|
|
|
if msg.Error != nil {
|
|
|
|
return "", err
|
2017-10-24 17:38:12 +01:00
|
|
|
|
2016-02-19 02:05:24 +00:00
|
|
|
}
|
2017-10-26 19:24:18 -04:00
|
|
|
result := new(ResultEcho)
|
|
|
|
err = json.Unmarshal(msg.Result, result)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
return result.Value, nil
|
2016-02-19 02:05:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-10 14:56:04 +04:00
|
|
|
func echoBytesViaWS(cl *client.WSClient, bytes []byte) ([]byte, error) {
|
|
|
|
params := map[string]interface{}{
|
|
|
|
"arg": bytes,
|
|
|
|
}
|
2017-07-31 18:44:46 -04:00
|
|
|
err := cl.Call(context.Background(), "echo_bytes", params)
|
2017-03-10 14:56:04 +04:00
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
|
2017-10-26 19:24:18 -04:00
|
|
|
msg := <-cl.ResponsesCh
|
|
|
|
if msg.Error != nil {
|
|
|
|
return []byte{}, msg.Error
|
2017-10-24 17:38:12 +01:00
|
|
|
|
2017-03-10 14:56:04 +04:00
|
|
|
}
|
2017-10-26 19:24:18 -04:00
|
|
|
result := new(ResultEchoBytes)
|
|
|
|
err = json.Unmarshal(msg.Result, result)
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
return result.Value, nil
|
2017-03-10 14:56:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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-19 02:05:24 +00:00
|
|
|
//-------------
|
|
|
|
|
2017-03-10 12:52:40 +04: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 21:07:49 +00:00
|
|
|
|
2017-06-24 21:27:16 -04:00
|
|
|
cl2 := client.NewJSONRPCClient(addr)
|
2017-03-10 12:52:40 +04:00
|
|
|
fmt.Printf("=== testing server on %s using %v client", addr, cl2)
|
|
|
|
testWithHTTPClient(t, cl2)
|
2016-02-19 02:05:24 +00:00
|
|
|
|
2017-06-24 21:27:16 -04:00
|
|
|
cl3 := client.NewWSClient(addr, websocketEndpoint)
|
2017-08-07 18:29:55 -04:00
|
|
|
cl3.SetLogger(log.TestingLogger())
|
2017-11-06 13:20:39 -05:00
|
|
|
err := cl3.Start()
|
2017-03-10 12:52:40 +04:00
|
|
|
require.Nil(t, err)
|
|
|
|
fmt.Printf("=== testing server on %s using %v client", addr, cl3)
|
|
|
|
testWithWSClient(t, cl3)
|
|
|
|
cl3.Stop()
|
|
|
|
}
|
2016-02-19 02:05:24 +00:00
|
|
|
}
|
2017-01-02 09:50:20 -08:00
|
|
|
|
|
|
|
func TestHexStringArg(t *testing.T) {
|
2017-03-08 01:22:35 +04:00
|
|
|
cl := client.NewURIClient(tcpAddr)
|
2017-01-02 09:50:20 -08:00
|
|
|
// should NOT be handled as hex
|
|
|
|
val := "0xabc"
|
2017-03-10 14:56:04 +04:00
|
|
|
got, err := echoViaHTTP(cl, val)
|
2017-03-10 12:03:16 +04:00
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, got, val)
|
2017-01-02 09:50:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestQuotedStringArg(t *testing.T) {
|
2017-03-08 01:22:35 +04:00
|
|
|
cl := client.NewURIClient(tcpAddr)
|
2017-01-02 09:50:20 -08:00
|
|
|
// should NOT be unquoted
|
|
|
|
val := "\"abc\""
|
2017-03-10 14:56:04 +04:00
|
|
|
got, err := echoViaHTTP(cl, val)
|
2017-03-10 12:03:16 +04:00
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, got, val)
|
2017-01-02 09:50:20 -08:00
|
|
|
}
|
2017-03-09 13:46:48 +04:00
|
|
|
|
2017-03-09 18:30:55 +04:00
|
|
|
func TestWSNewWSRPCFunc(t *testing.T) {
|
2017-03-10 14:56:04 +04:00
|
|
|
cl := client.NewWSClient(tcpAddr, websocketEndpoint)
|
2017-08-07 18:29:55 -04:00
|
|
|
cl.SetLogger(log.TestingLogger())
|
2017-11-06 13:20:39 -05:00
|
|
|
err := cl.Start()
|
2017-03-10 12:03:16 +04:00
|
|
|
require.Nil(t, err)
|
2017-03-09 18:30:55 +04:00
|
|
|
defer cl.Stop()
|
|
|
|
|
|
|
|
val := "acbd"
|
|
|
|
params := map[string]interface{}{
|
|
|
|
"arg": val,
|
|
|
|
}
|
2017-07-31 18:44:46 -04:00
|
|
|
err = cl.Call(context.Background(), "echo_ws", params)
|
2017-03-10 12:03:16 +04:00
|
|
|
require.Nil(t, err)
|
2017-03-09 18:30:55 +04:00
|
|
|
|
2017-11-27 22:35:16 +00:00
|
|
|
msg := <-cl.ResponsesCh
|
|
|
|
if msg.Error != nil {
|
|
|
|
t.Fatal(err)
|
2017-03-09 18:30:55 +04:00
|
|
|
}
|
2017-10-26 19:24:18 -04:00
|
|
|
result := new(ResultEcho)
|
2017-11-27 22:35:16 +00:00
|
|
|
err = json.Unmarshal(msg.Result, result)
|
2017-10-26 19:24:18 -04:00
|
|
|
require.Nil(t, err)
|
|
|
|
got := result.Value
|
|
|
|
assert.Equal(t, got, val)
|
2017-03-09 18:30:55 +04:00
|
|
|
}
|
2017-03-10 14:56:04 +04:00
|
|
|
|
2017-04-21 18:30:22 +03:00
|
|
|
func TestWSHandlesArrayParams(t *testing.T) {
|
|
|
|
cl := client.NewWSClient(tcpAddr, websocketEndpoint)
|
2017-08-07 18:29:55 -04:00
|
|
|
cl.SetLogger(log.TestingLogger())
|
2017-11-06 13:20:39 -05:00
|
|
|
err := cl.Start()
|
2017-04-21 18:30:22 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
defer cl.Stop()
|
|
|
|
|
|
|
|
val := "acbd"
|
|
|
|
params := []interface{}{val}
|
2017-07-31 18:44:46 -04:00
|
|
|
err = cl.CallWithArrayParams(context.Background(), "echo_ws", params)
|
2017-04-21 18:30:22 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
|
2017-11-27 22:35:16 +00:00
|
|
|
msg := <-cl.ResponsesCh
|
|
|
|
if msg.Error != nil {
|
|
|
|
t.Fatalf("%+v", err)
|
2017-04-21 18:30:22 +03:00
|
|
|
}
|
2017-10-26 19:24:18 -04:00
|
|
|
result := new(ResultEcho)
|
2017-11-27 22:35:16 +00:00
|
|
|
err = json.Unmarshal(msg.Result, result)
|
2017-10-26 19:24:18 -04:00
|
|
|
require.Nil(t, err)
|
|
|
|
got := result.Value
|
|
|
|
assert.Equal(t, got, val)
|
2017-04-21 18:30:22 +03:00
|
|
|
}
|
|
|
|
|
2017-07-31 18:44:46 -04:00
|
|
|
// TestWSClientPingPong checks that a client & server exchange pings
|
|
|
|
// & pongs so connection stays alive.
|
|
|
|
func TestWSClientPingPong(t *testing.T) {
|
|
|
|
cl := client.NewWSClient(tcpAddr, websocketEndpoint)
|
2017-08-07 18:29:55 -04:00
|
|
|
cl.SetLogger(log.TestingLogger())
|
2017-11-06 13:20:39 -05:00
|
|
|
err := cl.Start()
|
2017-07-31 18:44:46 -04:00
|
|
|
require.Nil(t, err)
|
|
|
|
defer cl.Stop()
|
|
|
|
|
2017-09-27 15:44:19 +04:00
|
|
|
time.Sleep(6 * time.Second)
|
2017-07-31 18:44:46 -04:00
|
|
|
}
|
|
|
|
|
2017-03-10 14:56:04 +04:00
|
|
|
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)
|
|
|
|
}
|