Browse Source

rpc client SetPingHandler with 30 second timeout; wire time json test

pull/144/head
Jae Kwon 9 years ago
parent
commit
726f5475b0
2 changed files with 20 additions and 7 deletions
  1. +16
    -4
      rpc/core_client/ws_client.go
  2. +4
    -3
      wire/reflect_test.go

+ 16
- 4
rpc/core_client/ws_client.go View File

@ -3,6 +3,7 @@ package core_client
import ( import (
"net/http" "net/http"
"strings" "strings"
"time"
"github.com/tendermint/tendermint/Godeps/_workspace/src/github.com/gorilla/websocket" "github.com/tendermint/tendermint/Godeps/_workspace/src/github.com/gorilla/websocket"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
@ -12,8 +13,11 @@ import (
"github.com/tendermint/tendermint/wire" "github.com/tendermint/tendermint/wire"
) )
const wsEventsChannelCapacity = 10
const wsResultsChannelCapacity = 10
const (
wsEventsChannelCapacity = 10
wsResultsChannelCapacity = 10
wsWriteTimeoutSeconds = 10
)
type WSClient struct { type WSClient struct {
QuitService QuitService
@ -53,6 +57,14 @@ func (wsc *WSClient) dial() error {
if err != nil { if err != nil {
return err return err
} }
// Set the ping/pong handlers
con.SetPingHandler(func(m string) error {
con.WriteControl(websocket.PongMessage, []byte(m), time.Now().Add(time.Second*wsWriteTimeoutSeconds))
return nil
})
con.SetPongHandler(func(m string) error {
return nil
})
wsc.Conn = con wsc.Conn = con
return nil return nil
} }
@ -65,14 +77,14 @@ func (wsc *WSClient) receiveEventsRoutine() {
for { for {
_, data, err := wsc.ReadMessage() _, data, err := wsc.ReadMessage()
if err != nil { if err != nil {
log.Info(Fmt("WSClient failed to read message: %v", err))
log.Info("WSClient failed to read message", "error", err, "data", string(data))
wsc.Stop() wsc.Stop()
break break
} else { } else {
var response ctypes.Response var response ctypes.Response
wire.ReadJSON(&response, data, &err) wire.ReadJSON(&response, data, &err)
if err != nil { if err != nil {
log.Info(Fmt("WSClient failed to parse message: %v", err))
log.Info("WSClient failed to parse message", "error", err)
wsc.Stop() wsc.Stop()
break break
} }


+ 4
- 3
wire/reflect_test.go View File

@ -51,6 +51,7 @@ var _ = RegisterInterface(
ConcreteType{&Viper{}, AnimalTypeViper}, ConcreteType{&Viper{}, AnimalTypeViper},
) )
// TODO: add assertions here ...
func TestAnimalInterface(t *testing.T) { func TestAnimalInterface(t *testing.T) {
var foo Animal var foo Animal
@ -100,7 +101,7 @@ func constructBasic() interface{} {
SimpleStruct{ SimpleStruct{
String: "String", String: "String",
Bytes: []byte("Bytes"), Bytes: []byte("Bytes"),
Time: time.Unix(123, 0),
Time: time.Unix(123, 456789999),
}, },
} }
return cat return cat
@ -118,8 +119,8 @@ func validateBasic(o interface{}, t *testing.T) {
if string(cat.Bytes) != "Bytes" { if string(cat.Bytes) != "Bytes" {
t.Errorf("Expected cat.Bytes == 'Bytes', got %X", cat.Bytes) t.Errorf("Expected cat.Bytes == 'Bytes', got %X", cat.Bytes)
} }
if cat.Time.Unix() != 123 {
t.Errorf("Expected cat.Time == 'Unix(123)', got %v", cat.Time)
if cat.Time.UnixNano() != 123456000000 { // Only milliseconds
t.Errorf("Expected cat.Time.UnixNano() == 123456000000, got %v", cat.Time.UnixNano())
} }
} }


Loading…
Cancel
Save