-
Notifications
You must be signed in to change notification settings - Fork 43
/
dedupqueue_test.go
91 lines (81 loc) · 1.92 KB
/
dedupqueue_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
package desync
import (
"reflect"
"sync"
"sync/atomic"
"testing"
"time"
)
func TestDedupQueueSimple(t *testing.T) {
// var requests int64
// store := &TestStore{
// GetChunkFunc: func(ChunkID) (*Chunk, error) {
// atomic.AddInt64(&requests, 1)
// return NewChunkFromUncompressed([]byte{0}), nil
// },
// }
exists := ChunkID{0}
notExists := ChunkID{1}
store := &TestStore{
Chunks: map[ChunkID][]byte{
exists: {0, 1, 2, 3},
},
}
q := NewDedupQueue(store)
// First compare we're getting the expected data in the positive case
bExpected, err := store.GetChunk(exists)
if err != nil {
t.Fatal(err)
}
bActual, err := q.GetChunk(exists)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(bActual, bExpected) {
t.Fatalf("got %v; want %v", bExpected, bActual)
}
// Now make sure errors too are passed correctly
_, err = q.GetChunk(notExists)
if _, ok := err.(ChunkMissing); !ok {
t.Fatalf("got '%v'; want chunk missing error", err)
}
// Check HasChunk() as well
hasChunk, err := q.HasChunk(exists)
if err != nil {
t.Fatal(err)
}
if !hasChunk {
t.Fatalf("HasChunk() = false; want true")
}
}
func TestDedupQueueParallel(t *testing.T) {
// Make a store that counts the requests to it
var requests int64
store := &TestStore{
GetChunkFunc: func(ChunkID) (*Chunk, error) {
time.Sleep(time.Millisecond) // make it artificially slow to not complete too early
atomic.AddInt64(&requests, 1)
return NewChunk([]byte{0}), nil
},
}
q := NewDedupQueue(store)
var (
wg sync.WaitGroup
start = make(chan struct{})
)
// Start several goroutines all asking for the same chunk from the store
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
<-start
q.GetChunk(ChunkID{0})
wg.Done()
}()
}
close(start)
wg.Wait()
// There should ideally be just one requests that was done on the upstream store
if requests > 1 {
t.Fatalf("%d requests to the store; want 1", requests)
}
}