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.

120 lines
2.7 KiB

  1. package abcicli_test
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. abcicli "github.com/tendermint/tendermint/abci/client"
  9. "github.com/tendermint/tendermint/abci/server"
  10. "github.com/tendermint/tendermint/abci/types"
  11. tmrand "github.com/tendermint/tendermint/libs/rand"
  12. "github.com/tendermint/tendermint/libs/service"
  13. )
  14. func TestProperSyncCalls(t *testing.T) {
  15. app := slowApp{}
  16. s, c := setupClientServer(t, app)
  17. t.Cleanup(func() {
  18. if err := s.Stop(); err != nil {
  19. t.Error(err)
  20. }
  21. })
  22. t.Cleanup(func() {
  23. if err := c.Stop(); err != nil {
  24. t.Error(err)
  25. }
  26. })
  27. resp := make(chan error, 1)
  28. go func() {
  29. // This is BeginBlockSync unrolled....
  30. reqres := c.BeginBlockAsync(types.RequestBeginBlock{})
  31. err := c.FlushSync()
  32. require.NoError(t, err)
  33. res := reqres.Response.GetBeginBlock()
  34. require.NotNil(t, res)
  35. resp <- c.Error()
  36. }()
  37. select {
  38. case <-time.After(time.Second):
  39. require.Fail(t, "No response arrived")
  40. case err, ok := <-resp:
  41. require.True(t, ok, "Must not close channel")
  42. assert.NoError(t, err, "This should return success")
  43. }
  44. }
  45. func TestHangingSyncCalls(t *testing.T) {
  46. app := slowApp{}
  47. s, c := setupClientServer(t, app)
  48. t.Cleanup(func() {
  49. if err := s.Stop(); err != nil {
  50. t.Log(err)
  51. }
  52. })
  53. t.Cleanup(func() {
  54. if err := c.Stop(); err != nil {
  55. t.Log(err)
  56. }
  57. })
  58. resp := make(chan error, 1)
  59. go func() {
  60. // Start BeginBlock and flush it
  61. reqres := c.BeginBlockAsync(types.RequestBeginBlock{})
  62. flush := c.FlushAsync()
  63. // wait 20 ms for all events to travel socket, but
  64. // no response yet from server
  65. time.Sleep(20 * time.Millisecond)
  66. // kill the server, so the connections break
  67. err := s.Stop()
  68. require.NoError(t, err)
  69. // wait for the response from BeginBlock
  70. reqres.Wait()
  71. flush.Wait()
  72. resp <- c.Error()
  73. }()
  74. select {
  75. case <-time.After(time.Second):
  76. require.Fail(t, "No response arrived")
  77. case err, ok := <-resp:
  78. require.True(t, ok, "Must not close channel")
  79. assert.Error(t, err, "We should get EOF error")
  80. }
  81. }
  82. func setupClientServer(t *testing.T, app types.Application) (
  83. service.Service, abcicli.Client) {
  84. // some port between 20k and 30k
  85. port := 20000 + tmrand.Int32()%10000
  86. addr := fmt.Sprintf("localhost:%d", port)
  87. s, err := server.NewServer(addr, "socket", app)
  88. require.NoError(t, err)
  89. err = s.Start()
  90. require.NoError(t, err)
  91. c := abcicli.NewSocketClient(addr, true)
  92. err = c.Start()
  93. require.NoError(t, err)
  94. return s, c
  95. }
  96. type slowApp struct {
  97. types.BaseApplication
  98. }
  99. func (slowApp) BeginBlock(req types.RequestBeginBlock) types.ResponseBeginBlock {
  100. time.Sleep(200 * time.Millisecond)
  101. return types.ResponseBeginBlock{}
  102. }