-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcorrectable_test.go
More file actions
230 lines (208 loc) · 5.65 KB
/
correctable_test.go
File metadata and controls
230 lines (208 loc) · 5.65 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
package gorums_test
import (
"fmt"
"testing"
"time"
"github.com/relab/gorums"
"github.com/relab/gorums/internal/testutils/mock"
pb "google.golang.org/protobuf/types/known/wrapperspb"
)
func TestCorrectableQuorumCall(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,
)
// Wait for 2 responses
corr := responses.Correctable(2)
<-corr.Watch(2)
reply, level, err := corr.Get()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if level < 2 {
t.Errorf("Expected level >= 2, got %d", level)
}
if reply.GetValue() != "echo: test" {
t.Errorf("Expected 'echo: test', got '%s'", reply.GetValue())
}
}
func TestCorrectableQuorumCallStream(t *testing.T) {
tests := []struct {
name string
threshold int
wantLevel int
wantValue string
}{
{
name: "Level1",
threshold: 1,
wantLevel: 1,
wantValue: "echo: test-1",
},
{
name: "Level2",
threshold: 2,
wantLevel: 2,
wantValue: "echo: test-2",
},
{
name: "Level3",
threshold: 3,
wantLevel: 3,
wantValue: "echo: test-3",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Using StreamServerFn which sends 3 responses:
// "echo: val-1", "echo: val-2", "echo: val-3"
config := gorums.TestConfiguration(t, 3, gorums.StreamServerFn)
ctx := gorums.TestContext(t, 2*time.Second)
responses := gorums.QuorumCallStream[*pb.StringValue, *pb.StringValue](
config.Context(ctx),
pb.String("test"),
mock.Stream,
)
corr := responses.Correctable(tt.threshold)
select {
case <-corr.Watch(tt.threshold):
case <-time.After(1 * time.Second):
t.Fatalf("Timeout waiting for level %d", tt.threshold)
}
reply, level, err := corr.Get()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if level < tt.threshold {
t.Errorf("Expected level >= %d, got %d", tt.threshold, level)
}
// Note: The value might be from a higher level if updates happened fast,
// but for this test we expect at least the threshold level's value or higher.
// With StreamServerFn, level N returns "echo: test-N".
// Since we wait for level N, we expect at least "echo: test-N".
// Actually, StreamServerFn sends 1, then 2, then 3. We check for non-empty response.
if reply.GetValue() == "" {
t.Error("Expected non-empty response")
}
})
}
}
func TestCorrectableWatch(t *testing.T) {
config := gorums.TestConfiguration(t, 3, gorums.StreamServerFn)
ctx := gorums.TestContext(t, 2*time.Second)
responses := gorums.QuorumCallStream[*pb.StringValue, *pb.StringValue](
config.Context(ctx),
pb.String("test"),
mock.Stream,
)
corr := responses.Correctable(3)
// Watch for level 1
select {
case <-corr.Watch(1):
// Success
case <-time.After(1 * time.Second):
t.Fatal("Timeout waiting for level 1")
}
// Watch for level 3
select {
case <-corr.Watch(3):
// Success
case <-time.After(1 * time.Second):
t.Fatal("Timeout waiting for level 3")
}
}
func BenchmarkCorrectable(b *testing.B) { // skipcq: GO-R1005
for _, numNodes := range []int{3, 5, 7, 9} {
b.Run(fmt.Sprintf("QuorumCall/%d", numNodes), func(b *testing.B) {
config := gorums.TestConfiguration(b, numNodes, gorums.EchoServerFn)
cfgCtx := config.Context(b.Context())
threshold := numNodes/2 + 1
b.ReportAllocs()
for b.Loop() {
responses := gorums.QuorumCall[*pb.StringValue, *pb.StringValue](
cfgCtx,
pb.String("test"),
mock.TestMethod,
)
corr := responses.Correctable(threshold)
<-corr.Watch(threshold)
_, _, err := corr.Get()
if err != nil {
b.Fatalf("Correctable error: %v", err)
}
}
})
b.Run(fmt.Sprintf("QuorumCallIterator/%d", numNodes), func(b *testing.B) {
config := gorums.TestConfiguration(b, numNodes, gorums.EchoServerFn)
cfgCtx := config.Context(b.Context())
threshold := numNodes/2 + 1
b.ReportAllocs()
for b.Loop() {
responses := gorums.QuorumCall[*pb.StringValue, *pb.StringValue](
cfgCtx,
pb.String("test"),
mock.TestMethod,
)
count := 0
for resp := range responses.Results() {
if resp.Err == nil {
count++
if count >= threshold {
break
}
}
}
if count < threshold {
b.Fatalf("Iterator failed to reach threshold")
}
}
})
b.Run(fmt.Sprintf("QuorumCallStream/%d", numNodes), func(b *testing.B) {
config := gorums.TestConfiguration(b, numNodes, gorums.StreamBenchmarkServerFn)
cfgCtx := config.Context(b.Context())
threshold := numNodes/2 + 1
b.ReportAllocs()
for b.Loop() {
responses := gorums.QuorumCallStream[*pb.StringValue, *pb.StringValue](
cfgCtx,
pb.String("test"),
mock.Stream,
)
corr := responses.Correctable(threshold)
<-corr.Watch(threshold)
_, _, err := corr.Get()
if err != nil {
b.Fatalf("Correctable error: %v", err)
}
}
})
b.Run(fmt.Sprintf("QuorumCallStreamIterator/%d", numNodes), func(b *testing.B) {
config := gorums.TestConfiguration(b, numNodes, gorums.StreamBenchmarkServerFn)
cfgCtx := config.Context(b.Context())
threshold := numNodes/2 + 1
b.ReportAllocs()
for b.Loop() {
responses := gorums.QuorumCallStream[*pb.StringValue, *pb.StringValue](
cfgCtx,
pb.String("test"),
mock.Stream,
)
count := 0
for resp := range responses.Results() {
if resp.Err == nil {
count++
if count >= threshold {
break
}
}
}
if count < threshold {
b.Fatalf("Iterator failed to reach threshold")
}
}
})
}
}