Search in sources :

Example 6 with HugeTask

use of com.baidu.hugegraph.task.HugeTask in project incubator-hugegraph by apache.

the class TaskCoreTest method testTask.

@Test
public void testTask() throws TimeoutException {
    HugeGraph graph = graph();
    TaskScheduler scheduler = graph.taskScheduler();
    TaskCallable<Object> callable = new SleepCallable<>();
    Id id = IdGenerator.of(88888);
    HugeTask<?> task = new HugeTask<>(id, null, callable);
    task.type("test");
    task.name("test-task");
    scheduler.schedule(task);
    Assert.assertEquals(id, task.id());
    Assert.assertFalse(task.completed());
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        scheduler.delete(id);
    }, e -> {
        Assert.assertContains("Can't delete incomplete task '88888'", e.getMessage());
    });
    task = scheduler.waitUntilTaskCompleted(task.id(), 10);
    Assert.assertEquals(id, task.id());
    Assert.assertEquals("test-task", task.name());
    Assert.assertEquals(TaskStatus.SUCCESS, task.status());
    Assert.assertEquals("test-task", scheduler.task(id).name());
    Assert.assertEquals("test-task", scheduler.tasks(Arrays.asList(id)).next().name());
    Iterator<HugeTask<Object>> iter = scheduler.tasks(ImmutableList.of(id));
    Assert.assertTrue(iter.hasNext());
    Assert.assertEquals("test-task", iter.next().name());
    Assert.assertFalse(iter.hasNext());
    iter = scheduler.tasks(TaskStatus.SUCCESS, 10, null);
    Assert.assertTrue(iter.hasNext());
    Assert.assertEquals("test-task", iter.next().name());
    Assert.assertFalse(iter.hasNext());
    iter = scheduler.tasks(null, 10, null);
    Assert.assertTrue(iter.hasNext());
    Assert.assertEquals("test-task", iter.next().name());
    Assert.assertFalse(iter.hasNext());
    scheduler.delete(id);
    iter = scheduler.tasks(null, 10, null);
    Assert.assertFalse(iter.hasNext());
    Assert.assertThrows(NotFoundException.class, () -> {
        scheduler.task(id);
    });
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) HugeTask(com.baidu.hugegraph.task.HugeTask) Id(com.baidu.hugegraph.backend.id.Id) TaskScheduler(com.baidu.hugegraph.task.TaskScheduler) Test(org.junit.Test)

Example 7 with HugeTask

use of com.baidu.hugegraph.task.HugeTask in project incubator-hugegraph by apache.

the class TaskCoreTest method setup.

@Before
@Override
public void setup() {
    super.setup();
    HugeGraph graph = graph();
    TaskScheduler scheduler = graph.taskScheduler();
    Iterator<HugeTask<Object>> iter = scheduler.tasks(null, -1, null);
    while (iter.hasNext()) {
        scheduler.delete(iter.next().id());
    }
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) HugeTask(com.baidu.hugegraph.task.HugeTask) TaskScheduler(com.baidu.hugegraph.task.TaskScheduler) Before(org.junit.Before)

Example 8 with HugeTask

use of com.baidu.hugegraph.task.HugeTask in project incubator-hugegraph by apache.

the class TaskCoreTest method testTaskWithFailure.

@Test
public void testTaskWithFailure() throws TimeoutException {
    HugeGraph graph = graph();
    TaskScheduler scheduler = graph.taskScheduler();
    TaskCallable<Integer> callable = new TaskCallable<Integer>() {

        @Override
        public Integer call() throws Exception {
            sleepAWhile();
            return 125;
        }

        @Override
        protected void done() {
            scheduler.save(this.task());
        }
    };
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        new HugeTask<>(null, null, callable);
    }, e -> {
        Assert.assertContains("Task id can't be null", e.getMessage());
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        Id id = IdGenerator.of("88888");
        new HugeTask<>(id, null, callable);
    }, e -> {
        Assert.assertContains("Invalid task id type, it must be number", e.getMessage());
    });
    Assert.assertThrows(NullPointerException.class, () -> {
        Id id = IdGenerator.of(88888);
        new HugeTask<>(id, null, null);
    });
    Assert.assertThrows(IllegalStateException.class, () -> {
        Id id = IdGenerator.of(88888);
        HugeTask<?> task2 = new HugeTask<>(id, null, callable);
        task2.name("test-task");
        scheduler.schedule(task2);
    }, e -> {
        Assert.assertContains("Task type can't be null", e.getMessage());
    });
    Assert.assertThrows(IllegalStateException.class, () -> {
        Id id = IdGenerator.of(88888);
        HugeTask<?> task2 = new HugeTask<>(id, null, callable);
        task2.type("test");
        scheduler.schedule(task2);
    }, e -> {
        Assert.assertContains("Task name can't be null", e.getMessage());
    });
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) HugeTask(com.baidu.hugegraph.task.HugeTask) Id(com.baidu.hugegraph.backend.id.Id) TaskScheduler(com.baidu.hugegraph.task.TaskScheduler) TaskCallable(com.baidu.hugegraph.task.TaskCallable) Test(org.junit.Test)

Aggregations

HugeTask (com.baidu.hugegraph.task.HugeTask)8 TaskScheduler (com.baidu.hugegraph.task.TaskScheduler)8 Id (com.baidu.hugegraph.backend.id.Id)5 HugeGraph (com.baidu.hugegraph.HugeGraph)3 TaskCallable (com.baidu.hugegraph.task.TaskCallable)2 Test (org.junit.Test)2 HugeException (com.baidu.hugegraph.HugeException)1 Timed (com.codahale.metrics.annotation.Timed)1 GET (jakarta.ws.rs.GET)1 Produces (jakarta.ws.rs.Produces)1 ArrayList (java.util.ArrayList)1 TimeoutException (java.util.concurrent.TimeoutException)1 Before (org.junit.Before)1