-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathasync_test.go
More file actions
122 lines (109 loc) · 2.8 KB
/
async_test.go
File metadata and controls
122 lines (109 loc) · 2.8 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
package gorums_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/relab/gorums"
"github.com/relab/gorums/internal/testutils/mock"
pb "google.golang.org/protobuf/types/known/wrapperspb"
)
func TestAsync(t *testing.T) {
// a type alias short hand for the responses type
type respType = *gorums.Responses[*pb.StringValue]
tests := []struct {
name string
call func(respType) *gorums.Async[*pb.StringValue]
numNodes int
wantValue string
wantErr bool
}{
{
name: "Majority",
call: respType.AsyncMajority,
numNodes: 3,
wantValue: "echo: test",
},
{
name: "First",
call: respType.AsyncFirst,
numNodes: 3,
wantValue: "echo: test",
},
{
name: "All",
call: respType.AsyncAll,
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,
)
future := tt.call(responses)
reply, err := future.Get()
if !checkQuorumCall(t, err, nil) {
return
}
if reply.GetValue() != tt.wantValue {
t.Errorf("Expected %q, got %q", tt.wantValue, reply.GetValue())
}
})
}
}
func TestAsync_Error(t *testing.T) {
// Use a configuration with no servers to force an error (or timeout)
config := gorums.TestConfiguration(t, 3, gorums.EchoServerFn)
ctx, cancel := context.WithCancel(context.Background())
cancel() // Cancel immediately
responses := gorums.QuorumCall[*pb.StringValue, *pb.StringValue](
config.Context(ctx),
pb.String("test"),
mock.TestMethod,
)
future := responses.AsyncMajority()
_, err := future.Get()
if err == nil {
t.Error("Expected error, got nil")
}
}
func BenchmarkAsyncQuorumCall(b *testing.B) {
for _, numNodes := range []int{3, 5, 7, 9} {
config := gorums.TestConfiguration(b, numNodes, gorums.EchoServerFn)
cfgCtx := config.Context(b.Context())
b.Run(fmt.Sprintf("AsyncMajority/%d", numNodes), func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
future := gorums.QuorumCall[*pb.StringValue, *pb.StringValue](
cfgCtx,
pb.String("test"),
mock.TestMethod,
).AsyncMajority()
_, err := future.Get()
if err != nil {
b.Fatalf("AsyncMajority error: %v", err)
}
}
})
// Compare with blocking Majority
b.Run(fmt.Sprintf("BlockingMajority/%d", numNodes), func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_, err := gorums.QuorumCall[*pb.StringValue, *pb.StringValue](
cfgCtx,
pb.String("test"),
mock.TestMethod,
).Majority()
if err != nil {
b.Fatalf("Majority error: %v", err)
}
}
})
}
}