-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtcp_sock_linux_test.go
104 lines (95 loc) · 1.98 KB
/
tcp_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
93
94
95
96
97
98
99
100
101
102
103
104
// ©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"
"testing"
)
func TestTCPSocket_ReadWrite(t *testing.T) {
addr0, err := sox.ResolveTCPAddr("tcp6", "[::1]:8088")
if err != nil {
t.Error(err)
return
}
p := []byte("test0123456789")
wait := make(chan struct{}, 1)
go func() {
lis, err := sox.ListenTCP6(addr0)
if err != nil {
t.Error(err)
return
}
wait <- struct{}{}
conn, err := lis.Accept()
if err != nil {
t.Error(err)
return
}
buf := make([]byte, len(p))
for {
r := sox.NewMessageReader(conn, sox.MessageOptionsTCPSocket)
rn, err := r.Read(buf)
if err != nil {
t.Errorf("read message: %v", err)
return
}
if rn != len(p) || !bytes.Equal(p, buf[:rn]) {
t.Errorf("read message expected %s but got %s", p, buf[:rn])
return
}
w := sox.NewMessageWriter(conn, sox.MessageOptionsTCPSocket)
wn, err := w.Write(buf[:rn])
if err != nil {
t.Errorf("write message: %v", err)
return
}
if wn != rn {
t.Errorf("short write")
return
}
break
}
}()
addr1, err := sox.ResolveTCPAddr("tcp6", "[::1]:8089")
if err != nil {
t.Error(err)
return
}
<-wait
conn, err := sox.DialTCP6(addr1, addr0)
if err != nil {
t.Error(err)
return
}
for sw := sox.NewParamSpinWait(); !sw.Closed(); sw.Once() {
w := sox.NewMessageWriter(conn, sox.MessageOptionsTCPSocket)
n, err := w.Write(p)
if err != nil {
t.Error(err)
return
}
if n != len(p) {
t.Error(io.ErrShortWrite)
return
}
buf := make([]byte, len(p))
r := sox.NewMessageReader(conn, sox.MessageOptionsTCPSocket)
n, err = r.Read(buf)
if err == sox.ErrTemporarilyUnavailable {
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
}
}