Search in sources :

Example 6 with Holder

use of org.eclipse.scout.rt.platform.holders.Holder in project scout.rt by eclipse.

the class WhenDoneTest method testSuccessWithJobAlreadyCompleted.

@Test
public void testSuccessWithJobAlreadyCompleted() throws InterruptedException {
    // synchronized because modified/read by different threads.
    final List<String> protocol = Collections.synchronizedList(new ArrayList<String>());
    final Holder<DoneEvent<String>> eventHolder = new Holder<>();
    final IFuture<String> future = Jobs.schedule(new Callable<String>() {

        @Override
        public String call() throws Exception {
            protocol.add("1");
            return "result";
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()));
    future.awaitDoneAndGet();
    final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(1);
    future.whenDone(new IDoneHandler<String>() {

        @Override
        public void onDone(DoneEvent<String> event) {
            protocol.add("2");
            if (future.isDone()) {
                protocol.add("done");
            }
            if (future.isCancelled()) {
                protocol.add("cancelled");
            }
            eventHolder.setValue(event);
            verifyLatch.countDown();
        }
    }, RunContexts.copyCurrent());
    assertTrue(verifyLatch.await());
    assertEquals(CollectionUtility.arrayList("1", "2", "done"), protocol);
    assertNull(eventHolder.getValue().getException());
    assertEquals("result", eventHolder.getValue().getResult());
    assertFalse(eventHolder.getValue().isCancelled());
    assertFalse(eventHolder.getValue().isFailed());
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) Holder(org.eclipse.scout.rt.platform.holders.Holder) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) Test(org.junit.Test)

Example 7 with Holder

use of org.eclipse.scout.rt.platform.holders.Holder 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 8 with Holder

use of org.eclipse.scout.rt.platform.holders.Holder 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)

Example 9 with Holder

use of org.eclipse.scout.rt.platform.holders.Holder in project scout.rt by eclipse.

the class AbstractJaxWsClientTest method testAcquirePortConcurrentlyInDifferentTransactions.

@Test
public void testAcquirePortConcurrentlyInDifferentTransactions() throws InterruptedException {
    final CountDownLatch txn1InitLatch = new CountDownLatch(1);
    final CountDownLatch txn2InitLatch = new CountDownLatch(1);
    final Holder<JaxWsConsumerTestServicePortType> txn1PortHolder = new Holder<>(JaxWsConsumerTestServicePortType.class);
    final Holder<JaxWsConsumerTestServicePortType> txn2PortHolder = new Holder<>(JaxWsConsumerTestServicePortType.class);
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            JaxWsConsumerTestServicePortType port0 = BEANS.get(JaxWsConsumerTestClient.class).newInvocationContext().getPort();
            assertSendEcho(port0, 0);
            txn1PortHolder.setValue(port0);
            txn1InitLatch.countDown();
            txn2InitLatch.await();
            JaxWsConsumerTestServicePortType port1 = BEANS.get(JaxWsConsumerTestClient.class).newInvocationContext().getPort();
            assertSamePort(port0, port1);
            assertSendEcho(port1, 1);
        }
    }, Jobs.newInput().withRunContext(ServerRunContexts.empty()));
    txn1InitLatch.await();
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            JaxWsConsumerTestServicePortType port0 = BEANS.get(JaxWsConsumerTestClient.class).newInvocationContext().getPort();
            assertSendEcho(port0, 0);
            txn2PortHolder.setValue(port0);
            txn2InitLatch.countDown();
            JaxWsConsumerTestServicePortType port1 = BEANS.get(JaxWsConsumerTestClient.class).newInvocationContext().getPort();
            assertSamePort(port0, port1);
            assertSendEcho(port1, 1);
        }
    }, Jobs.newInput().withRunContext(ServerRunContexts.empty()));
    txn2InitLatch.await();
    assertDifferentPort(txn1PortHolder.getValue(), txn2PortHolder.getValue());
}
Also used : Holder(org.eclipse.scout.rt.platform.holders.Holder) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) CountDownLatch(java.util.concurrent.CountDownLatch) JaxWsConsumerTestServicePortType(org.eclipse.scout.jaxws.consumer.jaxwsconsumertestservice.JaxWsConsumerTestServicePortType) SocketTimeoutException(java.net.SocketTimeoutException) WebServiceException(javax.xml.ws.WebServiceException) Test(org.junit.Test)

Example 10 with Holder

use of org.eclipse.scout.rt.platform.holders.Holder in project scout.rt by eclipse.

the class AbstractJaxWsClientTest method testAcquirePortInDifferentTransactions.

/*
   * ************************************************************
   * Test acquire port in different transactions
   * ************************************************************/
@Test
public void testAcquirePortInDifferentTransactions() throws InterruptedException {
    final Holder<JaxWsConsumerTestServicePortType> txn1PortHolder = new Holder<>(JaxWsConsumerTestServicePortType.class);
    final Holder<JaxWsConsumerTestServicePortType> txn2PortHolder = new Holder<>(JaxWsConsumerTestServicePortType.class);
    // This test case expects at most one port in the pool. It is guaranteed by discarding all pooled entries.
    BEANS.get(JaxWsConsumerTestClient.class).discardAllPoolEntries();
    ServerRunContexts.copyCurrent().run(new IRunnable() {

        @Override
        public void run() throws Exception {
            JaxWsConsumerTestServicePortType port = BEANS.get(JaxWsConsumerTestClient.class).newInvocationContext().getPort();
            assertSendEcho(port, 0);
            txn1PortHolder.setValue(port);
        }
    });
    ServerRunContexts.copyCurrent().run(new IRunnable() {

        @Override
        public void run() throws Exception {
            JaxWsConsumerTestServicePortType port = BEANS.get(JaxWsConsumerTestClient.class).newInvocationContext().getPort();
            assertSendEcho(port, 0);
            txn2PortHolder.setValue(port);
        }
    });
    if (BEANS.get(JaxWsImplementorSpecifics.class).isPoolingSupported()) {
        assertSamePort(txn1PortHolder.getValue(), txn2PortHolder.getValue());
    } else {
        assertDifferentPort(txn1PortHolder.getValue(), txn2PortHolder.getValue());
    }
}
Also used : Holder(org.eclipse.scout.rt.platform.holders.Holder) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) JaxWsConsumerTestServicePortType(org.eclipse.scout.jaxws.consumer.jaxwsconsumertestservice.JaxWsConsumerTestServicePortType) SocketTimeoutException(java.net.SocketTimeoutException) WebServiceException(javax.xml.ws.WebServiceException) JaxWsImplementorSpecifics(org.eclipse.scout.rt.server.jaxws.implementor.JaxWsImplementorSpecifics) Test(org.junit.Test)

Aggregations

Holder (org.eclipse.scout.rt.platform.holders.Holder)28 Test (org.junit.Test)20 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)6 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)6 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)6 IHolder (org.eclipse.scout.rt.platform.holders.IHolder)4 IPropertyHolder (org.eclipse.scout.rt.shared.data.form.IPropertyHolder)4 SocketTimeoutException (java.net.SocketTimeoutException)3 List (java.util.List)3 WebServiceException (javax.xml.ws.WebServiceException)3 JaxWsConsumerTestServicePortType (org.eclipse.scout.jaxws.consumer.jaxwsconsumertestservice.JaxWsConsumerTestServicePortType)3 IFormFieldVisitor (org.eclipse.scout.rt.client.ui.form.IFormFieldVisitor)3 IFormField (org.eclipse.scout.rt.client.ui.form.fields.IFormField)3 CallableChain (org.eclipse.scout.rt.platform.chain.callable.CallableChain)3 PlatformError (org.eclipse.scout.rt.platform.exception.PlatformError)3 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)3 IExtensibleObject (org.eclipse.scout.rt.shared.extension.IExtensibleObject)3 InOrder (org.mockito.InOrder)3 InvocationHandler (java.lang.reflect.InvocationHandler)2 Method (java.lang.reflect.Method)2