Browse Source

Merge pull request #30 from tendermint/develop

Develop
pull/1780/head
Ethan Buchman 8 years ago
committed by GitHub
parent
commit
0e2a6b41f8
13 changed files with 432 additions and 394 deletions
  1. +3
    -1
      client/client.go
  2. +89
    -57
      client/grpc_client.go
  3. +7
    -6
      client/local_client.go
  4. +7
    -0
      client/log.go
  5. +111
    -73
      client/socket_client.go
  6. +0
    -93
      example/dummy/dummy_test.go
  7. +3
    -0
      example/example.go
  8. +151
    -0
      example/example_test.go
  9. +0
    -148
      example/nil/nil_test.go
  10. +5
    -4
      server/grpc_server.go
  11. +7
    -0
      server/log.go
  12. +48
    -11
      server/socket_server.go
  13. +1
    -1
      tests/test_cli/test.sh

+ 3
- 1
client/client.go View File

@ -4,13 +4,15 @@ import (
"fmt" "fmt"
"sync" "sync"
. "github.com/tendermint/go-common"
"github.com/tendermint/tmsp/types" "github.com/tendermint/tmsp/types"
) )
type Client interface { type Client interface {
Service
SetResponseCallback(Callback) SetResponseCallback(Callback)
Error() error Error() error
Stop() bool
FlushAsync() *ReqRes FlushAsync() *ReqRes
EchoAsync(msg string) *ReqRes EchoAsync(msg string) *ReqRes


+ 89
- 57
client/grpc_client.go View File

@ -1,7 +1,6 @@
package tmspcli package tmspcli
import ( import (
"fmt"
"net" "net"
"sync" "sync"
"time" "time"
@ -44,42 +43,57 @@ func dialerFunc(addr string, timeout time.Duration) (net.Conn, error) {
func (cli *grpcClient) OnStart() error { func (cli *grpcClient) OnStart() error {
cli.QuitService.OnStart() cli.QuitService.OnStart()
RETRY_LOOP: RETRY_LOOP:
for { for {
conn, err := grpc.Dial(cli.addr, grpc.WithInsecure(), grpc.WithDialer(dialerFunc)) conn, err := grpc.Dial(cli.addr, grpc.WithInsecure(), grpc.WithDialer(dialerFunc))
if err != nil { if err != nil {
if cli.mustConnect { if cli.mustConnect {
return err return err
} else { } else {
fmt.Printf("tmsp.grpcClient failed to connect to %v. Retrying...\n", cli.addr)
log.Warn(Fmt("tmsp.grpcClient failed to connect to %v. Retrying...\n", cli.addr))
time.Sleep(time.Second * 3) time.Sleep(time.Second * 3)
continue RETRY_LOOP continue RETRY_LOOP
} }
} }
cli.client = types.NewTMSPApplicationClient(conn)
client := types.NewTMSPApplicationClient(conn)
ENSURE_CONNECTED:
for {
_, err := client.Echo(context.Background(), &types.RequestEcho{"hello"}, grpc.FailFast(true))
if err == nil {
break ENSURE_CONNECTED
}
time.Sleep(time.Second)
}
cli.client = client
return nil return nil
} }
} }
func (cli *grpcClient) OnStop() { func (cli *grpcClient) OnStop() {
cli.QuitService.OnStop() cli.QuitService.OnStop()
// TODO: how to close when TMSPApplicationClient interface doesn't expose Close ?
}
// Set listener for all responses
// NOTE: callback may get internally generated flush responses.
func (cli *grpcClient) SetResponseCallback(resCb Callback) {
cli.mtx.Lock() cli.mtx.Lock()
defer cli.mtx.Unlock() defer cli.mtx.Unlock()
cli.resCb = resCb
// TODO: how to close conn? its not a net.Conn and grpc doesn't expose a Close()
/*if cli.conn != nil {
cli.conn.Close()
}*/
} }
func (cli *grpcClient) StopForError(err error) { func (cli *grpcClient) StopForError(err error) {
cli.mtx.Lock() cli.mtx.Lock()
fmt.Printf("Stopping tmsp.grpcClient for error: %v\n", err.Error())
if !cli.IsRunning() {
return
}
if cli.err == nil { if cli.err == nil {
cli.err = err cli.err = err
} }
cli.mtx.Unlock() cli.mtx.Unlock()
log.Warn(Fmt("Stopping tmsp.grpcClient for error: %v", err.Error()))
cli.Stop() cli.Stop()
} }
@ -89,6 +103,14 @@ func (cli *grpcClient) Error() error {
return cli.err return cli.err
} }
// Set listener for all responses
// NOTE: callback may get internally generated flush responses.
func (cli *grpcClient) SetResponseCallback(resCb Callback) {
cli.mtx.Lock()
defer cli.mtx.Unlock()
cli.resCb = resCb
}
//---------------------------------------- //----------------------------------------
// GRPC calls are synchronous, but some callbacks expect to be called asynchronously // GRPC calls are synchronous, but some callbacks expect to be called asynchronously
// (eg. the mempool expects to be able to lock to remove bad txs from cache). // (eg. the mempool expects to be able to lock to remove bad txs from cache).
@ -99,99 +121,99 @@ func (cli *grpcClient) Error() error {
func (cli *grpcClient) EchoAsync(msg string) *ReqRes { func (cli *grpcClient) EchoAsync(msg string) *ReqRes {
req := types.ToRequestEcho(msg) req := types.ToRequestEcho(msg)
res, err := cli.client.Echo(context.Background(), req.GetEcho())
res, err := cli.client.Echo(context.Background(), req.GetEcho(), grpc.FailFast(true))
if err != nil { if err != nil {
cli.err = err
cli.StopForError(err)
} }
return cli.finishAsyncCall(req, &types.Response{&types.Response_Echo{res}}) return cli.finishAsyncCall(req, &types.Response{&types.Response_Echo{res}})
} }
func (cli *grpcClient) FlushAsync() *ReqRes { func (cli *grpcClient) FlushAsync() *ReqRes {
req := types.ToRequestFlush() req := types.ToRequestFlush()
res, err := cli.client.Flush(context.Background(), req.GetFlush())
res, err := cli.client.Flush(context.Background(), req.GetFlush(), grpc.FailFast(true))
if err != nil { if err != nil {
cli.err = err
cli.StopForError(err)
} }
return cli.finishAsyncCall(req, &types.Response{&types.Response_Flush{res}}) return cli.finishAsyncCall(req, &types.Response{&types.Response_Flush{res}})
} }
func (cli *grpcClient) InfoAsync() *ReqRes { func (cli *grpcClient) InfoAsync() *ReqRes {
req := types.ToRequestInfo() req := types.ToRequestInfo()
res, err := cli.client.Info(context.Background(), req.GetInfo())
res, err := cli.client.Info(context.Background(), req.GetInfo(), grpc.FailFast(true))
if err != nil { if err != nil {
cli.err = err
cli.StopForError(err)
} }
return cli.finishAsyncCall(req, &types.Response{&types.Response_Info{res}}) return cli.finishAsyncCall(req, &types.Response{&types.Response_Info{res}})
} }
func (cli *grpcClient) SetOptionAsync(key string, value string) *ReqRes { func (cli *grpcClient) SetOptionAsync(key string, value string) *ReqRes {
req := types.ToRequestSetOption(key, value) req := types.ToRequestSetOption(key, value)
res, err := cli.client.SetOption(context.Background(), req.GetSetOption())
res, err := cli.client.SetOption(context.Background(), req.GetSetOption(), grpc.FailFast(true))
if err != nil { if err != nil {
cli.err = err
cli.StopForError(err)
} }
return cli.finishAsyncCall(req, &types.Response{&types.Response_SetOption{res}}) return cli.finishAsyncCall(req, &types.Response{&types.Response_SetOption{res}})
} }
func (cli *grpcClient) AppendTxAsync(tx []byte) *ReqRes { func (cli *grpcClient) AppendTxAsync(tx []byte) *ReqRes {
req := types.ToRequestAppendTx(tx) req := types.ToRequestAppendTx(tx)
res, err := cli.client.AppendTx(context.Background(), req.GetAppendTx())
res, err := cli.client.AppendTx(context.Background(), req.GetAppendTx(), grpc.FailFast(true))
if err != nil { if err != nil {
cli.err = err
cli.StopForError(err)
} }
return cli.finishAsyncCall(req, &types.Response{&types.Response_AppendTx{res}}) return cli.finishAsyncCall(req, &types.Response{&types.Response_AppendTx{res}})
} }
func (cli *grpcClient) CheckTxAsync(tx []byte) *ReqRes { func (cli *grpcClient) CheckTxAsync(tx []byte) *ReqRes {
req := types.ToRequestCheckTx(tx) req := types.ToRequestCheckTx(tx)
res, err := cli.client.CheckTx(context.Background(), req.GetCheckTx())
res, err := cli.client.CheckTx(context.Background(), req.GetCheckTx(), grpc.FailFast(true))
if err != nil { if err != nil {
cli.err = err
cli.StopForError(err)
} }
return cli.finishAsyncCall(req, &types.Response{&types.Response_CheckTx{res}}) return cli.finishAsyncCall(req, &types.Response{&types.Response_CheckTx{res}})
} }
func (cli *grpcClient) QueryAsync(query []byte) *ReqRes { func (cli *grpcClient) QueryAsync(query []byte) *ReqRes {
req := types.ToRequestQuery(query) req := types.ToRequestQuery(query)
res, err := cli.client.Query(context.Background(), req.GetQuery())
res, err := cli.client.Query(context.Background(), req.GetQuery(), grpc.FailFast(true))
if err != nil { if err != nil {
cli.err = err
cli.StopForError(err)
} }
return cli.finishAsyncCall(req, &types.Response{&types.Response_Query{res}}) return cli.finishAsyncCall(req, &types.Response{&types.Response_Query{res}})
} }
func (cli *grpcClient) CommitAsync() *ReqRes { func (cli *grpcClient) CommitAsync() *ReqRes {
req := types.ToRequestCommit() req := types.ToRequestCommit()
res, err := cli.client.Commit(context.Background(), req.GetCommit())
res, err := cli.client.Commit(context.Background(), req.GetCommit(), grpc.FailFast(true))
if err != nil { if err != nil {
cli.err = err
cli.StopForError(err)
} }
return cli.finishAsyncCall(req, &types.Response{&types.Response_Commit{res}}) return cli.finishAsyncCall(req, &types.Response{&types.Response_Commit{res}})
} }
func (cli *grpcClient) InitChainAsync(validators []*types.Validator) *ReqRes { func (cli *grpcClient) InitChainAsync(validators []*types.Validator) *ReqRes {
req := types.ToRequestInitChain(validators) req := types.ToRequestInitChain(validators)
res, err := cli.client.InitChain(context.Background(), req.GetInitChain())
res, err := cli.client.InitChain(context.Background(), req.GetInitChain(), grpc.FailFast(true))
if err != nil { if err != nil {
cli.err = err
cli.StopForError(err)
} }
return cli.finishAsyncCall(req, &types.Response{&types.Response_InitChain{res}}) return cli.finishAsyncCall(req, &types.Response{&types.Response_InitChain{res}})
} }
func (cli *grpcClient) BeginBlockAsync(height uint64) *ReqRes { func (cli *grpcClient) BeginBlockAsync(height uint64) *ReqRes {
req := types.ToRequestBeginBlock(height) req := types.ToRequestBeginBlock(height)
res, err := cli.client.BeginBlock(context.Background(), req.GetBeginBlock())
res, err := cli.client.BeginBlock(context.Background(), req.GetBeginBlock(), grpc.FailFast(true))
if err != nil { if err != nil {
cli.err = err
cli.StopForError(err)
} }
return cli.finishAsyncCall(req, &types.Response{&types.Response_BeginBlock{res}}) return cli.finishAsyncCall(req, &types.Response{&types.Response_BeginBlock{res}})
} }
func (cli *grpcClient) EndBlockAsync(height uint64) *ReqRes { func (cli *grpcClient) EndBlockAsync(height uint64) *ReqRes {
req := types.ToRequestEndBlock(height) req := types.ToRequestEndBlock(height)
res, err := cli.client.EndBlock(context.Background(), req.GetEndBlock())
res, err := cli.client.EndBlock(context.Background(), req.GetEndBlock(), grpc.FailFast(true))
if err != nil { if err != nil {
cli.err = err
cli.StopForError(err)
} }
return cli.finishAsyncCall(req, &types.Response{&types.Response_EndBlock{res}}) return cli.finishAsyncCall(req, &types.Response{&types.Response_EndBlock{res}})
} }
@ -217,11 +239,23 @@ func (cli *grpcClient) finishAsyncCall(req *types.Request, res *types.Response)
return reqres return reqres
} }
func (cli *grpcClient) checkErrGetResult() types.Result {
if err := cli.Error(); err != nil {
// StopForError should already have been called if error is set
return types.ErrInternalError.SetLog(err.Error())
}
return types.Result{}
}
//---------------------------------------- //----------------------------------------
func (cli *grpcClient) EchoSync(msg string) (res types.Result) { func (cli *grpcClient) EchoSync(msg string) (res types.Result) {
r := cli.EchoAsync(msg).Response.GetEcho()
return types.NewResultOK([]byte(r.Message), LOG)
reqres := cli.EchoAsync(msg)
if res := cli.checkErrGetResult(); res.IsErr() {
return res
}
resp := reqres.Response.GetEcho()
return types.NewResultOK([]byte(resp.Message), LOG)
} }
func (cli *grpcClient) FlushSync() error { func (cli *grpcClient) FlushSync() error {
@ -229,14 +263,18 @@ func (cli *grpcClient) FlushSync() error {
} }
func (cli *grpcClient) InfoSync() (res types.Result) { func (cli *grpcClient) InfoSync() (res types.Result) {
r := cli.InfoAsync().Response.GetInfo()
return types.NewResultOK([]byte(r.Info), LOG)
reqres := cli.InfoAsync()
if res := cli.checkErrGetResult(); res.IsErr() {
return res
}
resp := reqres.Response.GetInfo()
return types.NewResultOK([]byte(resp.Info), LOG)
} }
func (cli *grpcClient) SetOptionSync(key string, value string) (res types.Result) { func (cli *grpcClient) SetOptionSync(key string, value string) (res types.Result) {
reqres := cli.SetOptionAsync(key, value) reqres := cli.SetOptionAsync(key, value)
if cli.err != nil {
return types.ErrInternalError.SetLog(cli.err.Error())
if res := cli.checkErrGetResult(); res.IsErr() {
return res
} }
resp := reqres.Response.GetSetOption() resp := reqres.Response.GetSetOption()
return types.Result{Code: OK, Data: nil, Log: resp.Log} return types.Result{Code: OK, Data: nil, Log: resp.Log}
@ -244,8 +282,8 @@ func (cli *grpcClient) SetOptionSync(key string, value string) (res types.Result
func (cli *grpcClient) AppendTxSync(tx []byte) (res types.Result) { func (cli *grpcClient) AppendTxSync(tx []byte) (res types.Result) {
reqres := cli.AppendTxAsync(tx) reqres := cli.AppendTxAsync(tx)
if cli.err != nil {
return types.ErrInternalError.SetLog(cli.err.Error())
if res := cli.checkErrGetResult(); res.IsErr() {
return res
} }
resp := reqres.Response.GetAppendTx() resp := reqres.Response.GetAppendTx()
return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log} return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log}
@ -253,8 +291,8 @@ func (cli *grpcClient) AppendTxSync(tx []byte) (res types.Result) {
func (cli *grpcClient) CheckTxSync(tx []byte) (res types.Result) { func (cli *grpcClient) CheckTxSync(tx []byte) (res types.Result) {
reqres := cli.CheckTxAsync(tx) reqres := cli.CheckTxAsync(tx)
if cli.err != nil {
return types.ErrInternalError.SetLog(cli.err.Error())
if res := cli.checkErrGetResult(); res.IsErr() {
return res
} }
resp := reqres.Response.GetCheckTx() resp := reqres.Response.GetCheckTx()
return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log} return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log}
@ -262,8 +300,8 @@ func (cli *grpcClient) CheckTxSync(tx []byte) (res types.Result) {
func (cli *grpcClient) QuerySync(query []byte) (res types.Result) { func (cli *grpcClient) QuerySync(query []byte) (res types.Result) {
reqres := cli.QueryAsync(query) reqres := cli.QueryAsync(query)
if cli.err != nil {
return types.ErrInternalError.SetLog(cli.err.Error())
if res := cli.checkErrGetResult(); res.IsErr() {
return res
} }
resp := reqres.Response.GetQuery() resp := reqres.Response.GetQuery()
return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log} return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log}
@ -271,8 +309,8 @@ func (cli *grpcClient) QuerySync(query []byte) (res types.Result) {
func (cli *grpcClient) CommitSync() (res types.Result) { func (cli *grpcClient) CommitSync() (res types.Result) {
reqres := cli.CommitAsync() reqres := cli.CommitAsync()
if cli.err != nil {
return types.ErrInternalError.SetLog(cli.err.Error())
if res := cli.checkErrGetResult(); res.IsErr() {
return res
} }
resp := reqres.Response.GetCommit() resp := reqres.Response.GetCommit()
return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log} return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log}
@ -280,24 +318,18 @@ func (cli *grpcClient) CommitSync() (res types.Result) {
func (cli *grpcClient) InitChainSync(validators []*types.Validator) (err error) { func (cli *grpcClient) InitChainSync(validators []*types.Validator) (err error) {
cli.InitChainAsync(validators) cli.InitChainAsync(validators)
if cli.err != nil {
return cli.err
}
return nil
return cli.Error()
} }
func (cli *grpcClient) BeginBlockSync(height uint64) (err error) { func (cli *grpcClient) BeginBlockSync(height uint64) (err error) {
cli.BeginBlockAsync(height) cli.BeginBlockAsync(height)
if cli.err != nil {
return cli.err
}
return nil
return cli.Error()
} }
func (cli *grpcClient) EndBlockSync(height uint64) (validators []*types.Validator, err error) { func (cli *grpcClient) EndBlockSync(height uint64) (validators []*types.Validator, err error) {
reqres := cli.EndBlockAsync(height) reqres := cli.EndBlockAsync(height)
if cli.err != nil {
return nil, cli.err
if err := cli.Error(); err != nil {
return nil, err
} }
return reqres.Response.GetEndBlock().Diffs, nil return reqres.Response.GetEndBlock().Diffs, nil
} }

+ 7
- 6
client/local_client.go View File

@ -1,11 +1,14 @@
package tmspcli package tmspcli
import ( import (
types "github.com/tendermint/tmsp/types"
"sync" "sync"
. "github.com/tendermint/go-common"
types "github.com/tendermint/tmsp/types"
) )
type localClient struct { type localClient struct {
BaseService
mtx *sync.Mutex mtx *sync.Mutex
types.Application types.Application
Callback Callback
@ -15,10 +18,12 @@ func NewLocalClient(mtx *sync.Mutex, app types.Application) *localClient {
if mtx == nil { if mtx == nil {
mtx = new(sync.Mutex) mtx = new(sync.Mutex)
} }
return &localClient{
cli := &localClient{
mtx: mtx, mtx: mtx,
Application: app, Application: app,
} }
cli.BaseService = *NewBaseService(log, "localClient", cli)
return cli
} }
func (app *localClient) SetResponseCallback(cb Callback) { func (app *localClient) SetResponseCallback(cb Callback) {
@ -32,10 +37,6 @@ func (app *localClient) Error() error {
return nil return nil
} }
func (app *localClient) Stop() bool {
return true
}
func (app *localClient) FlushAsync() *ReqRes { func (app *localClient) FlushAsync() *ReqRes {
// Do nothing // Do nothing
return newLocalReqRes(types.ToRequestFlush(), nil) return newLocalReqRes(types.ToRequestFlush(), nil)


+ 7
- 0
client/log.go View File

@ -0,0 +1,7 @@
package tmspcli
import (
"github.com/tendermint/go-logger"
)
var log = logger.New("module", "tmspcli")

client/remote_client.go → client/socket_client.go View File


+ 0
- 93
example/dummy/dummy_test.go View File

@ -1,93 +0,0 @@
package dummy
import (
"reflect"
"testing"
"time"
. "github.com/tendermint/go-common"
"github.com/tendermint/tmsp/server"
"github.com/tendermint/tmsp/types"
)
func TestStream(t *testing.T) {
numAppendTxs := 200000
// Start the listener
server, err := server.NewSocketServer("unix://test.sock", NewDummyApplication())
if err != nil {
Exit(err.Error())
}
defer server.Stop()
// Connect to the socket
conn, err := Connect("unix://test.sock")
if err != nil {
Exit(err.Error())
}
// Read response data
done := make(chan struct{})
go func() {
counter := 0
for {
var res = &types.Response{}
err := types.ReadMessage(conn, res)
if err != nil {
Exit(err.Error())
}
// Process response
switch r := res.Value.(type) {
case *types.Response_AppendTx:
counter += 1
if r.AppendTx.Code != types.CodeType_OK {
t.Error("AppendTx failed with ret_code", r.AppendTx.Code)
}
if counter > numAppendTxs {
t.Fatal("Too many AppendTx responses")
}
t.Log("response", counter)
if counter == numAppendTxs {
go func() {
time.Sleep(time.Second * 2) // Wait for a bit to allow counter overflow
close(done)
}()
}
case *types.Response_Flush:
// ignore
default:
t.Error("Unexpected response type", reflect.TypeOf(res.Value))
}
}
}()
// Write requests
for counter := 0; counter < numAppendTxs; counter++ {
// Send request
var req = types.ToRequestAppendTx([]byte("test"))
err := types.WriteMessage(req, conn)
if err != nil {
t.Fatal(err.Error())
}
// Sometimes send flush messages
if counter%123 == 0 {
t.Log("flush")
err := types.WriteMessage(types.ToRequestFlush(), conn)
if err != nil {
t.Fatal(err.Error())
}
}
}
// Send final flush message
err = types.WriteMessage(types.ToRequestFlush(), conn)
if err != nil {
t.Fatal(err.Error())
}
<-done
}

+ 3
- 0
example/example.go View File

@ -0,0 +1,3 @@
package example
// so the go tool doesn't return errors about no buildable go files ...

+ 151
- 0
example/example_test.go View File

@ -0,0 +1,151 @@
package example
import (
"fmt"
"net"
"reflect"
"testing"
"time"
"golang.org/x/net/context"
"google.golang.org/grpc"
. "github.com/tendermint/go-common"
"github.com/tendermint/tmsp/client"
"github.com/tendermint/tmsp/example/dummy"
nilapp "github.com/tendermint/tmsp/example/nil"
"github.com/tendermint/tmsp/server"
"github.com/tendermint/tmsp/types"
)
func TestDummy(t *testing.T) {
fmt.Println("### Testing Dummy")
testStream(t, dummy.NewDummyApplication())
}
func TestNilApp(t *testing.T) {
fmt.Println("### Testing NilApp")
testStream(t, nilapp.NewNilApplication())
}
func TestGRPC(t *testing.T) {
fmt.Println("### Testing GRPC")
testGRPCSync(t, types.NewGRPCApplication(nilapp.NewNilApplication()))
}
func testStream(t *testing.T, app types.Application) {
numAppendTxs := 200000
// Start the listener
server, err := server.NewSocketServer("unix://test.sock", app)
if err != nil {
Exit(Fmt("Error starting socket server: %v", err.Error()))
}
defer server.Stop()
// Connect to the socket
client, err := tmspcli.NewSocketClient("unix://test.sock", false)
if err != nil {
Exit(Fmt("Error starting socket client: %v", err.Error()))
}
client.Start()
defer client.Stop()
done := make(chan struct{})
counter := 0
client.SetResponseCallback(func(req *types.Request, res *types.Response) {
// Process response
switch r := res.Value.(type) {
case *types.Response_AppendTx:
counter += 1
if r.AppendTx.Code != types.CodeType_OK {
t.Error("AppendTx failed with ret_code", r.AppendTx.Code)
}
if counter > numAppendTxs {
t.Fatalf("Too many AppendTx responses. Got %d, expected %d", counter, numAppendTxs)
}
if counter == numAppendTxs {
go func() {
time.Sleep(time.Second * 2) // Wait for a bit to allow counter overflow
close(done)
}()
return
}
case *types.Response_Flush:
// ignore
default:
t.Error("Unexpected response type", reflect.TypeOf(res.Value))
}
})
// Write requests
for counter := 0; counter < numAppendTxs; counter++ {
// Send request
reqRes := client.AppendTxAsync([]byte("test"))
_ = reqRes
// check err ?
// Sometimes send flush messages
if counter%123 == 0 {
client.FlushAsync()
// check err ?
}
}
// Send final flush message
client.FlushAsync()
<-done
}
//-------------------------
// test grpc
func dialerFunc(addr string, timeout time.Duration) (net.Conn, error) {
return Connect(addr)
}
func testGRPCSync(t *testing.T, app *types.GRPCApplication) {
numAppendTxs := 2000
// Start the listener
server, err := server.NewGRPCServer("unix://test.sock", app)
if err != nil {
Exit(Fmt("Error starting GRPC server: %v", err.Error()))
}
defer server.Stop()
// Connect to the socket
conn, err := grpc.Dial("unix://test.sock", grpc.WithInsecure(), grpc.WithDialer(dialerFunc))
if err != nil {
Exit(Fmt("Error dialing GRPC server: %v", err.Error()))
}
defer conn.Close()
client := types.NewTMSPApplicationClient(conn)
// Write requests
for counter := 0; counter < numAppendTxs; counter++ {
// Send request
response, err := client.AppendTx(context.Background(), &types.RequestAppendTx{[]byte("test")})
if err != nil {
t.Fatalf("Error in GRPC AppendTx: %v", err.Error())
}
counter += 1
if response.Code != types.CodeType_OK {
t.Error("AppendTx failed with ret_code", response.Code)
}
if counter > numAppendTxs {
t.Fatal("Too many AppendTx responses")
}
t.Log("response", counter)
if counter == numAppendTxs {
go func() {
time.Sleep(time.Second * 2) // Wait for a bit to allow counter overflow
}()
}
}
}

+ 0
- 148
example/nil/nil_test.go View File

@ -1,148 +0,0 @@
package nilapp
import (
"net"
"reflect"
"testing"
"time"
"golang.org/x/net/context"
"google.golang.org/grpc"
. "github.com/tendermint/go-common"
"github.com/tendermint/tmsp/server"
"github.com/tendermint/tmsp/types"
)
func TestStream(t *testing.T) {
numAppendTxs := 200000
// Start the listener
server, err := server.NewSocketServer("unix://test.sock", NewNilApplication())
if err != nil {
Exit(err.Error())
}
defer server.Stop()
// Connect to the socket
conn, err := Connect("unix://test.sock")
if err != nil {
Exit(err.Error())
}
// Read response data
done := make(chan struct{})
go func() {
counter := 0
for {
var res = &types.Response{}
err := types.ReadMessage(conn, res)
if err != nil {
Exit(err.Error())
}
// Process response
switch r := res.Value.(type) {
case *types.Response_AppendTx:
counter += 1
if r.AppendTx.Code != types.CodeType_OK {
t.Error("AppendTx failed with ret_code", r.AppendTx.Code)
}
if counter > numAppendTxs {
t.Fatal("Too many AppendTx responses")
}
t.Log("response", counter)
if counter == numAppendTxs {
go func() {
time.Sleep(time.Second * 2) // Wait for a bit to allow counter overflow
close(done)
}()
}
case *types.Response_Flush:
// ignore
default:
t.Error("Unexpected response type", reflect.TypeOf(res.Value))
}
}
}()
// Write requests
for counter := 0; counter < numAppendTxs; counter++ {
// Send request
var req = types.ToRequestAppendTx([]byte("test"))
err := types.WriteMessage(req, conn)
if err != nil {
t.Fatal(err.Error())
}
// Sometimes send flush messages
if counter%123 == 0 {
t.Log("flush")
err := types.WriteMessage(types.ToRequestFlush(), conn)
if err != nil {
t.Fatal(err.Error())
}
}
}
// Send final flush message
err = types.WriteMessage(types.ToRequestFlush(), conn)
if err != nil {
t.Fatal(err.Error())
}
<-done
}
//-------------------------
// test grpc
func dialerFunc(addr string, timeout time.Duration) (net.Conn, error) {
return Connect(addr)
}
func TestGRPCSync(t *testing.T) {
numAppendTxs := 2000
// Start the listener
server, err := server.NewGRPCServer("unix://test.sock", types.NewGRPCApplication(NewNilApplication()))
if err != nil {
Exit(err.Error())
}
defer server.Stop()
// Connect to the socket
conn, err := grpc.Dial("unix://test.sock", grpc.WithInsecure(), grpc.WithDialer(dialerFunc))
if err != nil {
Exit(err.Error())
}
defer conn.Close()
client := types.NewTMSPApplicationClient(conn)
// Write requests
for counter := 0; counter < numAppendTxs; counter++ {
// Send request
response, err := client.AppendTx(context.Background(), &types.RequestAppendTx{[]byte("test")})
if err != nil {
t.Fatal(err.Error())
}
counter += 1
if response.Code != types.CodeType_OK {
t.Error("AppendTx failed with ret_code", response.Code)
}
if counter > numAppendTxs {
t.Fatal("Too many AppendTx responses")
}
t.Log("response", counter)
if counter == numAppendTxs {
go func() {
time.Sleep(time.Second * 2) // Wait for a bit to allow counter overflow
}()
}
}
}

+ 5
- 4
server/grpc_server.go View File

@ -18,6 +18,7 @@ type GRPCServer struct {
proto string proto string
addr string addr string
listener net.Listener listener net.Listener
server *grpc.Server
app types.TMSPApplicationServer app types.TMSPApplicationServer
} }
@ -43,13 +44,13 @@ func (s *GRPCServer) OnStart() error {
return err return err
} }
s.listener = ln s.listener = ln
grpcServer := grpc.NewServer()
types.RegisterTMSPApplicationServer(grpcServer, s.app)
go grpcServer.Serve(ln)
s.server = grpc.NewServer()
types.RegisterTMSPApplicationServer(s.server, s.app)
go s.server.Serve(s.listener)
return nil return nil
} }
func (s *GRPCServer) OnStop() { func (s *GRPCServer) OnStop() {
s.QuitService.OnStop() s.QuitService.OnStop()
s.listener.Close()
s.server.Stop()
} }

+ 7
- 0
server/log.go View File

@ -0,0 +1,7 @@
package server
import (
"github.com/tendermint/go-logger"
)
var log = logger.New("module", "tmsp-server")

+ 48
- 11
server/socket_server.go View File

@ -21,6 +21,10 @@ type SocketServer struct {
addr string addr string
listener net.Listener listener net.Listener
connsMtx sync.Mutex
conns map[int]net.Conn
nextConnID int
appMtx sync.Mutex appMtx sync.Mutex
app types.Application app types.Application
} }
@ -33,6 +37,7 @@ func NewSocketServer(protoAddr string, app types.Application) (Service, error) {
addr: addr, addr: addr,
listener: nil, listener: nil,
app: app, app: app,
conns: make(map[int]net.Conn),
} }
s.QuitService = *NewQuitService(nil, "TMSPServer", s) s.QuitService = *NewQuitService(nil, "TMSPServer", s)
_, err := s.Start() // Just start it _, err := s.Start() // Just start it
@ -53,6 +58,33 @@ func (s *SocketServer) OnStart() error {
func (s *SocketServer) OnStop() { func (s *SocketServer) OnStop() {
s.QuitService.OnStop() s.QuitService.OnStop()
s.listener.Close() s.listener.Close()
s.connsMtx.Lock()
for id, conn := range s.conns {
delete(s.conns, id)
conn.Close()
}
s.connsMtx.Unlock()
}
func (s *SocketServer) addConn(conn net.Conn) int {
s.connsMtx.Lock()
defer s.connsMtx.Unlock()
connID := s.nextConnID
s.nextConnID += 1
s.conns[connID] = conn
return connID
}
// deletes conn even if close errs
func (s *SocketServer) rmConn(connID int, conn net.Conn) error {
s.connsMtx.Lock()
defer s.connsMtx.Unlock()
delete(s.conns, connID)
return conn.Close()
} }
func (s *SocketServer) acceptConnectionsRoutine() { func (s *SocketServer) acceptConnectionsRoutine() {
@ -62,7 +94,7 @@ func (s *SocketServer) acceptConnectionsRoutine() {
// semaphore <- struct{}{} // semaphore <- struct{}{}
// Accept a connection // Accept a connection
fmt.Println("Waiting for new connection...")
log.Notice("Waiting for new connection...")
conn, err := s.listener.Accept() conn, err := s.listener.Accept()
if err != nil { if err != nil {
if !s.IsRunning() { if !s.IsRunning() {
@ -70,9 +102,11 @@ func (s *SocketServer) acceptConnectionsRoutine() {
} }
Exit("Failed to accept connection: " + err.Error()) Exit("Failed to accept connection: " + err.Error())
} else { } else {
fmt.Println("Accepted a new connection")
log.Notice("Accepted a new connection")
} }
connID := s.addConn(conn)
closeConn := make(chan error, 2) // Push to signal connection closed closeConn := make(chan error, 2) // Push to signal connection closed
responses := make(chan *types.Response, 1000) // A channel to buffer responses responses := make(chan *types.Response, 1000) // A channel to buffer responses
@ -84,16 +118,19 @@ func (s *SocketServer) acceptConnectionsRoutine() {
go func() { go func() {
// Wait until signal to close connection // Wait until signal to close connection
errClose := <-closeConn errClose := <-closeConn
if errClose != nil {
fmt.Printf("Connection error: %v\n", errClose)
if err == io.EOF {
log.Warn("Connection was closed by client")
} else if errClose != nil {
log.Warn("Connection error", "error", errClose)
} else { } else {
fmt.Println("Connection was closed.")
// never happens
log.Warn("Connection was closed.")
} }
// Close the connection // Close the connection
err := conn.Close()
err := s.rmConn(connID, conn)
if err != nil { if err != nil {
fmt.Printf("Error in closing connection: %v\n", err)
log.Warn("Error in closing connection", "error", err)
} }
// <-semaphore // <-semaphore
@ -111,9 +148,9 @@ func (s *SocketServer) handleRequests(closeConn chan error, conn net.Conn, respo
err := types.ReadMessage(bufReader, req) err := types.ReadMessage(bufReader, req)
if err != nil { if err != nil {
if err == io.EOF { if err == io.EOF {
closeConn <- fmt.Errorf("Connection closed by client")
closeConn <- err
} else { } else {
closeConn <- fmt.Errorf("Error in handleValue: %v", err.Error())
closeConn <- fmt.Errorf("Error reading message: %v", err.Error())
} }
return return
} }
@ -176,13 +213,13 @@ func (s *SocketServer) handleResponses(closeConn chan error, responses <-chan *t
var res = <-responses var res = <-responses
err := types.WriteMessage(res, bufWriter) err := types.WriteMessage(res, bufWriter)
if err != nil { if err != nil {
closeConn <- fmt.Errorf("Error in handleValue: %v", err.Error())
closeConn <- fmt.Errorf("Error writing message: %v", err.Error())
return return
} }
if _, ok := res.Value.(*types.Response_Flush); ok { if _, ok := res.Value.(*types.Response_Flush); ok {
err = bufWriter.Flush() err = bufWriter.Flush()
if err != nil { if err != nil {
closeConn <- fmt.Errorf("Error in handleValue: %v", err.Error())
closeConn <- fmt.Errorf("Error flushing write buffer: %v", err.Error())
return return
} }
} }


+ 1
- 1
tests/test_cli/test.sh View File

@ -9,7 +9,7 @@ function testExample() {
$APP &> /dev/null & $APP &> /dev/null &
sleep 2 sleep 2
tmsp-cli --verbose batch < $INPUT > "${INPUT}.out.new" tmsp-cli --verbose batch < $INPUT > "${INPUT}.out.new"
killall "$APP" > /dev/null
killall "$APP" &> /dev/null
pre=`shasum < "${INPUT}.out"` pre=`shasum < "${INPUT}.out"`
post=`shasum < "${INPUT}.out.new"` post=`shasum < "${INPUT}.out.new"`


Loading…
Cancel
Save