-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
339 lines (275 loc) · 7.25 KB
/
client_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
package pot
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"sync"
"testing"
"time"
"cloud.google.com/go/storage"
"google.golang.org/api/iterator"
)
type testStruct struct {
ID string `json:"id"`
Age int `json:"age"`
Path []string `json:"path"`
NiceThings []struct {
Name string `json:"name"`
}
}
func (t testStruct) Key() string {
return t.ID
}
func newTestAPIClient() *Client[testStruct] {
return NewClient[testStruct]("http://localhost:8080")
}
func cleanup(t *testing.T, testPath string) {
t.Helper()
gcs, err := storage.NewClient(context.Background())
if err != nil {
t.Fatal(err)
}
// cleanup before the test
bucket := gcs.Bucket("petomalina-pot-tests")
objs := bucket.Objects(context.Background(), &storage.Query{Prefix: testPath})
for {
obj, err := objs.Next()
if err == iterator.Done {
break
}
if err != nil {
t.Fatal(err)
}
if err := bucket.Object(obj.Name).Delete(context.Background()); err != nil {
t.Fatal(err)
}
}
}
func TestListPaths(t *testing.T) {
testPath := "test/path"
cleanup(t, testPath)
client := newTestAPIClient()
// first make sure there is nothing stored on the path
res, err := client.ListPaths(testPath)
if err != nil {
t.Fatal(err)
}
if len(res.Paths) != 0 {
t.Fatalf("expected no paths, got %v", res)
}
// store an object on the path
_, err = client.Create(testPath, []testStruct{{ID: "test"}}, WithNoRewrite(time.Minute))
if err != nil {
t.Fatal(err)
}
// get the object from the path
res, err = client.ListPaths(testPath)
if err != nil {
t.Fatal(err)
}
if len(res.Paths) != 1 {
t.Fatalf("expected 1 path, got %v", res)
}
if res.Paths[0] != testPath {
t.Fatalf("expected test, got %v", res.Paths)
}
}
func TestFlow(t *testing.T) {
testPath := "test/path"
cleanup(t, testPath)
// run the test
client := newTestAPIClient()
// first make sure there is nothing stored on the path
content, err := client.Get(testPath)
if err != nil {
t.Fatal(err)
}
if len(content) != 0 {
t.Fatalf("expected no content, got %v", content)
}
// store an object on the path
obj := testStruct{
ID: "test",
Age: 10,
Path: []string{
"test", "path", "to", "test",
},
NiceThings: []struct {
Name string `json:"name"`
}{
{Name: "test"},
{Name: "test2"},
},
}
_, err = client.Create(testPath, []testStruct{obj})
if err != nil {
t.Fatal(err)
}
// get the object from the path
content, err = client.Get(testPath)
if err != nil {
t.Fatal(err)
}
if len(content) != 1 {
t.Fatalf("expected 1 object, got %v", content)
}
objMarshal, err := json.Marshal(obj)
if err != nil {
t.Fatal(err)
}
contentMarshal, err := json.Marshal(content["test"])
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(objMarshal, contentMarshal) {
t.Fatalf("expected %v, got %v", obj, content)
}
// remove the object from the path
err = client.Remove(testPath, obj.ID)
if err != nil {
t.Fatal(err)
}
// get the object from the path
content, err = client.Get(testPath)
if err != nil {
t.Fatal(err)
}
if len(content) != 0 {
t.Fatalf("expected no content, got %v", content)
}
}
func TestElection(t *testing.T) {
testPath := "test/path"
cleanup(t, testPath)
client := newTestAPIClient()
// Run 5 different "clients" that will try to create the same object.
// Only one of them should succeed, while all others should receive
// the http.StatusLocked error and the content of the first client.
errs := make(chan error, 100)
wg := sync.WaitGroup{}
mut := sync.Mutex{}
ageIndex := 0
for i := 0; i < 5; i++ {
wg.Add(1)
go func(i int) {
client := newTestAPIClient()
defer wg.Done()
obj := testStruct{
ID: "test",
Age: i,
}
_, err := client.Create(testPath, []testStruct{obj}, WithNoRewrite(time.Minute))
if err != nil {
errs <- err
return
}
slog.Info("election winner", slog.Int("p", i))
mut.Lock()
ageIndex = i
mut.Unlock()
}(i)
}
wg.Wait()
close(errs)
i := 0
for err := range errs {
if !errors.Is(err, ErrNoRewriteViolated) {
t.Fatalf("expected no rewrite violation error, got %v", err)
}
i++
}
if i != 4 {
t.Fatalf("expected 4 errors, got %v", i)
}
// get the object from the path
content, err := client.Get(testPath)
if err != nil {
t.Fatal(err)
}
if len(content) != 1 {
t.Fatalf("expected 1 object, got %v", content)
}
if content["test"].Age != ageIndex {
t.Fatalf("expected %v, got %v", ageIndex, content["test"].Age)
}
}
func TestReElection(t *testing.T) {
testPath := "test/path"
cleanup(t, testPath)
wg := sync.WaitGroup{}
wg.Add(2)
errs := make(chan error, 100)
// testFn is a function simulating single client that tries to get the lock on the path
// The test runs following steps:
// - both clients try to get the lock on the path
// - one of them gets the lock and updates the object, becoming primary
// the other one fails to get the lock and receives ErrNoRewriteViolated error
// - primary client updates the object again, ensuring it can update the object
// secondary client tries to update the object, but fails with ErrNoRewriteViolated
// - primary client waits for the lock to expire
// - secondary client tries to get the lock and succeeds
testFn := func(id string) {
defer wg.Done()
client := newTestAPIClient()
// primary flags the client as the one holding the lock
primary := true
// try to get the lock on both, one of these processes must get the violation error
_, err := client.Create(testPath, []testStruct{{ID: "leader", Age: 1}}, WithNoRewrite(time.Second*5))
if err != nil {
if errors.Is(err, ErrNoRewriteViolated) {
primary = false
} else {
errs <- fmt.Errorf("secondary must fail on rewrite violation, but failed on: %w", err)
}
}
slog.Info("election result", slog.String("p", id), slog.Bool("primary", primary))
// try updating once more to make sure primary can update while secondary can't
_, err = client.Create(testPath, []testStruct{{ID: "leader", Age: 2}}, WithNoRewrite(time.Second*5))
if err != nil {
if errors.Is(err, ErrNoRewriteViolated) && primary {
errs <- fmt.Errorf("primary failed to update the object through ownership: %w", err)
}
if !errors.Is(err, ErrNoRewriteViolated) && !primary {
errs <- fmt.Errorf("secondary must fail on rewrite violation, but failed on: %w", err)
}
}
time.Sleep(time.Second * 5)
if !primary {
_, err = client.Create(testPath, []testStruct{{ID: "leader", Age: 1}}, WithNoRewrite(time.Second*5))
if err != nil {
errs <- fmt.Errorf("secondary failed to get the lock after desired time")
}
}
}
go testFn("1")
go testFn("2")
wg.Wait()
close(errs)
var errslice []error
for err := range errs {
errslice = append(errslice, err)
}
if len(errslice) > 0 {
t.Fatal(errslice)
}
}
func TestNoRewriteDuration(t *testing.T) {
const testPath = "test/path"
cleanup(t, testPath)
client := newTestAPIClient()
_, err := client.Create(testPath, []testStruct{{ID: "test"}}, WithNoRewrite(time.Second*10))
if err != nil {
t.Fatal(err)
}
_, err = client.Create(testPath, []testStruct{{ID: "test"}}, WithNoRewrite(time.Second*10))
if err != nil {
t.Fatal(err)
}
_, err = client.Create(testPath, []testStruct{{ID: "test"}}, WithNoRewrite(time.Second*10))
if err != nil {
t.Fatal(err)
}
}