use of com.netflix.conductor.client.worker.Worker in project conductor by Netflix.
the class WorkflowTaskCoordinatorTests method testReturnTaskWhenRejectedExecutionExceptionThrown.
@Test
public void testReturnTaskWhenRejectedExecutionExceptionThrown() {
Task testTask = new Task();
testTask.setStatus(Task.Status.IN_PROGRESS);
Worker worker = mock(Worker.class);
when(worker.getPollingInterval()).thenReturn(3000);
when(worker.getPollCount()).thenReturn(1);
when(worker.getTaskDefName()).thenReturn("test");
when(worker.preAck(any())).thenReturn(true);
when(worker.execute(any())).thenAnswer(invocation -> {
// Sleep for 2 seconds to trigger RejectedExecutionException
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
return new TaskResult(testTask);
});
TaskClient client = Mockito.mock(TaskClient.class);
WorkflowTaskCoordinator coordinator = new WorkflowTaskCoordinator.Builder().withWorkers(worker).withThreadCount(1).withWorkerQueueSize(1).withSleepWhenRetry(100000).withUpdateRetryCount(1).withTaskClient(client).withWorkerNamePrefix("test-worker-").build();
when(client.batchPollTasksInDomain(anyString(), isNull(), isNull(), anyInt(), anyInt())).thenReturn(ImmutableList.of(testTask, testTask, testTask));
when(client.ack(any(), any())).thenReturn(true);
CountDownLatch latch = new CountDownLatch(3);
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
TaskResult result = (TaskResult) args[0];
assertEquals(TaskResult.Status.IN_PROGRESS, result.getStatus());
latch.countDown();
return null;
}).when(client).updateTask(any());
coordinator.init();
Uninterruptibles.awaitUninterruptibly(latch);
// With worker queue set to 1, first two tasks can be submitted, and third one would get
// RejectedExceptionExcpetion, so worker.execute() should be called twice.
verify(worker, times(2)).execute(any());
// task must be updated with IN_PROGRESS status three times, two from worker.execute() and
// one from returnTask caused by RejectedExecutionException.
verify(client, times(3)).updateTask(any());
}
use of com.netflix.conductor.client.worker.Worker in project conductor by Netflix.
the class WorkflowTaskCoordinatorTests method testLargePayloadCanFailUpdateWithRetry.
@Test
public void testLargePayloadCanFailUpdateWithRetry() {
Task testTask = new Task();
testTask.setStatus(Task.Status.COMPLETED);
Worker worker = mock(Worker.class);
when(worker.getPollingInterval()).thenReturn(3000);
when(worker.getPollCount()).thenReturn(1);
when(worker.getTaskDefName()).thenReturn("test");
when(worker.preAck(any())).thenReturn(true);
when(worker.execute(any())).thenAnswer(invocation -> {
// Sleep for 2 seconds to trigger RejectedExecutionException
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
return new TaskResult(testTask);
});
TaskClient taskClient = Mockito.mock(TaskClient.class);
WorkflowTaskCoordinator coordinator = new WorkflowTaskCoordinator.Builder().withWorkers(worker).withThreadCount(1).withWorkerQueueSize(1).withSleepWhenRetry(100000).withUpdateRetryCount(3).withTaskClient(taskClient).withWorkerNamePrefix("test-worker-").build();
when(taskClient.batchPollTasksInDomain(anyString(), isNull(), isNull(), anyInt(), anyInt())).thenReturn(ImmutableList.of(testTask));
when(taskClient.ack(any(), any())).thenReturn(true);
doThrow(ConductorClientException.class).when(taskClient).evaluateAndUploadLargePayload(any(TaskResult.class), any());
CountDownLatch latch = new CountDownLatch(1);
doAnswer(invocation -> {
latch.countDown();
return null;
}).when(worker).onErrorUpdate(any());
coordinator.init();
Uninterruptibles.awaitUninterruptibly(latch);
// When evaluateAndUploadLargePayload fails indefinitely, task update shouldn't be called.
verify(taskClient, times(0)).updateTask(any());
}
use of com.netflix.conductor.client.worker.Worker in project conductor by Netflix.
the class WorkflowTaskCoordinatorTests method testThreadPool.
@Test
public void testThreadPool() {
Worker worker = Worker.create("test", TaskResult::new);
WorkflowTaskCoordinator coordinator = new WorkflowTaskCoordinator.Builder().withWorkers(worker, worker, worker).withTaskClient(new TaskClient()).build();
// Not initialized yet
assertEquals(-1, coordinator.getThreadCount());
coordinator.init();
assertEquals(3, coordinator.getThreadCount());
// 100 is the default value
assertEquals(100, coordinator.getWorkerQueueSize());
assertEquals(500, coordinator.getSleepWhenRetry());
assertEquals(3, coordinator.getUpdateRetryCount());
coordinator = new WorkflowTaskCoordinator.Builder().withWorkers(worker).withThreadCount(100).withWorkerQueueSize(400).withSleepWhenRetry(100).withUpdateRetryCount(10).withTaskClient(new TaskClient()).withWorkerNamePrefix("test-worker-").build();
assertEquals(100, coordinator.getThreadCount());
coordinator.init();
assertEquals(100, coordinator.getThreadCount());
assertEquals(400, coordinator.getWorkerQueueSize());
assertEquals(100, coordinator.getSleepWhenRetry());
assertEquals(10, coordinator.getUpdateRetryCount());
assertEquals("test-worker-", coordinator.getWorkerNamePrefix());
}
use of com.netflix.conductor.client.worker.Worker in project conductor by Netflix.
the class TaskPollExecutorTest method testTaskExecutionException.
@Test
public void testTaskExecutionException() {
Worker worker = Worker.create(TEST_TASK_DEF_NAME, task -> {
throw new NoSuchMethodError();
});
TaskClient taskClient = Mockito.mock(TaskClient.class);
TaskPollExecutor taskPollExecutor = new TaskPollExecutor(null, taskClient, 1, 1, new HashMap<>(), "test-worker-%d");
when(taskClient.pollTask(any(), any(), any())).thenReturn(testTask());
when(taskClient.ack(any(), any())).thenReturn(true);
CountDownLatch latch = new CountDownLatch(1);
doAnswer(invocation -> {
assertEquals("test-worker-1", Thread.currentThread().getName());
Object[] args = invocation.getArguments();
TaskResult result = (TaskResult) args[0];
assertEquals(TaskResult.Status.FAILED, result.getStatus());
latch.countDown();
return null;
}).when(taskClient).updateTask(any());
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> taskPollExecutor.pollAndExecute(worker), 0, 1, TimeUnit.SECONDS);
Uninterruptibles.awaitUninterruptibly(latch);
verify(taskClient).updateTask(any());
}
use of com.netflix.conductor.client.worker.Worker in project conductor by Netflix.
the class TaskPollExecutorTest method testMultipleTasksExecution.
@SuppressWarnings("rawtypes")
@Test
public void testMultipleTasksExecution() {
String outputKey = "KEY";
Task task = testTask();
Worker worker = mock(Worker.class);
when(worker.getPollingInterval()).thenReturn(3000);
when(worker.getTaskDefName()).thenReturn(TEST_TASK_DEF_NAME);
when(worker.execute(any())).thenAnswer(new Answer() {
private int count = 0;
Map<String, Object> outputMap = new HashMap<>();
public TaskResult answer(InvocationOnMock invocation) {
// Sleep for 2 seconds to simulate task execution
Uninterruptibles.sleepUninterruptibly(2, TimeUnit.SECONDS);
TaskResult taskResult = new TaskResult(task);
outputMap.put(outputKey, count++);
taskResult.setOutputData(outputMap);
return taskResult;
}
});
TaskClient taskClient = Mockito.mock(TaskClient.class);
TaskPollExecutor taskPollExecutor = new TaskPollExecutor(null, taskClient, 1, 1, new HashMap<>(), "test-worker-");
when(taskClient.pollTask(any(), any(), any())).thenReturn(task);
when(taskClient.ack(any(), any())).thenReturn(true);
CountDownLatch latch = new CountDownLatch(3);
doAnswer(new Answer() {
private int count = 0;
public TaskResult answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
TaskResult result = (TaskResult) args[0];
assertEquals(IN_PROGRESS, result.getStatus());
assertEquals(count, result.getOutputData().get(outputKey));
count++;
latch.countDown();
return null;
}
}).when(taskClient).updateTask(any());
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> taskPollExecutor.pollAndExecute(worker), 0, 1, TimeUnit.SECONDS);
Uninterruptibles.awaitUninterruptibly(latch);
// execute() is called 3 times on the worker (once for each task)
verify(worker, times(3)).execute(any());
verify(taskClient, times(3)).updateTask(any());
}
Aggregations