Search in sources :

Example 6 with Task

use of com.netflix.conductor.common.metadata.tasks.Task in project conductor by Netflix.

the class TaskTest method testTaskQueueWaitTime.

@Test
public void testTaskQueueWaitTime() {
    Task task = new Task();
    long currentTimeMillis = System.currentTimeMillis();
    // 30 seconds ago
    task.setScheduledTime(currentTimeMillis - 30_000);
    task.setStartTime(currentTimeMillis - 25_000);
    long queueWaitTime = task.getQueueWaitTime();
    assertEquals(5000L, queueWaitTime);
    task.setUpdateTime(currentTimeMillis - 20_000);
    task.setCallbackAfterSeconds(10);
    queueWaitTime = task.getQueueWaitTime();
    assertTrue(queueWaitTime > 0);
}
Also used : Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) Test(org.junit.Test)

Example 7 with Task

use of com.netflix.conductor.common.metadata.tasks.Task 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());
}
Also used : Task(com.netflix.conductor.common.metadata.tasks.Task) HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) TaskClient(com.netflix.conductor.client.http.TaskClient) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Worker(com.netflix.conductor.client.worker.Worker) TaskResult(com.netflix.conductor.common.metadata.tasks.TaskResult) Test(org.junit.Test)

Example 8 with Task

use of com.netflix.conductor.common.metadata.tasks.Task in project conductor by Netflix.

the class TaskPollExecutorTest method testTaskPoll.

@Test
public void testTaskPoll() {
    Task task = testTask();
    Worker worker = mock(Worker.class);
    when(worker.getPollingInterval()).thenReturn(3000);
    when(worker.getTaskDefName()).thenReturn("test");
    when(worker.execute(any())).thenReturn(new TaskResult(task));
    TaskClient taskClient = Mockito.mock(TaskClient.class);
    when(taskClient.pollTask(any(), any(), any())).thenReturn(new Task()).thenReturn(task);
    TaskPollExecutor taskPollExecutor = new TaskPollExecutor(null, taskClient, 1, 1, new HashMap<>(), "test-worker-");
    CountDownLatch latch = new CountDownLatch(1);
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        TaskResult result = (TaskResult) args[0];
        assertEquals(IN_PROGRESS, result.getStatus());
        assertEquals(task.getTaskId(), result.getTaskId());
        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());
}
Also used : TaskClient(com.netflix.conductor.client.http.TaskClient) Task(com.netflix.conductor.common.metadata.tasks.Task) Worker(com.netflix.conductor.client.worker.Worker) TaskResult(com.netflix.conductor.common.metadata.tasks.TaskResult) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 9 with Task

use of com.netflix.conductor.common.metadata.tasks.Task in project conductor by Netflix.

the class TaskRunnerConfigurerTest method testMultipleWorkersExecution.

@Test
public void testMultipleWorkersExecution() {
    String task1Name = "task1";
    Worker worker1 = mock(Worker.class);
    when(worker1.getPollingInterval()).thenReturn(3000);
    when(worker1.getTaskDefName()).thenReturn(task1Name);
    when(worker1.getIdentity()).thenReturn("worker1");
    when(worker1.execute(any())).thenAnswer(invocation -> {
        // Sleep for 2 seconds to simulate task execution
        Uninterruptibles.sleepUninterruptibly(2, TimeUnit.SECONDS);
        TaskResult taskResult = new TaskResult();
        taskResult.setStatus(COMPLETED);
        return taskResult;
    });
    String task2Name = "task2";
    Worker worker2 = mock(Worker.class);
    when(worker2.getPollingInterval()).thenReturn(3000);
    when(worker2.getTaskDefName()).thenReturn(task2Name);
    when(worker2.getIdentity()).thenReturn("worker2");
    when(worker2.execute(any())).thenAnswer(invocation -> {
        // Sleep for 2 seconds to simulate task execution
        Uninterruptibles.sleepUninterruptibly(2, TimeUnit.SECONDS);
        TaskResult taskResult = new TaskResult();
        taskResult.setStatus(COMPLETED);
        return taskResult;
    });
    Task task1 = testTask(task1Name);
    Task task2 = testTask(task2Name);
    TaskClient taskClient = Mockito.mock(TaskClient.class);
    TaskRunnerConfigurer configurer = new TaskRunnerConfigurer.Builder(taskClient, Arrays.asList(worker1, worker2)).withThreadCount(2).withSleepWhenRetry(100000).withUpdateRetryCount(1).withWorkerNamePrefix("test-worker-").build();
    when(taskClient.pollTask(any(), any(), any())).thenAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String taskName = args[0].toString();
        if (taskName.equals(task1Name)) {
            return task1;
        } else if (taskName.equals(task2Name)) {
            return task2;
        } else {
            return null;
        }
    });
    when(taskClient.ack(any(), any())).thenReturn(true);
    AtomicInteger task1Counter = new AtomicInteger(0);
    AtomicInteger task2Counter = new AtomicInteger(0);
    CountDownLatch latch = new CountDownLatch(2);
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        TaskResult result = (TaskResult) args[0];
        assertEquals(COMPLETED, result.getStatus());
        if (result.getWorkerId().equals("worker1")) {
            task1Counter.incrementAndGet();
        } else if (result.getWorkerId().equals("worker2")) {
            task2Counter.incrementAndGet();
        }
        latch.countDown();
        return null;
    }).when(taskClient).updateTask(any());
    configurer.init();
    Uninterruptibles.awaitUninterruptibly(latch);
    assertEquals(1, task1Counter.get());
    assertEquals(1, task2Counter.get());
}
Also used : TaskClient(com.netflix.conductor.client.http.TaskClient) Task(com.netflix.conductor.common.metadata.tasks.Task) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Worker(com.netflix.conductor.client.worker.Worker) TaskResult(com.netflix.conductor.common.metadata.tasks.TaskResult) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 10 with Task

use of com.netflix.conductor.common.metadata.tasks.Task in project conductor by Netflix.

the class TaskTest method test.

@Test
public void test() {
    Task task = new Task();
    task.setStatus(Status.FAILED);
    assertEquals(Status.FAILED, task.getStatus());
    Set<String> resultStatues = Arrays.stream(TaskResult.Status.values()).map(Enum::name).collect(Collectors.toSet());
    for (Status status : Status.values()) {
        if (resultStatues.contains(status.name())) {
            TaskResult.Status trStatus = TaskResult.Status.valueOf(status.name());
            assertEquals(status.name(), trStatus.name());
            task = new Task();
            task.setStatus(status);
            assertEquals(status, task.getStatus());
        }
    }
}
Also used : Status(com.netflix.conductor.common.metadata.tasks.Task.Status) Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) TaskResult(com.netflix.conductor.common.metadata.tasks.TaskResult) Test(org.junit.Test)

Aggregations

Task (com.netflix.conductor.common.metadata.tasks.Task)357 Workflow (com.netflix.conductor.common.run.Workflow)249 Test (org.junit.Test)248 WorkflowTask (com.netflix.conductor.common.metadata.workflow.WorkflowTask)227 HashMap (java.util.HashMap)147 WorkflowDef (com.netflix.conductor.common.metadata.workflow.WorkflowDef)121 SubWorkflow (com.netflix.conductor.core.execution.tasks.SubWorkflow)110 TaskDef (com.netflix.conductor.common.metadata.tasks.TaskDef)95 UserTask (com.netflix.conductor.tests.utils.UserTask)73 Map (java.util.Map)53 LinkedList (java.util.LinkedList)51 WorkflowSystemTask (com.netflix.conductor.core.execution.tasks.WorkflowSystemTask)45 List (java.util.List)45 ApplicationException (com.netflix.conductor.core.execution.ApplicationException)41 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)39 TaskResult (com.netflix.conductor.common.metadata.tasks.TaskResult)38 Status (com.netflix.conductor.common.metadata.tasks.Task.Status)32 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)29 Collectors (java.util.stream.Collectors)29 TaskType (com.netflix.conductor.common.metadata.workflow.TaskType)28