datarhei-dragonfork-core/app/webrtc/portalloc_test.go
Zac Gaetano 16ae17d2a1 feat(app/webrtc): port allocator + FFmpeg arg builder
Adds Alloc(), the ephemeral loopback UDP port grabber the subsystem
uses to pick the RTP port it will hand to FFmpeg and then re-bind with
core/webrtc.NewSourceOn. Covered by a 100x rebind test.

Adds BuildArgs(), which emits the -f rtp output fragments (video on
the passed port, audio on port+1) with copy codecs by default and an
H.264 baseline / libopus re-encode leg when ForceTranscode is set.
Covered by three unit tests.
2026-04-17 09:52:09 -04:00

43 lines
1 KiB
Go

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
}
}