github.com/petermattis/pebble@v0.0.0-20190905164901-ab51a2166067/cache/clockpro_test.go (about) 1 // Copyright 2018. All rights reserved. Use of this source code is governed by 2 // an MIT-style license that can be found in the LICENSE file. 3 4 package cache 5 6 import ( 7 "bufio" 8 "bytes" 9 "os" 10 "strconv" 11 "testing" 12 ) 13 14 func TestCache(t *testing.T) { 15 // Test data was generated from the python code 16 f, err := os.Open("testdata/cache") 17 if err != nil { 18 t.Fatal(err) 19 } 20 21 cache := newShards(200, 1) 22 scanner := bufio.NewScanner(f) 23 line := 1 24 25 for scanner.Scan() { 26 fields := bytes.Fields(scanner.Bytes()) 27 28 key, err := strconv.Atoi(string(fields[0])) 29 if err != nil { 30 t.Fatal(err) 31 } 32 wantHit := fields[1][0] == 'h' 33 34 var hit bool 35 h := cache.Get(0, uint64(key), 0) 36 if v := h.Get(); v == nil { 37 cache.Set(0, uint64(key), 0, append([]byte(nil), fields[0][0])) 38 } else { 39 hit = true 40 if !bytes.Equal(v, fields[0][:1]) { 41 t.Errorf("%d: cache returned bad data: got %s , want %s\n", line, v, fields[0][:1]) 42 } 43 } 44 h.Release() 45 if hit != wantHit { 46 t.Errorf("%d: cache hit mismatch: got %v, want %v\n", line, hit, wantHit) 47 } 48 line++ 49 } 50 } 51 52 func TestWeakHandle(t *testing.T) { 53 cache := newShards(5, 1) 54 cache.Set(0, 1, 0, bytes.Repeat([]byte("a"), 5)) 55 h := cache.Set(0, 0, 0, bytes.Repeat([]byte("b"), 5)) 56 if v := h.Get(); string(v) != "bbbbb" { 57 t.Fatalf("expected bbbbb, but found %v", v) 58 } 59 w := h.Weak() 60 h.Release() 61 if v := w.Get(); string(v) != "bbbbb" { 62 t.Fatalf("expected bbbbb, but found %v", v) 63 } 64 cache.Set(0, 2, 0, bytes.Repeat([]byte("a"), 5)) 65 if v := w.Get(); v != nil { 66 t.Fatalf("expected nil, but found %s", v) 67 } 68 } 69 70 func TestEvictFile(t *testing.T) { 71 cache := newShards(100, 1) 72 cache.Set(0, 0, 0, bytes.Repeat([]byte("a"), 5)) 73 cache.Set(0, 1, 0, bytes.Repeat([]byte("a"), 5)) 74 cache.Set(0, 2, 0, bytes.Repeat([]byte("a"), 5)) 75 cache.Set(0, 2, 1, bytes.Repeat([]byte("a"), 5)) 76 cache.Set(0, 2, 2, bytes.Repeat([]byte("a"), 5)) 77 if expected, size := int64(25), cache.Size(); expected != size { 78 t.Fatalf("expected cache size %d, but found %d", expected, size) 79 } 80 cache.EvictFile(0, 0) 81 if expected, size := int64(20), cache.Size(); expected != size { 82 t.Fatalf("expected cache size %d, but found %d", expected, size) 83 } 84 cache.EvictFile(0, 1) 85 if expected, size := int64(15), cache.Size(); expected != size { 86 t.Fatalf("expected cache size %d, but found %d", expected, size) 87 } 88 cache.EvictFile(0, 2) 89 if expected, size := int64(0), cache.Size(); expected != size { 90 t.Fatalf("expected cache size %d, but found %d", expected, size) 91 } 92 } 93 94 func TestEvictAll(t *testing.T) { 95 // Verify that it is okay to evict all of the data from a cache. Previously 96 // this would trigger a nil-pointer dereference. 97 cache := newShards(100, 1) 98 cache.Set(0, 0, 0, bytes.Repeat([]byte("a"), 101)) 99 cache.Set(0, 1, 0, bytes.Repeat([]byte("a"), 101)) 100 } 101 102 func TestMultipleDBs(t *testing.T) { 103 cache := newShards(100, 1) 104 cache.Set(0, 0, 0, bytes.Repeat([]byte("a"), 5)) 105 cache.Set(1, 0, 0, bytes.Repeat([]byte("b"), 5)) 106 if expected, size := int64(10), cache.Size(); expected != size { 107 t.Fatalf("expected cache size %d, but found %d", expected, size) 108 } 109 cache.EvictFile(0, 0) 110 if expected, size := int64(5), cache.Size(); expected != size { 111 t.Fatalf("expected cache size %d, but found %d", expected, size) 112 } 113 h := cache.Get(0, 0, 0) 114 if v := h.Get(); v != nil { 115 t.Fatalf("expected not present, but found %s", v) 116 } 117 h = cache.Get(1, 0, 0) 118 if v := h.Get(); string(v) != "bbbbb" { 119 t.Fatalf("expected bbbbb, but found %v", v) 120 } 121 }