/** * Tests {@link Ehcache#clear()} over an empty cache where * {@link Store#clear() Store.clear} throws a * {@link StoreAccessException StoreAccessException}. */ @Test public void testClearEmptyStoreAccessException() throws Exception { final FakeStore realStore = new FakeStore(Collections.<String, String>emptyMap()); this.store = spy(realStore); doThrow(new StoreAccessException("")).when(this.store).clear(); final Ehcache<String, String> ehcache = this.getEhcache(); ehcache.clear(); verify(this.resilienceStrategy).clearFailure(any(StoreAccessException.class)); }
/** * {@inheritDoc} */ @Override public boolean containsKey(final K key) { statusTransitioner.checkAvailable(); checkNonNull(key); try { return store.containsKey(key); } catch (StoreAccessException e) { return resilienceStrategy.containsKeyFailure(key, e); } }
protected Map<K,V> getAllInternal(Set<? extends K> keys, boolean includeNulls) { getAllObserver.begin(); try { statusTransitioner.checkAvailable(); checkNonNullContent(keys); if (keys.isEmpty()) { getAllObserver.end(GetAllOutcome.SUCCESS); return Collections.emptyMap(); } try { Map<K, V> result = doGetAllInternal(keys, includeNulls); getAllObserver.end(GetAllOutcome.SUCCESS); return result; } catch (StoreAccessException e) { Map<K, V> result = resilienceStrategy.getAllFailure(keys, e); getAllObserver.end(GetAllOutcome.FAILURE); return result; } } catch (Throwable e) { getAllObserver.end(GetAllOutcome.FAILURE); throw e; } }
/** * Tests the effect of a {@link Ehcache#remove(Object, Object)} for * <ul> * <li>key with unequal value present in {@code Store}</li> * <li>>{@code Store.remove} throws</li> * </ul> */ @Test public void testRemoveValueUnequalStoreEntryStoreAccessException() throws Exception { FakeStore fakeStore = new FakeStore(Collections.singletonMap("key", "unequalValue")); this.store = spy(fakeStore); doThrow(new StoreAccessException("")).when(this.store).remove(eq("key"), eq("value")); Ehcache<String, String> ehcache = this.getEhcache(); ehcache.remove("key", "value"); verify(this.store).remove(eq("key"), eq("value")); verify(this.resilienceStrategy).removeFailure(eq("key"), eq("value"), any(StoreAccessException.class)); validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.ConditionalRemoveOutcome.FAILURE)); }
/** * Tests the effect of a {@link Ehcache#replace(Object, Object, Object)} for * <ul> * <li>key not present in {@code Store}</li> * <li>>{@code Store.replace} throws</li> * </ul> */ @Test public void testReplaceValueNoStoreEntryStoreAccessException() throws Exception { FakeStore fakeStore = new FakeStore(Collections.emptyMap()); this.store = spy(fakeStore); doThrow(new StoreAccessException("")).when(this.store).replace(eq("key"), eq("oldValue"), eq("newValue")); Ehcache<String, String> ehcache = this.getEhcache(); ehcache.replace("key", "oldValue", "newValue"); verify(this.store).replace(eq("key"), eq("oldValue"), eq("newValue")); verify(this.resilienceStrategy).replaceFailure(eq("key"), eq("oldValue"), eq("newValue"), any(StoreAccessException.class)); validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.ReplaceOutcome.FAILURE)); }
/** * Tests the effect of a {@link Ehcache#get(Object)} for * <ul> * <li>key not present in {@code Store}</li> * <li>{@code Store.get} throws</li> * </ul> */ @Test public void testGetNoStoreEntryStoreAccessException() throws Exception { final FakeStore fakeStore = new FakeStore(Collections.<String, String>emptyMap()); this.store = spy(fakeStore); doThrow(new StoreAccessException("")).when(this.store).get(eq("key")); final Ehcache<String, String> ehcache = this.getEhcache(); ehcache.get("key"); verify(this.store).get(eq("key")); verify(this.resilienceStrategy).getFailure(eq("key"), any(StoreAccessException.class)); validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.GetOutcome.FAILURE)); }
/** * {@inheritDoc} */ @Override public void removeAll(Set<? extends K> keys) throws BulkCacheWritingException { removeAllObserver.begin(); try { statusTransitioner.checkAvailable(); checkNonNull(keys); if (keys.isEmpty()) { removeAllObserver.end(RemoveAllOutcome.SUCCESS); return; } for (K key : keys) { if (key == null) { throw new NullPointerException(); } } try { doRemoveAll(keys); removeAllObserver.end(RemoveAllOutcome.SUCCESS); } catch (StoreAccessException e) { resilienceStrategy.removeAllFailure(keys, e); removeAllObserver.end(RemoveAllOutcome.FAILURE); } } catch (Throwable e) { removeAllObserver.end(RemoveAllOutcome.FAILURE); throw e; } }
/** * Tests {@link Ehcache#putAll(Map)} for * <ul> * <li>empty request map</li> * <li>populated {@code Store} (keys not relevant)</li> * <li>no {@code CacheLoaderWriter}</li> * </ul> */ @Test public void testPutAllEmptyRequestNoWriter() throws Exception { final Map<String, String> originalStoreContent = getEntryMap(KEY_SET_A, KEY_SET_B); final FakeStore fakeStore = new FakeStore(originalStoreContent); this.store = spy(fakeStore); final Ehcache<String, String> ehcache = this.getEhcache(); ehcache.putAll(Collections.<String, String>emptyMap()); verify(this.store, never()).bulkCompute(eq(Collections.<String>emptySet()), getAnyEntryIterableFunction()); assertThat(fakeStore.getEntryMap(), equalTo(originalStoreContent)); verify(this.resilienceStrategy, never()).putAllFailure(eq(Collections.<String, String>emptyMap()), any(StoreAccessException.class)); validateStats(ehcache, EnumSet.noneOf(CacheOperationOutcomes.PutOutcome.class)); validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.PutAllOutcome.SUCCESS)); assertThat(ehcache.getBulkMethodEntries().get(BulkOps.PUT_ALL).intValue(), is(0)); }
/** * Tests the effect of a {@link Ehcache#put(Object, Object)} for * <ul> * <li>key present in {@code Store}</li> * <li>{@code Store.put} throws</li> * </ul> */ @Test public void testPutHasStoreEntryStoreAccessException() throws Exception { final FakeStore fakeStore = new FakeStore(Collections.singletonMap("key", "oldValue")); this.store = spy(fakeStore); doThrow(new StoreAccessException("")).when(this.store).put(eq("key"), eq("value")); final Ehcache<String, String> ehcache = this.getEhcache(); ehcache.put("key", "value"); verify(this.store).put(eq("key"), eq("value")); verify(this.resilienceStrategy).putFailure(eq("key"), eq("value"), any(StoreAccessException.class)); validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.PutOutcome.FAILURE)); }
/** * Tests the effect of a {@link Ehcache#putIfAbsent(Object, Object)} for * <ul> * <li>key present in {@code Store}</li> * <li>{@code Store.putIfAbsent} throws</li> * </ul> */ @Test public void testPutIfAbsentHasStoreEntryStoreAccessException() throws Exception { final FakeStore fakeStore = new FakeStore(Collections.singletonMap("key", "oldValue")); this.store = spy(fakeStore); doThrow(new StoreAccessException("")).when(this.store).putIfAbsent(eq("key"), eq("value"), any()); final Ehcache<String, String> ehcache = this.getEhcache(); ehcache.putIfAbsent("key", "value"); verify(this.store).putIfAbsent(eq("key"), eq("value"), any()); verify(this.resilienceStrategy).putIfAbsentFailure(eq("key"), eq("value"), any(StoreAccessException.class)); validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.PutIfAbsentOutcome.FAILURE)); }
/** * Tests the effect of a {@link Ehcache#remove(Object, Object)} for * <ul> * <li>key not present in {@code Store}</li> * <li>>{@code Store.remove} throws</li> * </ul> */ @Test public void testRemoveValueNoStoreEntryStoreAccessException() throws Exception { FakeStore fakeStore = new FakeStore(Collections.emptyMap()); this.store = spy(fakeStore); doThrow(new StoreAccessException("")).when(this.store).remove(eq("key"), eq("value")); Ehcache<String, String> ehcache = this.getEhcache(); ehcache.remove("key", "value"); verify(this.store).remove(eq("key"), eq("value")); verify(this.resilienceStrategy).removeFailure(eq("key"), eq("value"), any(StoreAccessException.class)); validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.ConditionalRemoveOutcome.FAILURE)); }
/** * Tests the effect of a {@link Ehcache#replace(Object, Object, Object)} for * <ul> * <li>key with unequal value present in {@code Store}</li> * <li>>{@code Store.replace} throws</li> * </ul> */ @Test public void testReplaceValueUnequalStoreEntryStoreAccessException() throws Exception { FakeStore fakeStore = new FakeStore(Collections.singletonMap("key", "unequalValue")); this.store = spy(fakeStore); doThrow(new StoreAccessException("")).when(this.store).replace(eq("key"), eq("oldValue"), eq("newValue")); Ehcache<String, String> ehcache = this.getEhcache(); ehcache.replace("key", "oldValue", "newValue"); verify(this.store).replace(eq("key"), eq("oldValue"), eq("newValue")); verify(this.resilienceStrategy).replaceFailure(eq("key"), eq("oldValue"), eq("newValue"), any(StoreAccessException.class)); validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.ReplaceOutcome.FAILURE)); }
/** * Tests the effect of a {@link Ehcache#get(Object)} for * <ul> * <li>key present in {@code Store}</li> * <li>{@code Store.get} throws</li> * </ul> */ @Test public void testGetHasStoreEntryStoreAccessExceptionNoCacheLoaderWriter() throws Exception { final FakeStore fakeStore = new FakeStore(Collections.singletonMap("key", "value")); this.store = spy(fakeStore); assertThat(fakeStore.getEntryMap().get("key"), equalTo("value")); doThrow(new StoreAccessException("")).when(this.store).get(eq("key")); final Ehcache<String, String> ehcache = this.getEhcache(); ehcache.get("key"); verify(this.store).get(eq("key")); verify(this.resilienceStrategy).getFailure(eq("key"), any(StoreAccessException.class)); validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.GetOutcome.FAILURE)); }
/** * Tests {@link Ehcache#removeAll(Set)} for * <ul> * <li>empty request set</li> * <li>populated {@code Store} (keys not relevant)</li> * <li>no {@code CacheLoaderWriter}</li> * </ul> */ @Test public void testRemoveAllEmptyRequestNoWriter() throws Exception { final Map<String, String> originalStoreContent = getEntryMap(KEY_SET_A, KEY_SET_B); final FakeStore fakeStore = new FakeStore(originalStoreContent); this.store = spy(fakeStore); final Ehcache<String, String> ehcache = this.getEhcache(); ehcache.removeAll(Collections.<String>emptySet()); verify(this.store, never()).bulkCompute(eq(Collections.<String>emptySet()), getAnyEntryIterableFunction()); assertThat(fakeStore.getEntryMap(), equalTo(originalStoreContent)); verify(this.resilienceStrategy, never()).removeAllFailure(eq(Collections.<String>emptySet()), any(StoreAccessException.class)); validateStats(ehcache, EnumSet.noneOf(CacheOperationOutcomes.RemoveOutcome.class)); validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.RemoveAllOutcome.SUCCESS)); assertThat(ehcache.getBulkMethodEntries().get(BulkOps.REMOVE_ALL).intValue(), is(0)); }
/** * {@inheritDoc} */ @Override public void putAll(Map<? extends K, ? extends V> entries) throws BulkCacheWritingException { putAllObserver.begin(); try { statusTransitioner.checkAvailable(); checkNonNull(entries); if(entries.isEmpty()) { putAllObserver.end(PutAllOutcome.SUCCESS); return; } try { doPutAll(entries); putAllObserver.end(PutAllOutcome.SUCCESS); } catch (StoreAccessException e) { resilienceStrategy.putAllFailure(entries, e); putAllObserver.end(PutAllOutcome.FAILURE); } } catch (Exception e) { putAllObserver.end(PutAllOutcome.FAILURE); throw e; } }
/** * Tests the effect of a {@link Ehcache#put(Object, Object)} for * <ul> * <li>key not present in {@code Store}</li> * <li>{@code Store.put} throws</li> * </ul> */ @Test public void testPutNoStoreEntryStoreAccessException() throws Exception { final FakeStore fakeStore = new FakeStore(Collections.<String, String>emptyMap()); this.store = spy(fakeStore); doThrow(new StoreAccessException("")).when(this.store).put(eq("key"), eq("value")); final Ehcache<String, String> ehcache = this.getEhcache(); ehcache.put("key", "value"); verify(this.store).put(eq("key"), eq("value")); verify(this.resilienceStrategy).putFailure(eq("key"), eq("value"), any(StoreAccessException.class)); validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.PutOutcome.FAILURE)); }
/** * Tests the effect of a {@link Ehcache#putIfAbsent(Object, Object)} for * <ul> * <li>key not present in {@code Store}</li> * <li>{@code Store.putIfAbsent} throws</li> * </ul> */ @Test public void testPutIfAbsentNoStoreEntryStoreAccessException() throws Exception { final FakeStore fakeStore = new FakeStore(Collections.<String, String>emptyMap()); this.store = spy(fakeStore); doThrow(new StoreAccessException("")).when(this.store).putIfAbsent(eq("key"), eq("value"), any()); final Ehcache<String, String> ehcache = this.getEhcache(); ehcache.putIfAbsent("key", "value"); verify(this.store).putIfAbsent(eq("key"), eq("value"), any()); verify(this.resilienceStrategy).putIfAbsentFailure(eq("key"), eq("value"), any(StoreAccessException.class)); validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.PutIfAbsentOutcome.FAILURE)); }
/** * Tests the effect of a {@link Ehcache#remove(Object, Object)} for * <ul> * <li>key with equal value present in {@code Store}</li> * <li>>{@code Store.remove} throws</li> * </ul> */ @Test public void testRemoveValueEqualStoreEntryStoreAccessException() throws Exception { FakeStore fakeStore = new FakeStore(Collections.singletonMap("key", "value")); this.store = spy(fakeStore); doThrow(new StoreAccessException("")).when(this.store).remove(eq("key"), eq("value")); Ehcache<String, String> ehcache = this.getEhcache(); ehcache.remove("key", "value"); verify(this.store).remove(eq("key"), eq("value")); verify(this.resilienceStrategy).removeFailure(eq("key"), eq("value"), any(StoreAccessException.class)); validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.ConditionalRemoveOutcome.FAILURE)); }
/** * Tests the effect of a {@link Ehcache#replace(Object, Object, Object)} for * <ul> * <li>key with equal value present in {@code Store}</li> * <li>>{@code Store.replace} throws</li> * </ul> */ @Test public void testReplaceValueEqualStoreEntryStoreAccessException() throws Exception { FakeStore fakeStore = new FakeStore(Collections.singletonMap("key", "oldValue")); this.store = spy(fakeStore); doThrow(new StoreAccessException("")).when(this.store).replace(eq("key"), eq("oldValue"), eq("newValue")); Ehcache<String, String> ehcache = this.getEhcache(); ehcache.replace("key", "oldValue", "newValue"); verify(this.store).replace(eq("key"), eq("oldValue"), eq("newValue")); verify(this.resilienceStrategy).replaceFailure(eq("key"), eq("oldValue"), eq("newValue"), any(StoreAccessException.class)); validateStats(ehcache, EnumSet.of(CacheOperationOutcomes.ReplaceOutcome.FAILURE)); }
protected V getNoLoader(K key) { getObserver.begin(); try { statusTransitioner.checkAvailable(); checkNonNull(key); try { Store.ValueHolder<V> valueHolder = store.get(key); // Check for expiry first if (valueHolder == null) { getObserver.end(GetOutcome.MISS); return null; } else { getObserver.end(GetOutcome.HIT); return valueHolder.get(); } } catch (StoreAccessException e) { V value = resilienceStrategy.getFailure(key, e); getObserver.end(GetOutcome.FAILURE); return value; } } catch (Throwable e) { getObserver.end(GetOutcome.FAILURE); throw e; } }