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.

151 lines
3.5 KiB

  1. package nilapp
  2. import (
  3. "fmt"
  4. "net"
  5. "reflect"
  6. "testing"
  7. "time"
  8. "golang.org/x/net/context"
  9. "google.golang.org/grpc"
  10. . "github.com/tendermint/go-common"
  11. "github.com/tendermint/tmsp/client"
  12. "github.com/tendermint/tmsp/example/dummy"
  13. nilapp "github.com/tendermint/tmsp/example/nil"
  14. "github.com/tendermint/tmsp/server"
  15. "github.com/tendermint/tmsp/types"
  16. )
  17. func TestDummy(t *testing.T) {
  18. fmt.Println("### Testing Dummy")
  19. testStream(t, dummy.NewDummyApplication())
  20. }
  21. func TestNilApp(t *testing.T) {
  22. fmt.Println("### Testing NilApp")
  23. testStream(t, nilapp.NewNilApplication())
  24. }
  25. func TestGRPC(t *testing.T) {
  26. fmt.Println("### Testing GRPC")
  27. testGRPCSync(t, types.NewGRPCApplication(nilapp.NewNilApplication()))
  28. }
  29. func testStream(t *testing.T, app types.Application) {
  30. numAppendTxs := 200000
  31. // Start the listener
  32. server, err := server.NewSocketServer("unix://test.sock", app)
  33. if err != nil {
  34. Exit(Fmt("Error starting socket server: %v", err.Error()))
  35. }
  36. defer server.Stop()
  37. // Connect to the socket
  38. client, err := tmspcli.NewSocketClient("unix://test.sock", false)
  39. if err != nil {
  40. Exit(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_AppendTx:
  50. counter += 1
  51. if r.AppendTx.Code != types.CodeType_OK {
  52. t.Error("AppendTx failed with ret_code", r.AppendTx.Code)
  53. }
  54. if counter > numAppendTxs {
  55. t.Fatalf("Too many AppendTx responses. Got %d, expected %d", counter, numAppendTxs)
  56. }
  57. if counter == numAppendTxs {
  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 < numAppendTxs; counter++ {
  72. // Send request
  73. reqRes := client.AppendTxAsync([]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 Connect(addr)
  90. }
  91. func testGRPCSync(t *testing.T, app *types.GRPCApplication) {
  92. numAppendTxs := 2000
  93. // Start the listener
  94. server, err := server.NewGRPCServer("unix://test.sock", app)
  95. if err != nil {
  96. Exit(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. Exit(Fmt("Error dialing GRPC server: %v", err.Error()))
  103. }
  104. defer conn.Close()
  105. client := types.NewTMSPApplicationClient(conn)
  106. // Write requests
  107. for counter := 0; counter < numAppendTxs; counter++ {
  108. // Send request
  109. response, err := client.AppendTx(context.Background(), &types.RequestAppendTx{[]byte("test")})
  110. if err != nil {
  111. t.Fatalf("Error in GRPC AppendTx: %v", err.Error())
  112. }
  113. counter += 1
  114. if response.Code != types.CodeType_OK {
  115. t.Error("AppendTx failed with ret_code", response.Code)
  116. }
  117. if counter > numAppendTxs {
  118. t.Fatal("Too many AppendTx responses")
  119. }
  120. t.Log("response", counter)
  121. if counter == numAppendTxs {
  122. go func() {
  123. time.Sleep(time.Second * 2) // Wait for a bit to allow counter overflow
  124. }()
  125. }
  126. }
  127. }