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.

153 lines
3.8 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 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. "net"
  5. "reflect"
  6. "testing"
  7. "time"
  8. "google.golang.org/grpc"
  9. "golang.org/x/net/context"
  10. abcicli "github.com/tendermint/abci/client"
  11. "github.com/tendermint/abci/example/dummy"
  12. "github.com/tendermint/abci/server"
  13. "github.com/tendermint/abci/types"
  14. cmn "github.com/tendermint/tmlibs/common"
  15. "github.com/tendermint/tmlibs/log"
  16. )
  17. func TestDummy(t *testing.T) {
  18. fmt.Println("### Testing Dummy")
  19. testStream(t, dummy.NewDummyApplication())
  20. }
  21. func TestBaseApp(t *testing.T) {
  22. fmt.Println("### Testing BaseApp")
  23. testStream(t, types.NewBaseApplication())
  24. }
  25. func TestGRPC(t *testing.T) {
  26. fmt.Println("### Testing GRPC")
  27. testGRPCSync(t, types.NewGRPCApplication(types.NewBaseApplication()))
  28. }
  29. func testStream(t *testing.T, app types.Application) {
  30. numDeliverTxs := 200000
  31. // Start the listener
  32. server, err := server.NewSocketServer("unix://test.sock", app)
  33. if err != nil {
  34. t.Fatalf("Error starting socket server: %v", err.Error())
  35. }
  36. server.SetLogger(log.TestingLogger().With("module", "abci-server"))
  37. defer server.Stop()
  38. // Connect to the socket
  39. client, err := abcicli.NewSocketClient("unix://test.sock", false)
  40. if err != nil {
  41. t.Fatalf("Error starting socket client: %v", err.Error())
  42. }
  43. client.SetLogger(log.TestingLogger().With("module", "abci-client"))
  44. client.Start()
  45. defer client.Stop()
  46. done := make(chan struct{})
  47. counter := 0
  48. client.SetResponseCallback(func(req *types.Request, res *types.Response) {
  49. // Process response
  50. switch r := res.Value.(type) {
  51. case *types.Response_DeliverTx:
  52. counter++
  53. if r.DeliverTx.Code != types.CodeType_OK {
  54. t.Error("DeliverTx failed with ret_code", r.DeliverTx.Code)
  55. }
  56. if counter > numDeliverTxs {
  57. t.Fatalf("Too many DeliverTx responses. Got %d, expected %d", counter, numDeliverTxs)
  58. }
  59. if counter == numDeliverTxs {
  60. go func() {
  61. time.Sleep(time.Second * 2) // Wait for a bit to allow counter overflow
  62. close(done)
  63. }()
  64. return
  65. }
  66. case *types.Response_Flush:
  67. // ignore
  68. default:
  69. t.Error("Unexpected response type", reflect.TypeOf(res.Value))
  70. }
  71. })
  72. // Write requests
  73. for counter := 0; counter < numDeliverTxs; counter++ {
  74. // Send request
  75. reqRes := client.DeliverTxAsync([]byte("test"))
  76. _ = reqRes
  77. // check err ?
  78. // Sometimes send flush messages
  79. if counter%123 == 0 {
  80. client.FlushAsync()
  81. // check err ?
  82. }
  83. }
  84. // Send final flush message
  85. client.FlushAsync()
  86. <-done
  87. }
  88. //-------------------------
  89. // test grpc
  90. func dialerFunc(addr string, timeout time.Duration) (net.Conn, error) {
  91. return cmn.Connect(addr)
  92. }
  93. func testGRPCSync(t *testing.T, app *types.GRPCApplication) {
  94. numDeliverTxs := 2000
  95. // Start the listener
  96. server, err := server.NewGRPCServer("unix://test.sock", app)
  97. if err != nil {
  98. t.Fatalf("Error starting GRPC server: %v", err.Error())
  99. }
  100. server.SetLogger(log.TestingLogger().With("module", "abci-server"))
  101. defer server.Stop()
  102. // Connect to the socket
  103. conn, err := grpc.Dial("unix://test.sock", grpc.WithInsecure(), grpc.WithDialer(dialerFunc))
  104. if err != nil {
  105. t.Fatalf("Error dialing GRPC server: %v", err.Error())
  106. }
  107. defer conn.Close()
  108. client := types.NewABCIApplicationClient(conn)
  109. // Write requests
  110. for counter := 0; counter < numDeliverTxs; counter++ {
  111. // Send request
  112. response, err := client.DeliverTx(context.Background(), &types.RequestDeliverTx{[]byte("test")})
  113. if err != nil {
  114. t.Fatalf("Error in GRPC DeliverTx: %v", err.Error())
  115. }
  116. counter++
  117. if response.Code != types.CodeType_OK {
  118. t.Error("DeliverTx failed with ret_code", response.Code)
  119. }
  120. if counter > numDeliverTxs {
  121. t.Fatal("Too many DeliverTx responses")
  122. }
  123. t.Log("response", counter)
  124. if counter == numDeliverTxs {
  125. go func() {
  126. time.Sleep(time.Second * 2) // Wait for a bit to allow counter overflow
  127. }()
  128. }
  129. }
  130. }