Skip to content

Commit bcf7175

Browse files
committed
ipv4: deflake multicast listener tests
Fixes golang/go#20558. Change-Id: Iead39205e508aef4fa4cdbe5dabac96b0a60133a Reviewed-on: https://go-review.googlesource.com/44775 Run-TryBot: Mikio Hara <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent e4fa1c5 commit bcf7175

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

ipv4/multicastlistener_test.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,16 @@ func TestUDPMultiplePacketConnWithMultipleGroupListeners(t *testing.T) {
6969
}
7070

7171
for _, gaddr := range udpMultipleGroupListenerTests {
72-
c1, err := net.ListenPacket("udp4", "224.0.0.0:1024") // wildcard address with reusable port
72+
c1, err := net.ListenPacket("udp4", "224.0.0.0:0") // wildcard address with reusable port
7373
if err != nil {
7474
t.Fatal(err)
7575
}
7676
defer c1.Close()
77-
78-
c2, err := net.ListenPacket("udp4", "224.0.0.0:1024") // wildcard address with reusable port
77+
_, port, err := net.SplitHostPort(c1.LocalAddr().String())
78+
if err != nil {
79+
t.Fatal(err)
80+
}
81+
c2, err := net.ListenPacket("udp4", net.JoinHostPort("224.0.0.0", port)) // wildcard address with reusable port
7982
if err != nil {
8083
t.Fatal(err)
8184
}
@@ -131,16 +134,29 @@ func TestUDPPerInterfaceSinglePacketConnWithSingleGroupListener(t *testing.T) {
131134
if err != nil {
132135
t.Fatal(err)
133136
}
137+
port := "0"
134138
for i, ifi := range ift {
135139
ip, ok := nettest.IsMulticastCapable("ip4", &ifi)
136140
if !ok {
137141
continue
138142
}
139-
c, err := net.ListenPacket("udp4", ip.String()+":"+"1024") // unicast address with non-reusable port
143+
c, err := net.ListenPacket("udp4", net.JoinHostPort(ip.String(), port)) // unicast address with non-reusable port
140144
if err != nil {
141-
t.Fatal(err)
145+
// The listen may fail when the serivce is
146+
// already in use, but it's fine because the
147+
// purpose of this is not to test the
148+
// bookkeeping of IP control block inside the
149+
// kernel.
150+
t.Log(err)
151+
continue
142152
}
143153
defer c.Close()
154+
if port == "0" {
155+
_, port, err = net.SplitHostPort(c.LocalAddr().String())
156+
if err != nil {
157+
t.Fatal(err)
158+
}
159+
}
144160
p := ipv4.NewPacketConn(c)
145161
if err := p.JoinGroup(&ifi, &gaddr); err != nil {
146162
t.Fatal(err)

0 commit comments

Comments
 (0)