forked from funny/link
/
all_test.go
173 lines (140 loc) · 3.27 KB
/
all_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
package link
import (
"io"
"math/rand"
"sync"
"testing"
"time"
"github.com/funny/unitest"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
type TestCodec struct{}
type TestEncoder struct {
w io.Writer
}
type TestDecoder struct {
r io.Reader
}
func (_ TestCodec) NewEncoder(w io.Writer) Encoder {
return TestEncoder{w}
}
func (_ TestCodec) NewDecoder(r io.Reader) Decoder {
return TestDecoder{r}
}
func (encoder TestEncoder) Encode(msg interface{}) error {
_, err := encoder.w.Write(msg.([]byte))
return err
}
func (decoder TestDecoder) Decode(msg interface{}) error {
_, err := io.ReadFull(decoder.r, msg.([]byte))
return err
}
func RandBytes(n int) []byte {
n = rand.Intn(n) + 1
b := make([]byte, n)
for i := 0; i < n; i++ {
b[i] = byte(rand.Intn(255))
}
return b
}
func SessionTest(t *testing.T, codecType CodecType, test func(*testing.T, *Session)) {
server, err := Serve("tcp", "0.0.0.0:0", TestCodec{})
unitest.AssertNotError(t, err)
addr := server.listener.Addr().String()
serverWait := new(sync.WaitGroup)
go func() {
for {
session, err := server.Accept()
if err != nil {
break
}
serverWait.Add(1)
go func() {
io.Copy(session.conn, session.conn)
serverWait.Done()
}()
}
}()
clientWait := new(sync.WaitGroup)
for i := 0; i < 60; i++ {
clientWait.Add(1)
go func() {
session, err := Connect("tcp", addr, codecType)
unitest.AssertNotError(t, err)
test(t, session)
session.Close()
clientWait.Done()
}()
}
clientWait.Wait()
server.Stop()
serverWait.Wait()
}
func BytesTest(t *testing.T, session *Session) {
for i := 0; i < 2000; i++ {
msg1 := RandBytes(512)
err := session.Send(msg1)
unitest.AssertNotError(t, err)
var msg2 = make([]byte, len(msg1))
err = session.Receive(msg2)
unitest.AssertNotError(t, err)
unitest.AssertBytes(t, msg1, msg2)
}
}
func Test_Bytes(t *testing.T) {
SessionTest(t, TestCodec{}, BytesTest)
}
func Test_Async_Bytes(t *testing.T) {
SessionTest(t, Async(1024, TestCodec{}), BytesTest)
}
func Test_Bufio_Bytes(t *testing.T) {
SessionTest(t, Bufio(TestCodec{}), BytesTest)
}
func Test_ThreadSafe_Bytes(t *testing.T) {
SessionTest(t, ThreadSafe(TestCodec{}), BytesTest)
}
func Test_ThreadSafe_Bufio_Bytes(t *testing.T) {
SessionTest(t, ThreadSafe(Bufio(TestCodec{})), BytesTest)
}
type TestObject struct {
X, Y, Z int
}
func RandObject() TestObject {
return TestObject{
X: rand.Int(), Y: rand.Int(), Z: rand.Int(),
}
}
func ObjectTest(t *testing.T, session *Session) {
for i := 0; i < 2000; i++ {
msg1 := RandObject()
err := session.Send(&msg1)
unitest.AssertNotError(t, err)
var msg2 TestObject
err = session.Receive(&msg2)
unitest.AssertNotError(t, err)
unitest.Assert(t, msg1 == msg2, msg1, msg2)
}
}
func Test_Gob(t *testing.T) {
SessionTest(t, Gob(), ObjectTest)
}
func Test_Bufio_Gob(t *testing.T) {
SessionTest(t, Bufio(Gob()), ObjectTest)
}
func Test_Json(t *testing.T) {
SessionTest(t, Json(), ObjectTest)
}
func Test_Bufio_Json(t *testing.T) {
SessionTest(t, Bufio(Json()), ObjectTest)
}
func Test_Xml(t *testing.T) {
SessionTest(t, Xml(), ObjectTest)
}
func Test_Bufio_Xml(t *testing.T) {
SessionTest(t, Bufio(Xml()), ObjectTest)
}
func Test_Packet(t *testing.T) {
SessionTest(t, Packet(2, 1024, 1024, LittleEndian, Json()), ObjectTest)
}