2015-10-19 12:34:53 -04:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2018-06-22 06:59:02 +02: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"
|
2017-04-25 14:50:20 -04:00
|
|
|
cmn "github.com/tendermint/tmlibs/common"
|
2017-05-16 19:06:35 +02:00
|
|
|
"github.com/tendermint/tmlibs/log"
|
2015-10-19 12:34:53 -04:00
|
|
|
)
|
|
|
|
|
2016-08-17 22:28:08 -04:00
|
|
|
//----------------------------------------
|
|
|
|
|
|
|
|
type AppConnTest interface {
|
2017-01-12 15:53:32 -05:00
|
|
|
EchoAsync(string) *abcicli.ReqRes
|
2016-08-17 22:28:08 -04:00
|
|
|
FlushSync() error
|
2017-11-22 18:55:09 -06:00
|
|
|
InfoSync(types.RequestInfo) (*types.ResponseInfo, error)
|
2016-08-17 22:28:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type appConnTest struct {
|
2017-01-12 15:53:32 -05:00
|
|
|
appConn abcicli.Client
|
2016-08-17 22:28:08 -04:00
|
|
|
}
|
|
|
|
|
2017-01-12 15:53:32 -05:00
|
|
|
func NewAppConnTest(appConn abcicli.Client) AppConnTest {
|
2016-08-17 22:28:08 -04:00
|
|
|
return &appConnTest{appConn}
|
|
|
|
}
|
|
|
|
|
2017-01-12 15:53:32 -05:00
|
|
|
func (app *appConnTest) EchoAsync(msg string) *abcicli.ReqRes {
|
2016-08-17 22:28:08 -04:00
|
|
|
return app.appConn.EchoAsync(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *appConnTest) FlushSync() error {
|
|
|
|
return app.appConn.FlushSync()
|
|
|
|
}
|
|
|
|
|
2017-11-22 18:55:09 -06:00
|
|
|
func (app *appConnTest) InfoSync(req types.RequestInfo) (*types.ResponseInfo, error) {
|
2017-09-22 11:42:40 -04:00
|
|
|
return app.appConn.InfoSync(req)
|
2016-08-17 22:28:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
|
|
2016-05-23 14:36:00 -04:00
|
|
|
var SOCKET = "socket"
|
|
|
|
|
2015-10-19 12:34:53 -04:00
|
|
|
func TestEcho(t *testing.T) {
|
2017-04-25 14:50:20 -04:00
|
|
|
sockPath := cmn.Fmt("unix:///tmp/echo_%v.sock", cmn.RandStr(6))
|
2016-09-10 17:14:55 -04:00
|
|
|
clientCreator := NewRemoteClientCreator(sockPath, SOCKET, true)
|
2016-02-08 00:48:58 -08:00
|
|
|
|
|
|
|
// Start server
|
2018-02-27 14:01:10 +00:00
|
|
|
s := server.NewSocketServer(sockPath, kvstore.NewKVStoreApplication())
|
2017-05-16 19:06:35 +02:00
|
|
|
s.SetLogger(log.TestingLogger().With("module", "abci-server"))
|
2017-11-06 13:20:39 -05:00
|
|
|
if err := s.Start(); err != nil {
|
2017-05-16 19:06:35 +02:00
|
|
|
t.Fatalf("Error starting socket server: %v", err.Error())
|
2015-10-19 12:34:53 -04:00
|
|
|
}
|
2016-02-21 23:45:10 -08:00
|
|
|
defer s.Stop()
|
2017-05-16 19:06:35 +02:00
|
|
|
|
2016-02-08 00:48:58 -08:00
|
|
|
// Start client
|
2017-01-12 15:53:32 -05:00
|
|
|
cli, err := clientCreator.NewABCIClient()
|
2015-10-19 12:34:53 -04:00
|
|
|
if err != nil {
|
2017-05-16 19:06:35 +02:00
|
|
|
t.Fatalf("Error creating ABCI client: %v", err.Error())
|
2015-10-19 12:34:53 -04:00
|
|
|
}
|
2017-05-16 19:06:35 +02:00
|
|
|
cli.SetLogger(log.TestingLogger().With("module", "abci-client"))
|
2017-11-06 13:20:39 -05:00
|
|
|
if err := cli.Start(); err != nil {
|
2017-05-16 19:06:35 +02:00
|
|
|
t.Fatalf("Error starting ABCI client: %v", err.Error())
|
|
|
|
}
|
|
|
|
|
2016-08-17 22:28:08 -04:00
|
|
|
proxy := NewAppConnTest(cli)
|
|
|
|
t.Log("Connected")
|
2015-10-19 12:34:53 -04:00
|
|
|
|
|
|
|
for i := 0; i < 1000; i++ {
|
2017-04-25 14:50:20 -04:00
|
|
|
proxy.EchoAsync(cmn.Fmt("echo-%v", i))
|
2015-10-19 12:34:53 -04:00
|
|
|
}
|
2017-09-06 11:50:43 -04:00
|
|
|
if err := proxy.FlushSync(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2015-10-19 12:34:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEcho(b *testing.B) {
|
|
|
|
b.StopTimer() // Initialize
|
2017-04-25 14:50:20 -04:00
|
|
|
sockPath := cmn.Fmt("unix:///tmp/echo_%v.sock", cmn.RandStr(6))
|
2016-09-10 17:14:55 -04:00
|
|
|
clientCreator := NewRemoteClientCreator(sockPath, SOCKET, true)
|
2017-05-16 19:06:35 +02:00
|
|
|
|
2016-02-08 00:48:58 -08:00
|
|
|
// Start server
|
2018-02-27 14:01:10 +00:00
|
|
|
s := server.NewSocketServer(sockPath, kvstore.NewKVStoreApplication())
|
2017-05-16 19:06:35 +02:00
|
|
|
s.SetLogger(log.TestingLogger().With("module", "abci-server"))
|
2017-11-06 13:20:39 -05:00
|
|
|
if err := s.Start(); err != nil {
|
2017-05-16 19:06:35 +02:00
|
|
|
b.Fatalf("Error starting socket server: %v", err.Error())
|
2015-10-19 12:34:53 -04:00
|
|
|
}
|
2016-02-21 23:45:10 -08:00
|
|
|
defer s.Stop()
|
2017-05-16 19:06:35 +02:00
|
|
|
|
2016-02-08 00:48:58 -08:00
|
|
|
// Start client
|
2017-01-12 15:53:32 -05:00
|
|
|
cli, err := clientCreator.NewABCIClient()
|
2015-10-19 12:34:53 -04:00
|
|
|
if err != nil {
|
2017-05-16 19:06:35 +02:00
|
|
|
b.Fatalf("Error creating ABCI client: %v", err.Error())
|
2015-10-19 12:34:53 -04:00
|
|
|
}
|
2017-05-16 19:06:35 +02:00
|
|
|
cli.SetLogger(log.TestingLogger().With("module", "abci-client"))
|
2017-11-06 13:20:39 -05:00
|
|
|
if err := cli.Start(); err != nil {
|
2017-05-16 19:06:35 +02:00
|
|
|
b.Fatalf("Error starting ABCI client: %v", err.Error())
|
|
|
|
}
|
|
|
|
|
2016-08-17 22:28:08 -04:00
|
|
|
proxy := NewAppConnTest(cli)
|
|
|
|
b.Log("Connected")
|
2015-10-19 12:34:53 -04:00
|
|
|
echoString := strings.Repeat(" ", 200)
|
|
|
|
b.StartTimer() // Start benchmarking tests
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
proxy.EchoAsync(echoString)
|
|
|
|
}
|
2017-09-06 11:50:43 -04:00
|
|
|
if err := proxy.FlushSync(); err != nil {
|
|
|
|
b.Error(err)
|
|
|
|
}
|
2015-10-19 12:34:53 -04:00
|
|
|
|
|
|
|
b.StopTimer()
|
2017-09-22 11:42:40 -04:00
|
|
|
// info := proxy.InfoSync(types.RequestInfo{""})
|
2015-10-19 12:34:53 -04:00
|
|
|
//b.Log("N: ", b.N, info)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInfo(t *testing.T) {
|
2017-04-25 14:50:20 -04:00
|
|
|
sockPath := cmn.Fmt("unix:///tmp/echo_%v.sock", cmn.RandStr(6))
|
2016-09-10 17:14:55 -04:00
|
|
|
clientCreator := NewRemoteClientCreator(sockPath, SOCKET, true)
|
2017-05-16 19:06:35 +02:00
|
|
|
|
2016-02-08 00:48:58 -08:00
|
|
|
// Start server
|
2018-02-27 14:01:10 +00:00
|
|
|
s := server.NewSocketServer(sockPath, kvstore.NewKVStoreApplication())
|
2017-05-16 19:06:35 +02:00
|
|
|
s.SetLogger(log.TestingLogger().With("module", "abci-server"))
|
2017-11-06 13:20:39 -05:00
|
|
|
if err := s.Start(); err != nil {
|
2017-05-16 19:06:35 +02:00
|
|
|
t.Fatalf("Error starting socket server: %v", err.Error())
|
2015-10-19 12:34:53 -04:00
|
|
|
}
|
2016-02-21 23:45:10 -08:00
|
|
|
defer s.Stop()
|
2017-05-16 19:06:35 +02:00
|
|
|
|
2016-02-08 00:48:58 -08:00
|
|
|
// Start client
|
2017-01-12 15:53:32 -05:00
|
|
|
cli, err := clientCreator.NewABCIClient()
|
2015-10-19 12:34:53 -04:00
|
|
|
if err != nil {
|
2017-05-16 19:06:35 +02:00
|
|
|
t.Fatalf("Error creating ABCI client: %v", err.Error())
|
|
|
|
}
|
|
|
|
cli.SetLogger(log.TestingLogger().With("module", "abci-client"))
|
2017-11-06 13:20:39 -05:00
|
|
|
if err := cli.Start(); err != nil {
|
2017-05-16 19:06:35 +02:00
|
|
|
t.Fatalf("Error starting ABCI client: %v", err.Error())
|
2015-10-19 12:34:53 -04:00
|
|
|
}
|
2017-05-16 19:06:35 +02:00
|
|
|
|
2016-08-17 22:28:08 -04:00
|
|
|
proxy := NewAppConnTest(cli)
|
|
|
|
t.Log("Connected")
|
|
|
|
|
2017-09-22 11:42:40 -04:00
|
|
|
resInfo, err := proxy.InfoSync(types.RequestInfo{""})
|
2017-01-12 15:13:47 -05:00
|
|
|
if err != nil {
|
2015-12-01 20:12:01 -08:00
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
}
|
2017-01-12 15:13:47 -05:00
|
|
|
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 12:34:53 -04:00
|
|
|
}
|
|
|
|
}
|