### Question:
TaskRunner extends Thread { protected void handleCancelRequests() { try { for (String taskId : runningTasks.keySet()) { Task t = runningTasks.get(taskId); if (t.isAlive() && !t.isCanceledOrInterrupted()) { TaskStatusInfo tsi = taskPersister.getTaskStatusInfo(taskId); if (tsi != null && tsi.cancelRequested) { t.setCanceled(true); t.interrupt(); } } } } catch (Exception e) { log.fine(e.getMessage()); } } TaskRunner(String nodeId, TaskFactory taskFactory, TaskPersister taskPersister); protected  TaskRunner(); @Override void run(); void cancelTask(String taskId); void notifyNewTaskAvailableForRun();  }### Answer:
@Test public void handleCancelRequests() throws InterruptedException { TaskRunner tested = getTested(); Task t1 = createTaskMock(true); Task t3 = createTaskMock(true); Task t5 = createTaskMock(true); Task t6 = createTaskMock(true); t6.setCanceled(true); tested.runningTasks.put("t1", t1); tested.runningTasks.put("t2", createTaskMock(false)); tested.runningTasks.put("t3", t3); tested.runningTasks.put("t4", createTaskMock(false)); tested.runningTasks.put("t5", t5); tested.runningTasks.put("t6", t6); Thread.sleep(1000); Mockito.when(tested.taskPersister.getTaskStatusInfo("t1")).thenReturn(createTaskStatusInfoCancelTest(false)); Mockito.when(tested.taskPersister.getTaskStatusInfo("t3")).thenReturn(createTaskStatusInfoCancelTest(true)); Mockito.when(tested.taskPersister.getTaskStatusInfo("t5")).thenReturn(null); tested.handleCancelRequests(); Assert.assertEquals(false, t1.isCanceledOrInterrupted()); Assert.assertEquals(true, t3.isCanceledOrInterrupted()); Assert.assertEquals(false, t5.isCanceledOrInterrupted()); Assert.assertEquals(true, t6.isCanceledOrInterrupted()); Mockito.verify(tested.taskPersister).getTaskStatusInfo("t1"); Mockito.verify(tested.taskPersister).getTaskStatusInfo("t3"); Mockito.verify(tested.taskPersister).getTaskStatusInfo("t5"); Mockito.verifyNoMoreInteractions(tested.taskPersister); }