| 1 | package gcache |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "testing" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | func TestLFUGet(t *testing.T) { |
| 10 | size := 1000 |
| 11 | numbers := 1000 |
| 12 | |
| 13 | gc := buildTestLoadingCache(t, TYPE_LFU, size, loader) |
| 14 | testSetCache(t, gc, numbers) |
| 15 | testGetCache(t, gc, numbers) |
| 16 | } |
| 17 | |
| 18 | func TestLoadingLFUGet(t *testing.T) { |
| 19 | size := 1000 |
| 20 | numbers := 1000 |
| 21 | |
| 22 | gc := buildTestLoadingCache(t, TYPE_LFU, size, loader) |
| 23 | testGetCache(t, gc, numbers) |
| 24 | } |
| 25 | |
| 26 | func TestLFULength(t *testing.T) { |
| 27 | gc := buildTestLoadingCache(t, TYPE_LFU, 1000, loader) |
| 28 | gc.Get("test1") |
| 29 | gc.Get("test2") |
| 30 | length := gc.Len(true) |
| 31 | expectedLength := 2 |
| 32 | if length != expectedLength { |
| 33 | t.Errorf("Expected length is %v, not %v", length, expectedLength) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestLFUEvictItem(t *testing.T) { |
| 38 | cacheSize := 10 |
| 39 | numbers := 11 |
| 40 | gc := buildTestLoadingCache(t, TYPE_LFU, cacheSize, loader) |
| 41 | |
| 42 | for i := 0; i < numbers; i++ { |
| 43 | _, err := gc.Get(fmt.Sprintf("Key-%d", i)) |
| 44 | if err != nil { |
| 45 | t.Errorf("Unexpected error: %v", err) |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func TestLFUGetIFPresent(t *testing.T) { |
| 51 | testGetIFPresent(t, TYPE_LFU) |
| 52 | } |
| 53 | |
| 54 | func TestLFUHas(t *testing.T) { |
| 55 | gc := buildTestLoadingCacheWithExpiration(t, TYPE_LFU, 2, 10*time.Millisecond) |
| 56 | |
| 57 | for i := 0; i < 10; i++ { |
| 58 | t.Run(fmt.Sprint(i), func(t *testing.T) { |
| 59 | gc.Get("test1") |
| 60 | gc.Get("test2") |
| 61 | |
| 62 | if gc.Has("test0") { |
| 63 | t.Fatal("should not have test0") |
| 64 | } |
| 65 | if !gc.Has("test1") { |
| 66 | t.Fatal("should have test1") |
| 67 | } |
| 68 | if !gc.Has("test2") { |
| 69 | t.Fatal("should have test2") |
| 70 | } |
| 71 | |
| 72 | time.Sleep(20 * time.Millisecond) |
| 73 | |
| 74 | if gc.Has("test0") { |
| 75 | t.Fatal("should not have test0") |
| 76 | } |
| 77 | if gc.Has("test1") { |
| 78 | t.Fatal("should not have test1") |
| 79 | } |
| 80 | if gc.Has("test2") { |
| 81 | t.Fatal("should not have test2") |
| 82 | } |
| 83 | }) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | func TestLFUFreqListOrder(t *testing.T) { |
| 88 | gc := buildTestCache(t, TYPE_LFU, 5) |
| 89 | for i := 4; i >= 0; i-- { |
| 90 | gc.Set(i, i) |
| 91 | for j := 0; j <= i; j++ { |
| 92 | gc.Get(i) |
| 93 | } |
| 94 | } |
| 95 | if l := gc.(*LFUCache).freqList.Len(); l != 6 { |
| 96 | t.Fatalf("%v != 6", l) |
| 97 | } |
| 98 | var i uint |
| 99 | for e := gc.(*LFUCache).freqList.Front(); e != nil; e = e.Next() { |
| 100 | if e.Value.(*freqEntry).freq != i { |
| 101 | t.Fatalf("%v != %v", e.Value.(*freqEntry).freq, i) |
| 102 | } |
| 103 | i++ |
| 104 | } |
| 105 | gc.Remove(1) |
| 106 | |
| 107 | if l := gc.(*LFUCache).freqList.Len(); l != 5 { |
| 108 | t.Fatalf("%v != 5", l) |
| 109 | } |
| 110 | gc.Set(1, 1) |
| 111 | if l := gc.(*LFUCache).freqList.Len(); l != 5 { |
| 112 | t.Fatalf("%v != 5", l) |
| 113 | } |
| 114 | gc.Get(1) |
| 115 | if l := gc.(*LFUCache).freqList.Len(); l != 5 { |
| 116 | t.Fatalf("%v != 5", l) |
| 117 | } |
| 118 | gc.Get(1) |
| 119 | if l := gc.(*LFUCache).freqList.Len(); l != 6 { |
| 120 | t.Fatalf("%v != 6", l) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | func TestLFUFreqListLength(t *testing.T) { |
| 125 | k0, v0 := "k0", "v0" |
| 126 | k1, v1 := "k1", "v1" |
| 127 | |
| 128 | { |
| 129 | gc := buildTestCache(t, TYPE_LFU, 5) |
| 130 | if l := gc.(*LFUCache).freqList.Len(); l != 1 { |
| 131 | t.Fatalf("%v != 1", l) |
| 132 | } |
| 133 | } |
| 134 | { |
| 135 | gc := buildTestCache(t, TYPE_LFU, 5) |
| 136 | gc.Set(k0, v0) |
| 137 | for i := 0; i < 5; i++ { |
| 138 | gc.Get(k0) |
| 139 | } |
| 140 | if l := gc.(*LFUCache).freqList.Len(); l != 2 { |
| 141 | t.Fatalf("%v != 2", l) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | { |
| 146 | gc := buildTestCache(t, TYPE_LFU, 5) |
| 147 | gc.Set(k0, v0) |
| 148 | gc.Set(k1, v1) |
| 149 | for i := 0; i < 5; i++ { |
| 150 | gc.Get(k0) |
| 151 | gc.Get(k1) |
| 152 | } |
| 153 | if l := gc.(*LFUCache).freqList.Len(); l != 2 { |
| 154 | t.Fatalf("%v != 2", l) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | { |
| 159 | gc := buildTestCache(t, TYPE_LFU, 5) |
| 160 | gc.Set(k0, v0) |
| 161 | gc.Set(k1, v1) |
| 162 | for i := 0; i < 5; i++ { |
| 163 | gc.Get(k0) |
| 164 | } |
| 165 | if l := gc.(*LFUCache).freqList.Len(); l != 2 { |
| 166 | t.Fatalf("%v != 2", l) |
| 167 | } |
| 168 | for i := 0; i < 5; i++ { |
| 169 | gc.Get(k1) |
| 170 | } |
| 171 | if l := gc.(*LFUCache).freqList.Len(); l != 2 { |
| 172 | t.Fatalf("%v != 2", l) |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | { |
| 177 | gc := buildTestCache(t, TYPE_LFU, 5) |
| 178 | gc.Set(k0, v0) |
| 179 | gc.Get(k0) |
| 180 | if l := gc.(*LFUCache).freqList.Len(); l != 2 { |
| 181 | t.Fatalf("%v != 2", l) |
| 182 | } |
| 183 | gc.Remove(k0) |
| 184 | if l := gc.(*LFUCache).freqList.Len(); l != 1 { |
| 185 | t.Fatalf("%v != 1", l) |
| 186 | } |
| 187 | gc.Set(k0, v0) |
| 188 | if l := gc.(*LFUCache).freqList.Len(); l != 1 { |
| 189 | t.Fatalf("%v != 1", l) |
| 190 | } |
| 191 | gc.Get(k0) |
| 192 | if l := gc.(*LFUCache).freqList.Len(); l != 2 { |
| 193 | t.Fatalf("%v != 2", l) |
| 194 | } |
| 195 | } |
| 196 | } |