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.

93 lines
1.9 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package nilapp
  2. import (
  3. "reflect"
  4. "testing"
  5. "time"
  6. . "github.com/tendermint/go-common"
  7. "github.com/tendermint/tmsp/server"
  8. "github.com/tendermint/tmsp/types"
  9. )
  10. func TestStream(t *testing.T) {
  11. numAppendTxs := 200000
  12. // Start the listener
  13. server, err := server.NewServer("unix://test.sock", NewNilApplication())
  14. if err != nil {
  15. Exit(err.Error())
  16. }
  17. defer server.Stop()
  18. // Connect to the socket
  19. conn, err := Connect("unix://test.sock")
  20. if err != nil {
  21. Exit(err.Error())
  22. }
  23. // Read response data
  24. done := make(chan struct{})
  25. go func() {
  26. counter := 0
  27. for {
  28. var res = &types.Response{}
  29. err := types.ReadMessage(conn, res)
  30. if err != nil {
  31. Exit(err.Error())
  32. }
  33. // Process response
  34. switch r := res.Value.(type) {
  35. case *types.Response_AppendTx:
  36. counter += 1
  37. if r.AppendTx.Code != types.CodeType_OK {
  38. t.Error("AppendTx failed with ret_code", r.AppendTx.Code)
  39. }
  40. if counter > numAppendTxs {
  41. t.Fatal("Too many AppendTx responses")
  42. }
  43. t.Log("response", counter)
  44. if counter == numAppendTxs {
  45. go func() {
  46. time.Sleep(time.Second * 2) // Wait for a bit to allow counter overflow
  47. close(done)
  48. }()
  49. }
  50. case *types.Response_Flush:
  51. // ignore
  52. default:
  53. t.Error("Unexpected response type", reflect.TypeOf(res.Value))
  54. }
  55. }
  56. }()
  57. // Write requests
  58. for counter := 0; counter < numAppendTxs; counter++ {
  59. // Send request
  60. var req = types.ToRequestAppendTx([]byte("test"))
  61. err := types.WriteMessage(req, conn)
  62. if err != nil {
  63. t.Fatal(err.Error())
  64. }
  65. // Sometimes send flush messages
  66. if counter%123 == 0 {
  67. t.Log("flush")
  68. err := types.WriteMessage(types.ToRequestFlush(), conn)
  69. if err != nil {
  70. t.Fatal(err.Error())
  71. }
  72. }
  73. }
  74. // Send final flush message
  75. err = types.WriteMessage(types.ToRequestFlush(), conn)
  76. if err != nil {
  77. t.Fatal(err.Error())
  78. }
  79. <-done
  80. }