Skip to content

Commit

Permalink
Use system-assigned epheral ports
Browse files Browse the repository at this point in the history
Added createConnectedUDPConnPair().
Relates to #270
  • Loading branch information
enobufs committed Dec 30, 2023
1 parent b8d93db commit cd16b2f
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2570,22 +2570,25 @@ func TestAssocMaxMessageSize(t *testing.T) {
})
}

func createAssocs() (*Association, *Association, error) {
addr1 := &net.UDPAddr{
IP: net.IP{127, 0, 0, 1},
Port: 1234,
// crateUDPConnPair creates a pair of net.UDPConn objects that are connected with each other
func createUDPConnPair(t *testing.T) (*net.UDPConn, *net.UDPConn, error) {
udp1, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP("127.0.0.1")})
if err != nil {
return nil, nil, err
}
addr1, ok := udp1.LocalAddr().(*net.UDPAddr)
require.True(t, ok)
err = udp1.Close()
require.NoError(t, err)

addr2 := &net.UDPAddr{
IP: net.IP{127, 0, 0, 1},
Port: 5678,
udp2, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP("127.0.0.1")})
if err != nil {
return nil, nil, err
}

var udp1 *net.UDPConn
var udp2 *net.UDPConn
var a1 *Association
var a2 *Association
var err error
addr2, ok := udp2.LocalAddr().(*net.UDPAddr)
require.True(t, ok)
err = udp2.Close()
require.NoError(t, err)

udp1, err = net.DialUDP("udp", addr1, addr2)
if err != nil {
Expand All @@ -2597,14 +2600,24 @@ func createAssocs() (*Association, *Association, error) {
return nil, nil, err
}

return udp1, udp2, nil
}

func createAssocs(t *testing.T) (*Association, *Association, error) {
udp1, udp2, err := createUDPConnPair(t)
if err != nil {
return nil, nil, err
}

loggerFactory := logging.NewDefaultLoggerFactory()

a1Chan := make(chan interface{})
a2Chan := make(chan interface{})

ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

go func() {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
a, err2 := createClientWithContext(ctx, Config{
NetConn: udp1,
LoggerFactory: loggerFactory,
Expand All @@ -2617,8 +2630,6 @@ func createAssocs() (*Association, *Association, error) {
}()

go func() {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
a, err2 := createClientWithContext(ctx, Config{
NetConn: udp2,
LoggerFactory: loggerFactory,
Expand All @@ -2630,6 +2641,9 @@ func createAssocs() (*Association, *Association, error) {
}
}()

var a1 *Association
var a2 *Association

loop:
for {
select {
Expand Down Expand Up @@ -2667,7 +2681,7 @@ func TestAssociation_Shutdown(t *testing.T) {
assert.Equal(t, n0, runtime.NumGoroutine(), "goroutine is leaked")
}()

a1, a2, err := createAssocs()
a1, a2, err := createAssocs(t)
require.NoError(t, err)

s11, err := a1.OpenStream(1, PayloadTypeWebRTCString)
Expand Down Expand Up @@ -2711,7 +2725,7 @@ func TestAssociation_ShutdownDuringWrite(t *testing.T) {
assert.Equal(t, n0, runtime.NumGoroutine(), "goroutine is leaked")
}()

a1, a2, err := createAssocs()
a1, a2, err := createAssocs(t)
require.NoError(t, err)

s11, err := a1.OpenStream(1, PayloadTypeWebRTCString)
Expand Down Expand Up @@ -2928,7 +2942,7 @@ func TestAssociation_Abort(t *testing.T) {
assert.Equal(t, n0, runtime.NumGoroutine(), "goroutine is leaked")
}()

a1, a2, err := createAssocs()
a1, a2, err := createAssocs(t)
require.NoError(t, err)

s11, err := a1.OpenStream(1, PayloadTypeWebRTCString)
Expand Down

0 comments on commit cd16b2f

Please sign in to comment.