tendermint/proxy/app_conn_test.go

125 lines
2.7 KiB
Go
Raw Normal View History

2015-10-19 09:34:53 -07:00
package proxy
import (
"strings"
"testing"
. "github.com/tendermint/go-common"
2016-08-17 19:28:08 -07:00
tmspcli "github.com/tendermint/tmsp/client"
2016-02-14 15:03:55 -08:00
"github.com/tendermint/tmsp/example/dummy"
2015-10-19 09:34:53 -07:00
"github.com/tendermint/tmsp/server"
2016-08-17 19:28:08 -07:00
"github.com/tendermint/tmsp/types"
2015-10-19 09:34:53 -07:00
)
2016-08-17 19:28:08 -07:00
//----------------------------------------
type AppConnTest interface {
EchoAsync(string) *tmspcli.ReqRes
FlushSync() error
InfoSync() (res types.Result)
}
type appConnTest struct {
appConn tmspcli.Client
}
func NewAppConnTest(appConn tmspcli.Client) AppConnTest {
return &appConnTest{appConn}
}
func (app *appConnTest) EchoAsync(msg string) *tmspcli.ReqRes {
return app.appConn.EchoAsync(msg)
}
func (app *appConnTest) FlushSync() error {
return app.appConn.FlushSync()
}
func (app *appConnTest) InfoSync() types.Result {
return app.appConn.InfoSync()
}
//----------------------------------------
2016-05-23 11:36:00 -07:00
var SOCKET = "socket"
2015-10-19 09:34:53 -07:00
func TestEcho(t *testing.T) {
sockPath := Fmt("unix:///tmp/echo_%v.sock", RandStr(6))
clientCreator := NewRemoteClientCreator(sockPath, SOCKET, true)
2016-02-08 00:48:58 -08:00
// Start server
2016-05-23 11:36:00 -07:00
s, err := server.NewSocketServer(sockPath, dummy.NewDummyApplication())
2015-10-19 09:34:53 -07:00
if err != nil {
Exit(err.Error())
}
2016-02-21 23:45:10 -08:00
defer s.Stop()
2016-02-08 00:48:58 -08:00
// Start client
cli, err := clientCreator.NewTMSPClient()
2015-10-19 09:34:53 -07:00
if err != nil {
Exit(err.Error())
}
2016-08-17 19:28:08 -07:00
proxy := NewAppConnTest(cli)
t.Log("Connected")
2015-10-19 09:34:53 -07:00
for i := 0; i < 1000; i++ {
proxy.EchoAsync(Fmt("echo-%v", i))
}
proxy.FlushSync()
}
func BenchmarkEcho(b *testing.B) {
b.StopTimer() // Initialize
sockPath := Fmt("unix:///tmp/echo_%v.sock", RandStr(6))
clientCreator := NewRemoteClientCreator(sockPath, SOCKET, true)
2016-02-08 00:48:58 -08:00
// Start server
2016-05-23 11:36:00 -07:00
s, err := server.NewSocketServer(sockPath, dummy.NewDummyApplication())
2015-10-19 09:34:53 -07:00
if err != nil {
Exit(err.Error())
}
2016-02-21 23:45:10 -08:00
defer s.Stop()
2016-02-08 00:48:58 -08:00
// Start client
cli, err := clientCreator.NewTMSPClient()
2015-10-19 09:34:53 -07:00
if err != nil {
Exit(err.Error())
}
2016-08-17 19:28:08 -07:00
proxy := NewAppConnTest(cli)
b.Log("Connected")
2015-10-19 09:34:53 -07:00
echoString := strings.Repeat(" ", 200)
b.StartTimer() // Start benchmarking tests
for i := 0; i < b.N; i++ {
proxy.EchoAsync(echoString)
}
proxy.FlushSync()
b.StopTimer()
// info := proxy.InfoSync()
//b.Log("N: ", b.N, info)
}
func TestInfo(t *testing.T) {
sockPath := Fmt("unix:///tmp/echo_%v.sock", RandStr(6))
clientCreator := NewRemoteClientCreator(sockPath, SOCKET, true)
2016-02-08 00:48:58 -08:00
// Start server
2016-05-23 11:36:00 -07:00
s, err := server.NewSocketServer(sockPath, dummy.NewDummyApplication())
2015-10-19 09:34:53 -07:00
if err != nil {
Exit(err.Error())
}
2016-02-21 23:45:10 -08:00
defer s.Stop()
2016-02-08 00:48:58 -08:00
// Start client
cli, err := clientCreator.NewTMSPClient()
2015-10-19 09:34:53 -07:00
if err != nil {
Exit(err.Error())
}
2016-08-17 19:28:08 -07:00
proxy := NewAppConnTest(cli)
t.Log("Connected")
2016-03-24 10:42:05 -07:00
res := proxy.InfoSync()
if res.IsErr() {
2015-12-01 20:12:01 -08:00
t.Errorf("Unexpected error: %v", err)
}
2016-03-24 10:42:05 -07:00
if string(res.Data) != "size:0" {
2015-10-19 09:34:53 -07:00
t.Error("Expected ResponseInfo with one element 'size:0' but got something else")
}
}