-
Notifications
You must be signed in to change notification settings - Fork 14
/
config_test.go
228 lines (210 loc) · 5.69 KB
/
config_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
package gorums_test
import (
"context"
"sync"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/relab/gorums"
"github.com/relab/gorums/tests/dummy"
)
func TestNewConfigurationNodeList(t *testing.T) {
mgr := gorums.NewRawManager(gorums.WithNoConnect())
cfg, err := gorums.NewRawConfiguration(mgr, gorums.WithNodeList(nodes))
if err != nil {
t.Fatal(err)
}
if cfg.Size() != len(nodes) {
t.Errorf("cfg.Size() = %d, expected %d", cfg.Size(), len(nodes))
}
contains := func(nodes []*gorums.RawNode, addr string) bool {
for _, node := range nodes {
if addr == node.Address() {
return true
}
}
return false
}
cfgNodes := cfg.Nodes()
for _, n := range nodes {
if !contains(cfgNodes, n) {
t.Errorf("cfg.Nodes() = %v, expected %s", cfgNodes, n)
}
}
if mgr.Size() != len(nodes) {
t.Errorf("mgr.Size() = %d, expected %d", mgr.Size(), len(nodes))
}
mgrNodes := cfg.Nodes()
for _, n := range nodes {
if !contains(mgrNodes, n) {
t.Errorf("mgr.Nodes() = %v, expected %s", mgrNodes, n)
}
}
}
func TestNewConfigurationNodeMap(t *testing.T) {
mgr := gorums.NewRawManager(gorums.WithNoConnect())
cfg, err := gorums.NewRawConfiguration(mgr, gorums.WithNodeMap(nodeMap))
if err != nil {
t.Fatal(err)
}
if cfg.Size() != len(nodeMap) {
t.Errorf("cfg.Size() = %d, expected %d", cfg.Size(), len(nodeMap))
}
for _, node := range cfg.Nodes() {
if nodeMap[node.Address()] != node.ID() {
t.Errorf("cfg.Nodes()[%s] = %d, expected %d", node.Address(), node.ID(), nodeMap[node.Address()])
}
}
if mgr.Size() != len(nodeMap) {
t.Errorf("mgr.Size() = %d, expected %d", mgr.Size(), len(nodeMap))
}
for _, node := range mgr.Nodes() {
if nodeMap[node.Address()] != node.ID() {
t.Errorf("mgr.Nodes()[%s] = %d, expected %d", node.Address(), node.ID(), nodeMap[node.Address()])
}
}
}
func TestNewConfigurationNodeIDs(t *testing.T) {
mgr := gorums.NewRawManager(gorums.WithNoConnect())
c1, err := gorums.NewRawConfiguration(mgr, gorums.WithNodeList(nodes))
if err != nil {
t.Fatal(err)
}
if c1.Size() != len(nodes) {
t.Errorf("c1.Size() = %d, expected %d", c1.Size(), len(nodes))
}
// Identical configurations c1 == c2
nodeIDs := c1.NodeIDs()
c2, err := gorums.NewRawConfiguration(mgr, gorums.WithNodeIDs(nodeIDs))
if err != nil {
t.Fatal(err)
}
if c2.Size() != len(nodes) {
t.Errorf("c2.Size() = %d, expected %d", c2.Size(), len(nodes))
}
if diff := cmp.Diff(c1, c2); diff != "" {
t.Errorf("Expected same configurations, but got (-c1 +c2):\n%s", diff)
}
// Configuration with one less node |c3| == |c1| - 1
c3, err := gorums.NewRawConfiguration(mgr, gorums.WithNodeIDs(nodeIDs[:len(nodeIDs)-1]))
if err != nil {
t.Fatal(err)
}
if c3.Size() != len(nodes)-1 {
t.Errorf("c3.Size() = %d, expected %d", c3.Size(), len(nodes)-1)
}
if diff := cmp.Diff(c1, c3); diff == "" {
t.Errorf("Expected different configurations, but found no difference")
}
}
func TestNewConfigurationAnd(t *testing.T) {
mgr := gorums.NewRawManager(gorums.WithNoConnect())
c1, err := gorums.NewRawConfiguration(mgr, gorums.WithNodeList(nodes))
if err != nil {
t.Fatal(err)
}
c2Nodes := []string{"127.0.0.1:8080"}
c2, err := gorums.NewRawConfiguration(mgr, gorums.WithNodeList(c2Nodes))
if err != nil {
t.Fatal(err)
}
// Add newNodes to c1, giving a new c3 with a total of 3+2 nodes
newNodes := []string{"127.0.0.1:9083", "127.0.0.1:9084"}
c3, err := gorums.NewRawConfiguration(
mgr,
c1.WithNewNodes(gorums.WithNodeList(newNodes)),
)
if err != nil {
t.Fatal(err)
}
if c3.Size() != len(nodes)+len(newNodes) {
t.Errorf("c3.Size() = %d, expected %d", c3.Size(), len(nodes)+len(newNodes))
}
// Combine c2 to c1, giving a new c4 with a total of 3+1 nodes
c4, err := gorums.NewRawConfiguration(
mgr,
c1.And(c2),
)
if err != nil {
t.Fatal(err)
}
if c4.Size() != len(nodes)+len(c2Nodes) {
t.Errorf("c4.Size() = %d, expected %d", c4.Size(), len(nodes)+len(c2Nodes))
}
// Combine c2 to c4, giving a new c5 with a total of 4 nodes
// c4 already contains all nodes from c2 (see above): c4 = c1+c2
// c5 should essentially just be a copy of c4 (ignoring duplicates from c2)
c5, err := gorums.NewRawConfiguration(
mgr,
c4.And(c2),
)
if err != nil {
t.Fatal(err)
}
if c5.Size() != c4.Size() {
t.Errorf("c5.Size() = %d, expected %d", c5.Size(), c4.Size())
}
}
func TestNewConfigurationExcept(t *testing.T) {
mgr := gorums.NewRawManager(gorums.WithNoConnect())
c1, err := gorums.NewRawConfiguration(mgr, gorums.WithNodeList(nodes))
if err != nil {
t.Fatal(err)
}
c2, err := gorums.NewRawConfiguration(
mgr,
c1.WithoutNodes(c1[0].ID()),
)
if err != nil {
t.Fatal(err)
}
if c2.Size() != c1.Size()-1 {
t.Errorf("c2.Size() = %d, expected %d", c2.Size(), c1.Size()-1)
}
newNodes := []string{"127.0.0.1:9083", "127.0.0.1:9084"}
c3, err := gorums.NewRawConfiguration(
mgr,
c1.WithNewNodes(gorums.WithNodeList(newNodes)),
)
if err != nil {
t.Fatal(err)
}
c4, err := gorums.NewRawConfiguration(
mgr,
c3.Except(c1),
)
if err != nil {
t.Fatal(err)
}
if c4.Size() != c3.Size()-c1.Size() {
t.Errorf("c4.Size() = %d, expected %d", c4.Size(), c3.Size()-c1.Size())
}
}
func TestConfigConcurrentAccess(t *testing.T) {
addrs, teardown := gorums.TestSetup(t, 1, func(_ int) gorums.ServerIface {
return initServer()
})
defer teardown()
mgr := gorumsTestMgr()
cfg, err := mgr.NewConfiguration(gorums.WithNodeList(addrs))
if err != nil {
t.Fatal(err)
}
errCh := make(chan error, 2)
var wg sync.WaitGroup
for j := 0; j < 2; j++ {
wg.Add(1)
go func() {
node := cfg.Nodes()[0]
_, err := node.Test(context.Background(), &dummy.Empty{})
if err != nil {
errCh <- err
}
wg.Done()
}()
}
wg.Wait()
close(errCh)
for err := range errCh {
t.Error(err)
}
}