-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
channel_rpc_test.go
283 lines (237 loc) · 4.97 KB
/
channel_rpc_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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package pool
import (
"math/rand"
"net"
"net/http"
"net/rpc"
"sync"
"testing"
"time"
)
var (
InitialCap = 5
MaxIdleCap = 10
MaximumCap = 100
network = "tcp"
address = "127.0.0.1:7777"
//factory = func() (interface{}, error) { return net.Dial(network, address) }
factory = func() (interface{}, error) {
return rpc.DialHTTP("tcp", address)
}
closeFac = func(v interface{}) error {
nc := v.(*rpc.Client)
return nc.Close()
}
)
func init() {
// used for factory function
go rpcServer()
time.Sleep(time.Millisecond * 300) // wait until tcp server has been settled
rand.Seed(time.Now().UTC().UnixNano())
}
func TestNew(t *testing.T) {
p, err := newChannelPool()
defer p.Release()
if err != nil {
t.Errorf("New error: %s", err)
}
}
func TestPool_Get_Impl(t *testing.T) {
p, _ := newChannelPool()
defer p.Release()
conn, err := p.Get()
if err != nil {
t.Errorf("Get error: %s", err)
}
_, ok := conn.(*rpc.Client)
if !ok {
t.Errorf("Conn is not of type poolConn")
}
p.Put(conn)
}
func TestPool_Get(t *testing.T) {
p, _ := newChannelPool()
defer p.Release()
_, err := p.Get()
if err != nil {
t.Errorf("Get error: %s", err)
}
// after one get, current capacity should be lowered by one.
if p.Len() != (InitialCap - 1) {
t.Errorf("Get error. Expecting %d, got %d",
(InitialCap - 1), p.Len())
}
// get them all
var wg sync.WaitGroup
for i := 0; i < (MaximumCap - 1); i++ {
wg.Add(1)
go func() {
defer wg.Done()
_, err := p.Get()
if err != nil {
t.Errorf("Get error: %s", err)
}
}()
}
wg.Wait()
if p.Len() != 0 {
t.Errorf("Get error. Expecting %d, got %d",
(InitialCap - 1), p.Len())
}
_, err = p.Get()
if err != ErrMaxActiveConnReached {
t.Errorf("Get error: %s", err)
}
}
func TestPool_Put(t *testing.T) {
pconf := Config{InitialCap: InitialCap, MaxCap: MaximumCap, Factory: factory, Close: closeFac, IdleTimeout: time.Second * 20,
MaxIdle:MaxIdleCap}
p, err := NewChannelPool(&pconf)
if err != nil {
t.Fatal(err)
}
defer p.Release()
// get/create from the pool
conns := make([]interface{}, MaximumCap)
for i := 0; i < MaximumCap; i++ {
conn, _ := p.Get()
conns[i] = conn
}
// now put them all back
for _, conn := range conns {
p.Put(conn)
}
if p.Len() != MaxIdleCap {
t.Errorf("Put error len. Expecting %d, got %d",
1, p.Len())
}
p.Release() // close pool
}
func TestPool_UsedCapacity(t *testing.T) {
p, _ := newChannelPool()
defer p.Release()
if p.Len() != InitialCap {
t.Errorf("InitialCap error. Expecting %d, got %d",
InitialCap, p.Len())
}
}
func TestPool_Close(t *testing.T) {
p, _ := newChannelPool()
// now close it and test all cases we are expecting.
p.Release()
c := p.(*channelPool)
if c.conns != nil {
t.Errorf("Close error, conns channel should be nil")
}
if c.factory != nil {
t.Errorf("Close error, factory should be nil")
}
_, err := p.Get()
if err == nil {
t.Errorf("Close error, get conn should return an error")
}
if p.Len() != 0 {
t.Errorf("Close error used capacity. Expecting 0, got %d", p.Len())
}
}
func TestPoolConcurrent(t *testing.T) {
p, _ := newChannelPool()
pipe := make(chan interface{}, 0)
go func() {
p.Release()
}()
for i := 0; i < MaximumCap; i++ {
go func() {
conn, _ := p.Get()
pipe <- conn
}()
go func() {
conn := <-pipe
if conn == nil {
return
}
p.Put(conn)
}()
}
}
func TestPoolWriteRead(t *testing.T) {
//p, _ := NewChannelPool(0, 30, factory)
p, _ := newChannelPool()
conn, _ := p.Get()
cli := conn.(*rpc.Client)
var resp int
err := cli.Call("Arith.Multiply", Args{1, 2}, &resp)
if err != nil {
t.Error(err)
}
if resp != 2 {
t.Error("rpc.err")
}
}
func TestPoolConcurrent2(t *testing.T) {
//p, _ := NewChannelPool(0, 30, factory)
p, _ := newChannelPool()
var wg sync.WaitGroup
go func() {
for i := 0; i < 10; i++ {
wg.Add(1)
go func(i int) {
conn, _ := p.Get()
time.Sleep(time.Millisecond * time.Duration(rand.Intn(100)))
p.Close(conn)
wg.Done()
}(i)
}
}()
for i := 0; i < 10; i++ {
wg.Add(1)
go func(i int) {
conn, _ := p.Get()
time.Sleep(time.Millisecond * time.Duration(rand.Intn(100)))
p.Close(conn)
wg.Done()
}(i)
}
wg.Wait()
}
//
//func TestPoolConcurrent3(t *testing.T) {
// p, _ := NewChannelPool(0, 1, factory)
//
// var wg sync.WaitGroup
//
// wg.Add(1)
// go func() {
// p.Close()
// wg.Done()
// }()
//
// if conn, err := p.Get(); err == nil {
// conn.Close()
// }
//
// wg.Wait()
//}
func newChannelPool() (Pool, error) {
pconf := Config{InitialCap: InitialCap, MaxCap: MaximumCap, Factory: factory, Close: closeFac, IdleTimeout: time.Second * 20,
MaxIdle:MaxIdleCap}
return NewChannelPool(&pconf)
}
func rpcServer() {
arith := new(Arith)
rpc.Register(arith)
rpc.HandleHTTP()
l, e := net.Listen("tcp", address)
if e != nil {
panic(e)
}
go http.Serve(l, nil)
}
type Args struct {
A, B int
}
type Arith int
func (t *Arith) Multiply(args *Args, reply *int) error {
*reply = args.A * args.B
return nil
}