You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
4.8 KiB

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