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

This commit is contained in:
Jae Kwon 2015-09-15 00:55:51 -07:00
parent c6e5948534
commit 726f5475b0
2 changed files with 20 additions and 7 deletions

View File

@ -3,6 +3,7 @@ package core_client
import (
"net/http"
"strings"
"time"
"github.com/tendermint/tendermint/Godeps/_workspace/src/github.com/gorilla/websocket"
. "github.com/tendermint/tendermint/common"
@ -12,8 +13,11 @@ import (
"github.com/tendermint/tendermint/wire"
)
const wsEventsChannelCapacity = 10
const wsResultsChannelCapacity = 10
const (
wsEventsChannelCapacity = 10
wsResultsChannelCapacity = 10
wsWriteTimeoutSeconds = 10
)
type WSClient struct {
QuitService
@ -53,6 +57,14 @@ func (wsc *WSClient) dial() error {
if err != nil {
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
return nil
}
@ -65,14 +77,14 @@ func (wsc *WSClient) receiveEventsRoutine() {
for {
_, data, err := wsc.ReadMessage()
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()
break
} else {
var response ctypes.Response
wire.ReadJSON(&response, data, &err)
if err != nil {
log.Info(Fmt("WSClient failed to parse message: %v", err))
log.Info("WSClient failed to parse message", "error", err)
wsc.Stop()
break
}

View File

@ -51,6 +51,7 @@ var _ = RegisterInterface(
ConcreteType{&Viper{}, AnimalTypeViper},
)
// TODO: add assertions here ...
func TestAnimalInterface(t *testing.T) {
var foo Animal
@ -100,7 +101,7 @@ func constructBasic() interface{} {
SimpleStruct{
String: "String",
Bytes: []byte("Bytes"),
Time: time.Unix(123, 0),
Time: time.Unix(123, 456789999),
},
}
return cat
@ -118,8 +119,8 @@ func validateBasic(o interface{}, t *testing.T) {
if string(cat.Bytes) != "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())
}
}