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.

85 lines
1.9 KiB

  1. package abciclient_test
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "time"
  7. "math/rand"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. abciclient "github.com/tendermint/tendermint/abci/client"
  11. "github.com/tendermint/tendermint/abci/server"
  12. "github.com/tendermint/tendermint/abci/types"
  13. "github.com/tendermint/tendermint/libs/log"
  14. "github.com/tendermint/tendermint/libs/service"
  15. )
  16. func TestProperSyncCalls(t *testing.T) {
  17. ctx, cancel := context.WithCancel(context.Background())
  18. defer cancel()
  19. app := slowApp{}
  20. logger := log.NewNopLogger()
  21. _, c := setupClientServer(ctx, t, logger, app)
  22. resp := make(chan error, 1)
  23. go func() {
  24. rsp, err := c.FinalizeBlock(ctx, types.RequestFinalizeBlock{})
  25. assert.NoError(t, err)
  26. assert.NoError(t, c.Flush(ctx))
  27. assert.NotNil(t, rsp)
  28. select {
  29. case <-ctx.Done():
  30. case resp <- c.Error():
  31. }
  32. }()
  33. select {
  34. case <-time.After(time.Second):
  35. require.Fail(t, "No response arrived")
  36. case err, ok := <-resp:
  37. require.True(t, ok, "Must not close channel")
  38. assert.NoError(t, err, "This should return success")
  39. }
  40. }
  41. func setupClientServer(
  42. ctx context.Context,
  43. t *testing.T,
  44. logger log.Logger,
  45. app types.Application,
  46. ) (service.Service, abciclient.Client) {
  47. t.Helper()
  48. // some port between 20k and 30k
  49. port := 20000 + rand.Int31()%10000
  50. addr := fmt.Sprintf("localhost:%d", port)
  51. s, err := server.NewServer(logger, addr, "socket", app)
  52. require.NoError(t, err)
  53. require.NoError(t, s.Start(ctx))
  54. t.Cleanup(s.Wait)
  55. c := abciclient.NewSocketClient(logger, addr, true)
  56. require.NoError(t, c.Start(ctx))
  57. t.Cleanup(c.Wait)
  58. require.True(t, s.IsRunning())
  59. require.True(t, c.IsRunning())
  60. return s, c
  61. }
  62. type slowApp struct {
  63. types.BaseApplication
  64. }
  65. func (slowApp) FinalizeBlock(req types.RequestFinalizeBlock) types.ResponseFinalizeBlock {
  66. time.Sleep(200 * time.Millisecond)
  67. return types.ResponseFinalizeBlock{}
  68. }