use of org.qi4j.api.usecase.Usecase in project qi4j-sdk by Qi4j.
the class SchedulerTest method testTaskWithoutScheduling.
@Test
public void testTaskWithoutScheduling() throws UnitOfWorkCompletionException {
Usecase usecase = UsecaseBuilder.newUsecase("testTask");
String taskId;
try (UnitOfWork uow = module.newUnitOfWork(usecase)) {
FooTask task = createFooTask(uow, "TestTask", BAZAR);
taskId = task.identity().get();
task.run();
uow.complete();
}
try (UnitOfWork uow = module.newUnitOfWork(usecase)) {
FooTask task = uow.get(FooTask.class, taskId);
assertThat(task.output().get(), equalTo(BAR));
}
}
use of org.qi4j.api.usecase.Usecase in project qi4j-sdk by Qi4j.
the class TestApplication method prepareTest.
@Before
public void prepareTest() throws Exception {
logger.info(name.getMethodName());
Usecase usecase = UsecaseBuilder.newUsecase("Usecase:" + name);
UnitOfWork uow = module.newUnitOfWork(usecase);
populateTestData();
ServiceReference<RouteSpecificationFactoryService> routeSpecFactoryServiceRef = module.findService(RouteSpecificationFactoryService.class);
routeSpecFactory = routeSpecFactoryServiceRef.get();
// Separate test suites in console output
System.out.println();
}
use of org.qi4j.api.usecase.Usecase in project qi4j-sdk by Qi4j.
the class SchedulerTest method testMinutely.
@Test
public void testMinutely() throws Exception {
Usecase usecase = UsecaseBuilder.newUsecase("testMinutely");
UnitOfWork uow = module.newUnitOfWork(usecase);
try {
Scheduler scheduler = module.<Scheduler>findService(Scheduler.class).get();
DateTime start = new DateTime();
FooTask task = createFooTask(uow, "TestMinutely", Constants.BAZAR);
String taskIdentity = task.identity().get();
DateTime expectedRun = start.withMillisOfSecond(0).withSecondOfMinute(0).plusMinutes(1);
scheduler.scheduleCron(task, "@minutely", true);
uow.complete();
LOGGER.info("Task scheduled on {} to be run at {}", start.getMillis(), expectedRun.getMillis());
// waiting a little more
Thread.sleep(new Interval(start, expectedRun).toDurationMillis() + 5000);
usecase = UsecaseBuilder.newUsecase("testMinutely");
uow = module.newUnitOfWork(usecase);
task = uow.get(FooTask.class, taskIdentity);
assertNotNull(task);
assertEquals(Constants.BAR, task.output().get());
Timeline timeline = module.<Timeline>findService(Timeline.class).get();
DateTime now = new DateTime();
// Queries returning past records
assertEquals(2, Iterables.count(timeline.getLastRecords(5)));
assertEquals(2, Iterables.count(timeline.getRecords(start.getMillis(), now.getMillis())));
// Queries returning future records
assertEquals(4, Iterables.count(timeline.getNextRecords(4)));
assertEquals(5, Iterables.count(timeline.getRecords(now.getMillis() + 100, now.plusMinutes(5).getMillis())));
// Queries returning mixed past and future records
assertEquals(7, Iterables.count(timeline.getRecords(start.getMillis(), now.plusMinutes(5).getMillis())));
uow.complete();
} finally {
if (uow.isOpen()) {
uow.discard();
}
}
}
use of org.qi4j.api.usecase.Usecase in project qi4j-sdk by Qi4j.
the class SchedulerTest method testTask.
@Test
public void testTask() throws UnitOfWorkCompletionException, InterruptedException {
Usecase usecase = UsecaseBuilder.newUsecase("testTask");
UnitOfWork uow = module.newUnitOfWork(usecase);
FooTask task = createFooTask(uow, "TestTask", Constants.BAZAR);
String taskId = task.identity().get();
task.run();
uow.complete();
usecase = UsecaseBuilder.newUsecase("testTask");
uow = module.newUnitOfWork(usecase);
task = uow.get(FooTask.class, taskId);
assertEquals(Constants.BAR, task.output().get());
Thread.sleep(10 * 1000);
uow.complete();
}
use of org.qi4j.api.usecase.Usecase in project qi4j-sdk by Qi4j.
the class SchedulerTest method testOnce.
@Test
public void testOnce() throws UnitOfWorkCompletionException, InterruptedException {
Usecase usecase = UsecaseBuilder.newUsecase("testOnce");
UnitOfWork uow = module.newUnitOfWork(usecase);
try {
Scheduler scheduler = module.<Scheduler>findService(Scheduler.class).get();
FooTask task = createFooTask(uow, "TestOnce", Constants.BAZAR);
String taskIdentity = task.identity().get();
scheduler.scheduleOnce(task, 4, true);
uow.complete();
Thread.sleep(5000);
usecase = UsecaseBuilder.newUsecase("testOnce");
uow = module.newUnitOfWork(usecase);
task = uow.get(FooTask.class, taskIdentity);
assertNotNull(task);
assertEquals(Constants.BAR, task.output().get());
uow.complete();
} finally {
if (uow.isOpen()) {
uow.discard();
}
}
}
Aggregations