ExecutionTaskWithStrategy implements ITask { @Override public void execute() throws TaskExecutionException { final ExecutionState state = new ExecutionState(); taskDispatcher.notifyThreadStart(); try { while (taskDispatcher.getExecutionTask() == this) { if (Thread.interrupted()) { Thread.currentThread().interrupt(); return; } ITask task = taskDispatcher.getTaskQueue().tryTake(); if (null == task) { return; } taskDispatcher.notifyTaskTaken(); taskDispatcher.getThreadPool().tryExecute(this); state.setTask(task); try { taskProcessStrategy.process(state); } catch (TaskProcessException | InvalidArgumentException e) { throw new TaskExecutionException(e); } } taskDispatcher.getThreadPool().tryExecute(taskDispatcher.getExecutionTask()); } finally { taskDispatcher.notifyThreadStop(); } } ExecutionTaskWithStrategy(
            final TaskDispatcher taskDispatcher, final ITaskProcessStrategy taskProcessStrategy); @Override void execute();  }
@Test(timeout = 1000L) public void Should_exit_When_thereIsNoTask() throws Exception { ExecutionTaskWithStrategy executionTask = new ExecutionTaskWithStrategy(dispatcherMock, processStrategyMock); when(dispatcherMock.getExecutionTask()).thenReturn(executionTask); when(queueMock.tryTake()).thenReturn(null); executionTask.execute(); }
@Test(timeout = 1000L) public void Should_exit_When_threadIsInterrupted() throws Exception { ExecutionTaskWithStrategy executionTask = new ExecutionTaskWithStrategy(dispatcherMock, processStrategyMock); when(dispatcherMock.getExecutionTask()).thenReturn(executionTask); Thread.currentThread().interrupt(); executionTask.execute(); verify(queueMock, never()).tryTake(); assertTrue(Thread.interrupted()); }
@Test(timeout = 1000L) public void Should_letStrategyExecuteTask() throws Exception { ITask taskMock = mock(ITask.class); when(queueMock.tryTake()) .thenReturn(taskMock) .thenReturn(null); ExecutionTaskWithStrategy executionTask = new ExecutionTaskWithStrategy(dispatcherMock, processStrategyMock); when(dispatcherMock.getExecutionTask()).thenReturn(executionTask); doAnswer(invocation -> { invocation.getArgumentAt(0, ITaskExecutionState.class).execute(); return null; }).when(processStrategyMock).process(any()); executionTask.execute(); verify(taskMock).execute(); }
@Test(timeout = 1000L) public void Should_letStrategyAccessTaskClass() throws Exception { ITask taskMock = mock(ITask.class); when(queueMock.tryTake()) .thenReturn(taskMock) .thenReturn(null); ExecutionTaskWithStrategy executionTask = new ExecutionTaskWithStrategy(dispatcherMock, processStrategyMock); when(dispatcherMock.getExecutionTask()).thenReturn(executionTask); doAnswer(invocation -> { Class c = invocation.getArgumentAt(0, ITaskExecutionState.class).getTaskClass(); assertSame(taskMock.getClass(), c); return null; }).when(processStrategyMock).process(any()); executionTask.execute(); }
@Test(timeout = 1000L) public void Should_letStrategyAccessManagedTask() throws Exception { ITask taskMock = mock(ITask.class); when(queueMock.tryTake()) .thenReturn(taskMock) .thenReturn(null); ExecutionTaskWithStrategy executionTask = new ExecutionTaskWithStrategy(dispatcherMock, processStrategyMock); when(dispatcherMock.getExecutionTask()).thenReturn(executionTask); doAnswer(invocation -> { try { invocation.getArgumentAt(0, ITaskExecutionState.class).getTaskAs(IManagedTask.class); fail(); } catch (InvalidArgumentException ignore) {} return null; }).when(processStrategyMock).process(any()); executionTask.execute(); IManagedTask managedTaskMock = mock(IManagedTask.class), managedTaskMock1 = mock(IManagedTask.class); when(managedTaskMock.getAs(IManagedTask.class)).thenReturn(managedTaskMock1); when(queueMock.tryTake()) .thenReturn(managedTaskMock) .thenReturn(null); doAnswer(invocation -> { IManagedTask mt = invocation.getArgumentAt(0, ITaskExecutionState.class).getTaskAs(IManagedTask.class); assertSame(managedTaskMock1, mt); return null; }).when(processStrategyMock).process(any()); executionTask.execute(); verify(managedTaskMock).getAs(IManagedTask.class); }
@Test(timeout = 100L) public void Should_exit_When_currentExecutionTaskChanges() throws Exception { ExecutionTaskWithStrategy executionTask = new ExecutionTaskWithStrategy(dispatcherMock, processStrategyMock); ITask nextExecTsk = mock(ExecutionTask.class); when(dispatcherMock.getExecutionTask()).thenReturn(nextExecTsk); executionTask.execute(); verify(queueMock, never()).tryTake(); verify(threadPoolMock).tryExecute(same(nextExecTsk)); }