Search in sources :

Example 41 with IRunnable

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

the class JobScheduleTest method testRuntimeExceptionWithCallable.

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

        @Override
        public void run() throws Exception {
            throw exception;
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
    try {
        future.awaitDoneAndGet();
        fail("Exception expected");
    } catch (RuntimeException e) {
        assertSame(exception, e);
        assertTrue(future.isDone());
    }
}
Also used : IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 42 with IRunnable

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

the class JobScheduleTest method testProcessingExceptionWithRunnable.

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

        @Override
        public void run() throws Exception {
            throw exception;
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
    try {
        future.awaitDoneAndGet();
        fail("Exception expected");
    } catch (Exception e) {
        assertSame(exception, e);
        assertTrue(future.isDone());
    }
}
Also used : IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) Test(org.junit.Test)

Example 43 with IRunnable

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

the class AwaitDoneTest method testAwaitDoneOrWaiting.

@Test
public void testAwaitDoneOrWaiting() {
    // synchronized because modified/read by different threads.
    final List<String> protocol = Collections.synchronizedList(new ArrayList<String>());
    final IBlockingCondition condition = Jobs.newBlockingCondition(true);
    IFuture<Void> future = Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("before-1");
            condition.waitFor();
            protocol.add("after-2");
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
    try {
        Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchFuture(future).toFilter(), 100, TimeUnit.MILLISECONDS);
        fail("timeout expected");
    } catch (TimedOutError e) {
    // NOOP
    }
    Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchFuture(future).andMatchNotState(JobState.WAITING_FOR_BLOCKING_CONDITION).toFilter(), 10, TimeUnit.SECONDS);
    assertEquals(Arrays.asList("before-1"), protocol);
    // Cleanup
    condition.setBlocking(false);
    Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchFuture(future).toFilter(), 10, TimeUnit.SECONDS);
}
Also used : IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError) Test(org.junit.Test)

Example 44 with IRunnable

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

the class AwaitDoneTest method testAwaitAllDone.

@Test
public void testAwaitAllDone() {
    // synchronized because modified/read by different threads.
    final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
    IFuture<Void> future = Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("run-1");
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()));
    future.awaitDone(10, TimeUnit.SECONDS);
    assertEquals(CollectionUtility.hashSet("run-1"), protocol);
    assertTrue(future.isDone());
}
Also used : IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Example 45 with IRunnable

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

the class BlockingConditionInterruptionTest method runTest.

private static void runTest(final List<String> protocol, final WaitMethod waitForMethod, final InterruptionAction interruptionAction) {
    final String HINT_BLOCKED = "blocked";
    final AtomicReference<Thread> runnerThread = new AtomicReference<>();
    final IBlockingCondition bc = Jobs.newBlockingCondition(true);
    // Schedule job to enter blocking condition
    final IFuture<Void> future = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            runnerThread.set(Thread.currentThread());
            if (InterruptionAction.INTERRUPT_BEFORE_ENTERING.equals(interruptionAction)) {
                Thread.currentThread().interrupt();
            }
            try {
                protocol.add("beforeBlockingCondition");
                switch(waitForMethod) {
                    case WAIT_INTERRUPTIBLY:
                        bc.waitFor(HINT_BLOCKED);
                        break;
                    case WAIT_FOR_INTERRUPTIBLY_WITH_TIMEOUT:
                        bc.waitFor(0, TimeUnit.MILLISECONDS, HINT_BLOCKED);
                        break;
                    case WAIT_FOR_UNINTERRUPTIBLY:
                        bc.waitForUninterruptibly(HINT_BLOCKED);
                        break;
                    case WAIT_FOR_UNINTERRUPTIBLY_WITH_TIMEOUT:
                        bc.waitForUninterruptibly(0, TimeUnit.MILLISECONDS, HINT_BLOCKED);
                        break;
                    default:
                        throw new UnsupportedOperationException();
                }
                protocol.add("afterBlockingCondition");
            } catch (ThreadInterruptedError e) {
                protocol.add("InterruptedException");
            } catch (TimedOutError e) {
                protocol.add("TimeoutException");
            }
            if (Thread.currentThread().isInterrupted()) {
                protocol.add("threadInterrupted");
            }
        }
    }, Jobs.newInput().withName("test job").withExecutionHint(JOB_IDENTIFIER));
    // Wait until the job enters blocking condition, or is done.
    JobTestUtil.waitForCondition(new ICondition() {

        @Override
        public boolean isFulfilled() {
            return future.containsExecutionHint(HINT_BLOCKED) || future.isDone();
        }
    });
    if (InterruptionAction.INTERRUPT_WHILE_BLOCKING.equals(interruptionAction)) {
        runnerThread.get().interrupt();
        // Sleep some time so that the runner is interrupted.
        // That is because a thread's interruption is asynchronous, meaning that once a thread (a) interrupts another threads
        // (b), and in turn thread (a) unblocks the condition which thread (b) is waiting for, it is not ensured that thread
        // (b) exists with an {@link InterruptedException}. However, many tests expect exactly that behavior.
        SleepUtil.sleepSafe(100, TimeUnit.MILLISECONDS);
    }
    bc.setBlocking(false);
    Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchExecutionHint(JOB_IDENTIFIER).toFilter(), 10, TimeUnit.SECONDS);
}
Also used : ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) AtomicReference(java.util.concurrent.atomic.AtomicReference) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError) ICondition(org.eclipse.scout.rt.testing.platform.job.JobTestUtil.ICondition)

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