1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
|
/*
* Copyright (C) 2011 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.common.cache;
import static com.google.common.cache.TestingCacheLoaders.identityLoader;
import static com.google.common.cache.TestingRemovalListeners.countingRemovalListener;
import static com.google.common.cache.TestingWeighers.constantWeigher;
import static com.google.common.cache.TestingWeighers.intKeyWeigher;
import static com.google.common.cache.TestingWeighers.intValueWeigher;
import static com.google.common.truth.Truth.assertThat;
import static java.util.Arrays.asList;
import com.google.common.cache.CacheTesting.Receiver;
import com.google.common.cache.TestingCacheLoaders.IdentityLoader;
import com.google.common.cache.TestingRemovalListeners.CountingRemovalListener;
import java.util.List;
import java.util.Set;
import junit.framework.TestCase;
/**
* Tests relating to cache eviction: what does and doesn't count toward maximumSize, what happens
* when maximumSize is reached, etc.
*
* @author mike nonemacher
*/
public class CacheEvictionTest extends TestCase {
static final int MAX_SIZE = 100;
public void testEviction_setMaxSegmentSize() {
IdentityLoader<Object> loader = identityLoader();
for (int i = 1; i < 1000; i++) {
LoadingCache<Object, Object> cache = CacheBuilder.newBuilder().maximumSize(i).build(loader);
assertEquals(i, CacheTesting.getTotalSegmentSize(cache));
}
}
public void testEviction_setMaxSegmentWeight() {
IdentityLoader<Object> loader = identityLoader();
for (int i = 1; i < 1000; i++) {
LoadingCache<Object, Object> cache =
CacheBuilder.newBuilder().maximumWeight(i).weigher(constantWeigher(1)).build(loader);
assertEquals(i, CacheTesting.getTotalSegmentSize(cache));
}
}
public void testEviction_maxSizeOneSegment() {
IdentityLoader<Integer> loader = identityLoader();
LoadingCache<Integer, Integer> cache =
CacheBuilder.newBuilder().concurrencyLevel(1).maximumSize(MAX_SIZE).build(loader);
for (int i = 0; i < 2 * MAX_SIZE; i++) {
cache.getUnchecked(i);
assertEquals(Math.min(i + 1, MAX_SIZE), cache.size());
}
assertEquals(MAX_SIZE, cache.size());
CacheTesting.checkValidState(cache);
}
public void testEviction_maxWeightOneSegment() {
IdentityLoader<Integer> loader = identityLoader();
LoadingCache<Integer, Integer> cache =
CacheBuilder.newBuilder()
.concurrencyLevel(1)
.maximumWeight(2 * MAX_SIZE)
.weigher(constantWeigher(2))
.build(loader);
for (int i = 0; i < 2 * MAX_SIZE; i++) {
cache.getUnchecked(i);
assertEquals(Math.min(i + 1, MAX_SIZE), cache.size());
}
assertEquals(MAX_SIZE, cache.size());
CacheTesting.checkValidState(cache);
}
public void testEviction_maxSize() {
CountingRemovalListener<Integer, Integer> removalListener = countingRemovalListener();
IdentityLoader<Integer> loader = identityLoader();
LoadingCache<Integer, Integer> cache =
CacheBuilder.newBuilder()
.maximumSize(MAX_SIZE)
.removalListener(removalListener)
.build(loader);
for (int i = 0; i < 2 * MAX_SIZE; i++) {
cache.getUnchecked(i);
assertTrue(cache.size() <= MAX_SIZE);
}
assertEquals(MAX_SIZE, CacheTesting.accessQueueSize(cache));
assertEquals(MAX_SIZE, cache.size());
CacheTesting.processPendingNotifications(cache);
assertEquals(MAX_SIZE, removalListener.getCount());
CacheTesting.checkValidState(cache);
}
public void testEviction_maxWeight() {
CountingRemovalListener<Integer, Integer> removalListener = countingRemovalListener();
IdentityLoader<Integer> loader = identityLoader();
LoadingCache<Integer, Integer> cache =
CacheBuilder.newBuilder()
.maximumWeight(2 * MAX_SIZE)
.weigher(constantWeigher(2))
.removalListener(removalListener)
.build(loader);
for (int i = 0; i < 2 * MAX_SIZE; i++) {
cache.getUnchecked(i);
assertTrue(cache.size() <= MAX_SIZE);
}
assertEquals(MAX_SIZE, CacheTesting.accessQueueSize(cache));
assertEquals(MAX_SIZE, cache.size());
CacheTesting.processPendingNotifications(cache);
assertEquals(MAX_SIZE, removalListener.getCount());
CacheTesting.checkValidState(cache);
}
/**
* With an unlimited-size cache with maxWeight of 0, entries weighing 0 should still be cached.
* Entries with positive weight should not be cached (nor dump existing cache).
*/
public void testEviction_maxWeight_zero() {
CountingRemovalListener<Integer, Integer> removalListener = countingRemovalListener();
IdentityLoader<Integer> loader = identityLoader();
// Even numbers are free, odd are too expensive
Weigher<Integer, Integer> evensOnly =
new Weigher<Integer, Integer>() {
@Override
public int weigh(Integer k, Integer v) {
return k % 2;
}
};
LoadingCache<Integer, Integer> cache =
CacheBuilder.newBuilder()
.concurrencyLevel(1)
.maximumWeight(0)
.weigher(evensOnly)
.removalListener(removalListener)
.build(loader);
// 1 won't be cached
assertThat(cache.getUnchecked(1)).isEqualTo(1);
assertThat(cache.asMap().keySet()).isEmpty();
CacheTesting.processPendingNotifications(cache);
assertThat(removalListener.getCount()).isEqualTo(1);
// 2 will be cached
assertThat(cache.getUnchecked(2)).isEqualTo(2);
assertThat(cache.asMap().keySet()).containsExactly(2);
CacheTesting.processPendingNotifications(cache);
CacheTesting.checkValidState(cache);
assertThat(removalListener.getCount()).isEqualTo(1);
// 4 will be cached
assertThat(cache.getUnchecked(4)).isEqualTo(4);
assertThat(cache.asMap().keySet()).containsExactly(2, 4);
CacheTesting.processPendingNotifications(cache);
assertThat(removalListener.getCount()).isEqualTo(1);
// 5 won't be cached, won't dump cache
assertThat(cache.getUnchecked(5)).isEqualTo(5);
assertThat(cache.asMap().keySet()).containsExactly(2, 4);
CacheTesting.processPendingNotifications(cache);
assertThat(removalListener.getCount()).isEqualTo(2);
// Should we pepper more of these calls throughout the above? Where?
CacheTesting.checkValidState(cache);
}
/**
* Tests that when a single entry exceeds the segment's max weight, the new entry is immediately
* evicted and nothing else.
*/
public void testEviction_maxWeight_entryTooBig() {
CountingRemovalListener<Integer, Integer> removalListener = countingRemovalListener();
IdentityLoader<Integer> loader = identityLoader();
LoadingCache<Integer, Integer> cache =
CacheBuilder.newBuilder()
.concurrencyLevel(1)
.maximumWeight(4)
.weigher(intValueWeigher())
.removalListener(removalListener)
.build(loader);
// caches 2
assertThat(cache.getUnchecked(2)).isEqualTo(2);
assertThat(cache.asMap().keySet()).containsExactly(2);
CacheTesting.processPendingNotifications(cache);
assertThat(removalListener.getCount()).isEqualTo(0);
// caches 3, evicts 2
assertThat(cache.getUnchecked(3)).isEqualTo(3);
assertThat(cache.asMap().keySet()).containsExactly(3);
CacheTesting.processPendingNotifications(cache);
assertThat(removalListener.getCount()).isEqualTo(1);
// doesn't cache 5, doesn't evict
assertThat(cache.getUnchecked(5)).isEqualTo(5);
assertThat(cache.asMap().keySet()).containsExactly(3);
CacheTesting.processPendingNotifications(cache);
assertThat(removalListener.getCount()).isEqualTo(2);
// caches 1, evicts nothing
assertThat(cache.getUnchecked(1)).isEqualTo(1);
assertThat(cache.asMap().keySet()).containsExactly(3, 1);
CacheTesting.processPendingNotifications(cache);
assertThat(removalListener.getCount()).isEqualTo(2);
// caches 4, evicts 1 and 3
assertThat(cache.getUnchecked(4)).isEqualTo(4);
assertThat(cache.asMap().keySet()).containsExactly(4);
CacheTesting.processPendingNotifications(cache);
assertThat(removalListener.getCount()).isEqualTo(4);
// Should we pepper more of these calls throughout the above? Where?
CacheTesting.checkValidState(cache);
}
public void testEviction_overflow() {
CountingRemovalListener<Object, Object> removalListener = countingRemovalListener();
IdentityLoader<Object> loader = identityLoader();
LoadingCache<Object, Object> cache =
CacheBuilder.newBuilder()
.concurrencyLevel(1)
.maximumWeight(1L << 31)
.weigher(constantWeigher(Integer.MAX_VALUE))
.removalListener(removalListener)
.build(loader);
cache.getUnchecked(objectWithHash(0));
cache.getUnchecked(objectWithHash(0));
CacheTesting.processPendingNotifications(cache);
assertEquals(1, removalListener.getCount());
}
public void testUpdateRecency_onGet() {
IdentityLoader<Integer> loader = identityLoader();
final LoadingCache<Integer, Integer> cache =
CacheBuilder.newBuilder().maximumSize(MAX_SIZE).build(loader);
CacheTesting.checkRecency(
cache,
MAX_SIZE,
new Receiver<ReferenceEntry<Integer, Integer>>() {
@Override
public void accept(ReferenceEntry<Integer, Integer> entry) {
cache.getUnchecked(entry.getKey());
}
});
}
public void testUpdateRecency_onInvalidate() {
IdentityLoader<Integer> loader = identityLoader();
final LoadingCache<Integer, Integer> cache =
CacheBuilder.newBuilder().maximumSize(MAX_SIZE).concurrencyLevel(1).build(loader);
CacheTesting.checkRecency(
cache,
MAX_SIZE,
new Receiver<ReferenceEntry<Integer, Integer>>() {
@Override
public void accept(ReferenceEntry<Integer, Integer> entry) {
Integer key = entry.getKey();
cache.invalidate(key);
}
});
}
public void testEviction_lru() {
// test lru within a single segment
IdentityLoader<Integer> loader = identityLoader();
LoadingCache<Integer, Integer> cache =
CacheBuilder.newBuilder().concurrencyLevel(1).maximumSize(10).build(loader);
CacheTesting.warmUp(cache, 0, 10);
Set<Integer> keySet = cache.asMap().keySet();
assertThat(keySet).containsExactly(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
// re-order
getAll(cache, asList(0, 1, 2));
CacheTesting.drainRecencyQueues(cache);
assertThat(keySet).containsExactly(3, 4, 5, 6, 7, 8, 9, 0, 1, 2);
// evict 3, 4, 5
getAll(cache, asList(10, 11, 12));
CacheTesting.drainRecencyQueues(cache);
assertThat(keySet).containsExactly(6, 7, 8, 9, 0, 1, 2, 10, 11, 12);
// re-order
getAll(cache, asList(6, 7, 8));
CacheTesting.drainRecencyQueues(cache);
assertThat(keySet).containsExactly(9, 0, 1, 2, 10, 11, 12, 6, 7, 8);
// evict 9, 0, 1
getAll(cache, asList(13, 14, 15));
CacheTesting.drainRecencyQueues(cache);
assertThat(keySet).containsExactly(2, 10, 11, 12, 6, 7, 8, 13, 14, 15);
}
public void testEviction_weightedLru() {
// test weighted lru within a single segment
IdentityLoader<Integer> loader = identityLoader();
LoadingCache<Integer, Integer> cache =
CacheBuilder.newBuilder()
.concurrencyLevel(1)
.maximumWeight(45)
.weigher(intKeyWeigher())
.build(loader);
CacheTesting.warmUp(cache, 0, 10);
Set<Integer> keySet = cache.asMap().keySet();
assertThat(keySet).containsExactly(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
// re-order
getAll(cache, asList(0, 1, 2));
CacheTesting.drainRecencyQueues(cache);
assertThat(keySet).containsExactly(3, 4, 5, 6, 7, 8, 9, 0, 1, 2);
// evict 3, 4, 5
getAll(cache, asList(10));
CacheTesting.drainRecencyQueues(cache);
assertThat(keySet).containsExactly(6, 7, 8, 9, 0, 1, 2, 10);
// re-order
getAll(cache, asList(6, 7, 8));
CacheTesting.drainRecencyQueues(cache);
assertThat(keySet).containsExactly(9, 0, 1, 2, 10, 6, 7, 8);
// evict 9, 1, 2, 10
getAll(cache, asList(15));
CacheTesting.drainRecencyQueues(cache);
assertThat(keySet).containsExactly(0, 6, 7, 8, 15);
// fill empty space
getAll(cache, asList(9));
CacheTesting.drainRecencyQueues(cache);
assertThat(keySet).containsExactly(0, 6, 7, 8, 15, 9);
// evict 6
getAll(cache, asList(1));
CacheTesting.drainRecencyQueues(cache);
assertThat(keySet).containsExactly(0, 7, 8, 15, 9, 1);
}
public void testEviction_overweight() {
// test weighted lru within a single segment
IdentityLoader<Integer> loader = identityLoader();
LoadingCache<Integer, Integer> cache =
CacheBuilder.newBuilder()
.concurrencyLevel(1)
.maximumWeight(45)
.weigher(intKeyWeigher())
.build(loader);
CacheTesting.warmUp(cache, 0, 10);
Set<Integer> keySet = cache.asMap().keySet();
assertThat(keySet).containsExactly(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
// add an at-the-maximum-weight entry
getAll(cache, asList(45));
CacheTesting.drainRecencyQueues(cache);
assertThat(keySet).containsExactly(0, 45);
// add an over-the-maximum-weight entry
getAll(cache, asList(46));
CacheTesting.drainRecencyQueues(cache);
assertThat(keySet).contains(0);
}
public void testEviction_invalidateAll() {
// test that .invalidateAll() resets total weight state correctly
IdentityLoader<Integer> loader = identityLoader();
LoadingCache<Integer, Integer> cache =
CacheBuilder.newBuilder().concurrencyLevel(1).maximumSize(10).build(loader);
Set<Integer> keySet = cache.asMap().keySet();
assertThat(keySet).isEmpty();
// add 0, 1, 2, 3, 4
getAll(cache, asList(0, 1, 2, 3, 4));
CacheTesting.drainRecencyQueues(cache);
assertThat(keySet).containsExactly(0, 1, 2, 3, 4);
// invalidate all
cache.invalidateAll();
CacheTesting.drainRecencyQueues(cache);
assertThat(keySet).isEmpty();
// add 5, 6, 7, 8, 9, 10, 11, 12
getAll(cache, asList(5, 6, 7, 8, 9, 10, 11, 12));
CacheTesting.drainRecencyQueues(cache);
assertThat(keySet).containsExactly(5, 6, 7, 8, 9, 10, 11, 12);
}
private static void getAll(LoadingCache<Integer, Integer> cache, List<Integer> keys) {
for (int i : keys) {
cache.getUnchecked(i);
}
}
private Object objectWithHash(final int hash) {
return new Object() {
@Override
public int hashCode() {
return hash;
}
};
}
}
|