-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathquorumcall_test.go
More file actions
464 lines (413 loc) · 12.6 KB
/
quorumcall_test.go
File metadata and controls
464 lines (413 loc) · 12.6 KB
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package gorums_test
import (
"errors"
"fmt"
"slices"
"testing"
"testing/synctest"
"time"
"github.com/relab/gorums"
"github.com/relab/gorums/internal/testutils/mock"
pb "google.golang.org/protobuf/types/known/wrapperspb"
)
// checkQuorumCall validates a quorum call's outcome against expectations.
// If wantErr is nil, it expects success and verifies no error occurred.
// If wantErr is non-nil, it validates that the error matches and optionally checks
// the number of node errors in QuorumCallError (if expectedNodeErrors is provided).
func checkQuorumCall(t *testing.T, gotErr, wantErr error, expectedNodeErrors ...int) bool {
t.Helper()
// Handle expected error case
if wantErr != nil {
if gotErr == nil {
t.Errorf("Expected error %v, got nil", wantErr)
return false
}
if !errors.Is(gotErr, wantErr) {
t.Errorf("Expected error %v, got %v", wantErr, gotErr)
return false
}
// Validate QuorumCallError details if expectedNodeErrors provided
if len(expectedNodeErrors) > 0 {
var qcErr gorums.QuorumCallError
if errors.As(gotErr, &qcErr) && qcErr.NodeErrors() != expectedNodeErrors[0] {
t.Errorf("Expected %d node errors, got %d", expectedNodeErrors[0], qcErr.NodeErrors())
return false
}
}
return true
}
// Handle expected success case
if gotErr != nil {
t.Errorf("QuorumCall failed: %v", gotErr)
return false
}
return true
}
func TestQuorumCall(t *testing.T) {
// type alias short hand for the responses type
type respType = *gorums.Responses[*pb.StringValue]
tests := []struct {
name string
call func(respType) (*pb.StringValue, error)
numNodes int
wantValue string
}{
{
name: "Majority",
call: respType.Majority,
numNodes: 3,
wantValue: "echo: test",
},
{
name: "First",
call: respType.First,
numNodes: 3,
wantValue: "echo: test",
},
{
name: "All",
call: respType.All,
numNodes: 3,
wantValue: "echo: test",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config := gorums.TestConfiguration(t, tt.numNodes, gorums.EchoServerFn)
ctx := gorums.TestContext(t, 2*time.Second)
responses := gorums.QuorumCall[*pb.StringValue, *pb.StringValue](
config.Context(ctx),
pb.String("test"),
mock.TestMethod,
)
result, err := tt.call(responses)
if !checkQuorumCall(t, err, nil) {
return
}
if result.GetValue() != tt.wantValue {
t.Errorf("Expected %q, got %q", tt.wantValue, result.GetValue())
}
})
}
}
func TestQuorumCallPartialFailures(t *testing.T) {
// Function type for the call to test
type callInfo struct {
name string
callFunc func(*gorums.ConfigContext, *pb.StringValue) error
}
const numServers = 3
type respType = *gorums.Responses[*pb.StringValue]
// Helper to create QuorumCall variants
quorumcall := func(name string, aggregateFunc func(respType) (*pb.StringValue, error)) callInfo {
return callInfo{
name: "QuorumCall/" + name,
callFunc: func(ctx *gorums.ConfigContext, req *pb.StringValue) error {
_, err := aggregateFunc(gorums.QuorumCall[*pb.StringValue, *pb.StringValue](ctx, req, mock.TestMethod))
return err
},
}
}
// Helper to create Multicast variants
multicast := func(name string, opts ...gorums.CallOption) callInfo {
return callInfo{
name: "Multicast/" + name,
callFunc: func(ctx *gorums.ConfigContext, req *pb.StringValue) error {
return gorums.Multicast(ctx, req, mock.TestMethod, opts...)
},
}
}
tests := []struct {
call callInfo
failing int // number of servers to stop (0 to 3)
wantErr error
}{
// Multicast: Fails if ANY node fails
{multicast("Wait"), 0, nil},
{multicast("Wait"), 1, gorums.ErrSendFailure},
{multicast("Wait"), 2, gorums.ErrSendFailure},
{multicast("Wait"), 3, gorums.ErrSendFailure},
// Multicast with IgnoreErrors: Should not return error even if nodes fail
{multicast("IgnoreErrors", gorums.IgnoreErrors()), 0, nil},
{multicast("IgnoreErrors", gorums.IgnoreErrors()), 3, nil},
// QuorumCall Majority (2/3): Tolerates 1 failure
{quorumcall("Majority", respType.Majority), 0, nil},
{quorumcall("Majority", respType.Majority), 1, nil},
{quorumcall("Majority", respType.Majority), 2, gorums.ErrIncomplete},
{quorumcall("Majority", respType.Majority), 3, gorums.ErrIncomplete},
// QuorumCall First (1/3): Tolerates 2 failures
{quorumcall("First", respType.First), 0, nil},
{quorumcall("First", respType.First), 1, nil},
{quorumcall("First", respType.First), 2, nil},
{quorumcall("First", respType.First), 3, gorums.ErrIncomplete},
// QuorumCall All (3/3): Fails if any node fails
{quorumcall("All", respType.All), 0, nil},
{quorumcall("All", respType.All), 1, gorums.ErrIncomplete},
{quorumcall("All", respType.All), 2, gorums.ErrIncomplete},
{quorumcall("All", respType.All), 3, gorums.ErrIncomplete},
}
for _, tt := range tests {
testName := fmt.Sprintf("%s/fail=%d", tt.call.name, tt.failing)
t.Run(testName, func(t *testing.T) {
var stopNodes func(...int)
config := gorums.TestConfiguration(t, numServers, gorums.DefaultTestServer, gorums.WithStopFunc(t, &stopNodes))
ctx := config.Context(t.Context())
req := pb.String("test")
// Warmup to ensure connections
if err := tt.call.callFunc(ctx, req); err != nil {
t.Fatalf("Warmup failed: %v", err)
}
if tt.failing > 0 {
// Collect indices to stop
stopNodes(slices.Collect(gorums.Range(tt.failing))...)
time.Sleep(50 * time.Millisecond)
}
// Execute test calls after stopping nodes, if any
var err error
for range 5 {
err = tt.call.callFunc(ctx, req)
if tt.wantErr != nil && err != nil {
break // failed as expected
}
if tt.wantErr == nil && err != nil {
break // failed unexpected
}
time.Sleep(10 * time.Millisecond)
}
// Validate error expectations using helper
if !checkQuorumCall(t, err, tt.wantErr, tt.failing) {
return
}
})
}
}
// TestQuorumCallCustomAggregation tests custom response aggregation
func TestQuorumCallCustomAggregation(t *testing.T) {
config := gorums.TestConfiguration(t, 3, gorums.DefaultTestServer) // uses default server that returns (i+1)*10
ctx := gorums.TestContext(t, 2*time.Second)
responses := gorums.QuorumCall[*pb.Int32Value, *pb.Int32Value](
config.Context(ctx),
pb.Int32(0),
mock.GetValueMethod,
)
// Custom aggregation: sum all responses
// Default server returns 10, 20, 30 for nodes 0, 1, 2
var sum int32
for r := range responses.Seq().IgnoreErrors() {
sum += r.Value.GetValue()
}
if sum != 60 { // 10 + 20 + 30 = 60
t.Errorf("Expected sum 60, got %d", sum)
}
}
// TestQuorumCallCollectAll tests collecting all responses
func TestQuorumCallCollectAll(t *testing.T) {
config := gorums.TestConfiguration(t, 3, gorums.EchoServerFn)
ctx := gorums.TestContext(t, 2*time.Second)
responses := gorums.QuorumCall[*pb.StringValue, *pb.StringValue](
config.Context(ctx),
pb.String("test"),
mock.TestMethod,
)
collected := responses.CollectAll()
if len(collected) != 3 {
t.Errorf("Expected 3 responses, got %d", len(collected))
}
}
func TestQuorumCallSynctest(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
// Create configuration inside synctest bubble for controlled time
config := gorums.TestConfiguration(t, 3, gorums.EchoServerFn, gorums.SkipGoleak())
ctx := gorums.TestContext(t, 2*time.Second)
cfgCtx := config.Context(ctx)
responses := gorums.QuorumCall[*pb.StringValue, *pb.StringValue](
cfgCtx,
pb.String("synctest-demo"),
mock.TestMethod,
)
resp, err := responses.Majority()
if err != nil {
t.Fatalf("QuorumCall failed: %v", err)
}
if resp.GetValue() != "echo: synctest-demo" {
t.Errorf("Expected 'echo: synctest-demo', got '%s'", resp.GetValue())
}
t.Log("Gorums basic operations work with synctest!")
})
}
func TestQuorumCallAsyncSynctest(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
config := gorums.TestConfiguration(t, 5, gorums.EchoServerFn, gorums.SkipGoleak())
ctx := gorums.TestContext(t, 3*time.Second)
cfgCtx := config.Context(ctx)
// Test async operations
future1 := gorums.QuorumCall[*pb.StringValue, *pb.StringValue](
cfgCtx,
pb.String("async1"),
mock.TestMethod,
).AsyncMajority()
future2 := gorums.QuorumCall[*pb.StringValue, *pb.StringValue](
cfgCtx,
pb.String("async2"),
mock.TestMethod,
).AsyncMajority()
// Wait for both to complete
reply1, err1 := future1.Get()
reply2, err2 := future2.Get()
if err1 != nil {
t.Fatalf("Future1 error: %v", err1)
}
if err2 != nil {
t.Fatalf("Future2 error: %v", err2)
}
if reply1.GetValue() != "echo: async1" {
t.Errorf("Expected 'echo: async1', got '%s'", reply1.GetValue())
}
if reply2.GetValue() != "echo: async2" {
t.Errorf("Expected 'echo: async2', got '%s'", reply2.GetValue())
}
t.Log("Gorums async operations work with synctest!")
})
}
// BenchmarkQuorumCallTerminalMethods benchmarks the built-in terminal methods with real servers.
func BenchmarkQuorumCallTerminalMethods(b *testing.B) {
for _, numNodes := range []int{3, 5, 7, 9, 13, 17, 19} {
config := gorums.TestConfiguration(b, numNodes, gorums.EchoServerFn)
cfgCtx := config.Context(b.Context())
b.Run(fmt.Sprintf("Majority/%d", numNodes), func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
resp, err := gorums.QuorumCall[*pb.StringValue, *pb.StringValue](
cfgCtx,
pb.String("test"),
mock.TestMethod,
).Majority()
if err != nil {
b.Fatalf("QuorumCall error: %v", err)
}
_ = resp.GetValue()
}
})
b.Run(fmt.Sprintf("Threshold/%d", numNodes), func(b *testing.B) {
b.ReportAllocs()
threshold := numNodes/2 + 1
for b.Loop() {
resp, err := gorums.QuorumCall[*pb.StringValue, *pb.StringValue](
cfgCtx,
pb.String("test"),
mock.TestMethod,
).Threshold(threshold)
if err != nil {
b.Fatalf("QuorumCall error: %v", err)
}
_ = resp.GetValue()
}
})
b.Run(fmt.Sprintf("First/%d", numNodes), func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
resp, err := gorums.QuorumCall[*pb.StringValue, *pb.StringValue](
cfgCtx,
pb.String("test"),
mock.TestMethod,
).First()
if err != nil {
b.Fatalf("QuorumCall error: %v", err)
}
_ = resp.GetValue()
}
})
b.Run(fmt.Sprintf("All/%d", numNodes), func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
resp, err := gorums.QuorumCall[*pb.StringValue, *pb.StringValue](
cfgCtx,
pb.String("test"),
mock.TestMethod,
).All()
if err != nil {
b.Fatalf("QuorumCall error: %v", err)
}
_ = resp.GetValue()
}
})
}
}
// BenchmarkQuorumCall benchmarks custom aggregation using different iterator patterns.
func BenchmarkQuorumCall(b *testing.B) {
for _, numNodes := range []int{3, 5, 7, 9, 13, 17, 19} {
config := gorums.TestConfiguration(b, numNodes, gorums.EchoServerFn)
cfgCtx := config.Context(b.Context())
// Using CollectAll and then checking quorum
b.Run(fmt.Sprintf("CollectAllThenCheck/%d", numNodes), func(b *testing.B) {
b.ReportAllocs()
quorum := numNodes/2 + 1
for b.Loop() {
responses := gorums.QuorumCall[*pb.StringValue, *pb.StringValue](
cfgCtx,
pb.String("test"),
mock.TestMethod,
)
replies := responses.CollectAll()
if len(replies) < quorum {
b.Fatalf("not enough replies: %d < %d", len(replies), quorum)
}
// Return first reply
for _, r := range replies {
_ = r.GetValue()
break
}
}
})
// Using CollectN for early termination
b.Run(fmt.Sprintf("CollectN/%d", numNodes), func(b *testing.B) {
b.ReportAllocs()
quorum := numNodes/2 + 1
for b.Loop() {
responses := gorums.QuorumCall[*pb.StringValue, *pb.StringValue](
cfgCtx,
pb.String("test"),
mock.TestMethod,
)
replies := responses.CollectN(quorum)
if len(replies) < quorum {
b.Fatalf("not enough replies: %d < %d", len(replies), quorum)
}
// Return first reply
for _, r := range replies {
_ = r.GetValue()
break
}
}
})
// Using manual iteration with early return
b.Run(fmt.Sprintf("Iterator/%d", numNodes), func(b *testing.B) {
b.ReportAllocs()
quorum := numNodes/2 + 1
for b.Loop() {
responses := gorums.QuorumCall[*pb.StringValue, *pb.StringValue](
cfgCtx,
pb.String("test"),
mock.TestMethod,
)
var firstResp *pb.StringValue
var count int
for result := range responses.Seq() {
if result.Err == nil {
count++
if firstResp == nil {
firstResp = result.Value
}
if count >= quorum {
break
}
}
}
if count < quorum {
b.Fatalf("not enough responses: %d < %d", count, quorum)
}
_ = firstResp.GetValue()
}
})
}
}