kubo/test/cli/harness/peering.go
Marcin Rataj 6a008fc74c
refactor: apply go fix modernizers from Go 1.26 (#11190)
* chore: apply go fix modernizers from Go 1.26

automated refactoring: interface{} to any, slices.Contains,
and other idiomatic updates.

* feat(ci): add `go fix` check to Go analysis workflow

ensures Go 1.26 modernizers are applied, fails CI if `go fix ./...`
produces any changes (similar to existing `go fmt` enforcement)
2026-02-11 01:01:32 +01:00

69 lines
1.3 KiB
Go

package harness
import (
"fmt"
"math/rand"
"net"
"sync"
"testing"
"github.com/ipfs/kubo/config"
)
type Peering struct {
From int
To int
}
var (
allocatedPorts = make(map[int]struct{})
portMutex sync.Mutex
)
func NewRandPort() int {
portMutex.Lock()
defer portMutex.Unlock()
for range 100 {
l, err := net.Listen("tcp", "localhost:0")
if err != nil {
continue
}
port := l.Addr().(*net.TCPAddr).Port
l.Close()
if _, used := allocatedPorts[port]; !used {
allocatedPorts[port] = struct{}{}
return port
}
}
// Fallback to random port if we can't get a unique one from the OS
for range 1000 {
port := 30000 + rand.Intn(10000)
if _, used := allocatedPorts[port]; !used {
allocatedPorts[port] = struct{}{}
return port
}
}
panic("failed to allocate unique port after 1100 attempts")
}
func CreatePeerNodes(t *testing.T, n int, peerings []Peering) (*Harness, Nodes) {
h := NewT(t)
nodes := h.NewNodes(n).Init()
nodes.ForEachPar(func(node *Node) {
node.UpdateConfig(func(cfg *config.Config) {
cfg.Routing.Type = config.NewOptionalString("none")
cfg.Addresses.Swarm = []string{fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", NewRandPort())}
})
})
for _, peering := range peerings {
nodes[peering.From].PeerWith(nodes[peering.To])
}
return h, nodes
}