Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(e2e): better port selection #60

Merged
merged 5 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ test:
go test -race ./...

test-e2e:
go test -mod=readonly --failfast -timeout=25m -v $(PACKAGES_E2E) -count=1 --parallel 12 --tags=e2e
go test -mod=readonly -failfast -timeout=15m -v $(PACKAGES_E2E) -count=1 --parallel 12 --tags=e2e

build-docker:
$(DOCKER) build --tag babylonlabs-io/vigilante -f Dockerfile \
Expand Down
45 changes: 4 additions & 41 deletions e2etest/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"context"
"fmt"
bbn "github.com/babylonlabs-io/babylon/types"
"github.com/babylonlabs-io/vigilante/testutil"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/cometbft/cometbft/libs/rand"
"net"
"regexp"
"strconv"
"testing"
Expand All @@ -16,7 +15,6 @@ import (
"github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker"
"github.com/stretchr/testify/require"
mrand "math/rand/v2"
)

const (
Expand Down Expand Up @@ -168,7 +166,7 @@ func (m *Manager) RunBitcoindResource(
},
func(config *docker.HostConfig) {
config.PortBindings = map[docker.Port][]docker.PortBinding{
"18443/tcp": {{HostIP: "", HostPort: strconv.Itoa(randomAvailablePort(t))}}, // only expose what we need
"18443/tcp": {{HostIP: "", HostPort: strconv.Itoa(testutil.AllocateUniquePort(t))}}, // only expose what we need
}
config.PublishAllPorts = false // because in dockerfile they already expose them
},
Expand Down Expand Up @@ -220,8 +218,8 @@ func (m *Manager) RunBabylondResource(
},
func(config *docker.HostConfig) {
config.PortBindings = map[docker.Port][]docker.PortBinding{
"9090/tcp": {{HostIP: "", HostPort: strconv.Itoa(randomAvailablePort(t))}},
"26657/tcp": {{HostIP: "", HostPort: strconv.Itoa(randomAvailablePort(t))}},
"9090/tcp": {{HostIP: "", HostPort: strconv.Itoa(testutil.AllocateUniquePort(t))}},
"26657/tcp": {{HostIP: "", HostPort: strconv.Itoa(testutil.AllocateUniquePort(t))}},
}
},
noRestart,
Expand Down Expand Up @@ -252,38 +250,3 @@ func noRestart(config *docker.HostConfig) {
Name: "no",
}
}

// randomAvailablePort tries to find an available TCP port on the localhost
// by testing multiple random ports within a specified range.
func randomAvailablePort(t *testing.T) int {
randPort := func(base, spread int) int {
return base + mrand.IntN(spread)
}

// Base port and spread range for port selection
const (
basePort = 20000
portRange = 20000
)

// Seed the random number generator to ensure randomness
rand.Seed(time.Now().UnixNano())

// Try up to 10 times to find an available port
for i := 0; i < 10; i++ {
port := randPort(basePort, portRange)
listener, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
if err != nil {
continue
}
if err := listener.Close(); err != nil {
continue
}

return port
}

// If no available port was found, fail the test
t.Fatalf("failed to find an available port in range %d-%d", basePort, basePort+portRange)
return 0
}
2 changes: 1 addition & 1 deletion e2etest/reporter_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (tm *TestManager) GenerateAndSubmitBlockNBlockStartingFromDepth(t *testing.
}

func TestReporter_BoostrapUnderFrequentBTCHeaders(t *testing.T) {
t.Parallel()
//t.Parallel() // todo(lazar): this test when run in parallel is very flaky, investigate why
// no need to much mature outputs, we are not going to submit transactions in this test
numMatureOutputs := uint32(150)

Expand Down
61 changes: 61 additions & 0 deletions testutil/port.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package testutil

import (
"fmt"
mrand "math/rand/v2"
"net"
"sync"
"testing"
)

// Track allocated ports, protected by a mutex
var (
allocatedPorts = make(map[int]struct{})
portMutex sync.Mutex
)

// AllocateUniquePort tries to find an available TCP port on the localhost
// by testing multiple random ports within a specified range.
func AllocateUniquePort(t *testing.T) int {
randPort := func(base, spread int) int {
return base + mrand.IntN(spread)
}

// Base port and spread range for port selection
const (
basePort = 20000
portRange = 30000
)

// Try up to 10 times to find an available port
for i := 0; i < 10; i++ {
port := randPort(basePort, portRange)

// Lock the mutex to check and modify the shared map
portMutex.Lock()
if _, exists := allocatedPorts[port]; exists {
// Port already allocated, try another one
portMutex.Unlock()
continue
}

listener, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
if err != nil {
portMutex.Unlock()
continue
}

allocatedPorts[port] = struct{}{}
portMutex.Unlock()

if err := listener.Close(); err != nil {
continue
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably not critical right now becouse there is not much tests but we should also have here:

		t.Cleanup(func() {
			portMutex.Lock()
			defer portMutex.Unlock()
			delete(allocatedPorts, port)
		})

return port
}

// If no available port was found, fail the test
t.Fatalf("failed to find an available port in range %d-%d", basePort, basePort+portRange)
return 0
}
Loading