@Override protected void insert(Cache<Number, CharSequence> testCache, Map<Number, CharSequence> entries) { for (Map.Entry<Number, CharSequence> entry : entries.entrySet()) { testCache.putIfAbsent(entry.getKey(), entry.getValue()); } } }
@Override public void record(long key) { Object value = cache.putIfAbsent(key, key); if (value == null) { size++; policyStats.recordMiss(); if (size > maximumSize) { policyStats.recordEviction(); size--; } } else { policyStats.recordHit(); } }
@Test public void testPutIfAbsentWithWriterException_should_call_writer() throws Exception { doThrow(new Exception("Mock Exception: cannot write 1")).when(cacheLoaderWriter).write(eq(1), eq("one")); try { testCache.putIfAbsent(1, "one"); fail("expected CacheWritingException"); } catch (CacheWritingException ex) { // expected } testCache.put(2, "two"); testCache.putIfAbsent(2, "two#2"); }
private void putIfAbsent(Cache<Long, String> cache, String value, boolean addToCacheRecords) { cache.putIfAbsent(KEY, value); if (addToCacheRecords) { cacheRecords.add(new Record(KEY, cache.get(KEY))); } }
@Test public void ttiExpiryPut() { Cache<Integer, String> cache = createCache(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofMillis(100))); cache.put(1, "a"); assertThat(cache.putIfAbsent(1, "b")).isEqualTo("a"); timeSource.setTimeMillis(100); assertThat(cache.putIfAbsent(1, "c")).isNull(); }
@Test public void testSimplePutIfAbsentWithLoaderAndWriter_absent() throws Exception { assertThat(testCache.containsKey(1), is(false)); assertThat(testCache.putIfAbsent(1, "one"), is(nullValue())); assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one")); verify(cacheLoaderWriter, times(1)).load(eq(1)); verify(cacheLoaderWriter, times(1)).write(eq(1), eq("one")); }
@Test public void testSimplePutIfAbsentWithLoaderAndWriter_existsInSor() throws Exception { when(cacheLoaderWriter.load(eq(1))).thenAnswer((Answer) invocation -> "un"); assertThat(testCache.containsKey(1), is(false)); assertThat(testCache.putIfAbsent(1, "one"), Matchers.<CharSequence>equalTo("un")); assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("un")); verify(cacheLoaderWriter, times(1)).load(eq(1)); verifyNoMoreInteractions(cacheLoaderWriter); }
@Test @SuppressWarnings("unchecked") public void testSimplePutIfAbsentWithLoaderAndWriter_existsInStore() throws Exception { testCache.put(1, "un"); reset(cacheLoaderWriter); assertThat(testCache.putIfAbsent(1, "one"), Matchers.<CharSequence>equalTo("un")); assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("un")); verifyZeroInteractions(cacheLoaderWriter); }
@Test public void putIfAbsent() { expect(cache.putIfAbsent(1, "a")).isNull(); changesOf(0, 1, 1, 0); expect(cache.putIfAbsent(1, "b")).isEqualTo("a"); changesOf(1, 0, 0, 0); }
@Test public void testSimplePutIfAbsent() throws Exception { Cache<Number, CharSequence> testCache = cacheManager.createCache("testCache", newCacheConfigurationBuilder(Number.class, CharSequence.class, heap(10))); CharSequence one = testCache.putIfAbsent(1, "one"); assertThat(one, is(nullValue())); CharSequence one_2 = testCache.putIfAbsent(1, "one#2"); assertThat(one_2, Matchers.<CharSequence>equalTo("one")); assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one")); }
@Test public void putIfAbsent() { expect(cache.putIfAbsent(1, "a")).isNull(); changesOf(0, 1, 1, 0); expect(cache.putIfAbsent(1, "b")).isEqualTo("a"); changesOf(1, 0, 0, 0); }
@Test public void testSimplePutWithExpiry_putIfAbsent() throws Exception { insert(testCache, getEntries()); assertThat(cacheSize(testCache), is(2)); manualTimeSource.setTimeMillis(1001); assertThat(testCache.putIfAbsent(1, "one#2"), is(nullValue())); assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one#2")); assertThat(testCache.putIfAbsent(2, "two#2"), is(nullValue())); assertThat(testCache.get(2), Matchers.<CharSequence>equalTo("two#2")); }
@Test public void putIfAbsent_absent() { createCacheManager(); cache = createCache(); assertThat(cache.putIfAbsent(1, "a")).isNull(); assertThat(cache.get(1)).isEqualTo("a"); changesOf(1, 1, 1, 0); }
@Test public void putIfAbsent_present() { createCacheManager(); cache = createCache(); cache.put(1, "a"); assertThat(cache.putIfAbsent(1, "b")).isEqualTo("a"); changesOf(1, 0, 1, 0); }
@Test public void putIfAbsentLoaderWriter_absentAndLoaded() throws Exception { createCacheManager(); CacheLoaderWriter<Integer, String> loader = mockLoader(); when(loader.load(1)).thenReturn("a"); CacheConfigurationBuilder<Integer, String> builder = baseConfig() .withLoaderWriter(loader); cache = createCache(builder); assertThat(cache.putIfAbsent(1, "b")).isEqualTo("a"); assertThat(cache.get(1)).isEqualTo("a"); changesOf(2, 0, 0, 0); }
@Test public void noExpiryPut() { Cache<Integer, String> cache = createCache(ExpiryPolicyBuilder.noExpiration()); cache.put(1, "a"); timeSource.setTimeMillis(Long.MAX_VALUE); assertThat(cache.putIfAbsent(1, "b")).isEqualTo("a"); }
@Test public void putIfAbsentLoaderWriterNotAtomic_absent() throws Exception { createNotAtomicCacheManager(); CacheLoaderWriter<Integer, String> loader = mockLoader(); CacheConfigurationBuilder<Integer, String> builder = baseConfig() .withLoaderWriter(loader); cache = createCache(builder); assertThat(cache.putIfAbsent(1, "a")).isNull(); verify(loader).write(1, "a"); assertThat(cache.get(1)).isEqualTo("a"); changesOf(1, 1, 1, 0); }
@Test public void putIfAbsentLoaderWriter_present() throws Exception { createCacheManager(); CacheLoaderWriter<Integer, String> loader = mockLoader(); CacheConfigurationBuilder<Integer, String> builder = baseConfig() .withLoaderWriter(loader); cache = createCache(builder); cache.put(1, "a"); assertThat(cache.putIfAbsent(1, "b")).isEqualTo("a"); verify(loader).write(1, "a"); changesOf(1, 0, 1, 0); }
@Test public void putIfAbsent_presentButExpired() { createCacheManager(); CacheConfigurationBuilder<Integer, String> builder = baseConfig() .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofMillis(10))); cache = createCache(builder); cache.put(1, "a"); timeSource.advanceTime(15); assertThat(cache.putIfAbsent(1, "b")).isNull(); assertThat(cache.get(1)).isEqualTo("b"); changesOf(1, 1, 2, 0); }
@Test public void testPutIfAbsentExpirationPropagatedToHigherTiers() throws CachePersistenceException { CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder = cacheManagerBuilder(oneSecondExpiration()); try(PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(true)) { Cache<Long, String> cache = cacheManager.getCache(CLUSTERED_CACHE, Long.class, String.class); cache.put(1L, "value"); // store on the cluster cache.putIfAbsent(1L, "newvalue"); // push it up on heap tier timeSource.advanceTime(1500); // go after expiration assertThat(cache.get(1L)).isEqualTo(null); // the value should have expired } } }