public Ehcache(CacheConfiguration<K, V> configuration, final Store<K, V> store, ResilienceStrategy<K, V> resilienceStrategy, CacheEventDispatcher<K, V> eventDispatcher, Logger logger, CacheLoaderWriter<? super K, V> cacheLoaderWriter) { super(new EhcacheRuntimeConfiguration<>(configuration), store, resilienceStrategy, eventDispatcher, logger, new StatusTransitioner(logger)); this.cacheLoaderWriter = cacheLoaderWriter; }
/** * Creates a new {@code Ehcache} based on the provided parameters. * * @param configuration the cache configuration * @param store the store to use * @param eventDispatcher the event dispatcher * @param logger the logger */ public Ehcache(CacheConfiguration<K, V> configuration, final Store<K, V> store, ResilienceStrategy<K, V> resilienceStrategy, CacheEventDispatcher<K, V> eventDispatcher, Logger logger) { this(new EhcacheRuntimeConfiguration<>(configuration), store, resilienceStrategy, eventDispatcher, logger, new StatusTransitioner(logger), null); }
/** * Creates a new {@code PersistentUserManagedCache} based on the provided parameters. * * @param configuration the cache configuration * @param store the underlying store * @param diskPersistenceService the persistence service * @param cacheLoaderWriter the optional loader writer * @param eventDispatcher the event dispatcher * @param id an id for this cache */ public PersistentUserManagedEhcache(CacheConfiguration<K, V> configuration, Store<K, V> store, ResilienceStrategy<K, V> resilienceStrategy, DiskResourceService diskPersistenceService, CacheLoaderWriter<? super K, V> cacheLoaderWriter, CacheEventDispatcher<K, V> eventDispatcher, String id) { this.logger = LoggerFactory.getLogger(PersistentUserManagedEhcache.class.getName() + "-" + id); this.statusTransitioner = new StatusTransitioner(logger); this.cache = new Ehcache<>(new EhcacheRuntimeConfiguration<>(configuration), store, resilienceStrategy, eventDispatcher, logger, statusTransitioner, cacheLoaderWriter); this.diskPersistenceService = diskPersistenceService; this.id = id; }
@Test public void testMaintenanceOnlyLetsTheOwningThreadInteract() throws InterruptedException { final StatusTransitioner transitioner = new StatusTransitioner(LoggerFactory.getLogger(StatusTransitionerTest.class)); transitioner.maintenance().succeeded(); transitioner.checkMaintenance(); Thread thread = new Thread() { @Override public void run() { try { transitioner.checkMaintenance(); fail(); } catch (IllegalStateException e) { assertThat(e.getMessage().contains(Status.MAINTENANCE.name()), is(true)); assertThat(e.getMessage().contains("own"), is(true)); } } }; thread.start(); thread.join(); }
@Test public void testFiresListeners() { StatusTransitioner transitioner = new StatusTransitioner(LoggerFactory.getLogger(StatusTransitionerTest.class)); final StateChangeListener listener = mock(StateChangeListener.class); transitioner.registerListener(listener); transitioner.init().succeeded(); verify(listener).stateTransition(Status.UNINITIALIZED, Status.AVAILABLE); reset(listener); transitioner.deregisterListener(listener); transitioner.close().succeeded(); verify(listener, never()).stateTransition(Status.AVAILABLE, Status.UNINITIALIZED); }
@Test public void testMaintenanceOnlyOwningThreadCanClose() throws InterruptedException { final StatusTransitioner transitioner = new StatusTransitioner(LoggerFactory.getLogger(StatusTransitionerTest.class)); transitioner.maintenance().succeeded(); Thread thread = new Thread() { @Override public void run() { try { transitioner.close(); fail(); } catch (IllegalStateException e) { assertThat(e.getMessage().contains(Status.MAINTENANCE.name()), is(true)); assertThat(e.getMessage().contains("own"), is(true)); } } }; thread.start(); thread.join(); transitioner.close(); }
@Test public void testFinishesTransitionOnListenerThrowing() { StatusTransitioner transitioner = new StatusTransitioner(LoggerFactory.getLogger(StatusTransitionerTest.class)); final StateChangeListener listener = mock(StateChangeListener.class); final RuntimeException runtimeException = new RuntimeException(); doThrow(runtimeException).when(listener).stateTransition(Status.UNINITIALIZED, Status.AVAILABLE); transitioner.registerListener(listener); try { transitioner.init().succeeded(); fail(); } catch (RuntimeException e) { assertThat(e, is(runtimeException)); } assertThat(transitioner.currentStatus(), is(Status.AVAILABLE)); }
@Test public void testClosesAllHooksOnFailure() throws Exception { final StatusTransitioner transitioner = new StatusTransitioner(LoggerFactory.getLogger(StatusTransitionerTest.class)); final LifeCycled first = mock(LifeCycled.class); final LifeCycled second = mock(LifeCycled.class); transitioner.addHook(first); transitioner.addHook(second); transitioner.init().succeeded(); reset(first); reset(second); final Exception toBeThrown = new Exception(); doThrow(toBeThrown).when(second).close(); try { transitioner.close().succeeded(); fail(); } catch (StateTransitionException e) { // expected } verify(first).close(); }
@Test public void testStopsInitedHooksOnFailure() throws Exception { final StatusTransitioner transitioner = new StatusTransitioner(LoggerFactory.getLogger(StatusTransitionerTest.class)); final LifeCycled first = mock(LifeCycled.class); final LifeCycled second = mock(LifeCycled.class); transitioner.addHook(first); transitioner.addHook(second); final Exception toBeThrown = new Exception(); doThrow(toBeThrown).when(second).init(); try { transitioner.init().succeeded(); fail(); } catch (StateTransitionException e) { // expected } verify(first).init(); verify(first).close(); }
@Test public void testDoesNoReInitsClosedHooksOnFailure() throws Exception { final StatusTransitioner transitioner = new StatusTransitioner(LoggerFactory.getLogger(StatusTransitionerTest.class)); final LifeCycled first = mock(LifeCycled.class); final LifeCycled second = mock(LifeCycled.class); transitioner.addHook(first); transitioner.addHook(second); transitioner.init().succeeded(); reset(first); reset(second); final Exception toBeThrown = new Exception(); doThrow(toBeThrown).when(first).close(); try { transitioner.close().succeeded(); fail(); } catch (StateTransitionException e) { // expected } verify(second).close(); verify(second, never()).init(); }
@Test public void testCheckAvailableAccountsForThreadLease() throws InterruptedException { final StatusTransitioner transitioner = new StatusTransitioner(LoggerFactory.getLogger(StatusTransitionerTest.class)); transitioner.maintenance().succeeded(); transitioner.checkAvailable(); Thread thread = new Thread() { @Override public void run() { try { transitioner.checkAvailable(); fail(); } catch (IllegalStateException e) { assertThat(e.getMessage().contains(Status.MAINTENANCE.name()), is(true)); assertThat(e.getMessage().contains("own"), is(true)); } } }; thread.start(); thread.join(); transitioner.close(); }
@Test public void testTransitionsToLowestStateOnFailure() { StatusTransitioner transitioner = new StatusTransitioner(LoggerFactory.getLogger(StatusTransitionerTest.class)); assertThat(transitioner.currentStatus(), CoreMatchers.is(Status.UNINITIALIZED)); transitioner.init().failed(new Throwable()); assertThat(transitioner.currentStatus(), is(Status.UNINITIALIZED)); transitioner.init().succeeded(); assertThat(transitioner.currentStatus(), is(Status.AVAILABLE)); transitioner.close().failed(new Throwable()); assertThat(transitioner.currentStatus(), is(Status.UNINITIALIZED)); }
@Test public void testTransitionDuringFailures() { StatusTransitioner transitioner = new StatusTransitioner(LoggerFactory.getLogger(StatusTransitionerTest.class)); assertThat(transitioner.currentStatus(), CoreMatchers.is(Status.UNINITIALIZED)); StatusTransitioner.Transition st = transitioner.init(); st.failed(new Throwable()); assertThat(transitioner.currentStatus(), is(Status.UNINITIALIZED)); try { st.failed(new Throwable()); fail(); } catch (AssertionError err) { assertThat(err.getMessage(), is("Throwable cannot be thrown if Transition is done.")); } st.failed(null); assertThat(transitioner.currentStatus(), is(Status.UNINITIALIZED)); StatusTransitioner.Transition st1 = transitioner.init(); assertThat(transitioner.currentStatus(), is(Status.AVAILABLE)); st1.failed(null); assertThat(transitioner.currentStatus(), is(Status.UNINITIALIZED)); }
@Test public void testRepectRegistrationOrder() { final List<LifeCycled> order = new ArrayList<>(); final StatusTransitioner transitioner = new StatusTransitioner(LoggerFactory.getLogger(StatusTransitionerTest.class)); final Recorder first = new Recorder(order, "first"); final Recorder second = new Recorder(order, "second"); transitioner.addHook(first); transitioner.addHook(second); transitioner.init().succeeded(); assertThat(order.get(0), CoreMatchers.<LifeCycled>sameInstance(first)); assertThat(order.get(1), CoreMatchers.<LifeCycled>sameInstance(second)); order.clear(); transitioner.close().succeeded(); assertThat(order.get(0), CoreMatchers.<LifeCycled>sameInstance(second)); assertThat(order.get(1), CoreMatchers.<LifeCycled>sameInstance(first)); }
@Test public void testLifeCycledAdapterCanBeUsedInsteadOfLifeCycled() { StatusTransitioner transitioner = new StatusTransitioner(LoggerFactory.getLogger(StatusTransitionerTest.class)); final List<String> calls = new LinkedList<>();
@Test public void testHookThrowingVetosTransition() throws Exception { final StatusTransitioner transitioner = new StatusTransitioner(LoggerFactory.getLogger(StatusTransitionerTest.class)); final LifeCycled mock = mock(LifeCycled.class); transitioner.addHook(mock); final Exception toBeThrown = new Exception(); doThrow(toBeThrown).when(mock).init(); try { transitioner.init().succeeded(); fail(); } catch (StateTransitionException e) { assertThat(e.getCause(), CoreMatchers.<Throwable>sameInstance(toBeThrown)); } assertThat(transitioner.currentStatus(), is(Status.UNINITIALIZED)); reset(mock); doThrow(toBeThrown).when(mock).close(); transitioner.init().succeeded(); try { transitioner.close().succeeded(); fail(); } catch (StateTransitionException e) { assertThat(e.getCause(), CoreMatchers.<Throwable>sameInstance(toBeThrown)); } }
/** * Creates a new {@code Ehcache} based on the provided parameters. * * @param configuration the cache configuration * @param store the store to use * @param eventDispatcher the event dispatcher * @param logger the logger */ public Ehcache(CacheConfiguration<K, V> configuration, final Store<K, V> store, ResilienceStrategy<K, V> resilienceStrategy, CacheEventDispatcher<K, V> eventDispatcher, Logger logger) { this(new EhcacheRuntimeConfiguration<>(configuration), store, resilienceStrategy, eventDispatcher, logger, new StatusTransitioner(logger)); }
EhcacheWithLoaderWriter(CacheConfiguration<K, V> runtimeConfiguration, Store<K, V> store, ResilienceStrategy<K, V> resilienceStrategy, CacheLoaderWriter<? super K, V> cacheLoaderWriter, CacheEventDispatcher<K, V> eventDispatcher, boolean useLoaderInAtomics, Logger logger) { this(new EhcacheRuntimeConfiguration<>(runtimeConfiguration), store, resilienceStrategy, cacheLoaderWriter, eventDispatcher, useLoaderInAtomics, logger, new StatusTransitioner(logger)); }
/** * Creates a new {@code PersistentUserManagedCache} based on the provided parameters. * * @param configuration the cache configuration * @param store the underlying store * @param diskPersistenceService the persistence service * @param cacheLoaderWriter the optional loader writer * @param eventDispatcher the event dispatcher * @param id an id for this cache */ public PersistentUserManagedEhcache(CacheConfiguration<K, V> configuration, Store<K, V> store, ResilienceStrategy<K, V> resilienceStrategy, DiskResourceService diskPersistenceService, CacheLoaderWriter<? super K, V> cacheLoaderWriter, CacheEventDispatcher<K, V> eventDispatcher, String id) { this.logger = LoggerFactory.getLogger(PersistentUserManagedEhcache.class.getName() + "-" + id); this.statusTransitioner = new StatusTransitioner(logger); if (cacheLoaderWriter == null) { this.cache = new Ehcache<>(new EhcacheRuntimeConfiguration<>(configuration), store, resilienceStrategy, eventDispatcher, logger, statusTransitioner); } else { this.cache = new EhcacheWithLoaderWriter<>(new EhcacheRuntimeConfiguration<>(configuration), store, resilienceStrategy, cacheLoaderWriter, eventDispatcher, true, logger, statusTransitioner); } this.diskPersistenceService = diskPersistenceService; this.id = id; }