MCPcopy
hub / github.com/kopia/kopia / TestContentCacheForData

Function TestContentCacheForData

internal/cache/content_cache_data_test.go:16–74  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

14)
15
16func TestContentCacheForData(t *testing.T) {
17 ctx := testlogging.Context(t)
18
19 underlyingData := blobtesting.DataMap{}
20 underlying := blobtesting.NewMapStorage(underlyingData, nil, nil)
21
22 cacheData := blobtesting.DataMap{}
23 cacheStorage := testutil.EnsureType[cache.Storage](t, blobtesting.NewMapStorage(cacheData, nil, nil))
24
25 dataCache, err := cache.NewContentCache(ctx, underlying, cache.Options{
26 Storage: cacheStorage,
27 HMACSecret: []byte{1, 2, 3, 4},
28 Sweep: cache.SweepSettings{
29 MaxSizeBytes: 150,
30 },
31 }, nil)
32 require.NoError(t, err)
33
34 var tmp gather.WriteBuffer
35 defer tmp.Close()
36
37 // get something we don't have in the underlying storage
38 require.ErrorIs(t, dataCache.GetContent(ctx, "key1", "blob1", 0, 3, &tmp), blob.ErrBlobNotFound)
39
40 require.NoError(t, underlying.PutBlob(ctx, "blob1", gather.FromSlice([]byte{1, 2, 3, 4, 5, 6}), blob.PutOptions{}))
41
42 require.NoError(t, dataCache.GetContent(ctx, "xkey1", "blob1", 0, 3, &tmp))
43 require.Equal(t, []byte{1, 2, 3}, tmp.ToByteSlice())
44
45 require.NoError(t, dataCache.GetContent(ctx, "xkey2", "blob1", 3, 3, &tmp))
46 require.Equal(t, []byte{4, 5, 6}, tmp.ToByteSlice())
47
48 // cache has 2 entries, one for each section of the blob.
49 require.Len(t, cacheData, 2)
50
51 // keys are mangled so that last character (which is always 0..9 a..f) is the first.
52 require.Contains(t, cacheData, blob.ID("key1x"))
53 require.Contains(t, cacheData, blob.ID("key2x"))
54
55 dataCache.Close(ctx)
56
57 // get slice with cache miss
58 require.NoError(t, underlying.PutBlob(ctx, "blob2", gather.FromSlice([]byte{1, 2, 3, 4, 5, 6}), blob.PutOptions{}))
59 require.NoError(t, dataCache.GetContent(ctx, "aaa1", "blob2", 3, 3, &tmp))
60 require.Equal(t, []byte{4, 5, 6}, tmp.ToByteSlice())
61
62 // even-length content IDs are not mangled
63 require.Len(t, cacheData, 3)
64 require.Contains(t, cacheData, blob.ID("aaa1"))
65
66 require.NoError(t, underlying.PutBlob(ctx, "pblob3", gather.FromSlice([]byte{1, 2, 3, 4, 5, 6}), blob.PutOptions{}))
67 dataCache.PrefetchBlob(ctx, "pblob3")
68 require.NoError(t, underlying.DeleteBlob(ctx, "pblob3"))
69
70 // make sure blob ID is properly mangled
71 require.Contains(t, cacheData, blob.ID("blob3p"))
72 require.NoError(t, dataCache.GetContent(ctx, "aaa3", "pblob3", 2, 4, &tmp))
73 require.Equal(t, []byte{3, 4, 5, 6}, tmp.ToByteSlice())

Callers

nothing calls this directly

Calls 15

CloseMethod · 0.95
GetContentMethod · 0.95
ToByteSliceMethod · 0.95
CloseMethod · 0.95
PrefetchBlobMethod · 0.95
ContextFunction · 0.92
NewMapStorageFunction · 0.92
EnsureTypeFunction · 0.92
NewContentCacheFunction · 0.92
FromSliceFunction · 0.92
IDTypeAlias · 0.92
EqualMethod · 0.80

Tested by

no test coverage detected