Search in sources :

Example 16 with TimedOutError

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

the class JmsMomImplementorTest method testSubscriptionDisposeSynchronized.

/**
 * In case of single threaded subscription, {@link ISubscription#dispose()} waits for any ongoing processing of this
 * subscription to finish.
 */
private void testSubscriptionDisposeSynchronized(SubscribeInput subscribeInput) throws InterruptedException {
    final Capturer<String> capturer = new Capturer<>();
    final CountDownLatch latch = new CountDownLatch(1);
    IDestination<String> queue = MOM.newDestination("test/mom/testPublishText", DestinationType.QUEUE, ResolveMethod.DEFINE, null);
    m_disposables.add(MOM.registerMarshaller(JmsTestMom.class, queue, BEANS.get(TextMarshaller.class)));
    MOM.publish(JmsTestMom.class, queue, "hello world");
    final ISubscription subscription = MOM.subscribe(JmsTestMom.class, queue, new IMessageListener<String>() {

        @Override
        public void onMessage(IMessage<String> message) {
            capturer.set(message.getTransferObject());
            try {
                latch.await();
            } catch (InterruptedException e) {
                throw BEANS.get(DefaultRuntimeExceptionTranslator.class).translate(e);
            }
        }
    }, subscribeInput);
    assertEquals("hello world", capturer.get());
    IFuture<Void> disposeFuture = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            subscription.dispose();
        }
    }, Jobs.newInput().withName("dispose subscription").withExecutionHint(m_testJobExecutionHint).withExceptionHandling(null, false));
    // Verify
    try {
        disposeFuture.awaitDoneAndGet(200, TimeUnit.MILLISECONDS);
        assertEquals(SubscribeInput.ACKNOWLEDGE_AUTO, subscribeInput.getAcknowledgementMode());
    } catch (TimedOutError e) {
        assertEquals(SubscribeInput.ACKNOWLEDGE_AUTO_SINGLE_THREADED, subscribeInput.getAcknowledgementMode());
    } finally {
        latch.countDown();
    }
}
Also used : IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError) CountDownLatch(java.util.concurrent.CountDownLatch) BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) DefaultRuntimeExceptionTranslator(org.eclipse.scout.rt.platform.exception.DefaultRuntimeExceptionTranslator) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) NamingException(javax.naming.NamingException) JMSException(javax.jms.JMSException) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) VetoException(org.eclipse.scout.rt.platform.exception.VetoException) ISubscription(org.eclipse.scout.rt.mom.api.ISubscription)

Example 17 with TimedOutError

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

the class TimeoutRunContextStatement method evaluate.

@Override
public void evaluate() throws Throwable {
    final SafeStatementInvoker invoker = new SafeStatementInvoker(m_next);
    final IFuture<Void> future = Jobs.schedule(invoker, Jobs.newInput().withRunContext(// Run in new TX, because the same TX is not allowed to be used by multiple threads.
    RunContext.CURRENT.get().copy().withTransactionScope(TransactionScope.REQUIRES_NEW)).withName("Running test with support for JUnit timeout"));
    try {
        future.awaitDone(m_timeoutMillis, TimeUnit.MILLISECONDS);
    } catch (ThreadInterruptedError | TimedOutError e) {
        // NOSONAR
        future.cancel(true);
        // JUnit timeout exception
        throw new TestTimedOutException(m_timeoutMillis, TimeUnit.MILLISECONDS);
    }
    invoker.throwOnError();
}
Also used : SafeStatementInvoker(org.eclipse.scout.rt.testing.platform.runner.SafeStatementInvoker) ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError) TestTimedOutException(org.junit.runners.model.TestTimedOutException)

Example 18 with TimedOutError

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

the class ClientSessionTest method testStopWithBlockingMessageBox.

@Test
public void testStopWithBlockingMessageBox() throws Exception {
    TestingUtility.registerBean(new BeanMetaData(TestEnvironmentClientSession.class));
    TestingUtility.registerBean(new BeanMetaData(JobCompletionDelayOnSessionShutdown.class).withProducer(new IBeanInstanceProducer<JobCompletionDelayOnSessionShutdown>() {

        @Override
        public JobCompletionDelayOnSessionShutdown produce(IBean<JobCompletionDelayOnSessionShutdown> bean) {
            return new JobCompletionDelayOnSessionShutdown() {

                @Override
                protected Long getDefaultValue() {
                    return 1000L;
                }
            };
        }
    }));
    session = BEANS.get(ClientSessionProvider.class).provide(ClientRunContexts.empty().withUserAgent(UserAgents.createDefault()));
    // show a messagebox
    IFuture<Integer> f = ModelJobs.schedule(new Callable<Integer>() {

        @Override
        public Integer call() throws Exception {
            messageBox = MessageBoxes.createYesNo();
            return messageBox.show();
        }
    }, ModelJobs.newInput(ClientRunContexts.empty().withSession(session, true)));
    try {
        f.awaitDoneAndGet(1, TimeUnit.SECONDS);
        fail("must throw a " + TimedOutError.class.getName());
    } catch (TimedOutError e) {
    // nop
    }
    assertFalse(f.isDone());
    // close from ui
    ModelJobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            session.getDesktop().getUIFacade().closeFromUI(true);
        }
    }, ModelJobs.newInput(ClientRunContexts.empty().withSession(session, true))).awaitDone();
    assertEquals(JobState.DONE, f.getState());
}
Also used : BeanMetaData(org.eclipse.scout.rt.platform.BeanMetaData) IBeanInstanceProducer(org.eclipse.scout.rt.platform.IBeanInstanceProducer) JobCompletionDelayOnSessionShutdown(org.eclipse.scout.rt.client.ClientConfigProperties.JobCompletionDelayOnSessionShutdown) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) IBean(org.eclipse.scout.rt.platform.IBean) TestEnvironmentClientSession(org.eclipse.scout.rt.client.testenvironment.TestEnvironmentClientSession) Test(org.junit.Test)

Example 19 with TimedOutError

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

the class BlockingCondition method awaitUntilSignaledOrTimeout.

/**
 * Waits until signaled or the timeout elapses. If <code>awaitInterruptibly</code> is set to <code>true</code>, this
 * method returns with an {@link ThreadInterruptedError} upon interruption. For either case, when this method
 * finally returns, the thread's interrupted status will still be set.
 */
protected void awaitUntilSignaledOrTimeout(final long timeout, final TimeUnit unit, final boolean awaitInterruptibly) {
    boolean interrupted = false;
    m_lock.lock();
    try {
        if (timeout == TIMEOUT_INDEFINITELY) {
            while (m_blocking) {
                // while-loop to address spurious wake-ups
                if (awaitInterruptibly) {
                    m_unblockedCondition.await();
                } else {
                    m_unblockedCondition.awaitUninterruptibly();
                }
            }
        } else {
            long nanos = unit.toNanos(timeout);
            while (m_blocking && nanos > 0L) {
                // while-loop to address spurious wake-ups
                if (awaitInterruptibly) {
                    nanos = m_unblockedCondition.awaitNanos(nanos);
                } else {
                    try {
                        // NOSONAR
                        nanos = m_unblockedCondition.awaitNanos(nanos);
                    } catch (final InterruptedException e) {
                        // remember interruption
                        interrupted = true;
                        // clear the interrupted status to continue waiting
                        Thread.interrupted();
                    }
                }
            }
            if (nanos <= 0L) {
                throw new TimedOutError("Timeout elapsed while waiting for a blocking condition to fall").withContextInfo("blockingCondition", this).withContextInfo("timeout", "{}ms", unit.toMillis(timeout)).withContextInfo("thread", Thread.currentThread().getName());
            }
        }
    } catch (final InterruptedException e) {
        interrupted = true;
        throw new ThreadInterruptedError("Interrupted while waiting for a blocking condition to fall").withContextInfo("blockingCondition", this).withContextInfo("thread", Thread.currentThread().getName());
    } finally {
        m_lock.unlock();
        if (interrupted) {
            // restore interruption status
            Thread.currentThread().interrupt();
        }
    }
}
Also used : ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError)

Example 20 with TimedOutError

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

the class JmsMomImplementorTest method testPublishTransactional.

@Test
public void testPublishTransactional() throws InterruptedException {
    final Capturer<Person> capturer = new Capturer<>();
    Person person = new Person();
    person.setLastname("smith");
    person.setFirstname("anna");
    ITransaction tx = BEANS.get(ITransaction.class);
    ITransaction.CURRENT.set(tx);
    IDestination<Person> queue = MOM.newDestination("test/mom/testPublishTransactional", DestinationType.QUEUE, ResolveMethod.DEFINE, null);
    m_disposables.add(MOM.registerMarshaller(JmsTestMom.class, queue, BEANS.get(ObjectMarshaller.class)));
    MOM.publish(JmsTestMom.class, queue, person, MOM.newPublishInput().withTransactional(true));
    m_disposables.add(MOM.subscribe(JmsTestMom.class, queue, new IMessageListener<Person>() {

        @Override
        public void onMessage(IMessage<Person> message) {
            capturer.set(message.getTransferObject());
        }
    }));
    try {
        capturer.get(2, TimeUnit.SECONDS);
        fail();
    } catch (TimedOutError e) {
    // expected
    }
    tx.commitPhase1();
    tx.commitPhase2();
    try {
        capturer.get(5, TimeUnit.SECONDS);
    } catch (TimedOutError e) {
        fail();
    } finally {
        tx.release();
    }
    // Verify
    Person testee = capturer.get();
    assertEquals("smith", testee.getLastname());
    assertEquals("anna", testee.getFirstname());
}
Also used : ITransaction(org.eclipse.scout.rt.platform.transaction.ITransaction) IMessage(org.eclipse.scout.rt.mom.api.IMessage) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError) IMessageListener(org.eclipse.scout.rt.mom.api.IMessageListener) Test(org.junit.Test)

Aggregations

TimedOutError (org.eclipse.scout.rt.platform.util.concurrent.TimedOutError)32 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)20 Test (org.junit.Test)20 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)11 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)9 ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)8 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)6 IFuture (org.eclipse.scout.rt.platform.job.IFuture)6 IExecutionSemaphore (org.eclipse.scout.rt.platform.job.IExecutionSemaphore)5 ArrayList (java.util.ArrayList)4 PlatformException (org.eclipse.scout.rt.platform.exception.PlatformException)4 IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)4 Times (org.eclipse.scout.rt.testing.platform.runner.Times)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 JMSException (javax.jms.JMSException)3 NamingException (javax.naming.NamingException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 JobCompletionDelayOnSessionShutdown (org.eclipse.scout.rt.client.ClientConfigProperties.JobCompletionDelayOnSessionShutdown)2