Skip to content

Commit

Permalink
use freeport to get free ports
Browse files Browse the repository at this point in the history
  • Loading branch information
makramkd committed Dec 18, 2023
1 parent 4215e9d commit 7791ad4
Showing 1 changed file with 3 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package integrationtesthelpers

import (
"context"
"crypto/rand"
"encoding/hex"
"fmt"
"math/big"
"net"
"net/http"
"net/http/httptest"
"strconv"
Expand All @@ -19,6 +17,7 @@ import (
"github.com/ethereum/go-ethereum/common"
types3 "github.com/ethereum/go-ethereum/core/types"
"github.com/google/uuid"
"github.com/hashicorp/consul/sdk/freeport"
"github.com/jmoiron/sqlx"
"github.com/onsi/gomega"
"github.com/pkg/errors"
Expand Down Expand Up @@ -838,7 +837,7 @@ func (c *CCIPIntegrationTestHarness) SetupAndStartNodes(ctx context.Context, t *
app, peerID, transmitter, kb := setupNodeCCIP(
t,
c.Dest.User,
bootstrapNodePort+1+i,
int64(freeport.GetOne(t)),
fmt.Sprintf("oracle_ccip%d", i),
c.Source.Chain,
c.Dest.Chain,
Expand Down Expand Up @@ -885,7 +884,7 @@ func (c *CCIPIntegrationTestHarness) SetUpNodesAndJobs(t *testing.T, pricePipeli
// setup Jobs
ctx := context.Background()
// Starts nodes and configures them in the OCR contracts.
bootstrapNode, _, configBlock := c.SetupAndStartNodes(ctx, t, generateRandomBootstrapPort())
bootstrapNode, _, configBlock := c.SetupAndStartNodes(ctx, t, int64(freeport.GetOne(t)))

jobParams := c.NewCCIPJobSpecParams(pricePipeline, configBlock, usdcAttestationAPI)

Expand Down Expand Up @@ -966,25 +965,3 @@ func (n NoopFeedsClient) RejectedJob(context.Context, *pb.RejectedJobRequest) (*
func (n NoopFeedsClient) CancelledJob(context.Context, *pb.CancelledJobRequest) (*pb.CancelledJobResponse, error) {
return &pb.CancelledJobResponse{}, nil
}

func generateRandomBootstrapPort() int64 {
minPort := int64(10000)
maxPort := int64(65500)
for {
i, _ := rand.Int(rand.Reader, big.NewInt(maxPort-minPort))
port := i.Int64() + minPort
if isPortAvailable(port) {
return port
}
}
}

func isPortAvailable(port int64) bool {
address := fmt.Sprintf(":%d", port)
listener, err := net.Listen("tcp", address)
if err != nil {
return false
}
listener.Close()
return true
}

0 comments on commit 7791ad4

Please sign in to comment.