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.4 KiB

  1. package privval
  2. import (
  3. "net"
  4. "testing"
  5. "time"
  6. "github.com/pkg/errors"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. "github.com/tendermint/tendermint/crypto/ed25519"
  10. cmn "github.com/tendermint/tendermint/libs/common"
  11. "github.com/tendermint/tendermint/libs/log"
  12. "github.com/tendermint/tendermint/types"
  13. )
  14. // TestRemoteSignerRetryTCPOnly will test connection retry attempts over TCP. We
  15. // don't need this for Unix sockets because the OS instantly knows the state of
  16. // both ends of the socket connection. This basically causes the
  17. // RemoteSigner.dialer() call inside RemoteSigner.connect() to return
  18. // successfully immediately, putting an instant stop to any retry attempts.
  19. func TestRemoteSignerRetryTCPOnly(t *testing.T) {
  20. var (
  21. attemptc = make(chan int)
  22. retries = 2
  23. )
  24. ln, err := net.Listen("tcp", "127.0.0.1:0")
  25. require.NoError(t, err)
  26. go func(ln net.Listener, attemptc chan<- int) {
  27. attempts := 0
  28. for {
  29. conn, err := ln.Accept()
  30. require.NoError(t, err)
  31. err = conn.Close()
  32. require.NoError(t, err)
  33. attempts++
  34. if attempts == retries {
  35. attemptc <- attempts
  36. break
  37. }
  38. }
  39. }(ln, attemptc)
  40. rs := NewRemoteSigner(
  41. log.TestingLogger(),
  42. cmn.RandStr(12),
  43. types.NewMockPV(),
  44. DialTCPFn(ln.Addr().String(), testConnDeadline, ed25519.GenPrivKey()),
  45. )
  46. defer rs.Stop()
  47. RemoteSignerConnDeadline(time.Millisecond)(rs)
  48. RemoteSignerConnRetries(retries)(rs)
  49. assert.Equal(t, rs.Start(), ErrDialRetryMax)
  50. select {
  51. case attempts := <-attemptc:
  52. assert.Equal(t, retries, attempts)
  53. case <-time.After(100 * time.Millisecond):
  54. t.Error("expected remote to observe connection attempts")
  55. }
  56. }
  57. func TestIsConnTimeoutForFundamentalTimeouts(t *testing.T) {
  58. // Generate a networking timeout
  59. dialer := DialTCPFn(testFreeTCPAddr(t), time.Millisecond, ed25519.GenPrivKey())
  60. _, err := dialer()
  61. assert.Error(t, err)
  62. assert.True(t, IsConnTimeout(err))
  63. }
  64. func TestIsConnTimeoutForWrappedConnTimeouts(t *testing.T) {
  65. dialer := DialTCPFn(testFreeTCPAddr(t), time.Millisecond, ed25519.GenPrivKey())
  66. _, err := dialer()
  67. assert.Error(t, err)
  68. err = cmn.ErrorWrap(ErrConnTimeout, err.Error())
  69. assert.True(t, IsConnTimeout(err))
  70. }
  71. func TestIsConnTimeoutForNonTimeoutErrors(t *testing.T) {
  72. assert.False(t, IsConnTimeout(cmn.ErrorWrap(ErrDialRetryMax, "max retries exceeded")))
  73. assert.False(t, IsConnTimeout(errors.New("completely irrelevant error")))
  74. }