-
Notifications
You must be signed in to change notification settings - Fork 126
/
suite.go
527 lines (451 loc) · 15.7 KB
/
suite.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
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
package graphtest
import (
"fmt"
"math/big"
"sort"
"sync"
"time"
"github.com/PacktPublishing/Hands-On-Software-Engineering-with-Golang/Chapter06/linkgraph/graph"
"github.com/google/uuid"
"golang.org/x/xerrors"
gc "gopkg.in/check.v1"
)
// SuiteBase defines a re-usable set of graph-related tests that can
// be executed against any type that implements graph.Graph.
type SuiteBase struct {
g graph.Graph
}
// SetGraph configures the test-suite to run all tests against g.
func (s *SuiteBase) SetGraph(g graph.Graph) {
s.g = g
}
// TestUpsertLink verifies the link upsert logic.
func (s *SuiteBase) TestUpsertLink(c *gc.C) {
// Create a new link
original := &graph.Link{
URL: "https://example.com",
RetrievedAt: time.Now().Add(-10 * time.Hour),
}
err := s.g.UpsertLink(original)
c.Assert(err, gc.IsNil)
c.Assert(original.ID, gc.Not(gc.Equals), uuid.Nil, gc.Commentf("expected a linkID to be assigned to the new link"))
// Update existing link with a newer timestamp and different URL
accessedAt := time.Now().Truncate(time.Second).UTC()
existing := &graph.Link{
ID: original.ID,
URL: "https://example.com",
RetrievedAt: accessedAt,
}
err = s.g.UpsertLink(existing)
c.Assert(err, gc.IsNil)
c.Assert(existing.ID, gc.Equals, original.ID, gc.Commentf("link ID changed while upserting"))
stored, err := s.g.FindLink(existing.ID)
c.Assert(err, gc.IsNil)
c.Assert(stored.RetrievedAt, gc.Equals, accessedAt, gc.Commentf("last accessed timestamp was not updated"))
// Attempt to insert a new link whose URL matches an existing link with
// and provide an older accessedAt value
sameURL := &graph.Link{
URL: existing.URL,
RetrievedAt: time.Now().Add(-10 * time.Hour).UTC(),
}
err = s.g.UpsertLink(sameURL)
c.Assert(err, gc.IsNil)
c.Assert(sameURL.ID, gc.Equals, existing.ID)
stored, err = s.g.FindLink(existing.ID)
c.Assert(err, gc.IsNil)
c.Assert(stored.RetrievedAt, gc.Equals, accessedAt, gc.Commentf("last accessed timestamp was overwritten with an older value"))
// Create a new link and then attempt to update its URL to the same as
// an existing link.
dup := &graph.Link{
URL: "foo",
}
err = s.g.UpsertLink(dup)
c.Assert(err, gc.IsNil)
c.Assert(dup.ID, gc.Not(gc.Equals), uuid.Nil, gc.Commentf("expected a linkID to be assigned to the new link"))
}
// TestFindLink verifies the link lookup logic.
func (s *SuiteBase) TestFindLink(c *gc.C) {
// Create a new link
link := &graph.Link{
URL: "https://example.com",
RetrievedAt: time.Now().Truncate(time.Second).UTC(),
}
err := s.g.UpsertLink(link)
c.Assert(err, gc.IsNil)
c.Assert(link.ID, gc.Not(gc.Equals), uuid.Nil, gc.Commentf("expected a linkID to be assigned to the new link"))
// Lookup link by ID
other, err := s.g.FindLink(link.ID)
c.Assert(err, gc.IsNil)
c.Assert(other, gc.DeepEquals, link, gc.Commentf("lookup by ID returned the wrong link"))
// Lookup link by unknown ID
_, err = s.g.FindLink(uuid.Nil)
c.Assert(xerrors.Is(err, graph.ErrNotFound), gc.Equals, true)
}
// TestConcurrentLinkIterators verifies that multiple clients can concurrently
// access the store.
func (s *SuiteBase) TestConcurrentLinkIterators(c *gc.C) {
var (
wg sync.WaitGroup
numIterators = 10
numLinks = 100
)
for i := 0; i < numLinks; i++ {
link := &graph.Link{URL: fmt.Sprint(i)}
c.Assert(s.g.UpsertLink(link), gc.IsNil)
}
wg.Add(numIterators)
for i := 0; i < numIterators; i++ {
go func(id int) {
defer wg.Done()
itTagComment := gc.Commentf("iterator %d", id)
seen := make(map[string]bool)
it, err := s.partitionedLinkIterator(c, 0, 1, time.Now())
c.Assert(err, gc.IsNil, itTagComment)
defer func() {
c.Assert(it.Close(), gc.IsNil, itTagComment)
}()
for i := 0; it.Next(); i++ {
link := it.Link()
linkID := link.ID.String()
c.Assert(seen[linkID], gc.Equals, false, gc.Commentf("iterator %d saw same link twice", id))
seen[linkID] = true
}
c.Assert(seen, gc.HasLen, numLinks, itTagComment)
c.Assert(it.Error(), gc.IsNil, itTagComment)
c.Assert(it.Close(), gc.IsNil, itTagComment)
}(i)
}
doneCh := make(chan struct{})
go func() {
wg.Wait()
close(doneCh)
}()
select {
case <-doneCh:
// test completed successfully
case <-time.After(10 * time.Second):
c.Fatal("timed out waiting for test to complete")
}
}
// TestLinkIteratorTimeFilter verifies that the time-based filtering of the
// link iterator works as expected.
func (s *SuiteBase) TestLinkIteratorTimeFilter(c *gc.C) {
linkUUIDs := make([]uuid.UUID, 3)
linkInsertTimes := make([]time.Time, len(linkUUIDs))
for i := 0; i < len(linkUUIDs); i++ {
link := &graph.Link{URL: fmt.Sprint(i), RetrievedAt: time.Now()}
c.Assert(s.g.UpsertLink(link), gc.IsNil)
linkUUIDs[i] = link.ID
linkInsertTimes[i] = time.Now()
}
for i, t := range linkInsertTimes {
c.Logf("fetching links created before edge %d", i)
s.assertIteratedLinkIDsMatch(c, t, linkUUIDs[:i+1])
}
}
func (s *SuiteBase) assertIteratedLinkIDsMatch(c *gc.C, updatedBefore time.Time, exp []uuid.UUID) {
it, err := s.partitionedLinkIterator(c, 0, 1, updatedBefore)
c.Assert(err, gc.IsNil)
var got []uuid.UUID
for it.Next() {
got = append(got, it.Link().ID)
}
c.Assert(it.Error(), gc.IsNil)
c.Assert(it.Close(), gc.IsNil)
sort.Slice(got, func(l, r int) bool { return got[l].String() < got[r].String() })
sort.Slice(exp, func(l, r int) bool { return exp[l].String() < exp[r].String() })
c.Assert(got, gc.DeepEquals, exp)
}
// TestPartitionedLinkIterators verifies that the graph partitioning logic
// works as expected even when partitions contain an uneven number of items.
func (s *SuiteBase) TestPartitionedLinkIterators(c *gc.C) {
numLinks := 100
numPartitions := 10
for i := 0; i < numLinks; i++ {
c.Assert(s.g.UpsertLink(&graph.Link{URL: fmt.Sprint(i)}), gc.IsNil)
}
// Check with both odd and even partition counts to check for rounding-related bugs.
c.Assert(s.iteratePartitionedLinks(c, numPartitions), gc.Equals, numLinks)
c.Assert(s.iteratePartitionedLinks(c, numPartitions+1), gc.Equals, numLinks)
}
func (s *SuiteBase) iteratePartitionedLinks(c *gc.C, numPartitions int) int {
seen := make(map[string]bool)
for partition := 0; partition < numPartitions; partition++ {
it, err := s.partitionedLinkIterator(c, partition, numPartitions, time.Now())
c.Assert(err, gc.IsNil)
defer func() {
c.Assert(it.Close(), gc.IsNil)
}()
for it.Next() {
link := it.Link()
linkID := link.ID.String()
c.Assert(seen[linkID], gc.Equals, false, gc.Commentf("iterator returned same link in different partitions"))
seen[linkID] = true
}
c.Assert(it.Error(), gc.IsNil)
c.Assert(it.Close(), gc.IsNil)
}
return len(seen)
}
// TestUpsertEdge verifies the edge upsert logic.
func (s *SuiteBase) TestUpsertEdge(c *gc.C) {
// Create links
linkUUIDs := make([]uuid.UUID, 3)
for i := 0; i < 3; i++ {
link := &graph.Link{URL: fmt.Sprint(i)}
c.Assert(s.g.UpsertLink(link), gc.IsNil)
linkUUIDs[i] = link.ID
}
// Create a edge
edge := &graph.Edge{
Src: linkUUIDs[0],
Dst: linkUUIDs[1],
}
err := s.g.UpsertEdge(edge)
c.Assert(err, gc.IsNil)
c.Assert(edge.ID, gc.Not(gc.Equals), uuid.Nil, gc.Commentf("expected an edgeID to be assigned to the new edge"))
c.Assert(edge.UpdatedAt.IsZero(), gc.Equals, false, gc.Commentf("UpdatedAt field not set"))
// Update existing edge
other := &graph.Edge{
ID: edge.ID,
Src: linkUUIDs[0],
Dst: linkUUIDs[1],
}
err = s.g.UpsertEdge(other)
c.Assert(err, gc.IsNil)
c.Assert(other.ID, gc.Equals, edge.ID, gc.Commentf("edge ID changed while upserting"))
c.Assert(other.UpdatedAt, gc.Not(gc.Equals), edge.UpdatedAt, gc.Commentf("UpdatedAt field not modified"))
// Create edge with unknown link IDs
bogus := &graph.Edge{
Src: linkUUIDs[0],
Dst: uuid.New(),
}
err = s.g.UpsertEdge(bogus)
c.Assert(xerrors.Is(err, graph.ErrUnknownEdgeLinks), gc.Equals, true)
}
// TestConcurrentEdgeIterators verifies that multiple clients can concurrently
// access the store.
func (s *SuiteBase) TestConcurrentEdgeIterators(c *gc.C) {
var (
wg sync.WaitGroup
numIterators = 10
numEdges = 100
linkUUIDs = make([]uuid.UUID, numEdges*2)
)
for i := 0; i < numEdges*2; i++ {
link := &graph.Link{URL: fmt.Sprint(i)}
c.Assert(s.g.UpsertLink(link), gc.IsNil)
linkUUIDs[i] = link.ID
}
for i := 0; i < numEdges; i++ {
c.Assert(s.g.UpsertEdge(&graph.Edge{
Src: linkUUIDs[0],
Dst: linkUUIDs[i],
}), gc.IsNil)
}
wg.Add(numIterators)
for i := 0; i < numIterators; i++ {
go func(id int) {
defer wg.Done()
itTagComment := gc.Commentf("iterator %d", id)
seen := make(map[string]bool)
it, err := s.partitionedEdgeIterator(c, 0, 1, time.Now())
c.Assert(err, gc.IsNil, itTagComment)
defer func() {
c.Assert(it.Close(), gc.IsNil, itTagComment)
}()
for i := 0; it.Next(); i++ {
edge := it.Edge()
edgeID := edge.ID.String()
c.Assert(seen[edgeID], gc.Equals, false, gc.Commentf("iterator %d saw same edge twice", id))
seen[edgeID] = true
}
c.Assert(seen, gc.HasLen, numEdges, itTagComment)
c.Assert(it.Error(), gc.IsNil, itTagComment)
c.Assert(it.Close(), gc.IsNil, itTagComment)
}(i)
}
doneCh := make(chan struct{})
go func() {
wg.Wait()
close(doneCh)
}()
select {
case <-doneCh:
// test completed successfully
case <-time.After(10 * time.Second):
c.Fatal("timed out waiting for test to complete")
}
}
// TestEdgeIteratorTimeFilter verifies that the time-based filtering of the
// edge iterator works as expected.
func (s *SuiteBase) TestEdgeIteratorTimeFilter(c *gc.C) {
linkUUIDs := make([]uuid.UUID, 3)
linkInsertTimes := make([]time.Time, len(linkUUIDs))
for i := 0; i < len(linkUUIDs); i++ {
link := &graph.Link{URL: fmt.Sprint(i)}
c.Assert(s.g.UpsertLink(link), gc.IsNil)
linkUUIDs[i] = link.ID
linkInsertTimes[i] = time.Now()
}
edgeUUIDs := make([]uuid.UUID, len(linkUUIDs))
edgeInsertTimes := make([]time.Time, len(linkUUIDs))
for i := 0; i < len(linkUUIDs); i++ {
edge := &graph.Edge{Src: linkUUIDs[0], Dst: linkUUIDs[i]}
c.Assert(s.g.UpsertEdge(edge), gc.IsNil)
edgeUUIDs[i] = edge.ID
edgeInsertTimes[i] = time.Now()
}
for i, t := range edgeInsertTimes {
c.Logf("fetching edges created before edge %d", i)
s.assertIteratedEdgeIDsMatch(c, t, edgeUUIDs[:i+1])
}
}
func (s *SuiteBase) assertIteratedEdgeIDsMatch(c *gc.C, updatedBefore time.Time, exp []uuid.UUID) {
it, err := s.partitionedEdgeIterator(c, 0, 1, updatedBefore)
c.Assert(err, gc.IsNil)
var got []uuid.UUID
for it.Next() {
got = append(got, it.Edge().ID)
}
c.Assert(it.Error(), gc.IsNil)
c.Assert(it.Close(), gc.IsNil)
sort.Slice(got, func(l, r int) bool { return got[l].String() < got[r].String() })
sort.Slice(exp, func(l, r int) bool { return exp[l].String() < exp[r].String() })
c.Assert(got, gc.DeepEquals, exp)
}
// TestPartitionedEdgeIterators verifies that the graph partitioning logic
// works as expected even when partitions contain an uneven number of items.
func (s *SuiteBase) TestPartitionedEdgeIterators(c *gc.C) {
numEdges := 100
numPartitions := 10
linkUUIDs := make([]uuid.UUID, numEdges*2)
for i := 0; i < numEdges*2; i++ {
link := &graph.Link{URL: fmt.Sprint(i)}
c.Assert(s.g.UpsertLink(link), gc.IsNil)
linkUUIDs[i] = link.ID
}
for i := 0; i < numEdges; i++ {
c.Assert(s.g.UpsertEdge(&graph.Edge{
Src: linkUUIDs[0],
Dst: linkUUIDs[i],
}), gc.IsNil)
}
// Check with both odd and even partition counts to check for rounding-related bugs.
c.Assert(s.iteratePartitionedEdges(c, numPartitions), gc.Equals, numEdges)
c.Assert(s.iteratePartitionedEdges(c, numPartitions+1), gc.Equals, numEdges)
}
func (s *SuiteBase) iteratePartitionedEdges(c *gc.C, numPartitions int) int {
seen := make(map[string]bool)
for partition := 0; partition < numPartitions; partition++ {
// Build list of expected edges per partition. An edge belongs to a
// partition if its origin link also belongs to the same partition.
linksInPartition := make(map[uuid.UUID]struct{})
linkIt, err := s.partitionedLinkIterator(c, partition, numPartitions, time.Now())
c.Assert(err, gc.IsNil)
for linkIt.Next() {
linkID := linkIt.Link().ID
linksInPartition[linkID] = struct{}{}
}
it, err := s.partitionedEdgeIterator(c, partition, numPartitions, time.Now())
c.Assert(err, gc.IsNil)
defer func() {
c.Assert(it.Close(), gc.IsNil)
}()
for it.Next() {
edge := it.Edge()
edgeID := edge.ID.String()
c.Assert(seen[edgeID], gc.Equals, false, gc.Commentf("iterator returned same edge in different partitions"))
seen[edgeID] = true
_, srcInPartition := linksInPartition[edge.Src]
c.Assert(srcInPartition, gc.Equals, true, gc.Commentf("iterator returned an edge whose source link belongs to a different partition"))
}
c.Assert(it.Error(), gc.IsNil)
c.Assert(it.Close(), gc.IsNil)
}
return len(seen)
}
// TestRemoveStaleEdges verifies that the edge deletion logic works as expected.
func (s *SuiteBase) TestRemoveStaleEdges(c *gc.C) {
numEdges := 100
linkUUIDs := make([]uuid.UUID, numEdges*4)
goneUUIDs := make(map[uuid.UUID]struct{})
for i := 0; i < numEdges*4; i++ {
link := &graph.Link{URL: fmt.Sprint(i)}
c.Assert(s.g.UpsertLink(link), gc.IsNil)
linkUUIDs[i] = link.ID
}
var lastTs time.Time
for i := 0; i < numEdges; i++ {
e1 := &graph.Edge{
Src: linkUUIDs[0],
Dst: linkUUIDs[i],
}
c.Assert(s.g.UpsertEdge(e1), gc.IsNil)
goneUUIDs[e1.ID] = struct{}{}
lastTs = e1.UpdatedAt
}
deleteBefore := lastTs.Add(time.Millisecond)
time.Sleep(250 * time.Millisecond)
// The following edges will have an updated at value > lastTs
for i := 0; i < numEdges; i++ {
e2 := &graph.Edge{
Src: linkUUIDs[0],
Dst: linkUUIDs[numEdges+i+1],
}
c.Assert(s.g.UpsertEdge(e2), gc.IsNil)
}
c.Assert(s.g.RemoveStaleEdges(linkUUIDs[0], deleteBefore), gc.IsNil)
it, err := s.partitionedEdgeIterator(c, 0, 1, time.Now())
c.Assert(err, gc.IsNil)
defer func() { c.Assert(it.Close(), gc.IsNil) }()
var seen int
for it.Next() {
id := it.Edge().ID
_, found := goneUUIDs[id]
c.Assert(found, gc.Equals, false, gc.Commentf("expected edge %s to be removed from the edge list", id.String()))
seen++
}
c.Assert(seen, gc.Equals, numEdges)
}
func (s *SuiteBase) partitionedLinkIterator(c *gc.C, partition, numPartitions int, accessedBefore time.Time) (graph.LinkIterator, error) {
from, to := s.partitionRange(c, partition, numPartitions)
return s.g.Links(from, to, accessedBefore)
}
func (s *SuiteBase) partitionedEdgeIterator(c *gc.C, partition, numPartitions int, updatedBefore time.Time) (graph.EdgeIterator, error) {
from, to := s.partitionRange(c, partition, numPartitions)
return s.g.Edges(from, to, updatedBefore)
}
func (s *SuiteBase) partitionRange(c *gc.C, partition, numPartitions int) (from, to uuid.UUID) {
if partition < 0 || partition >= numPartitions {
c.Fatal("invalid partition")
}
var minUUID = uuid.Nil
var maxUUID = uuid.MustParse("ffffffff-ffff-ffff-ffff-ffffffffffff")
var err error
// Calculate the size of each partition as: (2^128 / numPartitions)
tokenRange := big.NewInt(0)
partSize := big.NewInt(0)
partSize.SetBytes(maxUUID[:])
partSize = partSize.Div(partSize, big.NewInt(int64(numPartitions)))
// We model the partitions as a segment that begins at minUUID (all
// bits set to zero) and ends at maxUUID (all bits set to 1). By
// setting the end range for the *last* partition to maxUUID we ensure
// that we always cover the full range of UUIDs even if the range
// itself is not evenly divisible by numPartitions.
if partition == 0 {
from = minUUID
} else {
tokenRange.Mul(partSize, big.NewInt(int64(partition)))
from, err = uuid.FromBytes(tokenRange.Bytes())
c.Assert(err, gc.IsNil)
}
if partition == numPartitions-1 {
to = maxUUID
} else {
tokenRange.Mul(partSize, big.NewInt(int64(partition+1)))
to, err = uuid.FromBytes(tokenRange.Bytes())
c.Assert(err, gc.IsNil)
}
return from, to
}