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.

189 lines
4.6 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
  1. package example
  2. import (
  3. "context"
  4. "fmt"
  5. "math/rand"
  6. "net"
  7. "os"
  8. "reflect"
  9. "testing"
  10. "time"
  11. "github.com/stretchr/testify/require"
  12. "google.golang.org/grpc"
  13. "google.golang.org/grpc/credentials/insecure"
  14. "github.com/tendermint/tendermint/libs/log"
  15. tmnet "github.com/tendermint/tendermint/libs/net"
  16. abciclient "github.com/tendermint/tendermint/abci/client"
  17. "github.com/tendermint/tendermint/abci/example/code"
  18. "github.com/tendermint/tendermint/abci/example/kvstore"
  19. abciserver "github.com/tendermint/tendermint/abci/server"
  20. "github.com/tendermint/tendermint/abci/types"
  21. )
  22. func init() {
  23. rand.Seed(time.Now().UnixNano())
  24. }
  25. func TestKVStore(t *testing.T) {
  26. fmt.Println("### Testing KVStore")
  27. testStream(t, kvstore.NewApplication())
  28. }
  29. func TestBaseApp(t *testing.T) {
  30. fmt.Println("### Testing BaseApp")
  31. testStream(t, types.NewBaseApplication())
  32. }
  33. func TestGRPC(t *testing.T) {
  34. fmt.Println("### Testing GRPC")
  35. testGRPCSync(t, types.NewGRPCApplication(types.NewBaseApplication()))
  36. }
  37. func testStream(t *testing.T, app types.Application) {
  38. const numDeliverTxs = 20000
  39. socketFile := fmt.Sprintf("test-%08x.sock", rand.Int31n(1<<30))
  40. defer os.Remove(socketFile)
  41. socket := fmt.Sprintf("unix://%v", socketFile)
  42. // Start the listener
  43. server := abciserver.NewSocketServer(socket, app)
  44. server.SetLogger(log.TestingLogger().With("module", "abci-server"))
  45. err := server.Start()
  46. require.NoError(t, err)
  47. t.Cleanup(func() {
  48. if err := server.Stop(); err != nil {
  49. t.Error(err)
  50. }
  51. })
  52. // Connect to the socket
  53. client := abciclient.NewSocketClient(socket, false)
  54. client.SetLogger(log.TestingLogger().With("module", "abci-client"))
  55. err = client.Start()
  56. require.NoError(t, err)
  57. t.Cleanup(func() {
  58. if err := client.Stop(); err != nil {
  59. t.Error(err)
  60. }
  61. })
  62. done := make(chan struct{})
  63. counter := 0
  64. client.SetResponseCallback(func(req *types.Request, res *types.Response) {
  65. // Process response
  66. switch r := res.Value.(type) {
  67. case *types.Response_DeliverTx:
  68. counter++
  69. if r.DeliverTx.Code != code.CodeTypeOK {
  70. t.Error("DeliverTx failed with ret_code", r.DeliverTx.Code)
  71. }
  72. if counter > numDeliverTxs {
  73. t.Fatalf("Too many DeliverTx responses. Got %d, expected %d", counter, numDeliverTxs)
  74. }
  75. if counter == numDeliverTxs {
  76. go func() {
  77. time.Sleep(time.Second * 1) // Wait for a bit to allow counter overflow
  78. close(done)
  79. }()
  80. return
  81. }
  82. case *types.Response_Flush:
  83. // ignore
  84. default:
  85. t.Error("Unexpected response type", reflect.TypeOf(res.Value))
  86. }
  87. })
  88. ctx := context.Background()
  89. // Write requests
  90. for counter := 0; counter < numDeliverTxs; counter++ {
  91. // Send request
  92. _, err = client.DeliverTxAsync(ctx, types.RequestDeliverTx{Tx: []byte("test")})
  93. require.NoError(t, err)
  94. // Sometimes send flush messages
  95. if counter%128 == 0 {
  96. err = client.FlushSync(context.Background())
  97. require.NoError(t, err)
  98. }
  99. }
  100. // Send final flush message
  101. _, err = client.FlushAsync(ctx)
  102. require.NoError(t, err)
  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("/tmp/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,
  128. grpc.WithTransportCredentials(insecure.NewCredentials()),
  129. grpc.WithContextDialer(dialerFunc),
  130. )
  131. if err != nil {
  132. t.Fatalf("Error dialing GRPC server: %v", err.Error())
  133. }
  134. t.Cleanup(func() {
  135. if err := conn.Close(); err != nil {
  136. t.Error(err)
  137. }
  138. })
  139. client := types.NewABCIApplicationClient(conn)
  140. // Write requests
  141. for counter := 0; counter < numDeliverTxs; counter++ {
  142. // Send request
  143. response, err := client.DeliverTx(context.Background(), &types.RequestDeliverTx{Tx: []byte("test")})
  144. if err != nil {
  145. t.Fatalf("Error in GRPC DeliverTx: %v", err.Error())
  146. }
  147. counter++
  148. if response.Code != code.CodeTypeOK {
  149. t.Error("DeliverTx failed with ret_code", response.Code)
  150. }
  151. if counter > numDeliverTxs {
  152. t.Fatal("Too many DeliverTx responses")
  153. }
  154. t.Log("response", counter)
  155. if counter == numDeliverTxs {
  156. go func() {
  157. time.Sleep(time.Second * 1) // Wait for a bit to allow counter overflow
  158. }()
  159. }
  160. }
  161. }