use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.
the class ScheduleDelayedTest method testScheduleDelayed.
@Test
public void testScheduleDelayed() {
final AtomicReference<Long> actualExecutionTime = new AtomicReference<>();
long tStartMillis = System.currentTimeMillis();
long delayMillis = TimeUnit.SECONDS.toMillis(1);
IFuture<Void> future = Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
actualExecutionTime.set(System.currentTimeMillis());
}
}, Jobs.newInput().withRunContext(RunContexts.empty()).withExecutionTrigger(Jobs.newExecutionTrigger().withStartIn(delayMillis, TimeUnit.MILLISECONDS)));
// verify
future.awaitDone(10, TimeUnit.SECONDS);
long minExpectedExecutionTime = tStartMillis + delayMillis;
if (actualExecutionTime.get() < minExpectedExecutionTime) {
fail(String.format("actualExecutionTime=%s, minExpectedExecutionTime=[%s]", actualExecutionTime.get(), minExpectedExecutionTime));
}
}
use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.
the class ScheduleWithFixedDelayTest method testFiveRunsAndException.
@Test
public void testFiveRunsAndException() {
final List<Long> protocol = Collections.synchronizedList(new ArrayList<Long>());
final AtomicInteger counter = new AtomicInteger();
final int nRuns = 3;
long initialDelayMillis = 300;
long delayMillis = 500;
long tStartMillis = System.currentTimeMillis();
// Schedule a job which runs 'nRuns' times and cancels itself afterwards.
IFuture<Void> future = Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
if (counter.incrementAndGet() == nRuns) {
throw new Exception("expected JUnit test exception");
} else {
protocol.add(System.currentTimeMillis());
}
}
}, Jobs.newInput().withRunContext(RunContexts.empty()).withExceptionHandling(null, false).withExecutionTrigger(Jobs.newExecutionTrigger().withStartIn(initialDelayMillis, TimeUnit.MILLISECONDS).withSchedule(FixedDelayScheduleBuilder.repeatForever(delayMillis, TimeUnit.MILLISECONDS))));
// verify
Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchFuture(future).toFilter(), 30, TimeUnit.SECONDS);
assertEquals(nRuns, counter.get());
for (int i = 0; i < protocol.size(); i++) {
Long actualExecutionTime = protocol.get(i);
long expectedExecutionTime = tStartMillis + initialDelayMillis + i * delayMillis;
long expectedExecutionTimeMin = expectedExecutionTime;
if (actualExecutionTime < expectedExecutionTimeMin) {
fail(String.format("run=%s, actualExecutionTime=%s, expectedExecutionTime=%s", i, actualExecutionTime, expectedExecutionTimeMin));
}
}
}
use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.
the class ScheduleWithFixedDelayTest method testRepetiveWithEndTime.
@Test
public void testRepetiveWithEndTime() {
final AtomicInteger counter = new AtomicInteger();
Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
counter.incrementAndGet();
}
}, Jobs.newInput().withExecutionTrigger(Jobs.newExecutionTrigger().withEndIn(1, TimeUnit.SECONDS).withSchedule(FixedDelayScheduleBuilder.repeatForever(1, TimeUnit.MILLISECONDS)))).awaitDone(10, TimeUnit.SECONDS);
assertTrue(counter.get() > 10);
}
use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.
the class ScheduleWithFixedDelayTest method testSwallowException.
@Test
public void testSwallowException() {
final AtomicInteger counter = new AtomicInteger();
Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
if (counter.incrementAndGet() == 2) {
RunMonitor.CURRENT.get().cancel(false);
} else {
throw new Exception("expected JUnit test exception");
}
}
}, Jobs.newInput().withRunContext(RunContexts.empty()).withExceptionHandling(null, true).withExecutionTrigger(Jobs.newExecutionTrigger().withSchedule(FixedDelayScheduleBuilder.repeatForever(1, TimeUnit.MILLISECONDS)))).awaitDone(10, TimeUnit.SECONDS);
assertEquals(2, counter.get());
}
use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.
the class WhenDoneScheduleTest method testFunctionFutureState.
@Test
public void testFunctionFutureState() throws InterruptedException {
final BlockingCountDownLatch jobRunningLatch = new BlockingCountDownLatch(1);
final BlockingCountDownLatch functionJobRunningLatch = new BlockingCountDownLatch(1);
// Schedule future
IFuture<Void> future = Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
jobRunningLatch.countDownAndBlock();
}
}, Jobs.newInput().withExecutionHint(JOB_MARKER));
// Schedule function
IFuture<Void> functionFuture = future.whenDoneSchedule(new IBiConsumer<Void, Throwable>() {
@Override
public void accept(Void result, Throwable u) {
try {
functionJobRunningLatch.countDownAndBlock();
} catch (InterruptedException e) {
throw new ThreadInterruptedError("interrupted", e);
}
}
}, Jobs.newInput().withExecutionHint(JOB_MARKER));
assertTrue(jobRunningLatch.await());
assertEquals(JobState.RUNNING, future.getState());
assertEquals(JobState.NEW, functionFuture.getState());
jobRunningLatch.unblock();
assertTrue(functionJobRunningLatch.await());
assertEquals(JobState.DONE, future.getState());
assertEquals(JobState.RUNNING, functionFuture.getState());
functionJobRunningLatch.unblock();
// Wait until the job jobs are done
Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchExecutionHint(JOB_MARKER).toFilter(), 5, TimeUnit.SECONDS);
assertEquals(JobState.DONE, functionFuture.getState());
assertEquals(JobState.DONE, future.getState());
}
Aggregations