-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrpc_pool_test.go
188 lines (173 loc) · 3.84 KB
/
grpc_pool_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
package grpc_pool
import (
"net"
"os"
"sync"
"testing"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
)
type test struct {
srvInfo struct {
srv *grpc.Server
Addr string
IP net.IP
Port int
}
}
var te *test
func TestMain(m *testing.M) {
te = &test{}
if err := te.startServer(); err != nil {
panic(err)
}
defer te.stop()
os.Exit(m.Run())
}
func TestNewGrpcPool(t *testing.T) {
newClient := func() (*grpc.ClientConn, error) {
opts := []grpc.DialOption{grpc.WithInsecure(), grpc.WithBlock()}
return grpc.Dial(te.srvInfo.Addr, opts...)
}
pool := NewGrpcPool(newClient, 10, time.Second*1)
con, err := pool.GetConn()
if err != nil {
t.Fatal(err)
}
if con.GetState() != connectivity.Ready {
t.Fatal("client not ready")
}
if err := con.Release(); err != nil {
t.Fatal(err)
}
if pool.Len() < 1 {
t.Fatal("pool len is not right")
}
time.Sleep(time.Second)
con, err = pool.GetConn()
if err != nil {
t.Fatal(err)
}
if con.GetState() != connectivity.Ready {
t.Fatal("client not ready")
}
con.Release()
pool.CloseAllConn()
}
func Test_NewGrpcPoolInfinity(t *testing.T) {
newClient := func() (*grpc.ClientConn, error) {
opts := []grpc.DialOption{grpc.WithInsecure(), grpc.WithBlock()}
return grpc.Dial(te.srvInfo.Addr, opts...)
}
pool := NewGrpcPool(newClient, 10, -1)
con, err := pool.GetConn()
if err != nil {
t.Fatal(err)
}
if con.GetState() != connectivity.Ready {
t.Fatal("client not ready")
}
if err := con.Release(); err != nil {
t.Fatal(err)
}
if pool.Len() < 1 {
t.Fatal("pool len is not right")
}
time.Sleep(time.Second)
con, err = pool.GetConn()
if err != nil {
t.Fatal(err)
}
if con.GetState() != connectivity.Ready {
t.Fatal("client not ready")
}
con.Release()
pool.CloseAllConn()
}
func TestNewGrpcPool2(t *testing.T) {
newClient := func() (*grpc.ClientConn, error) {
opts := []grpc.DialOption{grpc.WithInsecure(), grpc.WithBlock()}
return grpc.Dial(te.srvInfo.Addr, opts...)
}
pool := NewGrpcPool(newClient, 5, 1)
getConn := func() {
con, err := pool.GetConn()
if err != nil {
t.Fatal(err)
}
if con.GetState() != connectivity.Ready {
t.Fatal("client not ready")
}
time.AfterFunc(time.Second/100, func() {
if err := con.Release(); err != nil {
t.Fatal(err)
}
})
}
for i := 0; i < 20; i++ {
t.Logf("index: %d", i)
t.Logf("current len of pool: %d\n", pool.Len())
getConn()
}
t.Logf("current len of pool: %d\n", pool.Len())
time.Sleep(time.Second * 2)
t.Logf("current len of pool: %d\n", pool.Len())
getConn()
getConn()
getConn()
t.Logf("current len of pool: %d\n", pool.Len())
time.Sleep(time.Second * 2)
t.Logf("current len of pool: %d\n", pool.Len())
pool.CloseAllConn()
}
func (te *test) startServer() error {
lis, err := net.Listen("tcp", "localhost:0")
if err != nil {
return err
}
server := grpc.NewServer()
te.srvInfo.srv = server
go server.Serve(lis)
te.srvInfo.Addr = lis.Addr().String()
te.srvInfo.IP = lis.Addr().(*net.TCPAddr).IP
te.srvInfo.Port = lis.Addr().(*net.TCPAddr).Port
return nil
}
func (te *test) stop() {
te.srvInfo.srv.Stop()
}
func TestNewGrpcPool3(t *testing.T) {
createdTotal := 0
newClient := func() (*grpc.ClientConn, error) {
opts := []grpc.DialOption{grpc.WithInsecure(), grpc.WithBlock()}
createdTotal++
return grpc.Dial(te.srvInfo.Addr, opts...)
}
pool := NewGrpcPool(newClient, 5, -1)
wg := sync.WaitGroup{}
total := 100
wg.Add(total)
getConn := func() {
t.Logf("current len of pool: %d\n", pool.Len())
con, err := pool.GetConn()
if err != nil {
t.Fatal(err)
}
con.Release()
t.Logf("release len of pool: %d\n", pool.Len())
}
for i := 0; i < total; i++ {
go func() {
getConn()
wg.Done()
}()
}
wg.Wait()
t.Log("created total: ", createdTotal)
for i := 0; i < 10; i++ {
getConn()
}
t.Logf("current len of pool: %d\n", pool.Len())
pool.CloseAllConn()
}