forked from CovenantSQL/CovenantSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool_test.go
249 lines (224 loc) · 6.39 KB
/
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
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
/*
* Copyright 2018 The CovenantSQL Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rpc
import (
"fmt"
"net"
"sync"
"sync/atomic"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
"github.com/CovenantSQL/CovenantSQL/proto"
"github.com/CovenantSQL/CovenantSQL/utils/log"
)
func benchCaller(b *testing.B, caller *Caller, remote proto.NodeID) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
err := caller.CallNode(remote, "Count.Add", &AddReq{Delta: 1}, &AddResp{})
if err != nil {
b.Error("call node failed: ", err)
}
}
})
}
func benchPCaller(b *testing.B, caller *PersistentCaller) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
err := caller.Call("Count.Add", &AddReq{Delta: 1}, &AddResp{})
if err != nil {
b.Error("call node failed: ", err)
}
}
})
}
func testCaller(c C, wg *sync.WaitGroup, pool NOClientPool, remote proto.NodeID, con int, quest int) {
defer wg.Done()
var finished int32
caller := NewCallerWithPool(pool)
iwg := &sync.WaitGroup{}
defer iwg.Wait()
for i := 0; i < con; i++ {
iwg.Add(1)
go func(c C) {
defer iwg.Done()
for atomic.AddInt32(&finished, 1) < int32(quest) {
err := caller.CallNode(remote, "Count.Add", &AddReq{Delta: 1}, &AddResp{})
c.So(err, ShouldBeNil)
}
}(c)
}
}
func testPCaller(c C, wg *sync.WaitGroup, pool NOClientPool, remote proto.NodeID, con int, quest int) {
defer wg.Done()
var finished int32
caller := NewPersistentCallerWithPool(pool, remote)
defer caller.Close()
iwg := &sync.WaitGroup{}
defer iwg.Wait()
for i := 0; i < con; i++ {
iwg.Add(1)
go func(c C) {
defer iwg.Done()
for atomic.AddInt32(&finished, 1) < int32(quest) {
err := caller.Call("Count.Add", &AddReq{Delta: 1}, &AddResp{})
c.So(err, ShouldBeNil)
}
}(c)
}
}
func BenchmarkRPCComponents(b *testing.B) {
originLevel := log.GetLevel()
defer log.SetLevel(originLevel)
log.SetLevel(log.FatalLevel)
nodes, stop, err := setupEnvironment(1, AcceptNAConn)
if err != nil {
b.Fatal("failed to setup servers")
}
defer stop()
b.Run("CallerWithPool", func(b *testing.B) {
pool := &ClientPool{}
defer func() { _ = pool.Close() }()
benchCaller(b, NewCallerWithPool(pool), nodes[0].ID)
})
b.Run("CallerWithoutPool", func(b *testing.B) {
benchCaller(b, NewCallerWithPool(&nilPool{}), nodes[0].ID)
})
b.Run("PCaller", func(b *testing.B) {
pool := &ClientPool{}
defer func() { _ = pool.Close() }()
benchPCaller(b, NewPersistentCallerWithPool(pool, nodes[0].ID))
})
}
func TestRPCComponents(t *testing.T) {
Convey("Setup a single server for pool test", t, func(c C) {
nodes, stop, err := setupEnvironment(1, AcceptNAConn)
So(err, ShouldBeNil)
defer stop()
target := nodes[0].ID
pool := &ClientPool{}
defer func() { _ = pool.Close() }()
So(pool.Len(), ShouldEqual, 0)
cli1, err := pool.GetEx(target, true)
So(err, ShouldBeNil)
_ = cli1.Close()
So(pool.Len(), ShouldEqual, 0)
cli2, err := pool.GetEx(target, false)
So(err, ShouldBeNil)
_ = cli2.Close()
So(pool.Len(), ShouldEqual, 1)
So(pool.Len(), ShouldEqual, 1)
pool.Remove(target)
So(pool.Len(), ShouldEqual, 0)
Convey("Test pool functionality", func() {
var resp AddResp
// Test broken pipe
cli3, err := pool.GetEx(target, false)
So(err, ShouldBeNil)
err = cli3.Call("Count.Add", &AddReq{Delta: 1}, &resp)
So(err, ShouldBeNil)
_ = cli3.(*pooledClient).Client.Close() // close underlying client, should produce an error upon any following Call
err = cli3.Call("Count.Add", &AddReq{Delta: 1}, &resp)
So(err, ShouldNotBeNil)
_ = cli3.Close() // should not return a broken client to pool
So(pool.Len(), ShouldEqual, 0)
})
Convey("Test caller functionality", func() {
pool := &ClientPool{}
defer func() { _ = pool.Close() }()
caller := NewPersistentCallerWithPool(pool, target)
defer caller.Close()
// Set a bad client with raw tcp
rawconn, err := net.Dial("tcp", nodes[0].Addr)
So(err, ShouldBeNil)
caller.client = NewClient(rawconn)
err = caller.Call("Count.Add", &AddReq{Delta: 1}, &AddResp{}) // should try reconnect, and return err
So(err, ShouldNotBeNil)
err = caller.Call("Count.Add", &AddReq{Delta: 1}, &AddResp{}) // should be no err
So(err, ShouldBeNil)
})
})
Convey("Setup servers", t, func(c C) {
nodes, stop, err := setupEnvironment(10, AcceptNAConn)
So(err, ShouldBeNil)
defer stop()
var (
totalQuest = 1000 // of each server
callerCount = 10 // of each server
callerConcurr = 10 // of each caller
)
Convey("Test RCP call with Caller", func(c C) {
pool := &ClientPool{}
defer func() {
_ = pool.Close()
So(pool.Len(), ShouldEqual, 0)
}()
wg := &sync.WaitGroup{}
for _, v := range nodes {
for i := 0; i < callerCount; i++ {
wg.Add(1)
go testCaller(
c, wg, pool, v.ID, callerConcurr, totalQuest/(callerCount*callerConcurr))
}
}
wg.Wait()
So(pool.Len(), ShouldBeGreaterThan, 0)
})
Convey("Test RCP call with PCaller", func(c C) {
pool := &ClientPool{}
defer func() {
_ = pool.Close()
So(pool.Len(), ShouldEqual, 0)
}()
wg := &sync.WaitGroup{}
for _, v := range nodes {
for i := 0; i < callerCount; i++ {
wg.Add(1)
go testPCaller(
c, wg, pool, v.ID, callerConcurr, totalQuest/(callerCount*callerConcurr))
}
}
wg.Wait()
So(pool.Len(), ShouldBeGreaterThan, 0)
})
})
}
func TestRecordRPCCost(t *testing.T) {
Convey("Bug: bad critical section for multiple values", t, func(c C) {
var (
start = time.Now()
rounds = 1000
concurrent = 10
wg = &sync.WaitGroup{}
body = func(i int) {
defer func() {
c.So(recover(), ShouldBeNil)
wg.Done()
}()
recordRPCCost(start, fmt.Sprintf("M%d", i), nil)
}
)
for i := 0; i < rounds; i++ {
for j := 0; j < concurrent; j++ {
wg.Add(1)
go body(i)
}
wg.Wait()
}
})
}