2018-03-10 09:27:21 -08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Get a free address for a test tendermint server
|
2018-03-12 08:31:27 -07:00
|
|
|
// protocol is either tcp, http, etc
|
2018-05-31 18:46:25 -07:00
|
|
|
func FreeTCPAddr() (addr, port string, err error) {
|
2019-06-26 13:30:36 -07:00
|
|
|
l, err := net.Listen("tcp", "localhost:0")
|
2018-05-31 18:46:25 -07:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
2018-06-28 15:52:10 -07:00
|
|
|
|
|
|
|
closer := func() {
|
|
|
|
err := l.Close()
|
|
|
|
if err != nil {
|
|
|
|
// TODO: Handle with #870
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
defer closer()
|
2018-03-10 09:27:21 -08:00
|
|
|
|
2018-05-31 18:46:25 -07:00
|
|
|
portI := l.Addr().(*net.TCPAddr).Port
|
|
|
|
port = fmt.Sprintf("%d", portI)
|
|
|
|
addr = fmt.Sprintf("tcp://0.0.0.0:%s", port)
|
|
|
|
return
|
2018-03-10 09:27:21 -08:00
|
|
|
}
|