Files
seaweedfs/test/testutil/ports_test.go
Chris Lu be9996962d fix(test): avoid port collision between master gRPC and volume ports
AllocateMiniPorts(1) reserved masterPort and masterPort+GrpcPortOffset
by holding listeners open, but closed them on return. The subsequent
AllocatePorts call bound 127.0.0.1:0, so the OS could immediately reuse
the just-released mini gRPC port as a volume port — causing the volume
server to fail at bind time with "address already in use".

Introduce AllocatePortSet(miniCount, regularCount) that holds every
listener open until the full set is chosen, and route the five volume
test cluster builders through it.
2026-04-21 23:33:57 -07:00

30 lines
897 B
Go

package testutil
import "testing"
func TestAllocatePortSetNoGrpcCollision(t *testing.T) {
// Run a few iterations to catch the OS-recycles-just-closed-port race
// that previously hit regular ports when the mini gRPC offset was freed
// between AllocateMiniPorts and AllocatePorts calls.
for iter := 0; iter < 20; iter++ {
mini, regular, err := AllocatePortSet(1, 3)
if err != nil {
t.Fatalf("iter %d: AllocatePortSet: %v", iter, err)
}
if len(mini) != 1 || len(regular) != 3 {
t.Fatalf("iter %d: unexpected counts mini=%d regular=%d", iter, len(mini), len(regular))
}
reserved := map[int]bool{
mini[0]: true,
mini[0] + GrpcPortOffset: true,
}
for _, p := range regular {
if reserved[p] {
t.Fatalf("iter %d: regular port %d collides with mini pair %d/%d",
iter, p, mini[0], mini[0]+GrpcPortOffset)
}
reserved[p] = true
}
}
}