package webrtc import ( "net" "testing" ) // TestAlloc_ReturnsRebindablePort exercises the alloc/close/rebind // sequence 100 times. If a fast rebind race existed in normal // conditions, this would surface it. func TestAlloc_ReturnsRebindablePort(t *testing.T) { for i := 0; i < 100; i++ { p, err := Alloc() if err != nil { t.Fatalf("iter %d: Alloc: %v", i, err) } if p == 0 { t.Fatalf("iter %d: expected non-zero port", i) } c, err := net.ListenUDP("udp4", &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: p}) if err != nil { t.Fatalf("iter %d: rebind port %d: %v", i, p, err) } c.Close() } } // TestAlloc_DistinctPorts confirms the OS doesn't hand us the same // ephemeral port twice in quick succession (it shouldn't — the socket // is briefly held in the bound state on close). func TestAlloc_DistinctPorts(t *testing.T) { seen := map[int]bool{} for i := 0; i < 10; i++ { p, err := Alloc() if err != nil { t.Fatal(err) } if seen[p] { t.Fatalf("duplicate port %d", p) } seen[p] = true } }