|
| 1 | +package mdcache |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/sahib/brig/catfs/mio/pagecache/page" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | +) |
| 9 | + |
| 10 | +func withL1Cache(t *testing.T, fn func(l1, backing *l1cache)) { |
| 11 | + // some fake in-mem cache that stores everything that got removed |
| 12 | + // out of l1 due to size restrictions. |
| 13 | + backing, err := newL1Cache(nil, int64(^uint64(0)>>1)) |
| 14 | + require.NoError(t, err) |
| 15 | + |
| 16 | + l1, err := newL1Cache(backing, 4*(page.Size+page.Meta)) |
| 17 | + require.NoError(t, err) |
| 18 | + |
| 19 | + fn(l1, backing) |
| 20 | + |
| 21 | + require.NoError(t, l1.Close()) |
| 22 | +} |
| 23 | + |
| 24 | +func TestL1GetSetDel(t *testing.T) { |
| 25 | + // NOTE: Only covers the very basic usage. |
| 26 | + withL1Cache(t, func(l1, _ *l1cache) { |
| 27 | + pk := pageKey{1, 0} |
| 28 | + _, err := l1.Get(pk) |
| 29 | + require.Error(t, page.ErrCacheMiss) |
| 30 | + |
| 31 | + pset := dummyPage(0, 1024) |
| 32 | + require.NoError(t, l1.Set(pk, pset)) |
| 33 | + |
| 34 | + pgot, err := l1.Get(pk) |
| 35 | + require.NoError(t, err) |
| 36 | + |
| 37 | + require.Equal(t, pset.Data, pgot.Data) |
| 38 | + require.Equal(t, pset.Extents, pgot.Extents) |
| 39 | + |
| 40 | + require.NoError(t, l1.Del([]pageKey{pk})) |
| 41 | + _, err = l1.Get(pk) |
| 42 | + require.Error(t, page.ErrCacheMiss) |
| 43 | + }) |
| 44 | +} |
| 45 | + |
| 46 | +func TestL1SwapPriority(t *testing.T) { |
| 47 | + withL1Cache(t, func(l1, backing *l1cache) { |
| 48 | + // Insert 8 pages, only 4 can stay in l1. |
| 49 | + for idx := 0; idx < 8; idx++ { |
| 50 | + pk := pageKey{1, uint32(idx)} |
| 51 | + require.NoError(t, l1.Set(pk, dummyPage(0, uint32((idx+1)*100)))) |
| 52 | + } |
| 53 | + |
| 54 | + for idx := 0; idx < 4; idx++ { |
| 55 | + pk := pageKey{1, uint32(idx)} |
| 56 | + _, err := l1.Get(pk) |
| 57 | + require.Error(t, err, page.ErrCacheMiss) |
| 58 | + |
| 59 | + // should be in backing store, check: |
| 60 | + p, err := backing.Get(pk) |
| 61 | + require.NoError(t, err) |
| 62 | + |
| 63 | + expected := dummyPage(0, uint32((idx+1)*100)) |
| 64 | + require.Equal(t, expected, p) |
| 65 | + } |
| 66 | + |
| 67 | + for idx := 4; idx < 8; idx++ { |
| 68 | + pk := pageKey{1, uint32(idx)} |
| 69 | + p, err := l1.Get(pk) |
| 70 | + require.NoError(t, err) |
| 71 | + |
| 72 | + expected := dummyPage(0, uint32((idx+1)*100)) |
| 73 | + require.Equal(t, expected, p) |
| 74 | + } |
| 75 | + }) |
| 76 | +} |
0 commit comments