(t *testing.T)
| 187 | } |
| 188 | |
| 189 | func TestCacheEviction(t *testing.T) { |
| 190 | once.Do(testSetup) |
| 191 | testKey := "TestCacheEviction-key" |
| 192 | getTestKey := func() { |
| 193 | var res string |
| 194 | for i := 0; i < 10; i++ { |
| 195 | if err := stringGroup.Get(dummyCtx, testKey, StringSink(&res)); err != nil { |
| 196 | t.Fatal(err) |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | fills := countFills(getTestKey) |
| 201 | if fills != 1 { |
| 202 | t.Fatalf("expected 1 cache fill; got %d", fills) |
| 203 | } |
| 204 | |
| 205 | g := stringGroup.(*Group) |
| 206 | evict0 := g.mainCache.nevict |
| 207 | |
| 208 | // Trash the cache with other keys. |
| 209 | var bytesFlooded int64 |
| 210 | // cacheSize/len(testKey) is approximate |
| 211 | for bytesFlooded < cacheSize+1024 { |
| 212 | var res string |
| 213 | key := fmt.Sprintf("dummy-key-%d", bytesFlooded) |
| 214 | stringGroup.Get(dummyCtx, key, StringSink(&res)) |
| 215 | bytesFlooded += int64(len(key) + len(res)) |
| 216 | } |
| 217 | evicts := g.mainCache.nevict - evict0 |
| 218 | if evicts <= 0 { |
| 219 | t.Errorf("evicts = %v; want more than 0", evicts) |
| 220 | } |
| 221 | |
| 222 | // Test that the key is gone. |
| 223 | fills = countFills(getTestKey) |
| 224 | if fills != 1 { |
| 225 | t.Fatalf("expected 1 cache fill after cache trashing; got %d", fills) |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | type fakePeer struct { |
| 230 | hits int |
nothing calls this directly
no test coverage detected
searching dependent graphs…