Skip to content

Commit 2871e0c

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
ipv4,ipv6: retry ENOBUFS and shut down the PacketConn on failure in TestPacketConnConcurrentReadWriteUnicastUDP
This ports CL 376094 and CL 376095 to the UDP variants of the test. Fixes golang/go#52549 (hopefully). Change-Id: I2537af1bc14a42b2e51882e5d646912e1239758c Reviewed-on: https://go-review.googlesource.com/c/net/+/402059 Run-TryBot: Bryan Mills <[email protected]> Auto-Submit: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 1d1ef93 commit 2871e0c

File tree

2 files changed

+74
-26
lines changed

2 files changed

+74
-26
lines changed

ipv4/readwrite_test.go

+37-13
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,31 @@ func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) {
244244
t.Fatal(err)
245245
}
246246

247+
var firstError sync.Once
248+
fatalf := func(format string, args ...interface{}) {
249+
// On the first error, close the PacketConn to unblock the remaining
250+
// goroutines. Suppress any further errors, which may occur simply due to
251+
// closing the PacketConn.
252+
first := false
253+
firstError.Do(func() {
254+
first = true
255+
p.Close()
256+
})
257+
if first {
258+
t.Helper()
259+
t.Errorf(format, args...)
260+
}
261+
runtime.Goexit()
262+
}
263+
247264
var wg sync.WaitGroup
248265
reader := func() {
249266
defer wg.Done()
250267
rb := make([]byte, 128)
251268
if n, cm, _, err := p.ReadFrom(rb); err != nil {
252-
t.Error(err)
253-
return
269+
fatalf("%v", err)
254270
} else if !bytes.Equal(rb[:n], wb) {
255-
t.Errorf("got %v; want %v", rb[:n], wb)
256-
return
271+
fatalf("got %v; want %v", rb[:n], wb)
257272
} else {
258273
s := cm.String()
259274
if strings.Contains(s, ",") {
@@ -270,15 +285,24 @@ func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) {
270285
cm.IfIndex = ifi.Index
271286
}
272287
if err := p.SetControlMessage(cf, toggle); err != nil {
273-
t.Error(err)
274-
return
275-
}
276-
if n, err := p.WriteTo(wb, &cm, dst); err != nil {
277-
t.Error(err)
278-
return
279-
} else if n != len(wb) {
280-
t.Errorf("got %d; want %d", n, len(wb))
281-
return
288+
fatalf("%v", err)
289+
}
290+
291+
backoff := time.Millisecond
292+
for {
293+
n, err := p.WriteTo(wb, &cm, dst)
294+
if err != nil {
295+
if n == 0 && isENOBUFS(err) {
296+
time.Sleep(backoff)
297+
backoff *= 2
298+
continue
299+
}
300+
fatalf("%v", err)
301+
}
302+
if n != len(wb) {
303+
fatalf("got %d; want %d", n, len(wb))
304+
}
305+
break
282306
}
283307
}
284308

ipv6/readwrite_test.go

+37-13
Original file line numberDiff line numberDiff line change
@@ -247,16 +247,31 @@ func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) {
247247
t.Fatal(err)
248248
}
249249

250+
var firstError sync.Once
251+
fatalf := func(format string, args ...interface{}) {
252+
// On the first error, close the PacketConn to unblock the remaining
253+
// goroutines. Suppress any further errors, which may occur simply due to
254+
// closing the PacketConn.
255+
first := false
256+
firstError.Do(func() {
257+
first = true
258+
p.Close()
259+
})
260+
if first {
261+
t.Helper()
262+
t.Errorf(format, args...)
263+
}
264+
runtime.Goexit()
265+
}
266+
250267
var wg sync.WaitGroup
251268
reader := func() {
252269
defer wg.Done()
253270
rb := make([]byte, 128)
254271
if n, cm, _, err := p.ReadFrom(rb); err != nil {
255-
t.Error(err)
256-
return
272+
fatalf("%v", err)
257273
} else if !bytes.Equal(rb[:n], wb) {
258-
t.Errorf("got %v; want %v", rb[:n], wb)
259-
return
274+
fatalf("got %v; want %v", rb[:n], wb)
260275
} else {
261276
s := cm.String()
262277
if strings.Contains(s, ",") {
@@ -274,15 +289,24 @@ func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) {
274289
cm.IfIndex = ifi.Index
275290
}
276291
if err := p.SetControlMessage(cf, toggle); err != nil {
277-
t.Error(err)
278-
return
279-
}
280-
if n, err := p.WriteTo(wb, &cm, dst); err != nil {
281-
t.Error(err)
282-
return
283-
} else if n != len(wb) {
284-
t.Errorf("got %d; want %d", n, len(wb))
285-
return
292+
fatalf("%v", err)
293+
}
294+
295+
backoff := time.Millisecond
296+
for {
297+
n, err := p.WriteTo(wb, &cm, dst)
298+
if err != nil {
299+
if n == 0 && isENOBUFS(err) {
300+
time.Sleep(backoff)
301+
backoff *= 2
302+
continue
303+
}
304+
fatalf("%v", err)
305+
}
306+
if n != len(wb) {
307+
fatalf("got %d; want %d", n, len(wb))
308+
}
309+
break
286310
}
287311
}
288312

0 commit comments

Comments
 (0)