MCPcopy
hub / github.com/golang/net / TestPacketConnConcurrentReadWriteUnicastUDP

Function TestPacketConnConcurrentReadWriteUnicastUDP

ipv6/readwrite_test.go:221–326  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

219}
220
221func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) {
222 switch runtime.GOOS {
223 case "fuchsia", "hurd", "js", "nacl", "plan9", "wasip1", "windows":
224 t.Skipf("not supported on %s", runtime.GOOS)
225 }
226 ifi, err := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
227 if err != nil {
228 t.Skip("ipv6 is not enabled for loopback interface")
229 }
230 c, err := nettest.NewLocalPacketListener("udp6")
231 if err != nil {
232 t.Fatal(err)
233 }
234 defer c.Close()
235 p := ipv6.NewPacketConn(c)
236 defer p.Close()
237
238 dst := c.LocalAddr()
239 cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
240 wb := []byte("HELLO-R-U-THERE")
241
242 if err := p.SetControlMessage(cf, true); err != nil { // probe before test
243 if protocolNotSupported(err) {
244 t.Skipf("not supported on %s", runtime.GOOS)
245 }
246 t.Fatal(err)
247 }
248
249 var firstError sync.Once
250 fatalf := func(format string, args ...interface{}) {
251 // On the first error, close the PacketConn to unblock the remaining
252 // goroutines. Suppress any further errors, which may occur simply due to
253 // closing the PacketConn.
254 first := false
255 firstError.Do(func() {
256 first = true
257 p.Close()
258 })
259 if first {
260 t.Helper()
261 t.Errorf(format, args...)
262 }
263 runtime.Goexit()
264 }
265
266 var wg sync.WaitGroup
267 reader := func() {
268 defer wg.Done()
269 rb := make([]byte, 128)
270 if n, cm, _, err := p.ReadFrom(rb); err != nil {
271 fatalf("%v", err)
272 } else if !bytes.Equal(rb[:n], wb) {
273 fatalf("got %v; want %v", rb[:n], wb)
274 } else {
275 s := cm.String()
276 if strings.Contains(s, ",") {
277 t.Errorf("should be space-separated values: %s", s)
278 }

Callers

nothing calls this directly

Calls 15

CloseMethod · 0.95
SetControlMessageMethod · 0.95
ReadFromMethod · 0.95
StringMethod · 0.95
WriteToMethod · 0.95
RoutedInterfaceFunction · 0.92
NewLocalPacketListenerFunction · 0.92
NewPacketConnFunction · 0.92
readerStruct · 0.85
writerInterface · 0.85
SkipMethod · 0.80
DoneMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…