forked from funny/link
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession_test.go
189 lines (156 loc) · 4.02 KB
/
session_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
package link
import (
"bytes"
"math/rand"
"runtime/pprof"
"sync"
"testing"
"time"
"github.com/funny/binary"
"github.com/funny/unitest"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
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
}
type TestObject struct {
X, Y, Z int
}
func (obj *TestObject) BinaryDecode(r *binary.Reader) error {
obj.X = int(r.ReadVarint())
obj.Y = int(r.ReadVarint())
obj.Z = int(r.ReadVarint())
return nil
}
func (obj *TestObject) BinaryEncode(w *binary.Writer) error {
w.WriteVarint(int64(obj.X))
w.WriteVarint(int64(obj.Y))
w.WriteVarint(int64(obj.Z))
return nil
}
func RandObject() TestObject {
return TestObject{
X: rand.Int(), Y: rand.Int(), Z: rand.Int(),
}
}
func SessionTest(t *testing.T, protocol ClientProtocol, codec CodecType, test func(*testing.T, *Session)) {
server, err := Serve("tcp://0.0.0.0:0", Stream(), nil)
unitest.NotError(t, err)
addr := server.listener.Addr().String()
serverWait := new(sync.WaitGroup)
go server.Loop(func(session *Session) {
serverWait.Add(1)
Echo(session)
serverWait.Done()
})
clientWait := new(sync.WaitGroup)
for i := 0; i < 60; i++ {
clientWait.Add(1)
go func() {
session, err := Connect("tcp://"+addr, protocol, codec)
unitest.NotError(t, err)
test(t, session)
session.Close()
clientWait.Done()
}()
}
clientWait.Wait()
server.Stop()
serverWait.Wait()
MakeSureSessionGoroutineExit(t)
}
func Test_BytesStream(t *testing.T) {
SessionTest(t, Stream(), Bytes(), func(t *testing.T, session *Session) {
for i := 0; i < 2000; i++ {
msg1 := RandBytes(1024)
err := session.Send(msg1)
unitest.NotError(t, err)
var msg2 = make([]byte, len(msg1))
err = session.Receive(msg2)
unitest.NotError(t, err)
unitest.Pass(t, bytes.Equal(msg1, msg2))
}
})
}
func Test_BytesPacket(t *testing.T) {
SessionTest(t, Packet(Uint16BE), Bytes(), func(t *testing.T, session *Session) {
for i := 0; i < 2000; i++ {
msg1 := RandBytes(1024)
err := session.Send(msg1)
unitest.NotError(t, err)
var msg2 []byte
err = session.Receive(&msg2)
unitest.NotError(t, err)
unitest.Pass(t, bytes.Equal(msg1, msg2))
}
})
}
func Test_StringPacket(t *testing.T) {
SessionTest(t, Packet(Uint16BE), String(), func(t *testing.T, session *Session) {
for i := 0; i < 2000; i++ {
msg1 := string(RandBytes(1024))
err := session.Send(msg1)
unitest.NotError(t, err)
var msg2 string
err = session.Receive(&msg2)
unitest.NotError(t, err)
unitest.Pass(t, msg1 == msg2)
}
})
}
func ObjectTest(t *testing.T, session *Session) {
for i := 0; i < 2000; i++ {
msg1 := RandObject()
err := session.Send(&msg1)
unitest.NotError(t, err)
var msg2 TestObject
err = session.Receive(&msg2)
unitest.NotError(t, err)
unitest.Pass(t, msg1 == msg2)
}
}
func Test_GobStream(t *testing.T) {
SessionTest(t, Stream(), Gob(), ObjectTest)
}
func Test_GobPacket(t *testing.T) {
SessionTest(t, Packet(Uint16BE), Gob(), ObjectTest)
}
func Test_JsonStream(t *testing.T) {
SessionTest(t, Stream(), Json(), ObjectTest)
}
func Test_JsonPacket(t *testing.T) {
SessionTest(t, Packet(Uint16BE), Json(), ObjectTest)
}
func Test_XmlStream(t *testing.T) {
SessionTest(t, Stream(), Xml(), ObjectTest)
}
func Test_XmlPacket(t *testing.T) {
SessionTest(t, Packet(Uint16BE), Xml(), ObjectTest)
}
func Test_XmlSocket(t *testing.T) {
SessionTest(t, Packet(Zero), Xml(), ObjectTest)
}
func Test_SelfCodecStream(t *testing.T) {
SessionTest(t, Stream(), SelfCodec(), ObjectTest)
}
func Test_SelfCodecPacket(t *testing.T) {
SessionTest(t, Packet(Uint16BE), SelfCodec(), ObjectTest)
}
func MakeSureSessionGoroutineExit(t *testing.T) {
buff := new(bytes.Buffer)
goroutines := pprof.Lookup("goroutine")
if err := goroutines.WriteTo(buff, 2); err != nil {
t.Fatalf("Dump goroutine failed: %v", err)
}
if n := bytes.Index(buff.Bytes(), []byte("link.HandlerFunc.Handle")); n >= 0 {
t.Log(buff.String())
t.Fatalf("Some handler goroutine running")
}
}