Search in sources :

Example 1 with IInvocationClient

use of org.jowidgets.invocation.client.api.IInvocationClient in project jo-client-platform by jo-source.

the class InvocationIntegrationTest method testCancelAfterMessageInvocation.

@Test
public void testCancelAfterMessageInvocation() {
    final String methodName = "Method";
    final Integer invocationId = Integer.valueOf(0);
    final String invocationParameter = "InvocationParameter";
    // setup server
    final ICancelService serverCancelService = Mockito.mock(ICancelService.class);
    InvocationServerToolkit.getRegistry(BROKER_ID).register(serverCancelService);
    final IMethod serverMethodMock = Mockito.mock(IMethod.class);
    InvocationServerToolkit.getRegistry(BROKER_ID).register(methodName, serverMethodMock);
    // setup client
    final IInvocationCallbackService clientInvocationCallback = Mockito.mock(IInvocationCallbackService.class);
    InvocationClientToolkit.getRegistry(BROKER_ID).register(clientInvocationCallback);
    final IInvocationClient client = InvocationClientToolkit.getClient(BROKER_ID);
    final IMethod clientMethod = client.getMethod(methodName);
    final ICancelService clientCancelService = client.getCancelService();
    // invoke methods on client
    clientMethod.invoke(invocationId, invocationParameter);
    Mockito.verify(serverMethodMock, Mockito.never()).invoke(invocationId, invocationParameter);
    messaging.getMessageReceiverBroker().dispatchMessages();
    Mockito.verify(serverMethodMock, Mockito.times(1)).invoke(invocationId, invocationParameter);
    clientCancelService.canceled(invocationId);
    Mockito.verify(serverCancelService, Mockito.never()).canceled(invocationId);
    messaging.getMessageReceiverBroker().dispatchMessages();
    Mockito.verify(serverCancelService, Mockito.times(1)).canceled(invocationId);
    assertNoLogErrorOrWarning();
}
Also used : IInvocationClient(org.jowidgets.invocation.client.api.IInvocationClient) IMethod(org.jowidgets.invocation.common.api.IMethod) ICancelService(org.jowidgets.invocation.common.api.ICancelService) IInvocationCallbackService(org.jowidgets.invocation.common.api.IInvocationCallbackService) Test(org.junit.Test)

Example 2 with IInvocationClient

use of org.jowidgets.invocation.client.api.IInvocationClient in project jo-client-platform by jo-source.

the class InvocationIntegrationTest method testCancelBeforeMethodInvocation.

@Test
public void testCancelBeforeMethodInvocation() {
    final String methodName = "Method";
    final Integer invocationId = Integer.valueOf(0);
    final String invocationParameter = "InvocationParameter";
    // setup server
    final ICancelService serverCancelService = Mockito.mock(ICancelService.class);
    InvocationServerToolkit.getRegistry(BROKER_ID).register(serverCancelService);
    final IMethod serverMethodMock = Mockito.mock(IMethod.class);
    InvocationServerToolkit.getRegistry(BROKER_ID).register(methodName, serverMethodMock);
    // setup client
    final IInvocationCallbackService clientInvocationCallback = Mockito.mock(IInvocationCallbackService.class);
    InvocationClientToolkit.getRegistry(BROKER_ID).register(clientInvocationCallback);
    final IInvocationClient client = InvocationClientToolkit.getClient(BROKER_ID);
    final IMethod clientMethod = client.getMethod(methodName);
    final ICancelService cancelService = client.getCancelService();
    // invoke methods on client
    clientMethod.invoke(invocationId, invocationParameter);
    cancelService.canceled(invocationId);
    Mockito.verify(serverMethodMock, Mockito.never()).invoke(invocationId, invocationParameter);
    Mockito.verify(serverCancelService, Mockito.never()).canceled(invocationId);
    messaging.getMessageReceiverBroker().dispatchMessages();
    Mockito.verify(serverMethodMock, Mockito.times(1)).invoke(invocationId, invocationParameter);
    Mockito.verify(serverCancelService, Mockito.times(1)).canceled(invocationId);
    assertNoLogErrorOrWarning();
}
Also used : IInvocationClient(org.jowidgets.invocation.client.api.IInvocationClient) IMethod(org.jowidgets.invocation.common.api.IMethod) ICancelService(org.jowidgets.invocation.common.api.ICancelService) IInvocationCallbackService(org.jowidgets.invocation.common.api.IInvocationCallbackService) Test(org.junit.Test)

Example 3 with IInvocationClient

use of org.jowidgets.invocation.client.api.IInvocationClient in project jo-client-platform by jo-source.

the class InvocationIntegrationTest method testCancelExceptionAfterCancel.

@Test
public void testCancelExceptionAfterCancel() {
    final String methodName = "Method";
    final Integer invocationId = Integer.valueOf(0);
    final String invocationParameter = "InvocationParameter";
    final RuntimeException cancelException = new RuntimeException("Service was canceled");
    final CountDownLatch cancelLatch = new CountDownLatch(1);
    final CountDownLatch serverFinsihedLatch = new CountDownLatch(1);
    // setup server
    final ICancelService serverCancelService = Mockito.mock(ICancelService.class);
    InvocationServerToolkit.getRegistry(BROKER_ID).register(serverCancelService);
    final IInvocationCallbackService serverInvocationCallback = InvocationServerToolkit.getServer(BROKER_ID).getInvocationCallbackService();
    final IMethod serverMethodMock = Mockito.mock(IMethod.class);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(final InvocationOnMock invocation) throws Throwable {
            // wait until service was canceled
            cancelLatch.await();
            // after service was canceled return a cancel exception
            serverInvocationCallback.exeption(invocationId, cancelException);
            return null;
        }
    }).when(serverMethodMock).invoke(Mockito.any(), Mockito.any());
    InvocationServerToolkit.getRegistry(BROKER_ID).register(methodName, serverMethodMock);
    // setup client
    final IInvocationCallbackService clientInvocationCallback = Mockito.mock(IInvocationCallbackService.class);
    InvocationClientToolkit.getRegistry(BROKER_ID).register(clientInvocationCallback);
    final IInvocationClient client = InvocationClientToolkit.getClient(BROKER_ID);
    final IMethod clientMethod = client.getMethod(methodName);
    final ICancelService cancelService = client.getCancelService();
    // invoke methods on client
    clientMethod.invoke(invocationId, invocationParameter);
    // dispatch method on server
    final AtomicReference<JUnitLogger> serverMethodThreadLogger = new AtomicReference<JUnitLogger>();
    new Thread(new Runnable() {

        @Override
        public void run() {
            serverMethodThreadLogger.set(JUnitLoggerProvider.getGlobalLogger());
            messaging.getMessageReceiverBroker().dispatchMessages();
            serverFinsihedLatch.countDown();
        }
    }).start();
    // invoke cancel on client
    cancelService.canceled(invocationId);
    // dispatch cancel message on server
    final AtomicReference<JUnitLogger> serverCancelThreadLogger = new AtomicReference<JUnitLogger>();
    new Thread(new Runnable() {

        @Override
        public void run() {
            serverCancelThreadLogger.set(JUnitLoggerProvider.getGlobalLogger());
            messaging.getMessageReceiverBroker().dispatchMessages();
            cancelLatch.countDown();
        }
    }).start();
    try {
        serverFinsihedLatch.await();
    } catch (final InterruptedException e) {
        throw new RuntimeException(e);
    }
    messaging.getMessageChannelBroker().dispatchReturnedMessages();
    Mockito.verify(serverCancelService, Mockito.times(1)).canceled(invocationId);
    Mockito.verify(clientInvocationCallback, Mockito.never()).exeption(invocationId, cancelException);
    Assert.assertFalse(serverMethodThreadLogger.get().hasMessage());
    Assert.assertFalse(serverCancelThreadLogger.get().hasMessage());
    assertNoLogErrorOrWarning();
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ICancelService(org.jowidgets.invocation.common.api.ICancelService) IInvocationCallbackService(org.jowidgets.invocation.common.api.IInvocationCallbackService) JUnitLogger(org.jowidgets.logging.tools.JUnitLogger) IInvocationClient(org.jowidgets.invocation.client.api.IInvocationClient) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IMethod(org.jowidgets.invocation.common.api.IMethod) Test(org.junit.Test)

Aggregations

IInvocationClient (org.jowidgets.invocation.client.api.IInvocationClient)3 ICancelService (org.jowidgets.invocation.common.api.ICancelService)3 IInvocationCallbackService (org.jowidgets.invocation.common.api.IInvocationCallbackService)3 IMethod (org.jowidgets.invocation.common.api.IMethod)3 Test (org.junit.Test)3 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 JUnitLogger (org.jowidgets.logging.tools.JUnitLogger)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1