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.

98 lines
2.2 KiB

  1. package proxy
  2. import (
  3. "context"
  4. "errors"
  5. "os"
  6. "os/signal"
  7. "syscall"
  8. "testing"
  9. "time"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/mock"
  12. "github.com/stretchr/testify/require"
  13. abciclient "github.com/tendermint/tendermint/abci/client"
  14. abcimocks "github.com/tendermint/tendermint/abci/client/mocks"
  15. "github.com/tendermint/tendermint/libs/log"
  16. )
  17. type noopStoppableClientImpl struct {
  18. abciclient.Client
  19. count int
  20. }
  21. func (c *noopStoppableClientImpl) Stop() error { c.count++; return nil }
  22. func TestAppConns_Start_Stop(t *testing.T) {
  23. ctx, cancel := context.WithCancel(context.Background())
  24. defer cancel()
  25. clientMock := &abcimocks.Client{}
  26. clientMock.On("Start", mock.Anything).Return(nil).Times(4)
  27. clientMock.On("Error").Return(nil)
  28. clientMock.On("Wait").Return(nil).Times(4)
  29. cl := &noopStoppableClientImpl{Client: clientMock}
  30. creatorCallCount := 0
  31. creator := func(logger log.Logger) (abciclient.Client, error) {
  32. creatorCallCount++
  33. return cl, nil
  34. }
  35. appConns := NewAppConns(creator, log.TestingLogger(), NopMetrics())
  36. err := appConns.Start(ctx)
  37. require.NoError(t, err)
  38. time.Sleep(100 * time.Millisecond)
  39. cancel()
  40. appConns.Wait()
  41. clientMock.AssertExpectations(t)
  42. assert.Equal(t, 4, cl.count)
  43. assert.Equal(t, 4, creatorCallCount)
  44. }
  45. // Upon failure, we call tmos.Kill
  46. func TestAppConns_Failure(t *testing.T) {
  47. ok := make(chan struct{})
  48. c := make(chan os.Signal, 1)
  49. signal.Notify(c, syscall.SIGTERM)
  50. go func() {
  51. for range c {
  52. close(ok)
  53. return
  54. }
  55. }()
  56. ctx, cancel := context.WithCancel(context.Background())
  57. defer cancel()
  58. clientMock := &abcimocks.Client{}
  59. clientMock.On("SetLogger", mock.Anything).Return()
  60. clientMock.On("Start", mock.Anything).Return(nil)
  61. clientMock.On("Wait").Return(nil)
  62. clientMock.On("Error").Return(errors.New("EOF"))
  63. cl := &noopStoppableClientImpl{Client: clientMock}
  64. creator := func(log.Logger) (abciclient.Client, error) {
  65. return cl, nil
  66. }
  67. appConns := NewAppConns(creator, log.TestingLogger(), NopMetrics())
  68. err := appConns.Start(ctx)
  69. require.NoError(t, err)
  70. t.Cleanup(func() { cancel(); appConns.Wait() })
  71. select {
  72. case <-ok:
  73. t.Log("SIGTERM successfully received")
  74. case <-time.After(5 * time.Second):
  75. t.Fatal("expected process to receive SIGTERM signal")
  76. }
  77. }