Skip to content

Commit 59a0b19

Browse files
committed
ipv6: deflake multicast listener tests
Fixes golang/go#20558. Change-Id: I8813d1eadd40680bd8619bb9a6539a3c45dcd76e Reviewed-on: https://go-review.googlesource.com/44770 Run-TryBot: Mikio Hara <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent bcf7175 commit 59a0b19

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

ipv6/multicastlistener_test.go

+22-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package ipv6_test
66

77
import (
8-
"fmt"
98
"net"
109
"runtime"
1110
"testing"
@@ -70,13 +69,16 @@ func TestUDPMultiplePacketConnWithMultipleGroupListeners(t *testing.T) {
7069
}
7170

7271
for _, gaddr := range udpMultipleGroupListenerTests {
73-
c1, err := net.ListenPacket("udp6", "[ff02::]:1024") // wildcard address with reusable port
72+
c1, err := net.ListenPacket("udp6", "[ff02::]:0") // wildcard address with reusable port
7473
if err != nil {
7574
t.Fatal(err)
7675
}
7776
defer c1.Close()
78-
79-
c2, err := net.ListenPacket("udp6", "[ff02::]: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("udp6", net.JoinHostPort("ff02::", port)) // wildcard address with reusable port
8082
if err != nil {
8183
t.Fatal(err)
8284
}
@@ -132,16 +134,29 @@ func TestUDPPerInterfaceSinglePacketConnWithSingleGroupListener(t *testing.T) {
132134
if err != nil {
133135
t.Fatal(err)
134136
}
137+
port := "0"
135138
for i, ifi := range ift {
136139
ip, ok := nettest.IsMulticastCapable("ip6", &ifi)
137140
if !ok {
138141
continue
139142
}
140-
c, err := net.ListenPacket("udp6", fmt.Sprintf("[%s%%%s]:1024", ip.String(), ifi.Name)) // unicast address with non-reusable port
143+
c, err := net.ListenPacket("udp6", net.JoinHostPort(ip.String()+"%"+ifi.Name, port)) // unicast address with non-reusable port
141144
if err != nil {
142-
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
143152
}
144153
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+
}
145160
p := ipv6.NewPacketConn(c)
146161
if err := p.JoinGroup(&ifi, &gaddr); err != nil {
147162
t.Fatal(err)
@@ -227,7 +242,7 @@ func TestIPPerInterfaceSinglePacketConnWithSingleGroupListener(t *testing.T) {
227242
if !ok {
228243
continue
229244
}
230-
c, err := net.ListenPacket("ip6:ipv6-icmp", fmt.Sprintf("%s%%%s", ip.String(), ifi.Name)) // unicast address
245+
c, err := net.ListenPacket("ip6:ipv6-icmp", ip.String()+"%"+ifi.Name) // unicast address
231246
if err != nil {
232247
t.Fatal(err)
233248
}

0 commit comments

Comments
 (0)