Search in sources :

Example 1 with ThreadInterruptedError

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

the class AbstractDesktop method closeInternal.

@Override
public void closeInternal() {
    setOpenedInternal(false);
    detachGui();
    List<IForm> showedForms = new ArrayList<IForm>();
    // Remove showed forms
    for (IForm form : m_formStore.values()) {
        hideForm(form);
        showedForms.add(form);
    }
    // extensions
    for (IDesktopExtension ext : getDesktopExtensions()) {
        try {
            ContributionCommand cc = ext.desktopClosingDelegate();
            if (cc == ContributionCommand.Stop) {
                break;
            }
        } catch (RuntimeException | PlatformError t) {
            LOG.error("extension {}", ext, t);
        }
    }
    // close messageboxes
    for (IMessageBox m : getMessageBoxes()) {
        if (m != null) {
            try {
                m.getUIFacade().setResultFromUI(IMessageBox.CANCEL_OPTION);
            } catch (RuntimeException | PlatformError e) {
                LOG.error("Exception while closing messagebox", e);
            }
        }
    }
    // close filechoosers
    for (IFileChooser f : getFileChoosers()) {
        if (f != null) {
            try {
                f.getUIFacade().setResultFromUI(Collections.<BinaryResource>emptyList());
            } catch (RuntimeException | PlatformError e) {
                LOG.error("Exception while closing filechooser", e);
            }
        }
    }
    // close client callbacks
    for (ClientCallback<?> c : CollectionUtility.arrayList(m_pendingPositionResponses)) {
        if (c != null) {
            try {
                c.cancel(true);
                c.failed(new ThreadInterruptedError("desktop is closing"));
            } catch (RuntimeException | PlatformError e) {
                LOG.error("Exception while closing client callback", e);
            }
        }
    }
    // close open forms
    for (IForm form : showedForms) {
        if (form != null) {
            try {
                form.doClose();
            } catch (RuntimeException | PlatformError e) {
                LOG.error("Exception while closing form", e);
            }
        }
    }
    // dispose outlines
    for (IOutline outline : getAvailableOutlines()) {
        try {
            outline.disposeTree();
        } catch (RuntimeException | PlatformError e) {
            LOG.warn("Exception while disposing outline.", e);
        }
    }
    ActionUtility.disposeActions(getActions());
    fireDesktopClosed();
}
Also used : IOutline(org.eclipse.scout.rt.client.ui.desktop.outline.IOutline) ArrayList(java.util.ArrayList) ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) IForm(org.eclipse.scout.rt.client.ui.form.IForm) IMessageBox(org.eclipse.scout.rt.client.ui.messagebox.IMessageBox) PlatformError(org.eclipse.scout.rt.platform.exception.PlatformError) IFileChooser(org.eclipse.scout.rt.client.ui.basic.filechooser.IFileChooser)

Example 2 with ThreadInterruptedError

use of org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError 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)

Example 3 with ThreadInterruptedError

use of org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError 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)

Example 4 with ThreadInterruptedError

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

the class BlockingConditionTest method testHintsUponInterruption.

@Test
public void testHintsUponInterruption() throws Throwable {
    final IBlockingCondition blockingCondition = Jobs.newBlockingCondition(true);
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
    final P_ExceptionCapturer exceptionCapturer = new P_ExceptionCapturer();
    IFuture<Void> future = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            setupLatch.countDown();
            try {
                blockingCondition.waitFor(10, TimeUnit.SECONDS, "hint-blocking");
                fail("ThreadInterruptedError expected");
            } catch (ThreadInterruptedError e) {
                assertFalse("hint not unset", IFuture.CURRENT.get().containsExecutionHint("hint-blocking"));
            }
        }
    }, Jobs.newInput().withExceptionHandling(exceptionCapturer, true));
    setupLatch.await();
    future.cancel(true);
    future.awaitFinished(10, TimeUnit.SECONDS);
    exceptionCapturer.throwOnError();
}
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)

Example 5 with ThreadInterruptedError

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

the class FutureAwaitTest method testAwaitDoneWithTimeout_Interrupted.

@Test(timeout = 5000)
public void testAwaitDoneWithTimeout_Interrupted() throws java.lang.InterruptedException {
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
    // Init
    final IFuture<String> future = Jobs.schedule(new Callable<String>() {

        @Override
        public String call() throws Exception {
            setupLatch.countDownAndBlock();
            return "result";
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()));
    // Run the test in a separate thread
    IFuture<Void> controller = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            Thread.currentThread().interrupt();
            try {
                future.awaitDone(10, TimeUnit.SECONDS);
                fail("interruption expected");
            } catch (ThreadInterruptedError e) {
                assertTrue(Thread.currentThread().isInterrupted());
            }
        }
    }, Jobs.newInput());
    controller.awaitDoneAndGet(10, TimeUnit.SECONDS);
    setupLatch.unblock();
    future.awaitDone(10, TimeUnit.SECONDS);
}
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

ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)36 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)20 Test (org.junit.Test)14 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)13 TimedOutError (org.eclipse.scout.rt.platform.util.concurrent.TimedOutError)8 FutureCancelledError (org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError)7 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)5 ArrayList (java.util.ArrayList)4 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)4 ClientRunContext (org.eclipse.scout.rt.client.context.ClientRunContext)3 PlatformException (org.eclipse.scout.rt.platform.exception.PlatformException)3 IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 JMSException (javax.jms.JMSException)2 NamingException (javax.naming.NamingException)2 IClientSession (org.eclipse.scout.rt.client.IClientSession)2 IMessageBox (org.eclipse.scout.rt.client.ui.messagebox.IMessageBox)2 RunMonitor (org.eclipse.scout.rt.platform.context.RunMonitor)2 VetoException (org.eclipse.scout.rt.platform.exception.VetoException)2