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.

234 lines
5.9 KiB

  1. package proxy
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "os"
  7. "os/signal"
  8. "strings"
  9. "syscall"
  10. "testing"
  11. "time"
  12. "github.com/stretchr/testify/mock"
  13. "github.com/stretchr/testify/require"
  14. abciclient "github.com/tendermint/tendermint/abci/client"
  15. abcimocks "github.com/tendermint/tendermint/abci/client/mocks"
  16. "github.com/tendermint/tendermint/abci/example/kvstore"
  17. "github.com/tendermint/tendermint/abci/server"
  18. "github.com/tendermint/tendermint/abci/types"
  19. "github.com/tendermint/tendermint/libs/log"
  20. tmrand "github.com/tendermint/tendermint/libs/rand"
  21. "gotest.tools/assert"
  22. )
  23. //----------------------------------------
  24. type appConnTestI interface {
  25. Echo(context.Context, string) (*types.ResponseEcho, error)
  26. Flush(context.Context) error
  27. Info(context.Context, types.RequestInfo) (*types.ResponseInfo, error)
  28. }
  29. type appConnTest struct {
  30. appConn abciclient.Client
  31. }
  32. func newAppConnTest(appConn abciclient.Client) appConnTestI {
  33. return &appConnTest{appConn}
  34. }
  35. func (app *appConnTest) Echo(ctx context.Context, msg string) (*types.ResponseEcho, error) {
  36. return app.appConn.Echo(ctx, msg)
  37. }
  38. func (app *appConnTest) Flush(ctx context.Context) error {
  39. return app.appConn.Flush(ctx)
  40. }
  41. func (app *appConnTest) Info(ctx context.Context, req types.RequestInfo) (*types.ResponseInfo, error) {
  42. return app.appConn.Info(ctx, req)
  43. }
  44. //----------------------------------------
  45. var SOCKET = "socket"
  46. func TestEcho(t *testing.T) {
  47. sockPath := fmt.Sprintf("unix:///tmp/echo_%v.sock", tmrand.Str(6))
  48. logger := log.NewNopLogger()
  49. client, err := abciclient.NewClient(logger, sockPath, SOCKET, true)
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. ctx, cancel := context.WithCancel(context.Background())
  54. defer cancel()
  55. // Start server
  56. s := server.NewSocketServer(logger.With("module", "abci-server"), sockPath, kvstore.NewApplication())
  57. require.NoError(t, s.Start(ctx), "error starting socket server")
  58. t.Cleanup(func() { cancel(); s.Wait() })
  59. // Start client
  60. require.NoError(t, client.Start(ctx), "Error starting ABCI client")
  61. proxy := newAppConnTest(client)
  62. t.Log("Connected")
  63. for i := 0; i < 1000; i++ {
  64. _, err = proxy.Echo(ctx, fmt.Sprintf("echo-%v", i))
  65. if err != nil {
  66. t.Error(err)
  67. }
  68. // flush sometimes
  69. if i%128 == 0 {
  70. if err := proxy.Flush(ctx); err != nil {
  71. t.Error(err)
  72. }
  73. }
  74. }
  75. if err := proxy.Flush(ctx); err != nil {
  76. t.Error(err)
  77. }
  78. }
  79. func BenchmarkEcho(b *testing.B) {
  80. b.StopTimer() // Initialize
  81. sockPath := fmt.Sprintf("unix:///tmp/echo_%v.sock", tmrand.Str(6))
  82. logger := log.NewNopLogger()
  83. client, err := abciclient.NewClient(logger, sockPath, SOCKET, true)
  84. if err != nil {
  85. b.Fatal(err)
  86. }
  87. ctx, cancel := context.WithCancel(context.Background())
  88. defer cancel()
  89. // Start server
  90. s := server.NewSocketServer(logger.With("module", "abci-server"), sockPath, kvstore.NewApplication())
  91. require.NoError(b, s.Start(ctx), "Error starting socket server")
  92. b.Cleanup(func() { cancel(); s.Wait() })
  93. // Start client
  94. require.NoError(b, client.Start(ctx), "Error starting ABCI client")
  95. proxy := newAppConnTest(client)
  96. b.Log("Connected")
  97. echoString := strings.Repeat(" ", 200)
  98. b.StartTimer() // Start benchmarking tests
  99. for i := 0; i < b.N; i++ {
  100. _, err = proxy.Echo(ctx, echoString)
  101. if err != nil {
  102. b.Error(err)
  103. }
  104. // flush sometimes
  105. if i%128 == 0 {
  106. if err := proxy.Flush(ctx); err != nil {
  107. b.Error(err)
  108. }
  109. }
  110. }
  111. if err := proxy.Flush(ctx); err != nil {
  112. b.Error(err)
  113. }
  114. b.StopTimer()
  115. // info := proxy.Info(types.RequestInfo{""})
  116. // b.Log("N: ", b.N, info)
  117. }
  118. func TestInfo(t *testing.T) {
  119. ctx, cancel := context.WithCancel(context.Background())
  120. defer cancel()
  121. sockPath := fmt.Sprintf("unix:///tmp/echo_%v.sock", tmrand.Str(6))
  122. logger := log.NewNopLogger()
  123. client, err := abciclient.NewClient(logger, sockPath, SOCKET, true)
  124. if err != nil {
  125. t.Fatal(err)
  126. }
  127. // Start server
  128. s := server.NewSocketServer(logger.With("module", "abci-server"), sockPath, kvstore.NewApplication())
  129. require.NoError(t, s.Start(ctx), "Error starting socket server")
  130. t.Cleanup(func() { cancel(); s.Wait() })
  131. // Start client
  132. require.NoError(t, client.Start(ctx), "Error starting ABCI client")
  133. proxy := newAppConnTest(client)
  134. t.Log("Connected")
  135. resInfo, err := proxy.Info(ctx, RequestInfo)
  136. require.NoError(t, err)
  137. if resInfo.Data != "{\"size\":0}" {
  138. t.Error("Expected ResponseInfo with one element '{\"size\":0}' but got something else")
  139. }
  140. }
  141. type noopStoppableClientImpl struct {
  142. abciclient.Client
  143. count int
  144. }
  145. func (c *noopStoppableClientImpl) Stop() { c.count++ }
  146. func TestAppConns_Start_Stop(t *testing.T) {
  147. ctx, cancel := context.WithCancel(context.Background())
  148. defer cancel()
  149. clientMock := &abcimocks.Client{}
  150. clientMock.On("Start", mock.Anything).Return(nil)
  151. clientMock.On("Error").Return(nil)
  152. clientMock.On("IsRunning").Return(true)
  153. clientMock.On("Wait").Return(nil).Times(1)
  154. cl := &noopStoppableClientImpl{Client: clientMock}
  155. appConns := New(cl, log.NewNopLogger(), NopMetrics())
  156. err := appConns.Start(ctx)
  157. require.NoError(t, err)
  158. time.Sleep(200 * time.Millisecond)
  159. cancel()
  160. appConns.Wait()
  161. clientMock.AssertExpectations(t)
  162. assert.Equal(t, 1, cl.count)
  163. }
  164. // Upon failure, we call tmos.Kill
  165. func TestAppConns_Failure(t *testing.T) {
  166. c := make(chan os.Signal, 1)
  167. signal.Notify(c, syscall.SIGTERM, syscall.SIGABRT)
  168. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  169. defer cancel()
  170. clientMock := &abcimocks.Client{}
  171. clientMock.On("SetLogger", mock.Anything).Return()
  172. clientMock.On("Start", mock.Anything).Return(nil)
  173. clientMock.On("IsRunning").Return(true)
  174. clientMock.On("Wait").Return(nil)
  175. clientMock.On("Error").Return(errors.New("EOF"))
  176. cl := &noopStoppableClientImpl{Client: clientMock}
  177. appConns := New(cl, log.NewNopLogger(), NopMetrics())
  178. err := appConns.Start(ctx)
  179. require.NoError(t, err)
  180. t.Cleanup(func() { cancel(); appConns.Wait() })
  181. select {
  182. case sig := <-c:
  183. t.Logf("signal %q successfully received", sig)
  184. case <-ctx.Done():
  185. t.Fatal("expected process to receive SIGTERM signal")
  186. }
  187. }