github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/network/p2p/netutil/error_test.go (about) 1 package netutil 2 3 import ( 4 "net" 5 "testing" 6 "time" 7 ) 8 9 func TestIsPacketTooBig(t *testing.T) { 10 listener, err := net.ListenPacket("udp", "127.0.0.1:0") 11 if err != nil { 12 t.Fatal(err) 13 } 14 defer listener.Close() 15 sender, err := net.Dial("udp", listener.LocalAddr().String()) 16 if err != nil { 17 t.Fatal(err) 18 } 19 defer sender.Close() 20 21 sendN := 1800 22 recvN := 300 23 for i := 0; i < 20; i++ { 24 go func() { 25 buf := make([]byte, sendN) 26 for i := range buf { 27 buf[i] = byte(i) 28 } 29 sender.Write(buf) 30 }() 31 32 buf := make([]byte, recvN) 33 listener.SetDeadline(time.Now().Add(1 * time.Second)) 34 n, _, err := listener.ReadFrom(buf) 35 if err != nil { 36 if nerr, ok := err.(net.Error); ok && nerr.Timeout() { 37 continue 38 } 39 if !isPacketTooBig(err) { 40 t.Fatalf("unexpected read error: %v", err) 41 } 42 continue 43 } 44 if n != recvN { 45 t.Fatalf("short read: %d, want %d", n, recvN) 46 } 47 for i := range buf { 48 if buf[i] != byte(i) { 49 t.Fatalf("error in pattern") 50 break 51 } 52 } 53 } 54 }