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.

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