-
Notifications
You must be signed in to change notification settings - Fork 1
/
udp_sock_linux_test.go
92 lines (83 loc) · 1.5 KB
/
udp_sock_linux_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
// ©Hayabusa Cloud Co., Ltd. 2022. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//go:build linux
package sox_test
import (
"bytes"
"hybscloud.com/sox"
"io"
"runtime"
"testing"
)
func TestUDPSocket_ReadWrite(t *testing.T) {
addr0, err := sox.ResolveUDPAddr("udp6", "[::1]:8088")
if err != nil {
t.Error(err)
return
}
p := []byte("test0123456789")
wait := make(chan struct{}, 1)
go func() {
conn, err := sox.ListenUDP6(addr0)
if err != nil {
t.Error(err)
return
}
buf := make([]byte, len(p))
wait <- struct{}{}
for {
_, addr, err := conn.RecvFrom(buf)
if err == sox.ErrTemporarilyUnavailable {
runtime.Gosched()
continue
}
if err != nil {
t.Error(err)
return
}
_, err = conn.SendTo(buf, addr)
if err != nil {
t.Error(err)
return
}
}
}()
addr1, err := sox.ResolveUDPAddr("udp6", "[::1]:8089")
if err != nil {
t.Error(err)
return
}
<-wait
conn, err := sox.DialUDP6(addr1, addr0)
if err != nil {
t.Error(err)
return
}
for {
n, err := conn.Write(p)
if err != nil {
t.Error(err)
return
}
if n != len(p) {
t.Error(io.ErrShortWrite)
return
}
buf := make([]byte, len(p))
n, err = conn.Read(buf)
if err == sox.ErrTemporarilyUnavailable {
runtime.Gosched()
continue
}
if err != nil {
t.Error(err)
return
}
if !bytes.Equal(buf, p) {
t.Errorf("udp read expected %s but %s", p, buf)
return
}
break
}
}