| 1 |
package desync |
| 2 |
|
| 3 |
import ( |
| 4 |
"sync" |
| 5 |
"testing" |
| 6 |
"testing/synctest" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
"github.com/stretchr/testify/require" |
| 11 |
) |
| 12 |
|
| 13 |
// Test read access before write access to ensure a failing read doesn't |
| 14 |
// impact the write operation (should use separate queues). |
| 15 |
func TestWriteDedupQueueParallelReadWrite(t *testing.T) { |
| 16 |
synctest.Test(t, func(t *testing.T) { |
| 17 |
c := NewChunk([]byte{1, 2, 3, 4}) |
| 18 |
sleeping := make(chan struct{}) |
| 19 |
store := &TestStore{ |
| 20 |
// Slow GetChunk operation |
| 21 |
GetChunkFunc: func(id ChunkID) (*Chunk, error) { |
| 22 |
close(sleeping) |
| 23 |
time.Sleep(time.Second) |
| 24 |
return nil, ChunkMissing{id} |
| 25 |
}, |
| 26 |
} |
| 27 |
q := NewWriteDedupQueue(store) |
| 28 |
|
| 29 |
// Queue up a slow GetChunk() operation, then perform a StoreChunk(). The store |
| 30 |
// operation should not be impacted by the ongoing read |
| 31 |
done := make(chan struct{}) |
| 32 |
go func() { |
| 33 |
defer close(done) |
| 34 |
q.GetChunk(c.ID()) |
| 35 |
}() |
| 36 |
<-sleeping |
| 37 |
|
| 38 |
start := time.Now() |
| 39 |
require.NoError(t, q.StoreChunk(c)) |
| 40 |
|
| 41 |
// The fake clock only advances while all goroutines are blocked, so any |
| 42 |
// time passing here means the write waited for the in-flight read |
| 43 |
require.Zero(t, time.Since(start), "StoreChunk() blocked on the ongoing GetChunk()") |
| 44 |
|
| 45 |
// Wait for the read to finish, all goroutines must be done before the test returns |
| 46 |
<-done |
| 47 |
}) |
| 48 |
} |
| 49 |
|
| 50 |
func TestWriteDedupQueueChunkNotShared(t *testing.T) { |
| 51 |
synctest.Test(t, func(t *testing.T) { |
| 52 |
data := []byte("some data") |
| 53 |
compressed, err := Compress(data) |
| 54 |
require.NoError(t, err) |
| 55 |
id := NewChunk(data).ID() |
| 56 |
|
| 57 |
// Use a chunk in storage form, its plain data is materialized (and |
| 58 |
// cached) lazily when the readers ask for it |
| 59 |
chunkIn, err := NewChunkFromStorage(id, compressed, Converters{Compressor{}}, true) |
| 60 |
require.NoError(t, err) |
| 61 |
|
| 62 |
inFlight := make(chan struct{}) |
| 63 |
store := &TestStore{ |
| 64 |
StoreChunkFunc: func(chunk *Chunk) error { |
| 65 |
close(inFlight) |
| 66 |
// The fake clock only advances once all other goroutines are |
| 67 |
// blocked, so this guarantees the readers registered as |
| 68 |
// waiters on this request before it completes |
| 69 |
time.Sleep(time.Millisecond) |
| 70 |
return nil |
| 71 |
}, |
| 72 |
} |
| 73 |
q := NewWriteDedupQueue(store) |
| 74 |
|
| 75 |
// Store a chunk while several readers request the same chunk ID. Every |
| 76 |
// reader must get its own copy since accessing the plain data modifies |
| 77 |
// the chunk, and the writer keeps using the original. Run with -race |
| 78 |
// to confirm the callers don't share state. |
| 79 |
var wg sync.WaitGroup |
| 80 |
chunks := make(chan *Chunk, 10) |
| 81 |
wg.Go(func() { |
| 82 |
assert.NoError(t, q.StoreChunk(chunkIn)) |
| 83 |
}) |
| 84 |
<-inFlight |
| 85 |
for range 10 { |
| 86 |
wg.Go(func() { |
| 87 |
c, err := q.GetChunk(id) |
| 88 |
if !assert.NoError(t, err) { |
| 89 |
return |
| 90 |
} |
| 91 |
b, err := c.Data() |
| 92 |
assert.NoError(t, err) |
| 93 |
assert.Equal(t, data, b) |
| 94 |
chunks <- c |
| 95 |
}) |
| 96 |
} |
| 97 |
wg.Wait() |
| 98 |
close(chunks) |
| 99 |
|
| 100 |
seen := map[*Chunk]struct{}{chunkIn: {}} |
| 101 |
for c := range chunks { |
| 102 |
_, shared := seen[c] |
| 103 |
assert.False(t, shared, "the same *Chunk was handed to multiple callers") |
| 104 |
seen[c] = struct{}{} |
| 105 |
} |
| 106 |
}) |
| 107 |
} |