private CacheBuilderFactory cacheFactory() { return new CacheBuilderFactory() .withKeyStrengths(ImmutableSet.of(Strength.STRONG, Strength.WEAK)) .withValueStrengths(ImmutableSet.copyOf(Strength.values())) .withConcurrencyLevels(ImmutableSet.of(1, 4, 16, 64)) .withMaximumSizes(ImmutableSet.of(0, 1, 10, 100, 1000)) .withInitialCapacities(ImmutableSet.of(0, 1, 10, 100, 1000)) .withExpireAfterWrites(ImmutableSet.of( DurationSpec.of(0, SECONDS), DurationSpec.of(1, SECONDS), DurationSpec.of(1, DAYS))) .withExpireAfterAccesses(ImmutableSet.of( DurationSpec.of(0, SECONDS), DurationSpec.of(1, SECONDS), DurationSpec.of(1, DAYS))) .withRefreshes(ImmutableSet.of( DurationSpec.of(1, SECONDS), DurationSpec.of(1, DAYS))); }
public void testEntryFactory() { assertSame(EntryFactory.STRONG, EntryFactory.getFactory(Strength.STRONG, false, false)); assertSame(EntryFactory.STRONG_ACCESS, EntryFactory.getFactory(Strength.STRONG, true, false)); assertSame(EntryFactory.STRONG_WRITE, EntryFactory.getFactory(Strength.STRONG, false, true)); assertSame(EntryFactory.STRONG_ACCESS_WRITE, EntryFactory.getFactory(Strength.STRONG, true, true)); assertSame(EntryFactory.WEAK, EntryFactory.getFactory(Strength.WEAK, false, false)); assertSame(EntryFactory.WEAK_ACCESS, EntryFactory.getFactory(Strength.WEAK, true, false)); assertSame(EntryFactory.WEAK_WRITE, EntryFactory.getFactory(Strength.WEAK, false, true)); assertSame(EntryFactory.WEAK_ACCESS_WRITE, EntryFactory.getFactory(Strength.WEAK, true, true)); }
private CacheBuilderFactory cacheFactory() { // This is trickier than expected. We plan to put 15 values in each of these (WARMUP_MIN to // WARMUP_MAX), but the tests assume no values get evicted. Even with a maximumSize of 100, one // of the values gets evicted. With weak keys, we use identity equality, which means using // System.identityHashCode, which means the assignment of keys to segments is nondeterministic, // so more than (maximumSize / #segments) keys could get assigned to the same segment, which // would cause one to be evicted. return new CacheBuilderFactory() .withKeyStrengths(ImmutableSet.of(Strength.STRONG, Strength.WEAK)) .withValueStrengths(ImmutableSet.copyOf(Strength.values())) .withConcurrencyLevels(ImmutableSet.of(1, 4, 16, 64)) .withMaximumSizes(ImmutableSet.of(400, 1000)) .withInitialCapacities(ImmutableSet.of(0, 1, 10, 100, 1000)) .withExpireAfterWrites(ImmutableSet.of( // DurationSpec.of(500, MILLISECONDS), DurationSpec.of(1, SECONDS), DurationSpec.of(1, DAYS))) .withExpireAfterAccesses(ImmutableSet.of( // DurationSpec.of(500, MILLISECONDS), DurationSpec.of(1, SECONDS), DurationSpec.of(1, DAYS))) .withRefreshes(ImmutableSet.of( // DurationSpec.of(500, MILLISECONDS), DurationSpec.of(1, SECONDS), DurationSpec.of(1, DAYS))); }
public void testParse_multipleKeys() { CacheBuilderSpec spec = parse("initialCapacity=10,maximumSize=20,concurrencyLevel=30," + "weakKeys,weakValues,expireAfterAccess=10m,expireAfterWrite=1h"); assertEquals(10, spec.initialCapacity.intValue()); assertEquals(20, spec.maximumSize.intValue()); assertNull(spec.maximumWeight); assertEquals(30, spec.concurrencyLevel.intValue()); assertEquals(Strength.WEAK, spec.keyStrength); assertEquals(Strength.WEAK, spec.valueStrength); assertEquals(TimeUnit.HOURS, spec.writeExpirationTimeUnit); assertEquals(TimeUnit.MINUTES, spec.accessExpirationTimeUnit); assertEquals(1L, spec.writeExpirationDuration); assertEquals(10L, spec.accessExpirationDuration); CacheBuilder<?, ?> expected = CacheBuilder.newBuilder() .initialCapacity(10) .maximumSize(20) .concurrencyLevel(30) .weakKeys() .weakValues() .expireAfterAccess(10L, TimeUnit.MINUTES) .expireAfterWrite(1L, TimeUnit.HOURS); assertCacheBuilderEquivalence(expected, CacheBuilder.from(spec)); }
public void testParse_whitespaceAllowed() { CacheBuilderSpec spec = parse(" initialCapacity=10,\nmaximumSize=20,\t\r" + "weakKeys \t ,softValues \n , \r expireAfterWrite \t = 15s\n\n"); assertEquals(10, spec.initialCapacity.intValue()); assertEquals(20, spec.maximumSize.intValue()); assertNull(spec.maximumWeight); assertNull(spec.concurrencyLevel); assertEquals(Strength.WEAK, spec.keyStrength); assertEquals(Strength.SOFT, spec.valueStrength); assertEquals(TimeUnit.SECONDS, spec.writeExpirationTimeUnit); assertEquals(15L, spec.writeExpirationDuration); assertNull(spec.accessExpirationTimeUnit); CacheBuilder<?, ?> expected = CacheBuilder.newBuilder() .initialCapacity(10) .maximumSize(20) .weakKeys() .softValues() .expireAfterWrite(15L, TimeUnit.SECONDS); assertCacheBuilderEquivalence(expected, CacheBuilder.from(spec)); }
Iterable<CacheBuilder<Object, Object>> buildAllPermutations() { @SuppressWarnings("unchecked") Iterable<List<Object>> combinations = buildCartesianProduct(concurrencyLevels, initialCapacities, maximumSizes, expireAfterWrites, expireAfterAccesses, refreshes, keyStrengths, valueStrengths); return Iterables.transform(combinations, new Function<List<Object>, CacheBuilder<Object, Object>>() { @Override public CacheBuilder<Object, Object> apply(List<Object> combination) { return createCacheBuilder( (Integer) combination.get(0), (Integer) combination.get(1), (Integer) combination.get(2), (DurationSpec) combination.get(3), (DurationSpec) combination.get(4), (DurationSpec) combination.get(5), (Strength) combination.get(6), (Strength) combination.get(7)); } }); }
private CacheBuilderFactory cacheFactory() { // This is trickier than expected. We plan to put 15 values in each of these (WARMUP_MIN to // WARMUP_MAX), but the tests assume no values get evicted. Even with a maximumSize of 100, one // of the values gets evicted. With weak keys, we use identity equality, which means using // System.identityHashCode, which means the assignment of keys to segments is nondeterministic, // so more than (maximumSize / #segments) keys could get assigned to the same segment, which // would cause one to be evicted. return new CacheBuilderFactory() .withKeyStrengths(ImmutableSet.of(Strength.STRONG, Strength.WEAK)) .withValueStrengths(ImmutableSet.copyOf(Strength.values())) .withConcurrencyLevels(ImmutableSet.of(1, 4, 16, 64)) .withMaximumSizes(ImmutableSet.of(400, 1000)) .withInitialCapacities(ImmutableSet.of(0, 1, 10, 100, 1000)) .withExpireAfterWrites( ImmutableSet.of( // DurationSpec.of(500, MILLISECONDS), DurationSpec.of(1, SECONDS), DurationSpec.of(1, DAYS))) .withExpireAfterAccesses( ImmutableSet.of( // DurationSpec.of(500, MILLISECONDS), DurationSpec.of(1, SECONDS), DurationSpec.of(1, DAYS))) .withRefreshes( ImmutableSet.of( // DurationSpec.of(500, MILLISECONDS), DurationSpec.of(1, SECONDS), DurationSpec.of(1, DAYS))); }
public void testParse_multipleKeys() { CacheBuilderSpec spec = parse( "initialCapacity=10,maximumSize=20,concurrencyLevel=30," + "weakKeys,weakValues,expireAfterAccess=10m,expireAfterWrite=1h"); assertEquals(10, spec.initialCapacity.intValue()); assertEquals(20, spec.maximumSize.intValue()); assertNull(spec.maximumWeight); assertEquals(30, spec.concurrencyLevel.intValue()); assertEquals(Strength.WEAK, spec.keyStrength); assertEquals(Strength.WEAK, spec.valueStrength); assertEquals(TimeUnit.HOURS, spec.writeExpirationTimeUnit); assertEquals(TimeUnit.MINUTES, spec.accessExpirationTimeUnit); assertEquals(1L, spec.writeExpirationDuration); assertEquals(10L, spec.accessExpirationDuration); CacheBuilder<?, ?> expected = CacheBuilder.newBuilder() .initialCapacity(10) .maximumSize(20) .concurrencyLevel(30) .weakKeys() .weakValues() .expireAfterAccess(10L, TimeUnit.MINUTES) .expireAfterWrite(1L, TimeUnit.HOURS); assertCacheBuilderEquivalence(expected, CacheBuilder.from(spec)); }
public void testParse_whitespaceAllowed() { CacheBuilderSpec spec = parse( " initialCapacity=10,\nmaximumSize=20,\t\r" + "weakKeys \t ,softValues \n , \r expireAfterWrite \t = 15s\n\n"); assertEquals(10, spec.initialCapacity.intValue()); assertEquals(20, spec.maximumSize.intValue()); assertNull(spec.maximumWeight); assertNull(spec.concurrencyLevel); assertEquals(Strength.WEAK, spec.keyStrength); assertEquals(Strength.SOFT, spec.valueStrength); assertEquals(TimeUnit.SECONDS, spec.writeExpirationTimeUnit); assertEquals(15L, spec.writeExpirationDuration); assertNull(spec.accessExpirationTimeUnit); CacheBuilder<?, ?> expected = CacheBuilder.newBuilder() .initialCapacity(10) .maximumSize(20) .weakKeys() .softValues() .expireAfterWrite(15L, TimeUnit.SECONDS); assertCacheBuilderEquivalence(expected, CacheBuilder.from(spec)); }
public void testDefaults() { LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder()); assertSame(Strength.STRONG, map.keyStrength); assertSame(Strength.STRONG, map.valueStrength); assertSame(map.keyStrength.defaultEquivalence(), map.keyEquivalence); assertSame(map.valueStrength.defaultEquivalence(), map.valueEquivalence); assertEquals(0, map.expireAfterAccessNanos); assertEquals(0, map.expireAfterWriteNanos); assertEquals(0, map.refreshNanos); assertEquals(CacheBuilder.UNSET_INT, map.maxWeight); assertSame(EntryFactory.STRONG, map.entryFactory); assertSame(CacheBuilder.NullListener.INSTANCE, map.removalListener); assertSame(DISCARDING_QUEUE, map.removalNotificationQueue); assertSame(NULL_TICKER, map.ticker); assertEquals(4, map.concurrencyLevel); // concurrency level assertThat(map.segments).hasLength(4); // initial capacity / concurrency level assertEquals(16 / map.segments.length, map.segments[0].table.length()); assertFalse(map.evictsBySize()); assertFalse(map.expires()); assertFalse(map.expiresAfterWrite()); assertFalse(map.expiresAfterAccess()); assertFalse(map.refreshes()); }
private static void checkStrength( LocalCache<Object, Object> map, Strength keyStrength, Strength valueStrength) { assertSame(keyStrength, map.keyStrength); assertSame(valueStrength, map.valueStrength); assertSame(keyStrength.defaultEquivalence(), map.keyEquivalence); assertSame(valueStrength.defaultEquivalence(), map.valueEquivalence); }
public void testParse_weakKeys() { CacheBuilderSpec spec = parse("weakKeys"); assertNull(spec.initialCapacity); assertNull(spec.maximumSize); assertNull(spec.maximumWeight); assertNull(spec.concurrencyLevel); assertEquals(Strength.WEAK, spec.keyStrength); assertNull(spec.valueStrength); assertNull(spec.writeExpirationTimeUnit); assertNull(spec.accessExpirationTimeUnit); assertCacheBuilderEquivalence( CacheBuilder.newBuilder().weakKeys(), CacheBuilder.from(spec)); }
public void testParse_softValues() { CacheBuilderSpec spec = parse("softValues"); assertNull(spec.initialCapacity); assertNull(spec.maximumSize); assertNull(spec.maximumWeight); assertNull(spec.concurrencyLevel); assertNull(spec.keyStrength); assertEquals(Strength.SOFT, spec.valueStrength); assertNull(spec.writeExpirationTimeUnit); assertNull(spec.accessExpirationTimeUnit); assertCacheBuilderEquivalence( CacheBuilder.newBuilder().softValues(), CacheBuilder.from(spec)); }
public void testParse_weakValues() { CacheBuilderSpec spec = parse("weakValues"); assertNull(spec.initialCapacity); assertNull(spec.maximumSize); assertNull(spec.maximumWeight); assertNull(spec.concurrencyLevel); assertNull(spec.keyStrength); assertEquals(Strength.WEAK, spec.valueStrength); assertNull(spec.writeExpirationTimeUnit); assertNull(spec.accessExpirationTimeUnit); assertCacheBuilderEquivalence( CacheBuilder.newBuilder().weakValues(), CacheBuilder.from(spec)); }
private CacheBuilder<Object, Object> createCacheBuilder( Integer concurrencyLevel, Integer initialCapacity, Integer maximumSize, DurationSpec expireAfterWrite, DurationSpec expireAfterAccess, DurationSpec refresh, Strength keyStrength, Strength valueStrength) { CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder(); if (concurrencyLevel != null) { builder.concurrencyLevel(concurrencyLevel); } if (initialCapacity != null) { builder.initialCapacity(initialCapacity); } if (maximumSize != null) { builder.maximumSize(maximumSize); } if (expireAfterWrite != null) { builder.expireAfterWrite(expireAfterWrite.duration, expireAfterWrite.unit); } if (expireAfterAccess != null) { builder.expireAfterAccess(expireAfterAccess.duration, expireAfterAccess.unit); } if (refresh != null) { builder.refreshAfterWrite(refresh.duration, refresh.unit); } if (keyStrength != null) { builder.setKeyStrength(keyStrength); } if (valueStrength != null) { builder.setValueStrength(valueStrength); } return builder; }
public void testDefaults() { LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder()); assertSame(Strength.STRONG, map.keyStrength); assertSame(Strength.STRONG, map.valueStrength); assertSame(map.keyStrength.defaultEquivalence(), map.keyEquivalence); assertSame(map.valueStrength.defaultEquivalence(), map.valueEquivalence); assertEquals(0, map.expireAfterAccessNanos); assertEquals(0, map.expireAfterWriteNanos); assertEquals(0, map.refreshNanos); assertEquals(CacheBuilder.UNSET_INT, map.maxWeight); assertSame(EntryFactory.STRONG, map.entryFactory); assertSame(CacheBuilder.NullListener.INSTANCE, map.removalListener); assertSame(DISCARDING_QUEUE, map.removalNotificationQueue); assertSame(NULL_TICKER, map.ticker); assertEquals(4, map.concurrencyLevel); // concurrency level assertEquals(4, map.segments.length); // initial capacity / concurrency level assertEquals(16 / map.segments.length, map.segments[0].table.length()); assertFalse(map.evictsBySize()); assertFalse(map.expires()); assertFalse(map.expiresAfterWrite()); assertFalse(map.expiresAfterAccess()); assertFalse(map.refreshes()); }