@Test public void testClear() throws Exception { offHeapStore = createAndInitStore(timeSource, ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofMillis(15L))); offHeapStore.put("1", "one"); offHeapStore.put("2", "two"); offHeapStore.put("3", "three"); offHeapStore.clear(); assertThat(offHeapStore.get("1"), is(nullValue())); assertThat(offHeapStore.get("2"), is(nullValue())); assertThat(offHeapStore.get("3"), is(nullValue())); }
@Test public void testGetAndRemoveValue() throws Exception { offHeapStore = createAndInitStore(timeSource, ExpiryPolicyBuilder.noExpiration()); offHeapStore.put("1", "one"); assertThat(offHeapStore.getAndRemove("1").get(), equalTo("one")); validateStats(offHeapStore, EnumSet.of(LowerCachingTierOperationsOutcome.GetAndRemoveOutcome.HIT_REMOVED)); assertThat(offHeapStore.get("1"), is(nullValue())); }
@Test public void testExpiryEventFiredOnExpiredCachedEntry() throws StoreAccessException { offHeapStore = createAndInitStore(timeSource, ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofMillis(10L))); final List<String> expiredKeys = new ArrayList<>(); offHeapStore.getStoreEventSource().addEventListener(event -> { if (event.getType() == EventType.EXPIRED) { expiredKeys.add(event.getKey()); } }); offHeapStore.put("key1", "value1"); offHeapStore.put("key2", "value2"); offHeapStore.get("key1"); // Bring the entry to the caching tier timeSource.advanceTime(11); // Expire the elements offHeapStore.get("key1"); offHeapStore.get("key2"); assertThat(expiredKeys, containsInAnyOrder("key1", "key2")); assertThat(getExpirationStatistic(offHeapStore).count(StoreOperationOutcomes.ExpirationOutcome.SUCCESS), is(2L)); }
@Test public void testGetWithExpiryOnAccess() throws Exception { offHeapStore = createAndInitStore(timeSource, expiry().access(Duration.ZERO).build()); offHeapStore.put("key", "value"); final AtomicReference<String> expired = new AtomicReference<>(); offHeapStore.getStoreEventSource().addEventListener(event -> { if (event.getType() == EventType.EXPIRED) { expired.set(event.getKey()); } }); assertThat(offHeapStore.get("key"), valueHeld("value")); assertThat(expired.get(), is("key")); }
@Test public void testWriteBackOfValueHolder() throws StoreAccessException { offHeapStore = createAndInitStore(timeSource, ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofMillis(15L))); offHeapStore.put("key1", "value1"); timeSource.advanceTime(10); OffHeapValueHolder<String> valueHolder = (OffHeapValueHolder<String>)offHeapStore.get("key1"); assertThat(valueHolder.lastAccessTime(), is(10L)); timeSource.advanceTime(10); assertThat(offHeapStore.get("key1"), notNullValue()); timeSource.advanceTime(16); assertThat(offHeapStore.get("key1"), nullValue()); }
@Test public void testInvalidateKeyPresent() throws Exception { offHeapStore = createAndInitStore(timeSource, ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofMillis(15L))); offHeapStore.put("1", "one"); final AtomicReference<Store.ValueHolder<String>> invalidated = new AtomicReference<>(); offHeapStore.setInvalidationListener((key, valueHolder) -> invalidated.set(valueHolder)); offHeapStore.invalidate("1"); assertThat(invalidated.get().get(), equalTo("one")); validateStats(offHeapStore, EnumSet.of(LowerCachingTierOperationsOutcome.InvalidateOutcome.REMOVED)); assertThat(offHeapStore.get("1"), is(nullValue())); }
@Test public void testExpiryAccessException() throws Exception { offHeapStore = createAndInitStore(timeSource, new ExpiryPolicy<String, String>() { @Override public Duration getExpiryForCreation(String key, String value) { return ExpiryPolicy.INFINITE; } @Override public Duration getExpiryForAccess(String key, Supplier<? extends String> value) { throw new RuntimeException(); } @Override public Duration getExpiryForUpdate(String key, Supplier<? extends String> oldValue, String newValue) { return null; } }); offHeapStore.put("key", "value"); assertThat(offHeapStore.get("key"), valueHeld("value")); assertNull(offHeapStore.get("key")); }
@Test public void testExpiryUpdateException() throws Exception { offHeapStore = createAndInitStore(timeSource, new ExpiryPolicy<String, String>() { @Override public Duration getExpiryForCreation(String key, String value) { return ExpiryPolicy.INFINITE; } @Override public Duration getExpiryForAccess(String key, Supplier<? extends String> value) { return ExpiryPolicy.INFINITE; } @Override public Duration getExpiryForUpdate(String key, Supplier<? extends String> oldValue, String newValue) { if (timeSource.getTimeMillis() > 0) { throw new RuntimeException(); } return ExpiryPolicy.INFINITE; } }); offHeapStore.put("key", "value"); assertThat(offHeapStore.get("key").get(), is("value")); timeSource.advanceTime(1000); offHeapStore.put("key", "newValue"); assertNull(offHeapStore.get("key")); }
@Test public void testExpiryCreateException() throws Exception { offHeapStore = createAndInitStore(timeSource, new ExpiryPolicy<String, String>() { @Override public Duration getExpiryForCreation(String key, String value) { throw new RuntimeException(); } @Override public Duration getExpiryForAccess(String key, Supplier<? extends String> value) { throw new AssertionError(); } @Override public Duration getExpiryForUpdate(String key, Supplier<? extends String> oldValue, String newValue) { throw new AssertionError(); } }); offHeapStore.put("key", "value"); assertNull(offHeapStore.get("key")); }