trpc.group/trpc-go/trpc-go@v1.0.3/internal/reuseport/udp_test.go (about) 1 // 2 // 3 // Tencent is pleased to support the open source community by making tRPC available. 4 // 5 // Copyright (C) 2023 THL A29 Limited, a Tencent company. 6 // All rights reserved. 7 // 8 // If you have downloaded a copy of the tRPC source code from Tencent, 9 // please note that tRPC source code is licensed under the Apache 2.0 License, 10 // A copy of the Apache 2.0 License is included in this file. 11 // 12 // 13 14 //go:build linux || darwin || dragonfly || freebsd || netbsd || openbsd 15 // +build linux darwin dragonfly freebsd netbsd openbsd 16 17 package reuseport 18 19 import ( 20 "testing" 21 22 "github.com/stretchr/testify/assert" 23 ) 24 25 func moreCaseNewReusablePortPacketConn(t *testing.T) { 26 listenerFour, err := NewReusablePortListener("udp6", ":10081") 27 assert.Nil(t, err) 28 defer listenerFour.Close() 29 30 listenerFive, err := NewReusablePortListener("udp4", ":10081") 31 assert.Nil(t, err) 32 defer listenerFive.Close() 33 34 listenerSix, err := NewReusablePortListener("udp", ":10081") 35 assert.Nil(t, err) 36 defer listenerSix.Close() 37 } 38 39 func TestNewReusablePortPacketConn(t *testing.T) { 40 listenerOne, err := NewReusablePortPacketConn("udp4", "localhost:10082") 41 assert.Nil(t, err) 42 defer listenerOne.Close() 43 44 listenerTwo, err := NewReusablePortPacketConn("udp", "127.0.0.1:10082") 45 assert.Nil(t, err) 46 defer listenerTwo.Close() 47 48 listenerThree, err := NewReusablePortPacketConn("udp6", ":10082") 49 assert.Nil(t, err) 50 defer listenerThree.Close() 51 52 moreCaseNewReusablePortPacketConn(t) 53 } 54 55 func TestListenPacket(t *testing.T) { 56 type args struct { 57 proto string 58 addr string 59 } 60 tests := []struct { 61 name string 62 args args 63 wantErr bool 64 }{ 65 { 66 name: "case1", 67 args: args{ 68 proto: "udp4", 69 addr: "localhost:10082", 70 }, 71 wantErr: false, 72 }, 73 { 74 name: "case2", 75 args: args{ 76 proto: "udp", 77 addr: "localhost:10082", 78 }, 79 wantErr: false, 80 }, 81 { 82 name: "case3", 83 args: args{ 84 proto: "udp6", 85 addr: ":10082", 86 }, 87 wantErr: false, 88 }, 89 { 90 name: "case4", 91 args: args{ 92 proto: "udp4", 93 addr: ":10081", 94 }, 95 wantErr: false, 96 }, 97 { 98 name: "case5", 99 args: args{ 100 proto: "udp6", 101 addr: ":10081", 102 }, 103 wantErr: false, 104 }, 105 { 106 name: "case6", 107 args: args{ 108 proto: "udp", 109 addr: ":10081", 110 }, 111 wantErr: false, 112 }, 113 { 114 name: "case7", 115 args: args{ 116 proto: "udp6_no_ipv_device", 117 addr: "[::1]:10081", 118 }, 119 wantErr: true, 120 }, 121 { 122 name: "case8_not_support_proto", 123 args: args{ 124 proto: "xxx", 125 addr: "[::1]:10081", 126 }, 127 wantErr: true, 128 }, 129 } 130 for _, tt := range tests { 131 t.Run(tt.name, func(t *testing.T) { 132 gotL, err := ListenPacket(tt.args.proto, tt.args.addr) 133 if (err != nil) != tt.wantErr { 134 t.Errorf("ListenPacket() error = %v, wantErr %v", err, tt.wantErr) 135 return 136 } 137 if gotL != nil { 138 _ = gotL.Close() 139 } 140 }) 141 } 142 } 143 144 func BenchmarkNewReusableUDPPortListener(b *testing.B) { 145 for i := 0; i < b.N; i++ { 146 listener, err := NewReusablePortPacketConn("udp4", "localhost:10082") 147 148 if err != nil { 149 b.Error(err) 150 } else { 151 listener.Close() 152 } 153 } 154 } 155 156 // TestNewReusablePortPacketConn2 一些边界覆盖 157 func TestNewReusablePortPacketConn2(t *testing.T) { 158 // new socket fd failed, unsupported protocol 159 _, err := NewReusablePortPacketConn("udp4xx", "localhost:10082") 160 assert.NotNil(t, err) 161 162 // reusePort failed 163 oldReusePort := reusePort 164 defer func() { 165 reusePort = oldReusePort 166 }() 167 reusePort = 0 168 _, err = NewReusablePortPacketConn("udp4", "localhost:10082") 169 assert.NotNil(t, err) 170 }