Search in sources :

Example 1 with ICondition

use of org.eclipse.scout.rt.testing.platform.job.JobTestUtil.ICondition 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

AtomicReference (java.util.concurrent.atomic.AtomicReference)1 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)1 ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)1 TimedOutError (org.eclipse.scout.rt.platform.util.concurrent.TimedOutError)1 ICondition (org.eclipse.scout.rt.testing.platform.job.JobTestUtil.ICondition)1