Search in sources :

Example 6 with IRunnable

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

Example 7 with IRunnable

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

Example 8 with IRunnable

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

Example 9 with IRunnable

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

Example 10 with IRunnable

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);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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