github.com/vipernet-xyz/tm@v0.34.24/proxy/app_conn_test.go (about)

     1  package proxy
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	abcicli "github.com/vipernet-xyz/tm/abci/client"
     9  	"github.com/vipernet-xyz/tm/abci/example/kvstore"
    10  	"github.com/vipernet-xyz/tm/abci/server"
    11  	"github.com/vipernet-xyz/tm/abci/types"
    12  	"github.com/vipernet-xyz/tm/libs/log"
    13  	tmrand "github.com/vipernet-xyz/tm/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  	t.Cleanup(func() {
    59  		if err := s.Stop(); err != nil {
    60  			t.Error(err)
    61  		}
    62  	})
    63  
    64  	// Start client
    65  	cli, err := clientCreator.NewABCIClient()
    66  	if err != nil {
    67  		t.Fatalf("Error creating ABCI client: %v", err.Error())
    68  	}
    69  	cli.SetLogger(log.TestingLogger().With("module", "abci-client"))
    70  	if err := cli.Start(); err != nil {
    71  		t.Fatalf("Error starting ABCI client: %v", err.Error())
    72  	}
    73  
    74  	proxy := NewAppConnTest(cli)
    75  	t.Log("Connected")
    76  
    77  	for i := 0; i < 1000; i++ {
    78  		proxy.EchoAsync(fmt.Sprintf("echo-%v", i))
    79  	}
    80  	if err := proxy.FlushSync(); err != nil {
    81  		t.Error(err)
    82  	}
    83  }
    84  
    85  func BenchmarkEcho(b *testing.B) {
    86  	b.StopTimer() // Initialize
    87  	sockPath := fmt.Sprintf("unix:///tmp/echo_%v.sock", tmrand.Str(6))
    88  	clientCreator := NewRemoteClientCreator(sockPath, SOCKET, true)
    89  
    90  	// Start server
    91  	s := server.NewSocketServer(sockPath, kvstore.NewApplication())
    92  	s.SetLogger(log.TestingLogger().With("module", "abci-server"))
    93  	if err := s.Start(); err != nil {
    94  		b.Fatalf("Error starting socket server: %v", err.Error())
    95  	}
    96  	b.Cleanup(func() {
    97  		if err := s.Stop(); err != nil {
    98  			b.Error(err)
    99  		}
   100  	})
   101  
   102  	// Start client
   103  	cli, err := clientCreator.NewABCIClient()
   104  	if err != nil {
   105  		b.Fatalf("Error creating ABCI client: %v", err.Error())
   106  	}
   107  	cli.SetLogger(log.TestingLogger().With("module", "abci-client"))
   108  	if err := cli.Start(); err != nil {
   109  		b.Fatalf("Error starting ABCI client: %v", err.Error())
   110  	}
   111  
   112  	proxy := NewAppConnTest(cli)
   113  	b.Log("Connected")
   114  	echoString := strings.Repeat(" ", 200)
   115  	b.StartTimer() // Start benchmarking tests
   116  
   117  	for i := 0; i < b.N; i++ {
   118  		proxy.EchoAsync(echoString)
   119  	}
   120  	if err := proxy.FlushSync(); err != nil {
   121  		b.Error(err)
   122  	}
   123  
   124  	b.StopTimer()
   125  	// info := proxy.InfoSync(types.RequestInfo{""})
   126  	// b.Log("N: ", b.N, info)
   127  }
   128  
   129  func TestInfo(t *testing.T) {
   130  	sockPath := fmt.Sprintf("unix:///tmp/echo_%v.sock", tmrand.Str(6))
   131  	clientCreator := NewRemoteClientCreator(sockPath, SOCKET, true)
   132  
   133  	// Start server
   134  	s := server.NewSocketServer(sockPath, kvstore.NewApplication())
   135  	s.SetLogger(log.TestingLogger().With("module", "abci-server"))
   136  	if err := s.Start(); err != nil {
   137  		t.Fatalf("Error starting socket server: %v", err.Error())
   138  	}
   139  	t.Cleanup(func() {
   140  		if err := s.Stop(); err != nil {
   141  			t.Error(err)
   142  		}
   143  	})
   144  
   145  	// Start client
   146  	cli, err := clientCreator.NewABCIClient()
   147  	if err != nil {
   148  		t.Fatalf("Error creating ABCI client: %v", err.Error())
   149  	}
   150  	cli.SetLogger(log.TestingLogger().With("module", "abci-client"))
   151  	if err := cli.Start(); err != nil {
   152  		t.Fatalf("Error starting ABCI client: %v", err.Error())
   153  	}
   154  
   155  	proxy := NewAppConnTest(cli)
   156  	t.Log("Connected")
   157  
   158  	resInfo, err := proxy.InfoSync(RequestInfo)
   159  	if err != nil {
   160  		t.Errorf("unexpected error: %v", err)
   161  	}
   162  	if resInfo.Data != "{\"size\":0}" {
   163  		t.Error("Expected ResponseInfo with one element '{\"size\":0}' but got something else")
   164  	}
   165  }