Search in sources :

Example 11 with IRunnable

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));
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Example 12 with IRunnable

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));
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Example 13 with IRunnable

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);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Example 14 with IRunnable

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Example 15 with IRunnable

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());
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Aggregations

IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)260 Test (org.junit.Test)210 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)82 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)68 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)40 ArrayList (java.util.ArrayList)36 PlatformException (org.eclipse.scout.rt.platform.exception.PlatformException)32 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)26 TimedOutError (org.eclipse.scout.rt.platform.util.concurrent.TimedOutError)21 IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)20 IExecutionSemaphore (org.eclipse.scout.rt.platform.job.IExecutionSemaphore)20 ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)20 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)19 JobEvent (org.eclipse.scout.rt.platform.job.listener.JobEvent)17 VetoException (org.eclipse.scout.rt.platform.exception.VetoException)13 Times (org.eclipse.scout.rt.testing.platform.runner.Times)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)11 IFuture (org.eclipse.scout.rt.platform.job.IFuture)10 IJobManager (org.eclipse.scout.rt.platform.job.IJobManager)10 JMSException (javax.jms.JMSException)9