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.

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