com.github.benmanes.caffeine.guava.CaffeinatedGuava Java Examples

The following examples show how to use com.github.benmanes.caffeine.guava.CaffeinatedGuava. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CacheEvictionTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
public void testEviction_overweight() {
  // test weighted lru within a single segment
  IdentityLoader<Integer> loader = identityLoader();
  LoadingCache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .executor(MoreExecutors.directExecutor())
      .maximumWeight(45)
      .weigher(intKeyWeigher()), 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);
}
 
Example #2
Source File: CacheLoadingTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
/**
 * On a successful concurrent computation, only one thread does the work, but all the threads get
 * the same result.
 */
private static void testConcurrentLoadingDefault(Caffeine<Object, Object> builder)
    throws InterruptedException {

  int count = 10;
  final AtomicInteger callCount = new AtomicInteger();
  final CountDownLatch startSignal = new CountDownLatch(count + 1);
  final Object result = new Object();

  LoadingCache<String, Object> cache = CaffeinatedGuava.build(builder,
      new CacheLoader<String, Object>() {
        @Override public Object load(String key) {
          callCount.incrementAndGet();
          assertTrue(Uninterruptibles.awaitUninterruptibly(startSignal, 300, TimeUnit.SECONDS));
          return result;
        }
      });

  List<Object> resultArray = doConcurrentGet(cache, "bar", count, startSignal);

  assertEquals(1, callCount.get());
  for (int i = 0; i < count; i++) {
    assertSame("result(" + i + ") didn't match expected", result, resultArray.get(i));
  }
}
 
Example #3
Source File: CacheLoadingTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
public void testBulkLoadUncheckedException() throws ExecutionException {
  Exception e = new RuntimeException();
  CacheLoader<Object, Object> loader = exceptionLoader(e);
  LoadingCache<Object, Object> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .recordStats(), bulkLoader(loader));
  CacheStats stats = cache.stats();
  assertEquals(0, stats.missCount());
  assertEquals(0, stats.loadSuccessCount());
  assertEquals(0, stats.loadExceptionCount());
  assertEquals(0, stats.hitCount());

  try {
    cache.getAll(asList(new Object()));
    fail();
  } catch (UncheckedExecutionException expected) {
    assertSame(e, expected.getCause());
  }
  stats = cache.stats();
  assertEquals(1, stats.missCount());
  assertEquals(0, stats.loadSuccessCount());
  assertEquals(1, stats.loadExceptionCount());
  assertEquals(0, stats.hitCount());
}
 
Example #4
Source File: CacheLoadingTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
public void testBulkLoadCheckedException() {
  Exception e = new Exception();
  CacheLoader<Object, Object> loader = exceptionLoader(e);
  LoadingCache<Object, Object> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .recordStats(), bulkLoader(loader));
  CacheStats stats = cache.stats();
  assertEquals(0, stats.missCount());
  assertEquals(0, stats.loadSuccessCount());
  assertEquals(0, stats.loadExceptionCount());
  assertEquals(0, stats.hitCount());

  try {
    cache.getAll(asList(new Object()));
    fail();
  } catch (ExecutionException expected) {
    assertSame(e, expected.getCause());
  }
  stats = cache.stats();
  assertEquals(1, stats.missCount());
  assertEquals(0, stats.loadSuccessCount());
  assertEquals(1, stats.loadExceptionCount());
  assertEquals(0, stats.hitCount());
}
 
Example #5
Source File: CacheLoadingTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
public void testBulkLoadError() throws ExecutionException {
  Error e = new Error();
  CacheLoader<Object, Object> loader = errorLoader(e);
  LoadingCache<Object, Object> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .recordStats(), bulkLoader(loader));
  CacheStats stats = cache.stats();
  assertEquals(0, stats.missCount());
  assertEquals(0, stats.loadSuccessCount());
  assertEquals(0, stats.loadExceptionCount());
  assertEquals(0, stats.hitCount());

  try {
    cache.getAll(asList(new Object()));
    fail();
  } catch (ExecutionError expected) {
    assertSame(e, expected.getCause());
  }
  stats = cache.stats();
  assertEquals(1, stats.missCount());
  assertEquals(0, stats.loadSuccessCount());
  assertEquals(1, stats.loadExceptionCount());
  assertEquals(0, stats.hitCount());
}
 
Example #6
Source File: CacheLoadingTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
public void testBulkLoadNull() throws ExecutionException {
  LoadingCache<Object, Object> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .recordStats().executor(MoreExecutors.directExecutor()), bulkLoader(constantLoader(null)));
  CacheStats stats = cache.stats();
  assertEquals(0, stats.missCount());
  assertEquals(0, stats.loadSuccessCount());
  assertEquals(0, stats.loadExceptionCount());
  assertEquals(0, stats.hitCount());

  try {
    cache.getAll(asList(new Object()));
    fail();
  } catch (InvalidCacheLoadException expected) {}
  stats = cache.stats();
  assertEquals(1, stats.missCount());
  assertEquals(0, stats.loadSuccessCount());
  assertEquals(1, stats.loadExceptionCount());
  assertEquals(0, stats.hitCount());
}
 
Example #7
Source File: CacheLoadingTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
public void testBulkLoad_partial() throws ExecutionException {
  final Object extraKey = new Object();
  final Object extraValue = new Object();
  CacheLoader<Object, Object> loader = new CacheLoader<Object, Object>() {
    @Override
    public Object load(Object key) {
      throw new AssertionError();
    }

    @Override
    public Map<Object, Object> loadAll(Iterable<? extends Object> keys) {
      Map<Object, Object> result = Maps.newHashMap();
      // ignore request keys
      result.put(extraKey, extraValue);
      return result;
    }
  };
  LoadingCache<Object, Object> cache = CaffeinatedGuava.build(Caffeine.newBuilder(), loader);

  Object[] lookupKeys = new Object[] { new Object(), new Object(), new Object() };
  try {
    cache.getAll(asList(lookupKeys));
    fail();
  } catch (InvalidCacheLoadException expected) {}
  assertSame(extraValue, cache.asMap().get(extraKey));
}
 
Example #8
Source File: CacheLoadingTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
public void testBulkLoadInterruptedException() {
  Exception e = new InterruptedException();
  CacheLoader<Object, Object> loader = exceptionLoader(e);
  LoadingCache<Object, Object> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .recordStats().executor(MoreExecutors.directExecutor()), bulkLoader(loader));
  CacheStats stats = cache.stats();
  assertEquals(0, stats.missCount());
  assertEquals(0, stats.loadSuccessCount());
  assertEquals(0, stats.loadExceptionCount());
  assertEquals(0, stats.hitCount());

  try {
    cache.getAll(asList(new Object()));
    fail();
  } catch (ExecutionException expected) {
    assertSame(e, expected.getCause());
  }
  assertTrue(Thread.interrupted());
  stats = cache.stats();
  assertEquals(1, stats.missCount());
  assertEquals(0, stats.loadSuccessCount());
  assertEquals(1, stats.loadExceptionCount());
  assertEquals(0, stats.hitCount());
}
 
Example #9
Source File: CacheBuilderGwtTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
public void testAsMapEntrySet() {
  Cache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .expireAfterWrite(1000, TimeUnit.MILLISECONDS)
      .executor(MoreExecutors.directExecutor())
      .ticker(fakeTicker::read));

  cache.put(10, 20);
  fakeTicker.advance(500, TimeUnit.MILLISECONDS);
  cache.put(20, 22);
  cache.put(5, 10);

  fakeTicker.advance(501, TimeUnit.MILLISECONDS);

  int sum = 0;
  for (Map.Entry<Integer, Integer> current : cache.asMap().entrySet()) {
    sum += current.getKey() + current.getValue();
  }
  assertEquals(57, sum);
}
 
Example #10
Source File: CacheBuilderGwtTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
public void testAsMapValues_contains() {
  Cache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .expireAfterWrite(1000, TimeUnit.MILLISECONDS)
      .executor(MoreExecutors.directExecutor())
      .ticker(fakeTicker::read));

  cache.put(10, 20);
  fakeTicker.advance(500, TimeUnit.MILLISECONDS);
  cache.put(20, 22);
  cache.put(5, 10);

  fakeTicker.advance(501, TimeUnit.MILLISECONDS);

  assertTrue(cache.asMap().values().contains(22));
  assertTrue(cache.asMap().values().contains(10));
  assertFalse(cache.asMap().values().contains(20));
}
 
Example #11
Source File: NullCacheTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
public void testGet() {
  Object computed = new Object();
  LoadingCache<Object, Object> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .executor(MoreExecutors.directExecutor())
      .maximumSize(0)
      .removalListener(listener),
      constantLoader(computed));

  Object key = new Object();
  assertSame(computed, cache.getUnchecked(key));
  RemovalNotification<Object, Object> notification = listener.remove();
  assertSame(key, notification.getKey());
  assertSame(computed, notification.getValue());
  assertSame(RemovalCause.SIZE, notification.getCause());
  assertTrue(listener.isEmpty());
  checkEmpty(cache);
}
 
Example #12
Source File: NullCacheTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
public void testGet_expireAfterWrite() {
  // Guava sends a notification with SIZE as the removal cause by redefining 0 expiration as
  // a maximum size of zero. This is not done as expiration can be dynamically updated

  Object computed = new Object();
  LoadingCache<Object, Object> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .executor(MoreExecutors.directExecutor())
      .expireAfterWrite(0, SECONDS)
      .removalListener(listener),
      constantLoader(computed));

  Object key = new Object();
  assertSame(computed, cache.getUnchecked(key));
  RemovalNotification<Object, Object> notification = listener.remove();
  assertSame(key, notification.getKey());
  assertSame(computed, notification.getValue());
  assertSame(RemovalCause.EXPIRED, notification.getCause());
  assertTrue(listener.isEmpty());
  checkEmpty(cache);
}
 
Example #13
Source File: NullCacheTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
public void testGet_expireAfterAccess() {
  // Guava sends a notification with SIZE as the removal cause by redefining 0 expiration as
  // a maximum size of zero. This is not done as expiration can be dynamically updated

  Object computed = new Object();
  LoadingCache<Object, Object> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .executor(MoreExecutors.directExecutor())
      .expireAfterAccess(0, SECONDS)
      .removalListener(listener),
      constantLoader(computed));

  Object key = new Object();
  assertSame(computed, cache.getUnchecked(key));
  RemovalNotification<Object, Object> notification = listener.remove();
  assertSame(key, notification.getKey());
  assertSame(computed, notification.getValue());
  assertSame(RemovalCause.EXPIRED, notification.getCause());
  assertTrue(listener.isEmpty());
  checkEmpty(cache);
}
 
Example #14
Source File: NullCacheTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
public void testGet_runtimeException() {
  final RuntimeException e = new RuntimeException();
  LoadingCache<Object, Object> map = CaffeinatedGuava.build(Caffeine.newBuilder()
      .maximumSize(0)
      .removalListener(listener),
      exceptionLoader(e));

  try {
    map.getUnchecked(new Object());
    fail();
  } catch (UncheckedExecutionException uee) {
    assertSame(e, uee.getCause());
  }
  assertTrue(listener.isEmpty());
  checkEmpty(map);
}
 
Example #15
Source File: CacheBuilderGwtTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
public void testAsMapKeySet_contains() {
  Cache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .expireAfterWrite(1000, TimeUnit.MILLISECONDS)
      .executor(MoreExecutors.directExecutor())
      .ticker(fakeTicker::read));

  cache.put(10, 20);
  fakeTicker.advance(500, TimeUnit.MILLISECONDS);
  cache.put(20, 22);
  cache.put(5, 10);

  fakeTicker.advance(501, TimeUnit.MILLISECONDS);

  assertTrue(cache.asMap().keySet().contains(20));
  assertTrue(cache.asMap().keySet().contains(5));
  assertFalse(cache.asMap().keySet().contains(10));
}
 
Example #16
Source File: CacheEvictionTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
public void testEviction_invalidateAll() {
  // test that .invalidateAll() resets total weight state correctly
  IdentityLoader<Integer> loader = identityLoader();
  LoadingCache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .maximumSize(10), 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);
}
 
Example #17
Source File: CacheBuilderGwtTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
public void testLoader() throws ExecutionException {

    final Cache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder());

    Callable<Integer> loader = new Callable<Integer>() {
      private int i = 0;

      @Override
      public Integer call() throws Exception {
        return ++i;
      }
    };

    cache.put(0, 10);

    assertEquals(Integer.valueOf(10), cache.get(0, loader));
    assertEquals(Integer.valueOf(1), cache.get(20, loader));
    assertEquals(Integer.valueOf(2), cache.get(34, loader));

    cache.invalidate(0);
    assertEquals(Integer.valueOf(3), cache.get(0, loader));

    cache.put(0, 10);
    cache.invalidateAll();
    assertEquals(Integer.valueOf(4), cache.get(0, loader));
  }
 
Example #18
Source File: CacheEvictionTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
public void testEviction_maxWeight() {
  CountingRemovalListener<Integer, Integer> removalListener = countingRemovalListener();
  IdentityLoader<Integer> loader = identityLoader();
  LoadingCache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .maximumWeight(2 * MAX_SIZE)
      .weigher(constantWeigher(2))
      .executor(MoreExecutors.directExecutor())
      .removalListener(removalListener), loader);
  for (int i = 0; i < 2 * MAX_SIZE; i++) {
    cache.getUnchecked(i);
    assertTrue(cache.size() <= MAX_SIZE);
  }

  assertEquals(MAX_SIZE, cache.size());
  assertEquals(MAX_SIZE, removalListener.getCount());
  CacheTesting.checkValidState(cache);
}
 
Example #19
Source File: CacheEvictionTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
public void testEviction_maxSize() {
  CountingRemovalListener<Integer, Integer> removalListener = countingRemovalListener();
  IdentityLoader<Integer> loader = identityLoader();
  LoadingCache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .maximumSize(MAX_SIZE)
      .executor(MoreExecutors.directExecutor())
      .removalListener(removalListener), loader);
  for (int i = 0; i < 2 * MAX_SIZE; i++) {
    cache.getUnchecked(i);
    assertTrue(cache.size() <= MAX_SIZE);
  }

  assertEquals(MAX_SIZE, cache.size());
  assertEquals(MAX_SIZE, removalListener.getCount());
  CacheTesting.checkValidState(cache);
}
 
Example #20
Source File: CacheBuilderGwtTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
public void testLoadingCache() throws ExecutionException {
  CacheLoader<Integer, Integer> loader = new CacheLoader<Integer, Integer>() {
    int i = 0;
    @Override
    public Integer load(Integer key) throws Exception {
      return i++;
    }

  };

  LoadingCache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder(), loader);

  cache.put(10, 20);

  ImmutableSet<Integer> keys = ImmutableSet.of(10, 20, 30, 54, 443, 1);
  Map<Integer, Integer> map = cache.getAll(keys);

  // Original test depended on order, but that was only expected for the emulated implementation
  assertEquals(keys, map.keySet());
  assertEquals(ImmutableSet.of(0, 1, 2, 3, 4, 20), ImmutableSet.copyOf(map.values()));

  assertEquals(Integer.valueOf(5), cache.get(6));
  assertEquals(Integer.valueOf(6), cache.apply(7));
}
 
Example #21
Source File: CacheBuilderGwtTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
public void testExpireAfterAccess() {
  final Cache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .expireAfterAccess(1000, TimeUnit.MILLISECONDS)
      .executor(MoreExecutors.directExecutor())
      .ticker(fakeTicker::read));

  cache.put(0, 10);
  cache.put(2, 30);

  fakeTicker.advance(999, TimeUnit.MILLISECONDS);
  assertEquals(Integer.valueOf(30), cache.getIfPresent(2));
  fakeTicker.advance(1, TimeUnit.MILLISECONDS);
  assertEquals(Integer.valueOf(30), cache.getIfPresent(2));
  fakeTicker.advance(1000, TimeUnit.MILLISECONDS);
  assertEquals(null, cache.getIfPresent(0));
}
 
Example #22
Source File: CacheBuilderGwtTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
public void testExpireAfterWrite() {
  final Cache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .expireAfterWrite(1000, TimeUnit.MILLISECONDS)
      .executor(MoreExecutors.directExecutor())
      .ticker(fakeTicker::read));

  cache.put(10, 100);
  cache.put(20, 200);
  cache.put(4, 2);

  fakeTicker.advance(999, TimeUnit.MILLISECONDS);
  assertEquals(Integer.valueOf(100), cache.getIfPresent(10));
  assertEquals(Integer.valueOf(200), cache.getIfPresent(20));
  assertEquals(Integer.valueOf(2), cache.getIfPresent(4));

  fakeTicker.advance(2, TimeUnit.MILLISECONDS);
  assertEquals(null, cache.getIfPresent(10));
  assertEquals(null, cache.getIfPresent(20));
  assertEquals(null, cache.getIfPresent(4));

  cache.put(10, 20);
  assertEquals(Integer.valueOf(20), cache.getIfPresent(10));

  fakeTicker.advance(1000, TimeUnit.MILLISECONDS);
  assertEquals(null, cache.getIfPresent(10));
}
 
Example #23
Source File: CacheBuilderGwtTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
public void testPutAll() {
  Cache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder());

  cache.putAll(ImmutableMap.of(10, 20, 30, 50, 60, 90));

  assertEquals(Integer.valueOf(20), cache.getIfPresent(10));
  assertEquals(Integer.valueOf(50), cache.getIfPresent(30));
  assertEquals(Integer.valueOf(90), cache.getIfPresent(60));

  cache.asMap().putAll(ImmutableMap.of(10, 50, 30, 20, 60, 70, 5, 5));

  assertEquals(Integer.valueOf(50), cache.getIfPresent(10));
  assertEquals(Integer.valueOf(20), cache.getIfPresent(30));
  assertEquals(Integer.valueOf(70), cache.getIfPresent(60));
  assertEquals(Integer.valueOf(5), cache.getIfPresent(5));
}
 
Example #24
Source File: CacheBuilderGwtTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
public void testInvalidateAll() {
  Cache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder());

  cache.put(654, 2675);
  cache.put(2456, 56);
  cache.put(2, 15);

  cache.invalidateAll();
  assertFalse(cache.asMap().containsKey(654));
  assertFalse(cache.asMap().containsKey(2456));
  assertFalse(cache.asMap().containsKey(2));

  cache.put(654, 2675);
  cache.put(2456, 56);
  cache.put(2, 15);
  cache.put(1, 3);

  cache.invalidateAll(ImmutableSet.of(1, 2));

  assertFalse(cache.asMap().containsKey(1));
  assertFalse(cache.asMap().containsKey(2));
  assertTrue(cache.asMap().containsKey(654));
  assertTrue(cache.asMap().containsKey(2456));
}
 
Example #25
Source File: CacheBuilderGwtTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
public void testAsMap_containsValue() {
  Cache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .expireAfterWrite(20000, TimeUnit.MILLISECONDS)
      .executor(MoreExecutors.directExecutor())
      .ticker(fakeTicker::read));

  cache.put(654, 2675);
  fakeTicker.advance(10000, TimeUnit.MILLISECONDS);
  cache.put(2456, 56);
  cache.put(2, 15);

  fakeTicker.advance(10001, TimeUnit.MILLISECONDS);

  assertTrue(cache.asMap().containsValue(15));
  assertTrue(cache.asMap().containsValue(56));
  assertFalse(cache.asMap().containsValue(2675));
}
 
Example #26
Source File: CacheBuilderGwtTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
public void testAsMap_containsKey() {
  Cache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .expireAfterWrite(20000, TimeUnit.MILLISECONDS)
      .executor(MoreExecutors.directExecutor())
      .ticker(fakeTicker::read));

  cache.put(654, 2675);
  fakeTicker.advance(10000, TimeUnit.MILLISECONDS);
  cache.put(2456, 56);
  cache.put(2, 15);

  fakeTicker.advance(10001, TimeUnit.MILLISECONDS);

  assertTrue(cache.asMap().containsKey(2));
  assertTrue(cache.asMap().containsKey(2456));
  assertFalse(cache.asMap().containsKey(654));
}
 
Example #27
Source File: CacheBuilderTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@GwtIncompatible("weigher")
public void testWeigher_withoutMaximumWeight() {
  Caffeine<Object, Object> builder = Caffeine.newBuilder()
      .weigher(constantWeigher(42));
  try {
    CaffeinatedGuava.build(builder, identityLoader());
    fail();
  } catch (IllegalStateException expected) {}
}
 
Example #28
Source File: CacheExpirationTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
public void testRemovalScheduler_expireAfterBoth() {
  FakeTicker ticker = new FakeTicker();
  CountingRemovalListener<String, Integer> removalListener = countingRemovalListener();
  WatchedCreatorLoader loader = new WatchedCreatorLoader();
  LoadingCache<String, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .expireAfterAccess(EXPIRING_TIME, MILLISECONDS)
      .expireAfterWrite(EXPIRING_TIME, MILLISECONDS)
      .executor(MoreExecutors.directExecutor())
      .removalListener(removalListener)
      .ticker(ticker::read),
      loader);
  runRemovalScheduler(cache, removalListener, loader, ticker, KEY_PREFIX, EXPIRING_TIME);
}
 
Example #29
Source File: CacheExpirationTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
public void testRemovalScheduler_expireAfterAccess() {
  FakeTicker ticker = new FakeTicker();
  CountingRemovalListener<String, Integer> removalListener = countingRemovalListener();
  WatchedCreatorLoader loader = new WatchedCreatorLoader();
  LoadingCache<String, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
      .expireAfterAccess(EXPIRING_TIME, MILLISECONDS)
      .executor(MoreExecutors.directExecutor())
      .removalListener(removalListener)
      .ticker(ticker::read),
      loader);
  runRemovalScheduler(cache, removalListener, loader, ticker, KEY_PREFIX, EXPIRING_TIME);
}
 
Example #30
Source File: CacheBuilderTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
public void testTimeToIdleAndToLive() {
  CaffeinatedGuava.build(Caffeine.newBuilder()
      .expireAfterWrite(1, NANOSECONDS)
      .expireAfterAccess(1, NANOSECONDS),
      identityLoader());
  // well, it didn't blow up.
}