-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpacket_test.go
310 lines (268 loc) · 5.86 KB
/
packet_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package sonic
import (
"errors"
"fmt"
"net"
"strings"
"syscall"
"testing"
"time"
"github.com/talostrading/sonic/internal"
"github.com/talostrading/sonic/sonicerrors"
)
func sendTo(b []byte, addr string) error {
fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, 0)
if err != nil {
return err
}
defer func() {
syscall.Close(fd)
}()
to, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return err
}
return syscall.Sendto(fd, b, 0, internal.ToSockaddr(to))
}
func recvFrom(b []byte, addr string) (int, net.Addr, error) {
fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, 0)
if err != nil {
return 0, nil, err
}
defer func() {
syscall.Close(fd)
}()
localAddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return 0, nil, err
}
err = syscall.Bind(fd, internal.ToSockaddr(localAddr))
if err != nil {
return 0, nil, err
}
n, peerAddr, err := syscall.Recvfrom(fd, b, 0)
return n, internal.FromSockaddr(peerAddr), err
}
func TestPacketError(t *testing.T) {
ioc := MustIO()
defer ioc.Close()
conn, err := NewPacketConn(ioc, "udp", "localhost:9080")
if err != nil {
t.Fatal(err)
}
defer conn.Close()
b := make([]byte, 1)
_, _, err = conn.ReadFrom(b)
if !errors.Is(err, sonicerrors.ErrWouldBlock) {
t.Fatal("should return ErrWouldBlock")
}
addr, err := net.ResolveUDPAddr("udp", "localhost:8181")
if err != nil {
t.Fatal(err)
}
err = conn.WriteTo([]byte("hello"), addr)
if err != nil {
t.Fatal("should not return error")
}
}
func TestPacketReadFrom(t *testing.T) {
marker := make(chan struct{}, 1)
go func() {
<-marker
for i := 0; i < 10; i++ {
sendTo([]byte("hello"), "localhost:8080")
time.Sleep(time.Millisecond)
}
}()
ioc := MustIO()
defer ioc.Close()
conn, err := NewPacketConn(ioc, "udp", "localhost:8080")
if err != nil {
t.Fatal(err)
}
defer func() {
conn.Close()
if !conn.Closed() {
t.Fatal("connection should be closed")
}
}()
if conn.LocalAddr() == nil {
t.Fatal("PacketConn socket should be bound to a local address")
}
if strings.Split(conn.LocalAddr().String(), ":")[1] != "8080" {
t.Fatalf("invalid port for local UDP address %v", conn.LocalAddr())
}
marker <- struct{}{}
b := make([]byte, 128)
for {
b = b[:cap(b)]
n, addr, err := conn.ReadFrom(b)
if err != nil {
if err == sonicerrors.ErrWouldBlock {
continue
} else {
t.Fatal(err)
}
} else {
b = b[:n]
if string(b) != "hello" {
t.Fatalf("wrong message expected \"hello\" but given \"%s\"", string(b))
}
if addr == nil {
t.Fatal("address should not be empty")
}
break
}
}
}
func TestPacketAsyncReadFrom(t *testing.T) {
marker := make(chan struct{}, 1)
go func() {
<-marker
for i := 0; i < 100; i++ {
sendTo([]byte("hello"), "localhost:8081")
time.Sleep(time.Millisecond)
}
}()
ioc := MustIO()
defer ioc.Close()
conn, err := NewPacketConn(ioc, "udp", "localhost:8081")
if err != nil {
t.Fatal(err)
}
defer func() {
conn.Close()
if !conn.Closed() {
t.Fatal("connection should be closed")
}
}()
if conn.LocalAddr() == nil {
t.Fatal("PacketConn socket should be bound to a local address")
}
if strings.Split(conn.LocalAddr().String(), ":")[1] != "8081" {
t.Fatalf("invalid port for local UDP address %v", conn.LocalAddr())
}
nread := 0
b := make([]byte, 128)
var onRead AsyncReadCallbackPacket
onRead = func(err error, n int, addr net.Addr) {
nread++
if err != nil {
t.Fatal(err)
} else {
b = b[:n]
if string(b) != "hello" {
t.Fatalf("wrong message expected \"hello\" but given \"%s\"", string(b))
}
if addr == nil {
t.Fatal("address should not be empty")
}
conn.AsyncReadFrom(b, onRead)
}
}
conn.AsyncReadFrom(b, onRead)
marker <- struct{}{}
start := time.Now()
for nread < 5 || time.Now().Sub(start) < time.Second {
ioc.RunOneFor(time.Millisecond)
}
if nread == 0 {
t.Fatal("did not read anything")
}
}
func TestPacketWriteTo(t *testing.T) {
marker := make(chan struct{}, 1)
go func() {
<-marker
b := make([]byte, 128)
n, addr, err := recvFrom(b, "localhost:8082")
if err != nil {
panic(err)
}
if addr == nil {
panic("address should not be nil")
}
b = b[:n]
if string(b) != "hello" {
panic(fmt.Errorf("expected to receive hello but instead got %s", string(b)))
}
marker <- struct{}{}
}()
toAddr, err := net.ResolveUDPAddr("udp", "localhost:8082")
if err != nil {
t.Fatal(err)
}
ioc := MustIO()
defer ioc.Close()
conn, err := NewPacketConn(ioc, "udp", "" /* assign randomly */)
if err != nil {
t.Fatal(err)
}
defer func() {
conn.Close()
if !conn.Closed() {
t.Fatal("connection should be closed")
}
}()
marker <- struct{}{}
outer:
for {
select {
case <-marker:
break outer
default:
time.Sleep(time.Millisecond)
err = conn.WriteTo([]byte("hello"), toAddr)
if err != nil {
t.Fatal(err)
}
}
}
}
func TestPacketAsyncWriteTo(t *testing.T) {
marker := make(chan struct{}, 1)
go func() {
<-marker
b := make([]byte, 128)
n, addr, err := recvFrom(b, "localhost:8083")
if err != nil {
panic(err)
}
if addr == nil {
panic("address should not be nil")
}
b = b[:n]
if string(b) != "hello" {
panic(fmt.Errorf("expected to receive hello but instead got %s", string(b)))
}
marker <- struct{}{}
}()
toAddr, err := net.ResolveUDPAddr("udp", "localhost:8083")
if err != nil {
t.Fatal(err)
}
ioc := MustIO()
defer ioc.Close()
conn, err := NewPacketConn(ioc, "udp", "" /* assign randomly */)
if err != nil {
t.Fatal(err)
}
defer func() {
conn.Close()
if !conn.Closed() {
t.Fatal("connection should be closed")
}
}()
marker <- struct{}{}
done := false
conn.AsyncWriteTo([]byte("hello"), toAddr, func(err error) {
done = true
if err != nil {
t.Fatal(err)
}
})
now := time.Now()
for !done || time.Now().Sub(now) < time.Second {
ioc.RunOneFor(time.Millisecond)
}
}