Skip to content

Commit

Permalink
simplify port selection in unitests
Browse files Browse the repository at this point in the history
  • Loading branch information
omerlavanet committed Mar 11, 2024
1 parent 888db6a commit b0f001a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
15 changes: 15 additions & 0 deletions protocol/integration/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,18 @@ func NewMockChainFetcher(startBlock, blocksToSave int64, callback func()) *MockC
}
return &mockCHainFetcher
}

type uniqueAddresGenerator struct {
seed int
lock sync.Mutex
}

func (ug *uniqueAddresGenerator) GetAddress() string {
ug.lock.Lock()
defer ug.lock.Unlock()
ug.seed++
if ug.seed < 100 {
return "localhost:10" + strconv.Itoa(ug.seed)
}
return "localhost:1" + strconv.Itoa(ug.seed)
}
11 changes: 6 additions & 5 deletions protocol/integration/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"net/http"
"os"
"strconv"
"testing"
"time"

Expand All @@ -34,12 +33,14 @@ import (
var (
seed int64
randomizer *sigs.ZeroReader
addressGen uniqueAddresGenerator
)

func TestMain(m *testing.M) {
// This code will run once before any test cases are executed.
seed = time.Now().Unix()
rand.SetSpecificSeed(seed)
addressGen = uniqueAddresGenerator{}
randomizer = sigs.NewZeroReader(seed)
lavasession.AllowInsecureConnectionToProviders = true
// Run the actual tests
Expand Down Expand Up @@ -256,7 +257,7 @@ func TestConsumerProviderBasic(t *testing.T) {

numProviders := 1

consumerListenAddress := "localhost:21111"
consumerListenAddress := addressGen.GetAddress()
pairingList := map[uint64]*lavasession.ConsumerSessionsWithProvider{}
type providerData struct {
account sigs.Account
Expand All @@ -277,7 +278,7 @@ func TestConsumerProviderBasic(t *testing.T) {
for i := 0; i < numProviders; i++ {
ctx := context.Background()
providerDataI := providers[i]
listenAddress := "localhost:111" + strconv.Itoa(i)
listenAddress := addressGen.GetAddress()
providers[i].server, providers[i].endpoint, providers[i].replySetter, providers[i].mockChainFetcher = createRpcProvider(t, ctx, consumerAccount.Addr.String(), specId, apiInterface, listenAddress, providerDataI.account, lavaChainID, []string(nil))
}
for i := 0; i < numProviders; i++ {
Expand Down Expand Up @@ -319,7 +320,7 @@ func TestConsumerProviderWithProviders(t *testing.T) {

numProviders := 5

consumerListenAddress := "localhost:21112"
consumerListenAddress := addressGen.GetAddress()
pairingList := map[uint64]*lavasession.ConsumerSessionsWithProvider{}
type providerData struct {
account sigs.Account
Expand All @@ -340,7 +341,7 @@ func TestConsumerProviderWithProviders(t *testing.T) {
for i := 0; i < numProviders; i++ {
ctx := context.Background()
providerDataI := providers[i]
listenAddress := "localhost:112" + strconv.Itoa(i)
listenAddress := addressGen.GetAddress()
providers[i].server, providers[i].endpoint, providers[i].replySetter, providers[i].mockChainFetcher = createRpcProvider(t, ctx, consumerAccount.Addr.String(), specId, apiInterface, listenAddress, providerDataI.account, lavaChainID, []string(nil))
providers[i].replySetter.replyDataBuf = []byte(fmt.Sprintf(`{"reply": %d}`, i))
}
Expand Down

0 comments on commit b0f001a

Please sign in to comment.