/** * Mark the invoker as closed and call the {@link Agent#onClose()} logic for clean up. * <p> * The clean up logic will only be performed once. */ public final void close() { try { if (!isClosed) { isRunning = false; isClosed = true; agent.onClose(); } } catch (final Throwable throwable) { errorHandler.onError(throwable); } }
agent.onClose();
agent.onClose();
private void remove(final Agent agent) { removeAgent.lazySet(null); final Agent[] newAgents = ArrayUtil.remove(agents, agent); try { if (newAgents != agents) { agent.onClose(); } } finally { agents = newAgents; } } }
private void add(final Agent agent) { addAgent.lazySet(null); try { agent.onStart(); } catch (final RuntimeException ex) { agent.onClose(); throw ex; } agents = ArrayUtil.add(agents, agent); }
agent.onClose();
@Test public void shouldNotDoWorkOnClosedRunnerButCallOnClose() throws Exception { invoker.close(); invoker.invoke(); verify(mockAgent, never()).onStart(); verify(mockAgent, never()).doWork(); verify(mockErrorHandler, never()).onError(any()); verify(mockAtomicCounter, never()).increment(); verify(mockAgent).onClose(); }
agent.onClose();
@Test public void shouldCloseAgents() throws Exception { final Agent mockAgentOne = mock(Agent.class); final Agent mockAgentTwo = mock(Agent.class); final DynamicCompositeAgent compositeAgent = new DynamicCompositeAgent(ROLE_NAME, mockAgentOne, mockAgentTwo); compositeAgent.onClose(); verify(mockAgentOne, never()).doWork(); verify(mockAgentTwo, never()).doWork(); verify(mockAgentOne, never()).onStart(); verify(mockAgentTwo, never()).onStart(); verify(mockAgentOne, times(1)).onClose(); verify(mockAgentTwo, times(1)).onClose(); assertEquals(DynamicCompositeAgent.Status.CLOSED, compositeAgent.status()); }
@Test public void shouldNotDoWorkOnClosedRunner() throws Exception { runner.close(); runner.run(); verify(mockAgent, never()).onStart(); verify(mockAgent, never()).doWork(); verify(mockErrorHandler, never()).onError(any()); verify(mockAtomicCounter, never()).increment(); verify(mockAgent, times(1)).onClose(); assertTrue(runner.isClosed()); }
@Test public void shouldReportExceptionThrownByAgent() throws Exception { final RuntimeException expectedException = new RuntimeException(); when(mockAgent.doWork()).thenThrow(expectedException); invoker.start(); invoker.invoke(); verify(mockAgent).doWork(); verify(mockErrorHandler).onError(expectedException); verify(mockAtomicCounter).increment(); verify(mockAgent, never()).onClose(); reset(mockAgent); invoker.invoke(); verify(mockAgent).doWork(); reset(mockAgent); invoker.close(); verify(mockAgent, never()).doWork(); verify(mockAgent).onClose(); }
@Test public void shouldHandleAgentTerminationExceptionThrownByAgent() throws Exception { final RuntimeException expectedException = new AgentTerminationException(); when(mockAgent.doWork()).thenThrow(expectedException); runner.run(); verify(mockAgent).doWork(); verify(mockErrorHandler).onError(expectedException); verify(mockAtomicCounter).increment(); verify(mockAgent, times(1)).onClose(); assertTrue(runner.isClosed()); }
@Test public void shouldFollowLifecycle() throws Exception { invoker.start(); invoker.start(); verify(mockAgent, times(1)).onStart(); verifyNoMoreInteractions(mockAgent); invoker.invoke(); invoker.invoke(); verify(mockAgent, times(2)).doWork(); verifyNoMoreInteractions(mockAgent); invoker.close(); invoker.close(); verify(mockAgent, times(1)).onClose(); verifyNoMoreInteractions(mockAgent); }
@Test public void shouldReportExceptionThrownOnStart() throws Exception { final RuntimeException expectedException = new RuntimeException(); Mockito.doThrow(expectedException).when(mockAgent).onStart(); invoker.start(); invoker.invoke(); verify(mockAgent, never()).doWork(); verify(mockErrorHandler).onError(expectedException); verify(mockAgent).onClose(); assertTrue(invoker.isStarted()); assertFalse(invoker.isRunning()); assertTrue(invoker.isClosed()); }
@Test public void shouldRemoveAgent() throws Exception { final Agent mockAgentOne = mock(Agent.class); final Agent mockAgentTwo = mock(Agent.class); final DynamicCompositeAgent compositeAgent = new DynamicCompositeAgent(ROLE_NAME, mockAgentOne, mockAgentTwo); final AgentInvoker invoker = new AgentInvoker(Throwable::printStackTrace, null, compositeAgent); invoker.start(); verify(mockAgentOne, times(1)).onStart(); verify(mockAgentTwo, times(1)).onStart(); invoker.invoke(); verify(mockAgentOne, times(1)).doWork(); verify(mockAgentTwo, times(1)).doWork(); assertTrue(compositeAgent.tryRemove(mockAgentTwo)); assertFalse(compositeAgent.hasRemoveAgentCompleted()); invoker.invoke(); assertTrue(compositeAgent.hasRemoveAgentCompleted()); verify(mockAgentOne, times(2)).doWork(); verify(mockAgentTwo, times(1)).doWork(); verify(mockAgentOne, times(1)).onStart(); verify(mockAgentTwo, times(1)).onStart(); verify(mockAgentTwo, times(1)).onClose(); }
@Test public void shouldHandleAgentTerminationExceptionThrownByAgent() throws Exception { final RuntimeException expectedException = new AgentTerminationException(); when(mockAgent.doWork()).thenThrow(expectedException); invoker.start(); invoker.invoke(); verify(mockAgent).doWork(); verify(mockErrorHandler).onError(expectedException); verify(mockAtomicCounter).increment(); verify(mockAgent).onClose(); assertTrue(invoker.isClosed()); reset(mockAgent); invoker.invoke(); verify(mockAgent, never()).doWork(); assertTrue(invoker.isClosed()); }
@Test public void shouldApplyLifecycleToAll() throws Exception { final CompositeAgent compositeAgent = new CompositeAgent(agents[0], agents[1], agents[2]); compositeAgent.onStart(); for (final Agent agent : agents) { verify(agent).onStart(); } compositeAgent.doWork(); for (final Agent agent : agents) { verify(agent).doWork(); } compositeAgent.onClose(); for (final Agent agent : agents) { verify(agent).onClose(); } }
public void onClose() { for (final Agent agent : agents) { agent.onClose(); } }
private void closeAnythingHoldingFileHandles() { if (monitoringAgent == null) { monitoringFile.close(); } else { monitoringAgent.onClose(); } }