Search in sources :

Example 26 with IRunnable

use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.

the class JobExceptionTranslationTest method testWithExplicitExceptionTranslator.

@Test
public void testWithExplicitExceptionTranslator() {
    final Exception error = new Exception("expected JUnit test exception");
    IFuture<Void> future = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            throw error;
        }
    }, Jobs.newInput());
    try {
        future.awaitDoneAndGet(DefaultExceptionTranslator.class);
        fail("Exception expected");
    } catch (Exception e) {
        assertSame(error, e);
    }
    try {
        future.awaitDoneAndGet(DefaultRuntimeExceptionTranslator.class);
        fail("PlatformException expected");
    } catch (PlatformException e) {
        assertSame(error, e.getCause());
    }
}
Also used : PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) ExecutionException(java.util.concurrent.ExecutionException) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) Test(org.junit.Test)

Example 27 with IRunnable

use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.

the class JobListenerTest method testLocalListener5.

@Test
public void testLocalListener5() throws InterruptedException {
    final BlockingCountDownLatch jobRunningLatch = new BlockingCountDownLatch(1);
    IFuture<Void> future = Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            jobRunningLatch.countDownAndBlock();
        }
    }, Jobs.newInput());
    assertTrue(jobRunningLatch.await());
    JobEventCaptureListener captureListener = new JobEventCaptureListener();
    future.addListener(Jobs.newEventFilterBuilder().andMatchEventType(JobEventType.JOB_STATE_CHANGED).andMatchState(JobState.DONE).toFilter(), captureListener);
    jobRunningLatch.unblock();
    future.awaitDone();
    // verify events
    int i = -1;
    List<JobEvent> capturedEvents = captureListener.getCapturedEvents();
    List<JobState> capturedFutureStates = captureListener.getCapturedFutureStates();
    i++;
    assertStateChangedEvent(future, JobState.DONE, capturedEvents.get(i));
    assertEquals(JobState.DONE, capturedFutureStates.get(i));
    assertEquals(i + 1, capturedEvents.size());
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Example 28 with IRunnable

use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.

the class JobListenerTest method testLocalListener3.

@Test
public void testLocalListener3() throws InterruptedException {
    IFuture<Void> future1 = Jobs.getJobManager().schedule(mock(IRunnable.class), Jobs.newInput());
    future1.awaitDone();
    final BlockingCountDownLatch job2RunningLatch = new BlockingCountDownLatch(1);
    IFuture<Void> future2 = Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            job2RunningLatch.countDownAndBlock();
        }
    }, Jobs.newInput());
    assertTrue(job2RunningLatch.await());
    JobEventCaptureListener captureListener = new JobEventCaptureListener();
    Jobs.getJobManager().addListener(Jobs.newEventFilterBuilder().andMatchNotFuture(future2).toFilter(), captureListener);
    job2RunningLatch.unblock();
    future2.awaitDone();
    // verify events
    assertTrue(captureListener.getCapturedEvents().isEmpty());
    assertTrue(captureListener.getCapturedFutureStates().isEmpty());
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Example 29 with IRunnable

use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.

the class JobManagerLoadTest method testDelayedExecuting.

@Test(timeout = 20_000)
public void testDelayedExecuting() {
    IFilter<IFuture<?>> filter = Jobs.newFutureFilterBuilder().andMatchExecutionHint(JOB_IDENTIFIER).toFilter();
    Date startDate = new Date(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(3));
    final AtomicLong counter = new AtomicLong();
    for (int i = 0; i < JOB_COUNT; i++) {
        Jobs.schedule(new IRunnable() {

            @Override
            public void run() throws Exception {
                counter.incrementAndGet();
            }
        }, Jobs.newInput().withExecutionHint(JOB_IDENTIFIER).withExecutionTrigger(Jobs.newExecutionTrigger().withStartAt(startDate)));
    }
    try {
        Jobs.getJobManager().awaitDone(filter, 10, TimeUnit.SECONDS);
        assertEquals(JOB_COUNT, counter.get());
    } catch (TimedOutError e) {
        Jobs.getJobManager().cancel(filter, true);
        fail("Scheduling 25'000 took longer than 10s");
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError) Date(java.util.Date) Test(org.junit.Test)

Example 30 with IRunnable

use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.

the class JobManagerTest method testShutdown.

@Test
public void testShutdown() throws Exception {
    // synchronized because modified/read by different threads.
    final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(3);
    final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(3);
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            try {
                setupLatch.countDownAndBlock();
            } catch (InterruptedException e) {
                protocol.add("interrupted-1");
            } finally {
                verifyLatch.countDown();
            }
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            try {
                setupLatch.countDownAndBlock();
            } catch (InterruptedException e) {
                protocol.add("interrupted-2");
            } finally {
                verifyLatch.countDown();
            }
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            try {
                setupLatch.countDownAndBlock();
            } catch (InterruptedException e) {
                protocol.add("interrupted-3");
            } finally {
                verifyLatch.countDown();
            }
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
    assertTrue(setupLatch.await());
    // RUN THE TEST
    Jobs.getJobManager().shutdown();
    // VERIFY
    assertTrue(verifyLatch.await());
    assertEquals(CollectionUtility.hashSet("interrupted-1", "interrupted-2", "interrupted-3"), protocol);
    try {
        Jobs.schedule(mock(IRunnable.class), Jobs.newInput());
        fail("AssertionError expected");
    } catch (AssertionException e) {
    // NOOP
    }
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) 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