github.com/vipernet-xyz/tendermint-core@v0.32.0/proxy/app_conn_test.go (about)

     1  package proxy
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	abcicli "github.com/tendermint/tendermint/abci/client"
     9  	"github.com/tendermint/tendermint/abci/example/kvstore"
    10  	"github.com/tendermint/tendermint/abci/server"
    11  	"github.com/tendermint/tendermint/abci/types"
    12  	"github.com/tendermint/tendermint/libs/log"
    13  	tmrand "github.com/tendermint/tendermint/libs/rand"
    14  )
    15  
    16  //----------------------------------------
    17  
    18  type AppConnTest interface {
    19  	EchoAsync(string) *abcicli.ReqRes
    20  	FlushSync() error
    21  	InfoSync(types.RequestInfo) (*types.ResponseInfo, error)
    22  }
    23  
    24  type appConnTest struct {
    25  	appConn abcicli.Client
    26  }
    27  
    28  func NewAppConnTest(appConn abcicli.Client) AppConnTest {
    29  	return &appConnTest{appConn}
    30  }
    31  
    32  func (app *appConnTest) EchoAsync(msg string) *abcicli.ReqRes {
    33  	return app.appConn.EchoAsync(msg)
    34  }
    35  
    36  func (app *appConnTest) FlushSync() error {
    37  	return app.appConn.FlushSync()
    38  }
    39  
    40  func (app *appConnTest) InfoSync(req types.RequestInfo) (*types.ResponseInfo, error) {
    41  	return app.appConn.InfoSync(req)
    42  }
    43  
    44  //----------------------------------------
    45  
    46  var SOCKET = "socket"
    47  
    48  func TestEcho(t *testing.T) {
    49  	sockPath := fmt.Sprintf("unix:///tmp/echo_%v.sock", tmrand.Str(6))
    50  	clientCreator := NewRemoteClientCreator(sockPath, SOCKET, true)
    51  
    52  	// Start server
    53  	s := server.NewSocketServer(sockPath, kvstore.NewApplication())
    54  	s.SetLogger(log.TestingLogger().With("module", "abci-server"))
    55  	if err := s.Start(); err != nil {
    56  		t.Fatalf("Error starting socket server: %v", err.Error())
    57  	}
    58  	defer s.Stop()
    59  
    60  	// Start client
    61  	cli, err := clientCreator.NewABCIClient()
    62  	if err != nil {
    63  		t.Fatalf("Error creating ABCI client: %v", err.Error())
    64  	}
    65  	cli.SetLogger(log.TestingLogger().With("module", "abci-client"))
    66  	if err := cli.Start(); err != nil {
    67  		t.Fatalf("Error starting ABCI client: %v", err.Error())
    68  	}
    69  
    70  	proxy := NewAppConnTest(cli)
    71  	t.Log("Connected")
    72  
    73  	for i := 0; i < 1000; i++ {
    74  		proxy.EchoAsync(fmt.Sprintf("echo-%v", i))
    75  	}
    76  	if err := proxy.FlushSync(); err != nil {
    77  		t.Error(err)
    78  	}
    79  }
    80  
    81  func BenchmarkEcho(b *testing.B) {
    82  	b.StopTimer() // Initialize
    83  	sockPath := fmt.Sprintf("unix:///tmp/echo_%v.sock", tmrand.Str(6))
    84  	clientCreator := NewRemoteClientCreator(sockPath, SOCKET, true)
    85  
    86  	// Start server
    87  	s := server.NewSocketServer(sockPath, kvstore.NewApplication())
    88  	s.SetLogger(log.TestingLogger().With("module", "abci-server"))
    89  	if err := s.Start(); err != nil {
    90  		b.Fatalf("Error starting socket server: %v", err.Error())
    91  	}
    92  	defer s.Stop()
    93  
    94  	// Start client
    95  	cli, err := clientCreator.NewABCIClient()
    96  	if err != nil {
    97  		b.Fatalf("Error creating ABCI client: %v", err.Error())
    98  	}
    99  	cli.SetLogger(log.TestingLogger().With("module", "abci-client"))
   100  	if err := cli.Start(); err != nil {
   101  		b.Fatalf("Error starting ABCI client: %v", err.Error())
   102  	}
   103  
   104  	proxy := NewAppConnTest(cli)
   105  	b.Log("Connected")
   106  	echoString := strings.Repeat(" ", 200)
   107  	b.StartTimer() // Start benchmarking tests
   108  
   109  	for i := 0; i < b.N; i++ {
   110  		proxy.EchoAsync(echoString)
   111  	}
   112  	if err := proxy.FlushSync(); err != nil {
   113  		b.Error(err)
   114  	}
   115  
   116  	b.StopTimer()
   117  	// info := proxy.InfoSync(types.RequestInfo{""})
   118  	//b.Log("N: ", b.N, info)
   119  }
   120  
   121  func TestInfo(t *testing.T) {
   122  	sockPath := fmt.Sprintf("unix:///tmp/echo_%v.sock", tmrand.Str(6))
   123  	clientCreator := NewRemoteClientCreator(sockPath, SOCKET, true)
   124  
   125  	// Start server
   126  	s := server.NewSocketServer(sockPath, kvstore.NewApplication())
   127  	s.SetLogger(log.TestingLogger().With("module", "abci-server"))
   128  	if err := s.Start(); err != nil {
   129  		t.Fatalf("Error starting socket server: %v", err.Error())
   130  	}
   131  	defer s.Stop()
   132  
   133  	// Start client
   134  	cli, err := clientCreator.NewABCIClient()
   135  	if err != nil {
   136  		t.Fatalf("Error creating ABCI client: %v", err.Error())
   137  	}
   138  	cli.SetLogger(log.TestingLogger().With("module", "abci-client"))
   139  	if err := cli.Start(); err != nil {
   140  		t.Fatalf("Error starting ABCI client: %v", err.Error())
   141  	}
   142  
   143  	proxy := NewAppConnTest(cli)
   144  	t.Log("Connected")
   145  
   146  	resInfo, err := proxy.InfoSync(RequestInfo)
   147  	if err != nil {
   148  		t.Errorf("unexpected error: %v", err)
   149  	}
   150  	if resInfo.Data != "{\"size\":0}" {
   151  		t.Error("Expected ResponseInfo with one element '{\"size\":0}' but got something else")
   152  	}
   153  }