Debian Codegraph @

dedupqueue_test.go

raw
1 package desync
2
3 import (
4 "sync"
5 "sync/atomic"
6 "testing"
7 "testing/synctest"
8 "time"
9
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 )
13
14 func TestDedupQueueSimple(t *testing.T) {
15 exists := ChunkID{0}
16 notExists := ChunkID{1}
17 store := &TestStore{
18 Chunks: map[ChunkID][]byte{
19 exists: {0, 1, 2, 3},
20 },
21 }
22 q := NewDedupQueue(store)
23
24 // First compare we're getting the expected data in the positive case
25 bExpected, err := store.GetChunk(exists)
26 require.NoError(t, err)
27 bActual, err := q.GetChunk(exists)
28 require.NoError(t, err)
29 require.Equal(t, bExpected, bActual)
30
31 // Now make sure errors too are passed correctly
32 _, err = q.GetChunk(notExists)
33 require.IsType(t, ChunkMissing{}, err)
34
35 // Check HasChunk() as well
36 hasChunk, err := q.HasChunk(exists)
37 require.NoError(t, err)
38 require.True(t, hasChunk)
39 }
40
41 func TestDedupQueueParallel(t *testing.T) {
42 synctest.Test(t, func(t *testing.T) {
43 // Make a store that counts the requests to it
44 var requests atomic.Int64
45 store := &TestStore{
46 GetChunkFunc: func(ChunkID) (*Chunk, error) {
47 // The fake clock only advances once all other goroutines are
48 // blocked, so this guarantees they all registered as waiters
49 // on this request before it completes
50 time.Sleep(time.Millisecond)
51 requests.Add(1)
52 return NewChunk([]byte{0}), nil
53 },
54 }
55 q := NewDedupQueue(store)
56
57 var (
58 wg sync.WaitGroup
59 start = make(chan struct{})
60 )
61
62 // Start several goroutines all asking for the same chunk from the store
63 for range 10 {
64 wg.Go(func() {
65 <-start
66 q.GetChunk(ChunkID{0})
67 })
68 }
69
70 close(start)
71 wg.Wait()
72
73 // There should be just one request that was done on the upstream store
74 require.EqualValues(t, 1, requests.Load(), "requests to the store")
75 })
76 }
77
78 func TestDedupQueueChunkNotShared(t *testing.T) {
79 data := []byte("some data")
80 id := NewChunk(data).ID()
81 compressed, err := Compress(data)
82 require.NoError(t, err)
83
84 store := &TestStore{
85 GetChunkFunc: func(id ChunkID) (*Chunk, error) {
86 // Give the other goroutines time to pile up on this request
87 time.Sleep(10 * time.Millisecond)
88 // Return the chunk in storage form, the plain data is only
89 // materialized lazily when the caller asks for it
90 return NewChunkFromStorage(id, compressed, Converters{Compressor{}}, true)
91 },
92 }
93 q := NewDedupQueue(store)
94
95 // Request the same chunk from many goroutines at once. Each caller must
96 // receive its own copy since accessing the plain data modifies the chunk.
97 // Run with -race to confirm the callers don't share state.
98 chunks := make(chan *Chunk, 10)
99 var wg sync.WaitGroup
100 for range 10 {
101 wg.Go(func() {
102 c, err := q.GetChunk(id)
103 if !assert.NoError(t, err) {
104 return
105 }
106 b, err := c.Data()
107 assert.NoError(t, err)
108 assert.Equal(t, data, b)
109 chunks <- c
110 })
111 }
112 wg.Wait()
113 close(chunks)
114
115 seen := make(map[*Chunk]struct{})
116 for c := range chunks {
117 _, shared := seen[c]
118 assert.False(t, shared, "the same *Chunk was handed to multiple callers")
119 seen[c] = struct{}{}
120 }
121 }