Browse Source

unifying and using cmn.GetFreePort()

pull/3370/head
Juan Leni 6 years ago
parent
commit
944d76d76e
No known key found for this signature in database GPG Key ID: 23F1452155140419
4 changed files with 20 additions and 29 deletions
  1. +6
    -3
      privval/socket_dialers_test.go
  2. +8
    -0
      privval/utils.go
  3. +1
    -11
      privval/utils_test.go
  4. +5
    -15
      tools/tm-signer-harness/internal/test_harness_test.go

+ 6
- 3
privval/socket_dialers_test.go View File

@ -7,12 +7,13 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto/ed25519"
cmn "github.com/tendermint/tendermint/libs/common"
)
func getDialerTestCases(t *testing.T) []dialerTestCase {
tcpAddr := fmt.Sprintf("tcp://%s", testFreeTCPAddr(t))
tcpAddr := GetFreeLocalhostAddrPort()
unixFilePath, err := testUnixAddr()
require.NoError(t, err)
unixAddr := fmt.Sprintf("unix://%s", unixFilePath)
@ -31,14 +32,16 @@ func getDialerTestCases(t *testing.T) []dialerTestCase {
func TestIsConnTimeoutForFundamentalTimeouts(t *testing.T) {
// Generate a networking timeout
dialer := DialTCPFn(testFreeTCPAddr(t), time.Millisecond, ed25519.GenPrivKey())
tcpAddr := GetFreeLocalhostAddrPort()
dialer := DialTCPFn(tcpAddr, time.Millisecond, ed25519.GenPrivKey())
_, err := dialer()
assert.Error(t, err)
assert.True(t, IsConnTimeout(err))
}
func TestIsConnTimeoutForWrappedConnTimeouts(t *testing.T) {
dialer := DialTCPFn(testFreeTCPAddr(t), time.Millisecond, ed25519.GenPrivKey())
tcpAddr := GetFreeLocalhostAddrPort()
dialer := DialTCPFn(tcpAddr, time.Millisecond, ed25519.GenPrivKey())
_, err := dialer()
assert.Error(t, err)
err = cmn.ErrorWrap(ErrListenerTimeout, err.Error())


+ 8
- 0
privval/utils.go View File

@ -49,3 +49,11 @@ func NewSignerListener(listenAddr string, logger log.Logger) (*SignerListenerEnd
return pve, nil
}
func GetFreeLocalhostAddrPort() string {
port, err := cmn.GetFreePort()
if err != nil {
panic(err)
}
return fmt.Sprintf("127.0.0.1:%d", port)
}

+ 1
- 11
privval/utils_test.go View File

@ -2,11 +2,10 @@ package privval
import (
"fmt"
"net"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
cmn "github.com/tendermint/tendermint/libs/common"
)
@ -14,12 +13,3 @@ func TestIsConnTimeoutForNonTimeoutErrors(t *testing.T) {
assert.False(t, IsConnTimeout(cmn.ErrorWrap(ErrDialRetryMax, "max retries exceeded")))
assert.False(t, IsConnTimeout(fmt.Errorf("completely irrelevant error")))
}
// testFreeTCPAddr claims a free port so we don't block on listener being ready.
func testFreeTCPAddr(t *testing.T) string {
ln, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
defer ln.Close()
return fmt.Sprintf("127.0.0.1:%d", ln.Addr().(*net.TCPAddr).Port)
}

+ 5
- 15
tools/tm-signer-harness/internal/test_harness_test.go View File

@ -3,19 +3,18 @@ package internal
import (
"fmt"
"io/ioutil"
"net"
"os"
"testing"
"time"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/privval"
"github.com/tendermint/tendermint/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/privval"
"github.com/tendermint/tendermint/types"
)
const (
@ -159,7 +158,7 @@ func harnessTest(t *testing.T, rsMaker func(th *TestHarness) *privval.SignerDial
func makeConfig(t *testing.T, acceptDeadline, acceptRetries int) TestHarnessConfig {
return TestHarnessConfig{
BindAddr: testFreeTCPAddr(t),
BindAddr: privval.GetFreeLocalhostAddrPort(),
KeyFile: makeTempFile("tm-testharness-keyfile", keyFileContents),
StateFile: makeTempFile("tm-testharness-statefile", stateFileContents),
GenesisFile: makeTempFile("tm-testharness-genesisfile", genesisFileContents),
@ -191,12 +190,3 @@ func makeTempFile(name, content string) string {
}
return tempFile.Name()
}
// testFreeTCPAddr claims a free port so we don't block on listener being ready.
func testFreeTCPAddr(t *testing.T) string {
ln, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
defer ln.Close()
return fmt.Sprintf("127.0.0.1:%d", ln.Addr().(*net.TCPAddr).Port)
}

Loading…
Cancel
Save