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.

163 lines
4.5 KiB

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