tendermint/proxy/app_conn_test.go

125 lines
2.8 KiB
Go
Raw Normal View History

2015-10-19 09:34:53 -07:00
package proxy
import (
"strings"
"testing"
. "github.com/tendermint/go-common"
2017-01-12 12:53:32 -08:00
abcicli "github.com/tendermint/abci/client"
"github.com/tendermint/abci/example/dummy"
"github.com/tendermint/abci/server"
"github.com/tendermint/abci/types"
2015-10-19 09:34:53 -07:00
)
2016-08-17 19:28:08 -07:00
//----------------------------------------
type AppConnTest interface {
2017-01-12 12:53:32 -08:00
EchoAsync(string) *abcicli.ReqRes
2016-08-17 19:28:08 -07:00
FlushSync() error
InfoSync() (types.ResponseInfo, error)
2016-08-17 19:28:08 -07:00
}
type appConnTest struct {
2017-01-12 12:53:32 -08:00
appConn abcicli.Client
2016-08-17 19:28:08 -07:00
}
2017-01-12 12:53:32 -08:00
func NewAppConnTest(appConn abcicli.Client) AppConnTest {
2016-08-17 19:28:08 -07:00
return &appConnTest{appConn}
}
2017-01-12 12:53:32 -08:00
func (app *appConnTest) EchoAsync(msg string) *abcicli.ReqRes {
2016-08-17 19:28:08 -07:00
return app.appConn.EchoAsync(msg)
}
func (app *appConnTest) FlushSync() error {
return app.appConn.FlushSync()
}
func (app *appConnTest) InfoSync() (types.ResponseInfo, error) {
2016-08-17 19:28:08 -07:00
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
2017-01-12 12:53:32 -08:00
cli, err := clientCreator.NewABCIClient()
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
2017-01-12 12:53:32 -08:00
cli, err := clientCreator.NewABCIClient()
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
2017-01-12 12:53:32 -08:00
cli, err := clientCreator.NewABCIClient()
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")
resInfo, err := proxy.InfoSync()
if err != nil {
2015-12-01 20:12:01 -08:00
t.Errorf("Unexpected error: %v", err)
}
if string(resInfo.Data) != "{\"size\":0}" {
2016-12-06 04:04:08 -08:00
t.Error("Expected ResponseInfo with one element '{\"size\":0}' but got something else")
2015-10-19 09:34:53 -07:00
}
}