Skip to content

Commit

Permalink
never use the same port
Browse files Browse the repository at this point in the history
  • Loading branch information
0xcb9ff9 committed Sep 22, 2022
1 parent 5ace3ea commit 65e8fc7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
10 changes: 3 additions & 7 deletions e2e/framework/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,18 +381,14 @@ func FindAvailablePort(from, to int) *ReservedPort {
}

func FindAvailablePorts(n, from, to int) ([]ReservedPort, error) {
ports := make([]ReservedPort, 0, n)
ports := []ReservedPort{}
nextFrom := from

for i := 0; i < n; i++ {
newPort := FindAvailablePort(nextFrom, to)
if newPort == nil {
// Close current reserved ports
for _, p := range ports {
p.Close()
}

return nil, errors.New("couldn't reserve required number of ports")
nextFrom = nextFrom + 1
continue
}

ports = append(ports, *newPort)
Expand Down
32 changes: 27 additions & 5 deletions e2e/framework/testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -44,11 +45,13 @@ import (
type TestServerConfigCallback func(*TestServerConfig)

const (
serverIP = "127.0.0.1"
initialPort = 12000
binaryName = "dogechain"
serverIP = "127.0.0.1"
binaryName = "dogechain"
)

var lock sync.Mutex
var initialPort = 12000

type TestServer struct {
t *testing.T

Expand All @@ -59,12 +62,17 @@ type TestServer struct {
func NewTestServer(t *testing.T, rootDir string, callback TestServerConfigCallback) *TestServer {
t.Helper()

// Reserve ports
ports, err := FindAvailablePorts(3, initialPort, initialPort+10000)
lock.Lock()
defer lock.Unlock()

// never use the same port
ports, err := FindAvailablePorts(3, initialPort, 65534)
if err != nil {
t.Fatal(err)
}

initialPort = ports[2].Port() + 1

// Sets the services to start on open ports
config := &TestServerConfig{
ReservedPorts: ports,
Expand Down Expand Up @@ -156,7 +164,19 @@ func (t *TestServer) IBFTOperator() ibftOp.IbftOperatorClient {
return ibftOp.NewIbftOperatorClient(conn)
}

func (t *TestServer) ReleaseReservedPorts() {
for _, p := range t.Config.ReservedPorts {
if err := p.Close(); err != nil {
t.t.Error(err)
}
}

t.Config.ReservedPorts = nil
}

func (t *TestServer) Stop() {
t.ReleaseReservedPorts()

if t.cmd != nil {
if err := processKill(t.cmd); err != nil {
t.t.Error(err)
Expand Down Expand Up @@ -356,6 +376,8 @@ func (t *TestServer) Start(ctx context.Context) error {
args = append(args, "--block-gas-target", *types.EncodeUint64(t.Config.BlockGasTarget))
}

t.ReleaseReservedPorts()

// Start the server
t.cmd = execCommand(t.Config.RootDir, binaryName, args...)

Expand Down

0 comments on commit 65e8fc7

Please sign in to comment.