use of javax.enterprise.concurrent.ManagedScheduledExecutorService in project tomee by apache.
the class ManagedScheduledExecutorServiceTest method triggerCallableSchedule.
@Test
public void triggerCallableSchedule() throws Exception {
final ManagedScheduledExecutorService es = new ManagedScheduledExecutorServiceImplFactory().create();
final CountDownLatch counter = new CountDownLatch(5);
final FutureAwareCallable callable = new FutureAwareCallable(counter);
final Future<Long> future = es.schedule((Callable<Long>) callable, new Trigger() {
@Override
public Date getNextRunTime(final LastExecution lastExecutionInfo, final Date taskScheduledTime) {
if (lastExecutionInfo == null) {
return new Date();
}
return new Date(System.currentTimeMillis() + 100);
}
@Override
public boolean skipRun(final LastExecution lastExecutionInfo, final Date scheduledRunTime) {
return false;
}
});
assertFalse(future.isDone());
assertFalse(future.isCancelled());
counter.await(1, TimeUnit.SECONDS);
assertEquals("Future was not called", 0L, future.get().longValue());
future.cancel(true);
assertEquals("Counter did not count down in time", 0L, counter.getCount());
assertTrue(future.isDone());
assertTrue(future.isCancelled());
}
use of javax.enterprise.concurrent.ManagedScheduledExecutorService in project tomee by apache.
the class ManagedScheduledExecutorServiceTest method triggerRunnableSchedule.
@Test
public void triggerRunnableSchedule() throws Exception {
final ManagedScheduledExecutorService es = new ManagedScheduledExecutorServiceImplFactory().create();
final CountDownLatch counter = new CountDownLatch(5);
final FutureAwareCallable callable = new FutureAwareCallable(counter);
final ScheduledFuture<?> future = es.schedule(Runnable.class.cast(callable), new Trigger() {
@Override
public Date getNextRunTime(final LastExecution lastExecutionInfo, final Date taskScheduledTime) {
if (lastExecutionInfo == null) {
return new Date();
}
return new Date(lastExecutionInfo.getRunEnd().getTime() + 200);
}
@Override
public boolean skipRun(final LastExecution lastExecutionInfo, final Date scheduledRunTime) {
return false;
}
});
assertFalse(future.isDone());
assertFalse(future.isCancelled());
//Should easily get 5 invocations within 1 second
counter.await(2, TimeUnit.SECONDS);
future.cancel(true);
assertEquals("Counter did not count down in time", 0L, counter.getCount());
final boolean done = future.isDone();
assertTrue(done);
final boolean cancelled = future.isCancelled();
assertTrue(cancelled);
}
use of javax.enterprise.concurrent.ManagedScheduledExecutorService in project tomee by apache.
the class ManagedScheduledExecutorServiceTest method simpleSchedule.
@Test
public void simpleSchedule() throws Exception {
final ManagedScheduledExecutorService es = new ManagedScheduledExecutorServiceImplFactory().create();
final long start = System.currentTimeMillis();
final ScheduledFuture<Long> future = es.schedule(new Callable<Long>() {
@Override
public Long call() throws Exception {
Thread.sleep(4000);
return System.currentTimeMillis();
}
}, 2, TimeUnit.SECONDS);
assertEquals(6, TimeUnit.MILLISECONDS.toSeconds(future.get() - start), 1);
}
Aggregations