This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
reqrep_test.go
425 lines (370 loc) · 12.7 KB
/
reqrep_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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// Copyright (c) 2013 Project Iris. All rights reserved.
//
// The current language binding is an official support library of the Iris
// cloud messaging framework, and as such, the same licensing terms apply.
// For details please see http://iris.karalabe.com/downloads#License
package iris
import (
"errors"
"fmt"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/project-iris/iris/pool"
)
// Service handler for the request/reply tests.
type requestTestHandler struct {
conn *Connection
}
func (r *requestTestHandler) Init(conn *Connection) error { r.conn = conn; return nil }
func (r *requestTestHandler) HandleBroadcast(msg []byte) { panic("not implemented") }
func (r *requestTestHandler) HandleRequest(req []byte) ([]byte, error) { return req, nil }
func (r *requestTestHandler) HandleTunnel(tun *Tunnel) { panic("not implemented") }
func (r *requestTestHandler) HandleDrop(reason error) { panic("not implemented") }
// Service handler for the request/reply failure tests.
type requestFailTestHandler struct {
conn *Connection
}
func (r *requestFailTestHandler) Init(conn *Connection) error { r.conn = conn; return nil }
func (r *requestFailTestHandler) HandleBroadcast(msg []byte) { panic("not implemented") }
func (r *requestFailTestHandler) HandleTunnel(tun *Tunnel) { panic("not implemented") }
func (r *requestFailTestHandler) HandleDrop(reason error) { panic("not implemented") }
func (r *requestFailTestHandler) HandleRequest(req []byte) ([]byte, error) {
return nil, errors.New(string(req))
}
// Tests multiple concurrent client and service requests.
func TestRequest(t *testing.T) {
// Test specific configurations
conf := struct {
clients int
servers int
requests int
}{25, 25, 25}
barrier := newBarrier(conf.clients + conf.servers)
shutdown := new(sync.WaitGroup)
// Start up the concurrent requesting clients
for i := 0; i < conf.clients; i++ {
shutdown.Add(1)
go func(client int) {
defer shutdown.Done()
// Connect to the local relay
conn, err := Connect(config.relay)
if err != nil {
barrier.Exit(fmt.Errorf("connection failed: %v", err))
return
}
defer conn.Close()
barrier.Sync()
// Request from the service cluster
for i := 0; i < conf.requests; i++ {
request := fmt.Sprintf("client #%d, request %d", client, i)
if reply, err := conn.Request(config.cluster, []byte(request), time.Second); err != nil {
barrier.Exit(fmt.Errorf("client request failed: %v", err))
return
} else if string(reply) != request {
barrier.Exit(fmt.Errorf("client invalid reply: have %v, want %v", string(reply), request))
return
}
}
barrier.Exit(nil)
}(i)
}
// Start up the concurrent request services
for i := 0; i < conf.servers; i++ {
shutdown.Add(1)
go func(server int) {
defer shutdown.Done()
// Create the service handler
handler := new(requestTestHandler)
// Register a new service to the relay
serv, err := Register(config.relay, config.cluster, handler, nil)
if err != nil {
barrier.Exit(fmt.Errorf("registration failed: %v", err))
return
}
defer serv.Unregister()
barrier.Sync()
// Request from the service cluster
for i := 0; i < conf.requests; i++ {
request := fmt.Sprintf("server #%d, request %d", server, i)
if reply, err := handler.conn.Request(config.cluster, []byte(request), time.Second); err != nil {
barrier.Exit(fmt.Errorf("server request failed: %v", err))
return
} else if string(reply) != request {
barrier.Exit(fmt.Errorf("server invalid reply: have %v, want %v", string(reply), request))
return
}
}
barrier.Exit(nil)
}(i)
}
// Schedule the parallel operations
if errs := barrier.Wait(); len(errs) != 0 {
t.Fatalf("startup phase failed: %v.", errs)
}
if errs := barrier.Wait(); len(errs) != 0 {
t.Fatalf("request phase failed: %v.", errs)
}
// Make sure all children terminated
shutdown.Wait()
}
// Tests request failure forwarding.
func TestRequestFail(t *testing.T) {
// Test specific configurations
conf := struct {
requests int
}{125}
// Create the service handler
handler := new(requestFailTestHandler)
// Register a new service to the relay
serv, err := Register(config.relay, config.cluster, handler, nil)
if err != nil {
t.Fatalf("registration failed: %v", err)
}
defer serv.Unregister()
// Request from the failing service cluster
for i := 0; i < conf.requests; i++ {
request := fmt.Sprintf("failure %d", i)
reply, err := handler.conn.Request(config.cluster, []byte(request), time.Second)
if err == nil {
t.Fatalf("request didn't fail: %v.", reply)
} else if _, ok := err.(*RemoteError); !ok {
t.Fatalf("request didn't fail remotely: %v.", err)
} else if err.Error() != request {
t.Fatalf("error message mismatch: have %v, want %v.", err, request)
}
}
}
// Service handler for the request/reply limit tests.
type requestTestTimedHandler struct {
conn *Connection
sleep time.Duration
}
func (r *requestTestTimedHandler) Init(conn *Connection) error { r.conn = conn; return nil }
func (r *requestTestTimedHandler) HandleBroadcast(msg []byte) { panic("not implemented") }
func (r *requestTestTimedHandler) HandleTunnel(tun *Tunnel) { panic("not implemented") }
func (r *requestTestTimedHandler) HandleDrop(reason error) { panic("not implemented") }
func (r *requestTestTimedHandler) HandleRequest(req []byte) ([]byte, error) {
time.Sleep(r.sleep)
return req, nil
}
// Tests the request timeouts.
func TestRequestTimeout(t *testing.T) {
// Test specific configurations
conf := struct {
sleep time.Duration
}{25 * time.Millisecond}
// Create the service handler
handler := &requestTestTimedHandler{
sleep: conf.sleep,
}
// Register a new service to the relay
serv, err := Register(config.relay, config.cluster, handler, nil)
if err != nil {
t.Fatalf("registration failed: %v.", err)
}
defer serv.Unregister()
// Check that the timeouts are complied with.
if _, err := handler.conn.Request(config.cluster, []byte{0x00}, conf.sleep*2); err != nil {
t.Fatalf("longer timeout failed: %v.", err)
}
if rep, err := handler.conn.Request(config.cluster, []byte{0x00}, conf.sleep/2); err == nil {
t.Fatalf("shorter timeout succeeded: %v.", rep)
}
}
// Tests the request thread limitation.
func TestRequestThreadLimit(t *testing.T) {
// Test specific configurations
conf := struct {
requests int
sleep time.Duration
}{4, 25 * time.Millisecond}
// Create the service handler and limiter
handler := &requestTestTimedHandler{
sleep: conf.sleep,
}
limits := &ServiceLimits{RequestThreads: 1}
// Register a new service to the relay
serv, err := Register(config.relay, config.cluster, handler, limits)
if err != nil {
t.Fatalf("registration failed: %v.", err)
}
defer serv.Unregister()
// Start a batch of requesters
done := make(chan struct{}, conf.requests)
for i := 0; i < conf.requests; i++ {
go func() {
defer func() { done <- struct{}{} }()
if _, err := handler.conn.Request(config.cluster, []byte{0x00}, time.Duration(conf.requests+1)*conf.sleep); err != nil {
t.Fatalf("request failed: %v.", err)
}
}()
}
// Wait for half of them to complete and verify completion
time.Sleep(time.Duration(conf.requests/2)*conf.sleep + conf.sleep/2)
for i := 0; i < conf.requests/2; i++ {
select {
case <-done:
default:
t.Fatalf("request #%d not completed.", i)
}
}
select {
case <-done:
t.Fatalf("extra request completed.")
default:
}
// Wait for the rest to complete
time.Sleep(time.Duration(conf.requests/2)*conf.sleep + conf.sleep/2)
for i := conf.requests / 2; i < conf.requests; i++ {
select {
case <-done:
default:
t.Fatalf("request #%d not completed.", i)
}
}
}
// Tests the request memory limitation.
func TestRequestMemoryLimit(t *testing.T) {
// Create the service handler and limiter
handler := new(requestTestHandler)
limits := &ServiceLimits{RequestMemory: 1}
// Register a new service to the relay
serv, err := Register(config.relay, config.cluster, handler, limits)
if err != nil {
t.Fatalf("registration failed: %v.", err)
}
defer serv.Unregister()
// Check that a 1 byte request passes
if _, err := handler.conn.Request(config.cluster, []byte{0x00}, 25*time.Millisecond); err != nil {
t.Fatalf("small request failed: %v.", err)
}
// Check that a 2 byte request is dropped
if rep, err := handler.conn.Request(config.cluster, []byte{0x00, 0x00}, 25*time.Millisecond); err != ErrTimeout {
t.Fatalf("large request didn't time out: %v : %v.", rep, err)
}
// Check that space freed gets replenished
if _, err := handler.conn.Request(config.cluster, []byte{0x00}, 25*time.Millisecond); err != nil {
t.Fatalf("second small request failed: %v.", err)
}
}
// Service handler for the request/reply expiry tests.
type requestTestExpiryHandler struct {
conn *Connection
sleep time.Duration
done int32
}
func (r *requestTestExpiryHandler) Init(conn *Connection) error { r.conn = conn; return nil }
func (r *requestTestExpiryHandler) HandleBroadcast(msg []byte) { panic("not implemented") }
func (r *requestTestExpiryHandler) HandleTunnel(tun *Tunnel) { panic("not implemented") }
func (r *requestTestExpiryHandler) HandleDrop(reason error) { panic("not implemented") }
func (r *requestTestExpiryHandler) HandleRequest(req []byte) ([]byte, error) {
time.Sleep(r.sleep)
atomic.AddInt32(&r.done, 1)
return req, nil
}
// Tests that enqueued but expired requests don't get executed.
func TestRequestExpiration(t *testing.T) {
// Test specific configurations
conf := struct {
requests int
sleep time.Duration
}{4, 25 * time.Millisecond}
// Create the service handler and limiter
handler := &requestTestExpiryHandler{
sleep: conf.sleep,
}
limits := &ServiceLimits{RequestThreads: 1}
// Register a new service to the relay
serv, err := Register(config.relay, config.cluster, handler, limits)
if err != nil {
t.Fatalf("registration failed: %v.", err)
}
defer serv.Unregister()
// Start a batch of concurrent requesters (all but one should be scheduled remotely)
var pend sync.WaitGroup
for i := 0; i < conf.requests; i++ {
pend.Add(1)
go func() {
defer pend.Done()
handler.conn.Request(config.cluster, []byte{0x00}, time.Millisecond)
}()
}
// Wait for all of them to complete and verify that all but 1 expired
pend.Wait()
time.Sleep(time.Duration(conf.requests+1) * conf.sleep)
if done := atomic.LoadInt32(&handler.done); done != 1 {
t.Fatalf("executed request count mismatch: have %v, want %v.", done, 1)
}
}
// Benchmarks the latency of a single request/reply operation.
func BenchmarkRequestLatency(b *testing.B) {
// Create the service handler
handler := new(requestTestHandler)
// Register a new service to the relay
serv, err := Register(config.relay, config.cluster, handler, nil)
if err != nil {
b.Fatalf("registration failed: %v.", err)
}
defer serv.Unregister()
// Reset timer and benchmark the message transfer
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := handler.conn.Request(config.cluster, []byte{byte(i)}, time.Second); err != nil {
b.Fatalf("request failed: %v.", err)
}
}
// Stop the timer (don't measure deferred cleanup)
b.StopTimer()
}
// Benchmarks the throughput of a stream of concurrent requests.
func BenchmarkRequestThroughput1Threads(b *testing.B) {
benchmarkRequestThroughput(1, b)
}
func BenchmarkRequestThroughput2Threads(b *testing.B) {
benchmarkRequestThroughput(2, b)
}
func BenchmarkRequestThroughput4Threads(b *testing.B) {
benchmarkRequestThroughput(4, b)
}
func BenchmarkRequestThroughput8Threads(b *testing.B) {
benchmarkRequestThroughput(8, b)
}
func BenchmarkRequestThroughput16Threads(b *testing.B) {
benchmarkRequestThroughput(16, b)
}
func BenchmarkRequestThroughput32Threads(b *testing.B) {
benchmarkRequestThroughput(32, b)
}
func BenchmarkRequestThroughput64Threads(b *testing.B) {
benchmarkRequestThroughput(64, b)
}
func BenchmarkRequestThroughput128Threads(b *testing.B) {
benchmarkRequestThroughput(128, b)
}
func benchmarkRequestThroughput(threads int, b *testing.B) {
// Create the service handler
handler := new(requestTestHandler)
// Register a new service to the relay
serv, err := Register(config.relay, config.cluster, handler, nil)
if err != nil {
b.Fatalf("registration failed: %v.", err)
}
defer serv.Unregister()
// Create the thread pool with the concurrent requests
workers := pool.NewThreadPool(threads)
for i := 0; i < b.N; i++ {
workers.Schedule(func() {
if _, err := handler.conn.Request(config.cluster, []byte{byte(i)}, 10*time.Second); err != nil {
b.Fatalf("request failed: %v.", err)
}
})
}
// Reset timer and benchmark the message transfer
b.ResetTimer()
workers.Start()
workers.Terminate(false)
// Stop the timer (don't measure deferred cleanup)
b.StopTimer()
}