Search in sources :

Example 56 with IRunnable

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

the class RunContextTest method testHardCancellation.

@Test
public void testHardCancellation() throws InterruptedException {
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
    final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(1);
    final AtomicBoolean interrupted = new AtomicBoolean();
    final RunMonitor monitor = BEANS.get(RunMonitor.class);
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            RunContexts.empty().withRunMonitor(monitor).run(new IRunnable() {

                @Override
                public void run() throws Exception {
                    try {
                        assertTrue(setupLatch.countDownAndBlock(10, TimeUnit.SECONDS));
                    } catch (InterruptedException e) {
                        interrupted.set(true);
                    } finally {
                        verifyLatch.countDown();
                    }
                }
            });
        }
    }, Jobs.newInput());
    assertTrue(setupLatch.await());
    monitor.cancel(true);
    assertTrue(verifyLatch.await());
    assertTrue(interrupted.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 57 with IRunnable

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

the class ThreadInterruptionTest method testThreadInterrupted.

@Test
public void testThreadInterrupted() {
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            Thread.currentThread().interrupt();
            assertTrue(Thread.currentThread().isInterrupted());
            IRestorer interruption = ThreadInterruption.clear();
            assertFalse(Thread.currentThread().isInterrupted());
            interruption.restore();
            assertTrue(Thread.currentThread().isInterrupted());
        }
    }, Jobs.newInput().withExceptionHandling(null, false)).awaitDoneAndGet();
}
Also used : IRestorer(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruption.IRestorer) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Example 58 with IRunnable

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

the class InvocationContextTest method testCancel.

@Test(timeout = 5000)
public void testCancel() {
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
    final IBlockingCondition processingCondition = Jobs.newBlockingCondition(true);
    final InvocationContext<TestPort> invocationContext = new InvocationContext<>(m_port, "name");
    invocationContext.withEndpointUrl("http://localhost");
    // Make Stub.webMethod to block until cancelled.
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            setupLatch.countDown();
            processingCondition.waitForUninterruptibly(10, TimeUnit.SECONDS);
            return null;
        }
    }).when(m_port).webMethod();
    final RunMonitor runMonitor = new RunMonitor();
    // Cancel the 'webMethod' once blocking.
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            setupLatch.await();
            runMonitor.cancel(true);
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()));
    // Run the test by invoking the web service with a specific RunMonitor to test cancellation.
    try {
        RunContexts.empty().withRunMonitor(runMonitor).run(new IRunnable() {

            @Override
            public void run() throws Exception {
                try {
                    // this method blocks until cancelled.
                    invocationContext.getPort().webMethod();
                    fail("WebServiceRequestCancelledException expected");
                } catch (WebServiceRequestCancelledException e) {
                    verify(m_implementorSpecifics).closeSocket(same(m_port), anyString());
                }
            }
        });
    } finally {
        processingCondition.setBlocking(false);
    }
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RunMonitor(org.eclipse.scout.rt.platform.context.RunMonitor) IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition) Test(org.junit.Test)

Example 59 with IRunnable

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

the class InvocationContextTest method testWithException.

@Test
public void testWithException() {
    // Unregister JUnit exception handler
    BEANS.getBeanManager().unregisterBean(BEANS.getBeanManager().getBean(JUnitExceptionHandler.class));
    final Holder<ITransaction> currentTransaction = new Holder<>();
    final Holder<ITransaction> invocationTransaction = new Holder<>();
    final Holder<IServerSession> invocationServerSession = new Holder<>();
    final Holder<Exception> callableException = new Holder<>();
    // simulate that 'webMethod' throws an exception.
    final RuntimeException exception = new RuntimeException();
    doThrow(exception).when(m_port).webMethod();
    try {
        ServerRunContexts.copyCurrent().withCorrelationId(TESTING_CORRELATION_ID).withTransactionScope(// set transaction boundary
        TransactionScope.REQUIRES_NEW).run(new IRunnable() {

            @Override
            public void run() throws Exception {
                currentTransaction.setValue(ITransaction.CURRENT.get());
                InvocationContext<TestPort> invocationContext = new InvocationContext<>(m_port, "name");
                invocationContext.withEndpointUrl("http://localhost");
                invocationContext.whenCommit(m_commitListener);
                invocationContext.whenRollback(m_rollbackListener);
                invocationContext.whenInvoke(new InvocationHandler() {

                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        invocationTransaction.setValue(ITransaction.CURRENT.get());
                        invocationServerSession.setValue(ServerSessionProvider.currentSession());
                        return method.invoke(proxy, args);
                    }
                });
                // run the test
                try {
                    invocationContext.getPort().webMethod();
                } catch (Exception e) {
                    callableException.setValue(e);
                    throw e;
                }
            }
        });
        fail("RuntimeException expected");
    } catch (RuntimeException e) {
    // NOOP
    }
    assertSame(currentTransaction.getValue(), invocationTransaction.getValue());
    assertSame(ISession.CURRENT.get(), invocationServerSession.getValue());
    assertEquals(TESTING_CORRELATION_ID, m_port.getRequestContext().get(MessageContexts.PROP_CORRELATION_ID));
    verify(m_port).webMethod();
    verify(m_commitListener, never()).onCommitPhase1();
    verify(m_commitListener, never()).onCommitPhase2();
    verify(m_rollbackListener).onRollback();
    assertSame(callableException.getValue(), exception);
}
Also used : ITransaction(org.eclipse.scout.rt.platform.transaction.ITransaction) BooleanHolder(org.eclipse.scout.rt.platform.holders.BooleanHolder) Holder(org.eclipse.scout.rt.platform.holders.Holder) JUnitExceptionHandler(org.eclipse.scout.rt.testing.platform.runner.JUnitExceptionHandler) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Method(java.lang.reflect.Method) WebMethod(javax.jws.WebMethod) InvocationHandler(java.lang.reflect.InvocationHandler) IServerSession(org.eclipse.scout.rt.server.IServerSession) Test(org.junit.Test)

Example 60 with IRunnable

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

the class InvocationContextTest method testWithSuccess.

@Test
public void testWithSuccess() {
    final Holder<ITransaction> currentTransaction = new Holder<>();
    final Holder<ITransaction> invocationTransaction = new Holder<>();
    final Holder<IServerSession> invocationServerSession = new Holder<>();
    ServerRunContexts.copyCurrent().withCorrelationId(TESTING_CORRELATION_ID).withTransactionScope(// set transaction boundary
    TransactionScope.REQUIRES_NEW).run(new IRunnable() {

        @Override
        public void run() throws Exception {
            currentTransaction.setValue(ITransaction.CURRENT.get());
            InvocationContext<TestPort> invocationContext = new InvocationContext<>(m_port, "name");
            invocationContext.withEndpointUrl("http://localhost");
            invocationContext.whenCommit(m_commitListener);
            invocationContext.whenRollback(m_rollbackListener);
            invocationContext.whenInvoke(new InvocationHandler() {

                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    invocationTransaction.setValue(ITransaction.CURRENT.get());
                    invocationServerSession.setValue(ServerSessionProvider.currentSession());
                    return method.invoke(proxy, args);
                }
            });
            // run the test
            invocationContext.getPort().webMethod();
        }
    });
    assertSame(currentTransaction.getValue(), invocationTransaction.getValue());
    assertSame(ISession.CURRENT.get(), invocationServerSession.getValue());
    assertEquals(TESTING_CORRELATION_ID, m_port.getRequestContext().get(MessageContexts.PROP_CORRELATION_ID));
    verify(m_port).webMethod();
    verify(m_commitListener).onCommitPhase1();
    verify(m_commitListener).onCommitPhase2();
    verify(m_rollbackListener, never()).onRollback();
}
Also used : ITransaction(org.eclipse.scout.rt.platform.transaction.ITransaction) BooleanHolder(org.eclipse.scout.rt.platform.holders.BooleanHolder) Holder(org.eclipse.scout.rt.platform.holders.Holder) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Method(java.lang.reflect.Method) WebMethod(javax.jws.WebMethod) InvocationHandler(java.lang.reflect.InvocationHandler) IServerSession(org.eclipse.scout.rt.server.IServerSession) 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