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.

99 lines
2.3 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() { c.count++ }
  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)
  27. clientMock.On("Error").Return(nil)
  28. clientMock.On("IsRunning").Return(true)
  29. clientMock.On("Wait").Return(nil).Times(1)
  30. cl := &noopStoppableClientImpl{Client: clientMock}
  31. creatorCallCount := 0
  32. creator := func(logger log.Logger) (abciclient.Client, error) {
  33. creatorCallCount++
  34. return cl, nil
  35. }
  36. appConns := NewAppConns(creator, log.TestingLogger(), NopMetrics())
  37. err := appConns.Start(ctx)
  38. require.NoError(t, err)
  39. time.Sleep(200 * time.Millisecond)
  40. cancel()
  41. appConns.Wait()
  42. clientMock.AssertExpectations(t)
  43. assert.Equal(t, 1, cl.count)
  44. assert.Equal(t, 1, creatorCallCount)
  45. }
  46. // Upon failure, we call tmos.Kill
  47. func TestAppConns_Failure(t *testing.T) {
  48. ok := make(chan struct{})
  49. c := make(chan os.Signal, 1)
  50. signal.Notify(c, syscall.SIGTERM)
  51. go func() {
  52. for range c {
  53. close(ok)
  54. return
  55. }
  56. }()
  57. ctx, cancel := context.WithCancel(context.Background())
  58. defer cancel()
  59. clientMock := &abcimocks.Client{}
  60. clientMock.On("SetLogger", mock.Anything).Return()
  61. clientMock.On("Start", mock.Anything).Return(nil)
  62. clientMock.On("IsRunning").Return(true)
  63. clientMock.On("Wait").Return(nil)
  64. clientMock.On("Error").Return(errors.New("EOF"))
  65. cl := &noopStoppableClientImpl{Client: clientMock}
  66. creator := func(log.Logger) (abciclient.Client, error) {
  67. return cl, nil
  68. }
  69. appConns := NewAppConns(creator, log.TestingLogger(), NopMetrics())
  70. err := appConns.Start(ctx)
  71. require.NoError(t, err)
  72. t.Cleanup(func() { cancel(); appConns.Wait() })
  73. select {
  74. case <-ok:
  75. t.Log("SIGTERM successfully received")
  76. case <-time.After(5 * time.Second):
  77. t.Fatal("expected process to receive SIGTERM signal")
  78. }
  79. }