Browse Source

cmn: GetFreePort (#3255)

pull/3268/head
Ethan Buchman 6 years ago
committed by GitHub
parent
commit
4429826229
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 22 deletions
  1. +17
    -0
      libs/common/net.go
  2. +6
    -16
      p2p/test_util.go
  3. +9
    -6
      rpc/test/helpers.go

+ 17
- 0
libs/common/net.go View File

@ -24,3 +24,20 @@ func ProtocolAndAddress(listenAddr string) (string, string) {
} }
return protocol, address return protocol, address
} }
// GetFreePort gets a free port from the operating system.
// Ripped from https://github.com/phayes/freeport.
// BSD-licensed.
func GetFreePort() (int, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return 0, err
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return 0, err
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
}

+ 6
- 16
p2p/test_util.go View File

@ -247,35 +247,25 @@ func testNodeInfo(id ID, name string) NodeInfo {
} }
func testNodeInfoWithNetwork(id ID, name, network string) NodeInfo { func testNodeInfoWithNetwork(id ID, name, network string) NodeInfo {
port, err := getFreePort()
if err != nil {
panic(err)
}
return DefaultNodeInfo{ return DefaultNodeInfo{
ProtocolVersion: defaultProtocolVersion, ProtocolVersion: defaultProtocolVersion,
ID_: id, ID_: id,
ListenAddr: fmt.Sprintf("127.0.0.1:%d", port),
ListenAddr: fmt.Sprintf("127.0.0.1:%d", getFreePort()),
Network: network, Network: network,
Version: "1.2.3-rc0-deadbeef", Version: "1.2.3-rc0-deadbeef",
Channels: []byte{testCh}, Channels: []byte{testCh},
Moniker: name, Moniker: name,
Other: DefaultNodeInfoOther{ Other: DefaultNodeInfoOther{
TxIndex: "on", TxIndex: "on",
RPCAddress: fmt.Sprintf("127.0.0.1:%d", port),
RPCAddress: fmt.Sprintf("127.0.0.1:%d", getFreePort()),
}, },
} }
} }
func getFreePort() (int, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
func getFreePort() int {
port, err := cmn.GetFreePort()
if err != nil { if err != nil {
return 0, err
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return 0, err
panic(err)
} }
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
return port
} }

+ 9
- 6
rpc/test/helpers.go View File

@ -11,9 +11,9 @@ import (
"github.com/tendermint/tendermint/libs/log" "github.com/tendermint/tendermint/libs/log"
abci "github.com/tendermint/tendermint/abci/types" abci "github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"
cfg "github.com/tendermint/tendermint/config" cfg "github.com/tendermint/tendermint/config"
cmn "github.com/tendermint/tendermint/libs/common"
nm "github.com/tendermint/tendermint/node" nm "github.com/tendermint/tendermint/node"
"github.com/tendermint/tendermint/p2p" "github.com/tendermint/tendermint/p2p"
"github.com/tendermint/tendermint/privval" "github.com/tendermint/tendermint/privval"
@ -64,14 +64,17 @@ func makePathname() string {
} }
func randPort() int { func randPort() int {
return int(cmn.RandUint16()/2 + 10000)
port, err := cmn.GetFreePort()
if err != nil {
panic(err)
}
return port
} }
func makeAddrs() (string, string, string) { func makeAddrs() (string, string, string) {
start := randPort()
return fmt.Sprintf("tcp://0.0.0.0:%d", start),
fmt.Sprintf("tcp://0.0.0.0:%d", start+1),
fmt.Sprintf("tcp://0.0.0.0:%d", start+2)
return fmt.Sprintf("tcp://0.0.0.0:%d", randPort()),
fmt.Sprintf("tcp://0.0.0.0:%d", randPort()),
fmt.Sprintf("tcp://0.0.0.0:%d", randPort())
} }
// GetConfig returns a config for the test cases as a singleton // GetConfig returns a config for the test cases as a singleton


Loading…
Cancel
Save