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.

186 lines
4.5 KiB

8 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package example
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "net"
  6. "os"
  7. "reflect"
  8. "testing"
  9. "time"
  10. "github.com/stretchr/testify/require"
  11. "google.golang.org/grpc"
  12. "golang.org/x/net/context"
  13. "github.com/tendermint/tendermint/libs/log"
  14. tmnet "github.com/tendermint/tendermint/libs/net"
  15. abcicli "github.com/tendermint/tendermint/abci/client"
  16. "github.com/tendermint/tendermint/abci/example/code"
  17. "github.com/tendermint/tendermint/abci/example/kvstore"
  18. abciserver "github.com/tendermint/tendermint/abci/server"
  19. "github.com/tendermint/tendermint/abci/types"
  20. )
  21. func init() {
  22. rand.Seed(time.Now().UnixNano())
  23. }
  24. func TestKVStore(t *testing.T) {
  25. fmt.Println("### Testing KVStore")
  26. testStream(t, kvstore.NewApplication())
  27. }
  28. func TestBaseApp(t *testing.T) {
  29. fmt.Println("### Testing BaseApp")
  30. testStream(t, types.NewBaseApplication())
  31. }
  32. func TestGRPC(t *testing.T) {
  33. fmt.Println("### Testing GRPC")
  34. testGRPCSync(t, types.NewGRPCApplication(types.NewBaseApplication()))
  35. }
  36. func testStream(t *testing.T, app types.Application) {
  37. numDeliverTxs := 20000
  38. socketFile := fmt.Sprintf("test-%08x.sock", rand.Int31n(1<<30))
  39. defer os.Remove(socketFile)
  40. socket := fmt.Sprintf("unix://%v", socketFile)
  41. // Start the listener
  42. server := abciserver.NewSocketServer(socket, app)
  43. server.SetLogger(log.TestingLogger().With("module", "abci-server"))
  44. if err := server.Start(); err != nil {
  45. require.NoError(t, err, "Error starting socket server")
  46. }
  47. t.Cleanup(func() {
  48. if err := server.Stop(); err != nil {
  49. t.Error(err)
  50. }
  51. })
  52. // Connect to the socket
  53. client := abcicli.NewSocketClient(socket, false)
  54. client.SetLogger(log.TestingLogger().With("module", "abci-client"))
  55. if err := client.Start(); err != nil {
  56. t.Fatalf("Error starting socket client: %v", err.Error())
  57. }
  58. t.Cleanup(func() {
  59. if err := client.Stop(); err != nil {
  60. t.Error(err)
  61. }
  62. })
  63. done := make(chan struct{})
  64. counter := 0
  65. client.SetResponseCallback(func(req *types.Request, res *types.Response) {
  66. // Process response
  67. switch r := res.Value.(type) {
  68. case *types.Response_DeliverTx:
  69. counter++
  70. if r.DeliverTx.Code != code.CodeTypeOK {
  71. t.Error("DeliverTx failed with ret_code", r.DeliverTx.Code)
  72. }
  73. if counter > numDeliverTxs {
  74. t.Fatalf("Too many DeliverTx responses. Got %d, expected %d", counter, numDeliverTxs)
  75. }
  76. if counter == numDeliverTxs {
  77. go func() {
  78. time.Sleep(time.Second * 1) // Wait for a bit to allow counter overflow
  79. close(done)
  80. }()
  81. return
  82. }
  83. case *types.Response_Flush:
  84. // ignore
  85. default:
  86. t.Error("Unexpected response type", reflect.TypeOf(res.Value))
  87. }
  88. })
  89. // Write requests
  90. for counter := 0; counter < numDeliverTxs; counter++ {
  91. // Send request
  92. reqRes := client.DeliverTxAsync(types.RequestDeliverTx{Tx: []byte("test")})
  93. _ = reqRes
  94. // check err ?
  95. // Sometimes send flush messages
  96. if counter%123 == 0 {
  97. client.FlushAsync()
  98. // check err ?
  99. }
  100. }
  101. // Send final flush message
  102. client.FlushAsync()
  103. <-done
  104. }
  105. //-------------------------
  106. // test grpc
  107. func dialerFunc(ctx context.Context, addr string) (net.Conn, error) {
  108. return tmnet.Connect(addr)
  109. }
  110. func testGRPCSync(t *testing.T, app types.ABCIApplicationServer) {
  111. numDeliverTxs := 2000
  112. socketFile := fmt.Sprintf("test-%08x.sock", rand.Int31n(1<<30))
  113. defer os.Remove(socketFile)
  114. socket := fmt.Sprintf("unix://%v", socketFile)
  115. // Start the listener
  116. server := abciserver.NewGRPCServer(socket, app)
  117. server.SetLogger(log.TestingLogger().With("module", "abci-server"))
  118. if err := server.Start(); err != nil {
  119. t.Fatalf("Error starting GRPC server: %v", err.Error())
  120. }
  121. t.Cleanup(func() {
  122. if err := server.Stop(); err != nil {
  123. t.Error(err)
  124. }
  125. })
  126. // Connect to the socket
  127. conn, err := grpc.Dial(socket, grpc.WithInsecure(), grpc.WithContextDialer(dialerFunc))
  128. if err != nil {
  129. t.Fatalf("Error dialing GRPC server: %v", err.Error())
  130. }
  131. t.Cleanup(func() {
  132. if err := conn.Close(); err != nil {
  133. t.Error(err)
  134. }
  135. })
  136. client := types.NewABCIApplicationClient(conn)
  137. // Write requests
  138. for counter := 0; counter < numDeliverTxs; counter++ {
  139. // Send request
  140. response, err := client.DeliverTx(context.Background(), &types.RequestDeliverTx{Tx: []byte("test")})
  141. if err != nil {
  142. t.Fatalf("Error in GRPC DeliverTx: %v", err.Error())
  143. }
  144. counter++
  145. if response.Code != code.CodeTypeOK {
  146. t.Error("DeliverTx failed with ret_code", response.Code)
  147. }
  148. if counter > numDeliverTxs {
  149. t.Fatal("Too many DeliverTx responses")
  150. }
  151. t.Log("response", counter)
  152. if counter == numDeliverTxs {
  153. go func() {
  154. time.Sleep(time.Second * 1) // Wait for a bit to allow counter overflow
  155. }()
  156. }
  157. }
  158. }