use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.
the class ScheduleAtFixedRateTest method testFiveLongRunsAndException.
@Test
public void testFiveLongRunsAndException() {
final List<Long> protocol = Collections.synchronizedList(new ArrayList<Long>());
final AtomicInteger counter = new AtomicInteger();
final int nRuns = 3;
final long sleepTimeMillis = 1500;
long initialDelayMillis = 300;
long periodMillis = 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());
Thread.sleep(TimeUnit.MILLISECONDS.toMillis(sleepTimeMillis));
}
}
}, Jobs.newInput().withRunContext(RunContexts.empty()).withExceptionHandling(null, false).withExecutionTrigger(Jobs.newExecutionTrigger().withStartIn(initialDelayMillis, TimeUnit.MILLISECONDS).withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInMilliseconds(TimeUnit.MILLISECONDS.toMillis(periodMillis)).repeatForever())));
// 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 * sleepTimeMillis;
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 ScheduleAtFixedRateTest 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 periodMillis = 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(SimpleScheduleBuilder.simpleSchedule().withIntervalInMilliseconds(TimeUnit.MILLISECONDS.toMillis(periodMillis)).repeatForever())));
// 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 * periodMillis;
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 ScheduleAtFixedRateTest method testFiveShortRunsAndException.
@Test
public void testFiveShortRunsAndException() {
final List<Long> protocol = Collections.synchronizedList(new ArrayList<Long>());
final AtomicInteger counter = new AtomicInteger();
final int nRuns = 3;
final long sleepTimeMillis = 300;
long initialDelayMillis = 300;
long periodMillis = 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());
Thread.sleep(TimeUnit.MILLISECONDS.toMillis(sleepTimeMillis));
}
}
}, Jobs.newInput().withRunContext(RunContexts.empty()).withExceptionHandling(null, false).withExecutionTrigger(Jobs.newExecutionTrigger().withStartIn(initialDelayMillis, TimeUnit.MILLISECONDS).withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInMilliseconds(TimeUnit.MILLISECONDS.toMillis(periodMillis)).repeatForever())));
// 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 * periodMillis;
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 ScheduleAtFixedRateTest 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(SimpleScheduleBuilder.simpleSchedule().withIntervalInMilliseconds(1).repeatForever()))).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 ScheduleAtFixedRateTest 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(SimpleScheduleBuilder.simpleSchedule().withIntervalInMilliseconds(1).repeatForever()))).awaitDone(10, TimeUnit.SECONDS);
assertTrue(counter.get() > 10);
}
Aggregations