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 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. appConns.Stop()
  28. clientMock.AssertExpectations(t)
  29. }
  30. // Upon failure, we call tmos.Kill
  31. func TestAppConns_Failure(t *testing.T) {
  32. ok := make(chan struct{})
  33. c := make(chan os.Signal, 1)
  34. signal.Notify(c, syscall.SIGTERM)
  35. go func() {
  36. for range c {
  37. close(ok)
  38. }
  39. }()
  40. quitCh := make(chan struct{})
  41. var recvQuitCh <-chan struct{} // nolint:gosimple
  42. recvQuitCh = quitCh
  43. clientCreatorMock := &mocks.ClientCreator{}
  44. clientMock := &abcimocks.Client{}
  45. clientMock.On("SetLogger", mock.Anything).Return()
  46. clientMock.On("Start").Return(nil)
  47. clientMock.On("Stop").Return(nil)
  48. clientMock.On("Quit").Return(recvQuitCh)
  49. clientMock.On("Error").Return(errors.New("EOF")).Once()
  50. clientCreatorMock.On("NewABCIClient").Return(clientMock, nil)
  51. appConns := NewAppConns(clientCreatorMock)
  52. err := appConns.Start()
  53. require.NoError(t, err)
  54. defer appConns.Stop()
  55. // simulate failure
  56. close(quitCh)
  57. select {
  58. case <-ok:
  59. t.Log("SIGTERM successfully received")
  60. case <-time.After(5 * time.Second):
  61. t.Fatal("expected process to receive SIGTERM signal")
  62. }
  63. }