(t *testing.T)
| 161 | } |
| 162 | |
| 163 | func TestCache(t *testing.T) { |
| 164 | ctx := testlogging.Context(t) |
| 165 | c := NewCache(&Options{ |
| 166 | MaxCachedDirectories: 4, |
| 167 | MaxCachedEntries: 100, |
| 168 | }) |
| 169 | |
| 170 | if len(c.data) != 0 || c.totalDirectoryEntries != 0 || c.head != nil || c.tail != nil { |
| 171 | t.Errorf("invalid initial state: %v %v %v %v", c.data, c.totalDirectoryEntries, c.head, c.tail) |
| 172 | } |
| 173 | |
| 174 | cs := newCacheSource() |
| 175 | cv := cacheVerifier{cacheSource: cs, cache: c} |
| 176 | id1 := "1" |
| 177 | id2 := "2" |
| 178 | id3 := "3" |
| 179 | id4 := "4" |
| 180 | id5 := "5" |
| 181 | id6 := "6" |
| 182 | id7 := "7" |
| 183 | |
| 184 | cs.setEntryCount(id1, 3) |
| 185 | cs.setEntryCount(id2, 3) |
| 186 | cs.setEntryCount(id3, 3) |
| 187 | cs.setEntryCount(id4, 95) |
| 188 | cs.setEntryCount(id5, 70) |
| 189 | cs.setEntryCount(id6, 100) |
| 190 | cs.setEntryCount(id7, 101) |
| 191 | |
| 192 | cv.verifyCacheOrdering(t) |
| 193 | |
| 194 | // fetch id1 |
| 195 | _, _ = c.getEntries(ctx, id1, expirationTime, cs.get(id1), identityWrapper) |
| 196 | cv.verifyCacheMiss(t, id1) |
| 197 | cv.verifyCacheOrdering(t, id1) |
| 198 | |
| 199 | // fetch id1 again - cache hit, no change |
| 200 | _, _ = c.getEntries(ctx, id1, expirationTime, cs.get(id1), identityWrapper) |
| 201 | cv.verifyCacheHit(t, id1) |
| 202 | cv.verifyCacheOrdering(t, id1) |
| 203 | |
| 204 | // fetch id2 |
| 205 | _, _ = c.getEntries(ctx, id2, expirationTime, cs.get(id2), identityWrapper) |
| 206 | cv.verifyCacheMiss(t, id2) |
| 207 | cv.verifyCacheOrdering(t, id2, id1) |
| 208 | |
| 209 | // fetch id1 again - cache hit, id1 moved to the top of the LRU list |
| 210 | _, _ = c.getEntries(ctx, id1, expirationTime, cs.get(id1), identityWrapper) |
| 211 | cv.verifyCacheHit(t, id1) |
| 212 | cv.verifyCacheOrdering(t, id1, id2) |
| 213 | |
| 214 | // fetch id2 again |
| 215 | _, _ = c.getEntries(ctx, id2, expirationTime, cs.get(id2), identityWrapper) |
| 216 | cv.verifyCacheHit(t, id2) |
| 217 | cv.verifyCacheOrdering(t, id2, id1) |
| 218 | |
| 219 | // fetch id3 |
| 220 | _, _ = c.getEntries(ctx, id3, expirationTime, cs.get(id3), identityWrapper) |
nothing calls this directly
no test coverage detected