Search in sources :

Example 1 with RunnableTask

use of com.enonic.xp.task.RunnableTask in project xp by enonic.

the class LocalTaskManagerImplTest method submitTaskWithError.

@Test
public void submitTaskWithError() {
    final RunnableTask runnableTask = (id, progressReporter) -> {
        throw new RuntimeException("Some error");
    };
    assertEquals(0, taskMan.getAllTasks().size());
    assertEquals(0, taskMan.getRunningTasks().size());
    assertNull(taskMan.getTaskInfo(TaskId.from("1")));
    final DescribedTaskImpl describedTask = new DescribedTaskImpl(runnableTask, "task 1", TEST_TASK_CONTEXT);
    taskMan.submitTask(describedTask);
    assertNotNull(taskMan.getTaskInfo(describedTask.getTaskId()));
    assertEquals(1, taskMan.getAllTasks().size());
    assertEquals(0, taskMan.getRunningTasks().size());
    assertEquals(TaskState.FAILED, taskMan.getTaskInfo(describedTask.getTaskId()).getState());
    assertEquals(4, eventsPublished.size());
    assertEquals("task.submitted , task.updated , task.updated , task.failed", eventTypes());
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) TaskState(com.enonic.xp.task.TaskState) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) RecurringJob(com.enonic.xp.core.internal.concurrent.RecurringJob) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Branch(com.enonic.xp.branch.Branch) TaskInfo(com.enonic.xp.task.TaskInfo) ArrayList(java.util.ArrayList) RepositoryId(com.enonic.xp.repository.RepositoryId) RunnableTask(com.enonic.xp.task.RunnableTask) Event(com.enonic.xp.event.Event) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Bundle(org.osgi.framework.Bundle) OsgiSupportMock(com.enonic.xp.core.internal.osgi.OsgiSupportMock) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Mockito.when(org.mockito.Mockito.when) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) TaskId(com.enonic.xp.task.TaskId) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) TaskContext(com.enonic.xp.impl.task.distributed.TaskContext) ChronoUnit(java.time.temporal.ChronoUnit) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Clock(java.time.Clock) Mockito.mock(org.mockito.Mockito.mock) RunnableTask(com.enonic.xp.task.RunnableTask) Test(org.junit.jupiter.api.Test)

Example 2 with RunnableTask

use of com.enonic.xp.task.RunnableTask in project xp by enonic.

the class LocalTaskManagerImplTest method testRemoveExpiredTasks.

@Test
public void testRemoveExpiredTasks() throws InterruptedException {
    Instant initTime = Instant.now();
    taskMan.setClock(Clock.fixed(initTime, ZoneId.systemDefault()));
    CountDownLatch latch = new CountDownLatch(1);
    RunnableTask runnableTask = (id, progressReporter) -> {
        latch.countDown();
    };
    final DescribedTaskImpl describedTask = new DescribedTaskImpl(runnableTask, "task 1", TEST_TASK_CONTEXT);
    taskMan.submitTask(describedTask);
    cleanupScheduler.rerun();
    assertNotNull(taskMan.getTaskInfo(describedTask.getTaskId()));
    assertEquals(1, taskMan.getAllTasks().size());
    assertTrue(taskMan.getRunningTasks().size() <= 1);
    latch.await();
    Instant laterTime = initTime.plus(LocalTaskManagerImpl.KEEP_COMPLETED_MAX_TIME_SEC + 1, ChronoUnit.SECONDS);
    taskMan.setClock(Clock.fixed(laterTime, ZoneId.systemDefault()));
    cleanupScheduler.rerun();
    assertNull(taskMan.getTaskInfo(describedTask.getTaskId()));
    assertEquals(0, taskMan.getAllTasks().size());
    assertEquals(0, taskMan.getRunningTasks().size());
    assertEquals(4, eventsPublished.size());
    assertEquals("task.submitted , task.updated , task.finished , task.removed", eventTypes());
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) TaskState(com.enonic.xp.task.TaskState) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) RecurringJob(com.enonic.xp.core.internal.concurrent.RecurringJob) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Branch(com.enonic.xp.branch.Branch) TaskInfo(com.enonic.xp.task.TaskInfo) ArrayList(java.util.ArrayList) RepositoryId(com.enonic.xp.repository.RepositoryId) RunnableTask(com.enonic.xp.task.RunnableTask) Event(com.enonic.xp.event.Event) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Bundle(org.osgi.framework.Bundle) OsgiSupportMock(com.enonic.xp.core.internal.osgi.OsgiSupportMock) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Mockito.when(org.mockito.Mockito.when) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) TaskId(com.enonic.xp.task.TaskId) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) TaskContext(com.enonic.xp.impl.task.distributed.TaskContext) ChronoUnit(java.time.temporal.ChronoUnit) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Clock(java.time.Clock) Mockito.mock(org.mockito.Mockito.mock) Instant(java.time.Instant) RunnableTask(com.enonic.xp.task.RunnableTask) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 3 with RunnableTask

use of com.enonic.xp.task.RunnableTask in project xp by enonic.

the class LocalTaskManagerImplTest method submitTask.

@Test
public void submitTask() {
    final RunnableTask runnableTask = (id, progressReporter) -> {
        for (int i = 0; i < 5; i++) {
            progressReporter.progress(1, 10);
            progressReporter.info("Step " + i);
        }
    };
    assertEquals(0, taskMan.getAllTasks().size());
    assertEquals(0, taskMan.getRunningTasks().size());
    assertNull(taskMan.getTaskInfo(TaskId.from("1")));
    final DescribedTaskImpl describedTask = new DescribedTaskImpl(runnableTask, "task 1", TEST_TASK_CONTEXT);
    taskMan.submitTask(describedTask);
    assertNotNull(taskMan.getTaskInfo(describedTask.getTaskId()));
    for (int i = 0; i < 25; i++) {
        final TaskInfo taskInfo = taskMan.getTaskInfo(describedTask.getTaskId());
        System.out.printf("Task %s, details: %s\r\n", describedTask.getTaskId(), taskInfo);
        System.out.flush();
    }
    assertEquals(1, taskMan.getAllTasks().size());
    assertEquals(0, taskMan.getRunningTasks().size());
    assertNotNull(taskMan.getTaskInfo(describedTask.getTaskId()));
    assertEquals(13, eventsPublished.size());
    assertEquals("task.submitted , task.updated , task.updated , task.updated , task.updated , task.updated , " + "task.updated , task.updated , task.updated , task.updated , task.updated , task.updated , task.finished", eventTypes());
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) TaskState(com.enonic.xp.task.TaskState) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) RecurringJob(com.enonic.xp.core.internal.concurrent.RecurringJob) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Branch(com.enonic.xp.branch.Branch) TaskInfo(com.enonic.xp.task.TaskInfo) ArrayList(java.util.ArrayList) RepositoryId(com.enonic.xp.repository.RepositoryId) RunnableTask(com.enonic.xp.task.RunnableTask) Event(com.enonic.xp.event.Event) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Bundle(org.osgi.framework.Bundle) OsgiSupportMock(com.enonic.xp.core.internal.osgi.OsgiSupportMock) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Mockito.when(org.mockito.Mockito.when) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) TaskId(com.enonic.xp.task.TaskId) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) TaskContext(com.enonic.xp.impl.task.distributed.TaskContext) ChronoUnit(java.time.temporal.ChronoUnit) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Clock(java.time.Clock) Mockito.mock(org.mockito.Mockito.mock) TaskInfo(com.enonic.xp.task.TaskInfo) RunnableTask(com.enonic.xp.task.RunnableTask) Test(org.junit.jupiter.api.Test)

Example 4 with RunnableTask

use of com.enonic.xp.task.RunnableTask in project xp by enonic.

the class NamedTaskFactoryImplTest method createExisting.

@Test
void createExisting() {
    final DescriptorKey descriptorKey = DescriptorKey.from("myapplication:mytask");
    final TaskDescriptor descriptor = TaskDescriptor.create().key(descriptorKey).build();
    when(taskDescriptorService.getTask(descriptorKey)).thenReturn(descriptor);
    final RunnableTask runnableTask = namedTaskScriptFactory.create(descriptor, new PropertyTree());
    assertNotNull(runnableTask);
    runnableTask.run(TaskId.from("123"), null);
}
Also used : TaskDescriptor(com.enonic.xp.task.TaskDescriptor) PropertyTree(com.enonic.xp.data.PropertyTree) DescriptorKey(com.enonic.xp.page.DescriptorKey) RunnableTask(com.enonic.xp.task.RunnableTask) Test(org.junit.jupiter.api.Test)

Aggregations

RunnableTask (com.enonic.xp.task.RunnableTask)4 Test (org.junit.jupiter.api.Test)4 Branch (com.enonic.xp.branch.Branch)3 RecurringJob (com.enonic.xp.core.internal.concurrent.RecurringJob)3 OsgiSupportMock (com.enonic.xp.core.internal.osgi.OsgiSupportMock)3 Event (com.enonic.xp.event.Event)3 TaskContext (com.enonic.xp.impl.task.distributed.TaskContext)3 RepositoryId (com.enonic.xp.repository.RepositoryId)3 AuthenticationInfo (com.enonic.xp.security.auth.AuthenticationInfo)3 TaskId (com.enonic.xp.task.TaskId)3 TaskInfo (com.enonic.xp.task.TaskInfo)3 TaskState (com.enonic.xp.task.TaskState)3 Clock (java.time.Clock)3 Instant (java.time.Instant)3 ZoneId (java.time.ZoneId)3 ChronoUnit (java.time.temporal.ChronoUnit)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Collectors (java.util.stream.Collectors)3