Search in sources :

Example 6 with IMethod

use of org.jowidgets.invocation.common.api.IMethod 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 7 with IMethod

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

the class InvocationServiceClientImpl method getMethodService.

@Override
public <RES, INT_RES, REQ, RESP, PARAM> IMethodInvocationService<RES, INT_RES, REQ, RESP, PARAM> getMethodService(final String methodName, final long timeout) {
    Assert.paramNotNull(methodName, "methodName");
    return new IMethodInvocationService<RES, INT_RES, REQ, RESP, PARAM>() {

        @Override
        public void invoke(final IInvocationCallback<RES> invocationCallback, final IInterimResponseCallback<INT_RES> interimResponseCallback, final IInterimRequestCallback<REQ, RESP> interimRequestCallback, final PARAM parameter) {
            final IMethod method = invocationClient.getMethod(methodName);
            if (method == null) {
                throw new IllegalArgumentException("No server method registered for method name '" + methodName + "'.");
            } else {
                final Object invocationId = invocationCallbackService.registerInvocation(invocationCallback, interimResponseCallback, interimRequestCallback, timeout, invocationClient);
                method.invoke(invocationId, parameter);
            }
        }
    };
}
Also used : IMethodInvocationService(org.jowidgets.invocation.service.common.api.IMethodInvocationService) IInterimResponseCallback(org.jowidgets.invocation.service.common.api.IInterimResponseCallback) IInterimRequestCallback(org.jowidgets.invocation.service.common.api.IInterimRequestCallback) IInvocationCallback(org.jowidgets.invocation.service.common.api.IInvocationCallback) IMethod(org.jowidgets.invocation.common.api.IMethod)

Example 8 with IMethod

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

the class InvocationClientImpl method getMethod.

@Override
public IMethod getMethod(final String methodName) {
    return new IMethod() {

        @Override
        public void invoke(final Object invocationId, final Object parameter) {
            final MethodInvocationMessage message = new MethodInvocationMessage(invocationId, methodName, parameter);
            messageChannel.send(message, new ExceptionCallback(invocationClientServiceRegistry, invocationId));
        }
    };
}
Also used : MethodInvocationMessage(org.jowidgets.invocation.common.impl.MethodInvocationMessage) IMethod(org.jowidgets.invocation.common.api.IMethod)

Example 9 with IMethod

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

the class InvocationServerServiceRegistryImpl method onMethodInvocation.

public void onMethodInvocation(final MethodInvocationMessage message) {
    final IMethod method = methods.get(message.getMethodName());
    method.invoke(message.getInvocationId(), message.getParameter());
}
Also used : IMethod(org.jowidgets.invocation.common.api.IMethod)

Example 10 with IMethod

use of org.jowidgets.invocation.common.api.IMethod 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

IMethod (org.jowidgets.invocation.common.api.IMethod)14 Test (org.junit.Test)10 IInvocationCallbackService (org.jowidgets.invocation.common.api.IInvocationCallbackService)9 InvocationOnMock (org.mockito.invocation.InvocationOnMock)7 IInvocationClient (org.jowidgets.invocation.client.api.IInvocationClient)3 ICancelService (org.jowidgets.invocation.common.api.ICancelService)3 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 IResponseService (org.jowidgets.invocation.common.api.IResponseService)1 MethodInvocationMessage (org.jowidgets.invocation.common.impl.MethodInvocationMessage)1 IInterimRequestCallback (org.jowidgets.invocation.service.common.api.IInterimRequestCallback)1 IInterimResponseCallback (org.jowidgets.invocation.service.common.api.IInterimResponseCallback)1 IInvocationCallback (org.jowidgets.invocation.service.common.api.IInvocationCallback)1 IMethodInvocationService (org.jowidgets.invocation.service.common.api.IMethodInvocationService)1 JUnitLogger (org.jowidgets.logging.tools.JUnitLogger)1