forked from funny/link
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession_test.go
165 lines (143 loc) · 2.91 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
package link
import (
"bytes"
"encoding/binary"
"io"
"math/rand"
"sync"
"testing"
"time"
"github.com/funny/utest"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func NewTestCodec(rw io.ReadWriter) (Codec, error) {
return &TestCodec{
rw: rw.(io.ReadWriteCloser),
}, nil
}
type TestCodec struct {
rw io.ReadWriteCloser
}
func (c TestCodec) Send(msg interface{}) error {
var head [2]byte
binary.LittleEndian.PutUint16(head[:], uint16(len(msg.([]byte))))
_, err := c.rw.Write(head[:])
if err != nil {
return err
}
_, err = c.rw.Write(msg.([]byte))
if err != nil {
return err
}
return nil
}
func (c TestCodec) Receive() (interface{}, error) {
var head [2]byte
_, err := io.ReadFull(c.rw, head[:])
if err != nil {
return nil, err
}
n := binary.LittleEndian.Uint16(head[:])
buf := make([]byte, n)
_, err = io.ReadFull(c.rw, buf)
if err != nil {
return nil, err
}
return buf, nil
}
func (c TestCodec) Close() error {
return c.rw.Close()
}
func (c TestCodec) ClearSendChan(ch <-chan interface{}) {
for _ = range ch {
}
}
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, sendChanSize int, test func(*testing.T, *Session)) {
server, err := Serve("tcp", "0.0.0.0:0", ProtocolFunc(NewTestCodec), sendChanSize)
utest.IsNilNow(t, err)
addr := server.Listener().Addr().String()
go server.Serve(HandlerFunc(func(session *Session) {
defer session.Close()
for {
msg, err := session.Receive()
if err != nil {
return
}
err = session.Send(msg)
if err != nil {
return
}
}
}))
clientWait := new(sync.WaitGroup)
for i := 0; i < 60; i++ {
clientWait.Add(1)
go func() {
session, err := Connect("tcp", addr, ProtocolFunc(NewTestCodec), sendChanSize)
utest.IsNilNow(t, err)
test(t, session)
session.Close()
clientWait.Done()
}()
}
clientWait.Wait()
server.Stop()
}
func BytesTest(t *testing.T, session *Session) {
for i := 0; i < 2000; i++ {
msg1 := RandBytes(512)
err := session.Send(msg1)
utest.IsNilNow(t, err)
msg2, err := session.Receive()
utest.IsNilNow(t, err)
utest.Assert(t, bytes.Equal(msg1, msg2.([]byte)))
}
}
func Test_Sync(t *testing.T) {
SessionTest(t, 0, BytesTest)
}
func Test_Async(t *testing.T) {
SessionTest(t, 1024, BytesTest)
}
func Benchmark_BytesToInterface(b *testing.B) {
var a = []byte{}
var x interface{}
for i := 0; i < b.N; i++ {
x = a
}
_ = x
}
func Benchmark_InterfaceToBytes(b *testing.B) {
var x interface{} = []byte{}
var a []byte
for i := 0; i < b.N; i++ {
a = x.([]byte)
}
_ = a
}
func Benchmark_PointerToInterface(b *testing.B) {
var a = struct{}{}
var x interface{}
for i := 0; i < b.N; i++ {
x = &a
}
_ = x
}
func Benchmark_InterfaceToPointer(b *testing.B) {
var x interface{} = new(struct{})
var a *struct{}
for i := 0; i < b.N; i++ {
a = x.(*struct{})
}
_ = a
}