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.

90 lines
2.0 KiB

  1. package proxy
  2. import (
  3. "errors"
  4. "os"
  5. "os/signal"
  6. "syscall"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/mock"
  10. "github.com/stretchr/testify/require"
  11. abcimocks "github.com/tendermint/tendermint/abci/client/mocks"
  12. "github.com/tendermint/tendermint/proxy/mocks"
  13. )
  14. func TestAppConns_Start_Stop(t *testing.T) {
  15. quitCh := make(<-chan struct{})
  16. clientCreatorMock := &mocks.ClientCreator{}
  17. clientMock := &abcimocks.Client{}
  18. clientMock.On("SetLogger", mock.Anything).Return().Times(4)
  19. clientMock.On("Start").Return(nil).Times(4)
  20. clientMock.On("Stop").Return(nil).Times(4)
  21. clientMock.On("Quit").Return(quitCh).Times(4)
  22. clientCreatorMock.On("NewABCIClient").Return(clientMock, nil).Times(4)
  23. appConns := NewAppConns(clientCreatorMock)
  24. err := appConns.Start()
  25. require.NoError(t, err)
  26. time.Sleep(100 * time.Millisecond)
  27. err = appConns.Stop()
  28. require.NoError(t, err)
  29. clientMock.AssertExpectations(t)
  30. }
  31. // Upon failure, we call tmos.Kill
  32. func TestAppConns_Failure(t *testing.T) {
  33. ok := make(chan struct{})
  34. c := make(chan os.Signal, 1)
  35. signal.Notify(c, syscall.SIGTERM)
  36. go func() {
  37. for range c {
  38. close(ok)
  39. }
  40. }()
  41. quitCh := make(chan struct{})
  42. var recvQuitCh <-chan struct{} // nolint:gosimple
  43. recvQuitCh = quitCh
  44. clientCreatorMock := &mocks.ClientCreator{}
  45. clientMock := &abcimocks.Client{}
  46. clientMock.On("SetLogger", mock.Anything).Return()
  47. clientMock.On("Start").Return(nil)
  48. clientMock.On("Stop").Return(nil)
  49. clientMock.On("Quit").Return(recvQuitCh)
  50. clientMock.On("Error").Return(errors.New("EOF")).Once()
  51. clientCreatorMock.On("NewABCIClient").Return(clientMock, nil)
  52. appConns := NewAppConns(clientCreatorMock)
  53. err := appConns.Start()
  54. require.NoError(t, err)
  55. t.Cleanup(func() {
  56. if err := appConns.Stop(); err != nil {
  57. t.Error(err)
  58. }
  59. })
  60. // simulate failure
  61. close(quitCh)
  62. select {
  63. case <-ok:
  64. t.Log("SIGTERM successfully received")
  65. case <-time.After(5 * time.Second):
  66. t.Fatal("expected process to receive SIGTERM signal")
  67. }
  68. }