/** * {@inheritDoc} */ @Override public void close() { statusTransitioner.close().succeeded(); }
@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)); } } };
@Override public void close() { final StatusTransitioner.Transition st = statusTransitioner.close();
@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 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 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 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 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)); }
assertThat(calls, equalTo(asList("adapter2-init", "adapter4-init"))); transitioner.close().succeeded(); assertThat(calls, equalTo(asList("adapter2-init", "adapter4-init", "adapter4-close", "adapter3-close")));
@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)); } }
/** * {@inheritDoc} */ @Override public void close() { statusTransitioner.close().succeeded(); }
@Override public void close() { final StatusTransitioner.Transition st = statusTransitioner.close();