tendermint/proxy/app_conn_test.go

154 lines
4.1 KiB
Go
Raw Normal View History

2015-10-19 09:34:53 -07:00
package proxy
import (
"fmt"
2015-10-19 09:34:53 -07:00
"strings"
"testing"
2018-06-21 21:59:02 -07:00
abcicli "github.com/tendermint/tendermint/abci/client"
"github.com/tendermint/tendermint/abci/example/kvstore"
"github.com/tendermint/tendermint/abci/server"
"github.com/tendermint/tendermint/abci/types"
2018-07-01 19:36:49 -07:00
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/log"
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.RequestInfo) (*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(req types.RequestInfo) (*types.ResponseInfo, error) {
return app.appConn.InfoSync(req)
2016-08-17 19:28:08 -07:00
}
//----------------------------------------
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.Sprintf("unix:///tmp/echo_%v.sock", cmn.RandStr(6))
clientCreator := NewRemoteClientCreator(sockPath, SOCKET, true)
2016-02-08 00:48:58 -08:00
// Start server
s := server.NewSocketServer(sockPath, kvstore.NewKVStoreApplication())
2017-05-16 10:06:35 -07:00
s.SetLogger(log.TestingLogger().With("module", "abci-server"))
if err := s.Start(); err != nil {
2017-05-16 10:06:35 -07:00
t.Fatalf("Error starting socket server: %v", err.Error())
2015-10-19 09:34:53 -07:00
}
2016-02-21 23:45:10 -08:00
defer s.Stop()
2017-05-16 10:06:35 -07:00
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 {
2017-05-16 10:06:35 -07:00
t.Fatalf("Error creating ABCI client: %v", err.Error())
2015-10-19 09:34:53 -07:00
}
2017-05-16 10:06:35 -07:00
cli.SetLogger(log.TestingLogger().With("module", "abci-client"))
if err := cli.Start(); err != nil {
2017-05-16 10:06:35 -07:00
t.Fatalf("Error starting ABCI client: %v", 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.Sprintf("echo-%v", i))
2015-10-19 09:34:53 -07:00
}
2017-09-06 08:50:43 -07:00
if err := proxy.FlushSync(); err != nil {
t.Error(err)
}
2015-10-19 09:34:53 -07:00
}
func BenchmarkEcho(b *testing.B) {
b.StopTimer() // Initialize
sockPath := fmt.Sprintf("unix:///tmp/echo_%v.sock", cmn.RandStr(6))
clientCreator := NewRemoteClientCreator(sockPath, SOCKET, true)
2017-05-16 10:06:35 -07:00
2016-02-08 00:48:58 -08:00
// Start server
s := server.NewSocketServer(sockPath, kvstore.NewKVStoreApplication())
2017-05-16 10:06:35 -07:00
s.SetLogger(log.TestingLogger().With("module", "abci-server"))
if err := s.Start(); err != nil {
2017-05-16 10:06:35 -07:00
b.Fatalf("Error starting socket server: %v", err.Error())
2015-10-19 09:34:53 -07:00
}
2016-02-21 23:45:10 -08:00
defer s.Stop()
2017-05-16 10:06:35 -07:00
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 {
2017-05-16 10:06:35 -07:00
b.Fatalf("Error creating ABCI client: %v", err.Error())
2015-10-19 09:34:53 -07:00
}
2017-05-16 10:06:35 -07:00
cli.SetLogger(log.TestingLogger().With("module", "abci-client"))
if err := cli.Start(); err != nil {
2017-05-16 10:06:35 -07:00
b.Fatalf("Error starting ABCI client: %v", 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)
}
2017-09-06 08:50:43 -07:00
if err := proxy.FlushSync(); err != nil {
b.Error(err)
}
2015-10-19 09:34:53 -07:00
b.StopTimer()
// info := proxy.InfoSync(types.RequestInfo{""})
2015-10-19 09:34:53 -07:00
//b.Log("N: ", b.N, info)
}
func TestInfo(t *testing.T) {
sockPath := fmt.Sprintf("unix:///tmp/echo_%v.sock", cmn.RandStr(6))
clientCreator := NewRemoteClientCreator(sockPath, SOCKET, true)
2017-05-16 10:06:35 -07:00
2016-02-08 00:48:58 -08:00
// Start server
s := server.NewSocketServer(sockPath, kvstore.NewKVStoreApplication())
2017-05-16 10:06:35 -07:00
s.SetLogger(log.TestingLogger().With("module", "abci-server"))
if err := s.Start(); err != nil {
2017-05-16 10:06:35 -07:00
t.Fatalf("Error starting socket server: %v", err.Error())
2015-10-19 09:34:53 -07:00
}
2016-02-21 23:45:10 -08:00
defer s.Stop()
2017-05-16 10:06:35 -07:00
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 {
2017-05-16 10:06:35 -07:00
t.Fatalf("Error creating ABCI client: %v", err.Error())
}
cli.SetLogger(log.TestingLogger().With("module", "abci-client"))
if err := cli.Start(); err != nil {
2017-05-16 10:06:35 -07:00
t.Fatalf("Error starting ABCI client: %v", err.Error())
2015-10-19 09:34:53 -07:00
}
2017-05-16 10:06:35 -07:00
2016-08-17 19:28:08 -07:00
proxy := NewAppConnTest(cli)
t.Log("Connected")
resInfo, err := proxy.InfoSync(types.RequestInfo{Version: ""})
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
}
}