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.

168 lines
4.3 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. defer server.Stop()
  48. // Connect to the socket
  49. client := abcicli.NewSocketClient(socket, false)
  50. client.SetLogger(log.TestingLogger().With("module", "abci-client"))
  51. if err := client.Start(); err != nil {
  52. t.Fatalf("Error starting socket client: %v", err.Error())
  53. }
  54. defer client.Stop()
  55. done := make(chan struct{})
  56. counter := 0
  57. client.SetResponseCallback(func(req *types.Request, res *types.Response) {
  58. // Process response
  59. switch r := res.Value.(type) {
  60. case *types.Response_DeliverTx:
  61. counter++
  62. if r.DeliverTx.Code != code.CodeTypeOK {
  63. t.Error("DeliverTx failed with ret_code", r.DeliverTx.Code)
  64. }
  65. if counter > numDeliverTxs {
  66. t.Fatalf("Too many DeliverTx responses. Got %d, expected %d", counter, numDeliverTxs)
  67. }
  68. if counter == numDeliverTxs {
  69. go func() {
  70. time.Sleep(time.Second * 1) // Wait for a bit to allow counter overflow
  71. close(done)
  72. }()
  73. return
  74. }
  75. case *types.Response_Flush:
  76. // ignore
  77. default:
  78. t.Error("Unexpected response type", reflect.TypeOf(res.Value))
  79. }
  80. })
  81. // Write requests
  82. for counter := 0; counter < numDeliverTxs; counter++ {
  83. // Send request
  84. reqRes := client.DeliverTxAsync(types.RequestDeliverTx{Tx: []byte("test")})
  85. _ = reqRes
  86. // check err ?
  87. // Sometimes send flush messages
  88. if counter%123 == 0 {
  89. client.FlushAsync()
  90. // check err ?
  91. }
  92. }
  93. // Send final flush message
  94. client.FlushAsync()
  95. <-done
  96. }
  97. //-------------------------
  98. // test grpc
  99. func dialerFunc(ctx context.Context, addr string) (net.Conn, error) {
  100. return tmnet.Connect(addr)
  101. }
  102. func testGRPCSync(t *testing.T, app types.ABCIApplicationServer) {
  103. numDeliverTxs := 2000
  104. socketFile := fmt.Sprintf("test-%08x.sock", rand.Int31n(1<<30))
  105. defer os.Remove(socketFile)
  106. socket := fmt.Sprintf("unix://%v", socketFile)
  107. // Start the listener
  108. server := abciserver.NewGRPCServer(socket, app)
  109. server.SetLogger(log.TestingLogger().With("module", "abci-server"))
  110. if err := server.Start(); err != nil {
  111. t.Fatalf("Error starting GRPC server: %v", err.Error())
  112. }
  113. defer server.Stop()
  114. // Connect to the socket
  115. conn, err := grpc.Dial(socket, grpc.WithInsecure(), grpc.WithContextDialer(dialerFunc))
  116. if err != nil {
  117. t.Fatalf("Error dialing GRPC server: %v", err.Error())
  118. }
  119. defer conn.Close()
  120. client := types.NewABCIApplicationClient(conn)
  121. // Write requests
  122. for counter := 0; counter < numDeliverTxs; counter++ {
  123. // Send request
  124. response, err := client.DeliverTx(context.Background(), &types.RequestDeliverTx{Tx: []byte("test")})
  125. if err != nil {
  126. t.Fatalf("Error in GRPC DeliverTx: %v", err.Error())
  127. }
  128. counter++
  129. if response.Code != code.CodeTypeOK {
  130. t.Error("DeliverTx failed with ret_code", response.Code)
  131. }
  132. if counter > numDeliverTxs {
  133. t.Fatal("Too many DeliverTx responses")
  134. }
  135. t.Log("response", counter)
  136. if counter == numDeliverTxs {
  137. go func() {
  138. time.Sleep(time.Second * 1) // Wait for a bit to allow counter overflow
  139. }()
  140. }
  141. }
  142. }