github.com/line/ostracon@v1.0.10-0.20230328032236-7f20145f065d/proxy/app_conn_test.go (about)

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