Search in sources :

Example 1 with ImmutableMessage

use of joynr.ImmutableMessage in project joynr by bmwcarit.

the class CcMessageRouterTest method testFailedTransmitDoesNotLeadToThreadStarvation.

@Test(timeout = 3000)
public void testFailedTransmitDoesNotLeadToThreadStarvation() throws Exception {
    final int MESSAGE_LOAD = 10;
    ImmutableMessage failingMessage = mock(ImmutableMessage.class);
    when(failingMessage.isTtlAbsolute()).thenReturn(true);
    when(failingMessage.getTtlMs()).thenReturn(ExpiryDate.fromRelativeTtl(1000L).getValue());
    when(failingMessage.getRecipient()).thenReturn("to");
    when(routingTable.get("to")).thenReturn(channelAddress);
    Set<Address> addressSet = new HashSet<>();
    addressSet.add(channelAddress);
    Mockito.doReturn(addressSet).when(addressManager).getAddresses(failingMessage);
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            assertEquals(invocation.getArguments().length, 3);
            FailureAction failureAction = (FailureAction) invocation.getArguments()[2];
            failureAction.execute(new Exception("Some error"));
            return null;
        }
    }).when(messagingStubMock).transmit(eq(failingMessage), any(SuccessAction.class), any(FailureAction.class));
    for (int i = 0; i < MESSAGE_LOAD; i++) {
        messageRouter.route(failingMessage);
    }
    Thread.sleep(2000);
    verify(messagingStubMock, atLeast(MESSAGE_LOAD * 3)).transmit(eq(failingMessage), any(SuccessAction.class), any(FailureAction.class));
    ImmutableMessage anotherMessage = mock(ImmutableMessage.class);
    when(anotherMessage.isTtlAbsolute()).thenReturn(true);
    when(anotherMessage.getTtlMs()).thenReturn(ExpiryDate.fromRelativeTtl(1000L).getValue());
    when(anotherMessage.getRecipient()).thenReturn("to");
    Mockito.doReturn(addressSet).when(addressManager).getAddresses(anotherMessage);
    final Semaphore semaphore = new Semaphore(0);
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            assertEquals(invocation.getArguments().length, 3);
            SuccessAction successAction = (SuccessAction) invocation.getArguments()[1];
            successAction.execute();
            semaphore.release();
            return null;
        }
    }).when(messagingStubMock).transmit(eq(anotherMessage), any(SuccessAction.class), any(FailureAction.class));
    messageRouter.route(anotherMessage);
    assertTrue(semaphore.tryAcquire(100, TimeUnit.MILLISECONDS));
}
Also used : FailureAction(io.joynr.messaging.FailureAction) Address(joynr.system.RoutingTypes.Address) ChannelAddress(joynr.system.RoutingTypes.ChannelAddress) Semaphore(java.util.concurrent.Semaphore) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) JoynrMessageNotSentException(io.joynr.exceptions.JoynrMessageNotSentException) JoynrDelayMessageException(io.joynr.exceptions.JoynrDelayMessageException) SuccessAction(io.joynr.messaging.SuccessAction) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ImmutableMessage(joynr.ImmutableMessage) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Test(org.junit.Test)

Example 2 with ImmutableMessage

use of joynr.ImmutableMessage in project joynr by bmwcarit.

the class CcMessageRouterTest method testMulticastMessageIsDroppedIfNoAddressIsFound.

@Test
public void testMulticastMessageIsDroppedIfNoAddressIsFound() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    final MulticastPublication multicastPublication = new MulticastPublication(new JoynrRuntimeException("Test Exception"), "multicastId");
    final MutableMessage mutableMessage = messageFactory.createMulticast("fromParticipantId", multicastPublication, new MessagingQos());
    final ImmutableMessage immutableMessage = mutableMessage.getImmutableMessage();
    MessageProcessedListener mockMsgProcessedListener = Mockito.mock(MessageProcessedListener.class);
    messageRouter.registerMessageProcessedListener(mockMsgProcessedListener);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            semaphore.release();
            return null;
        }
    }).when(mockMsgProcessedListener).messageProcessed(anyString());
    messageRouter.route(immutableMessage);
    semaphore.tryAcquire(1000, TimeUnit.MILLISECONDS);
    verify(mockMsgProcessedListener).messageProcessed(immutableMessage.getId());
    verifyNoMoreInteractions(messagingStubMock);
}
Also used : MessagingQos(io.joynr.messaging.MessagingQos) MulticastPublication(joynr.MulticastPublication) MutableMessage(joynr.MutableMessage) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ImmutableMessage(joynr.ImmutableMessage) Semaphore(java.util.concurrent.Semaphore) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) Test(org.junit.Test)

Example 3 with ImmutableMessage

use of joynr.ImmutableMessage in project joynr by bmwcarit.

the class CcMessageRouterTest method testMessageProcessedListenerCalledOnSuccess.

@Test
public void testMessageProcessedListenerCalledOnSuccess() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    joynrMessage.setTtlMs(ExpiryDate.fromRelativeTtl(100000000).getValue());
    joynrMessage.setTtlAbsolute(true);
    final ImmutableMessage immutableMessage = joynrMessage.getImmutableMessage();
    final MessageProcessedListener mockMessageProcessedListener = mock(MessageProcessedListener.class);
    messageRouter.registerMessageProcessedListener(mockMessageProcessedListener);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            verify(mockMessageProcessedListener, times(0)).messageProcessed(eq(immutableMessage.getId()));
            invocation.getArgumentAt(1, SuccessAction.class).execute();
            semaphore.release();
            return null;
        }
    }).when(messagingStubMock).transmit(any(ImmutableMessage.class), any(SuccessAction.class), any(FailureAction.class));
    messageRouter.route(immutableMessage);
    semaphore.tryAcquire(1000, TimeUnit.MILLISECONDS);
    verify(mockMessageProcessedListener).messageProcessed(eq(immutableMessage.getId()));
}
Also used : SuccessAction(io.joynr.messaging.SuccessAction) FailureAction(io.joynr.messaging.FailureAction) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ImmutableMessage(joynr.ImmutableMessage) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test)

Example 4 with ImmutableMessage

use of joynr.ImmutableMessage in project joynr by bmwcarit.

the class CcMessageRouterTest method testNotRoutableReplyDropped.

@Test
public void testNotRoutableReplyDropped() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    final String unknownParticipantId = "unknown_participant_id";
    final String requestReplyId = "some_request_reply_id";
    final Reply reply = new Reply(requestReplyId, new JoynrRuntimeException("TestException"));
    final MutableMessage mutableMessage = messageFactory.createReply(fromParticipantId, unknownParticipantId, reply, new MessagingQos());
    final ImmutableMessage immutableMessage = mutableMessage.getImmutableMessage();
    MessageProcessedListener mockMsgProcessedListener = Mockito.mock(MessageProcessedListener.class);
    messageRouter.registerMessageProcessedListener(mockMsgProcessedListener);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            semaphore.release();
            return null;
        }
    }).when(mockMsgProcessedListener).messageProcessed(anyString());
    messageRouter.route(immutableMessage);
    semaphore.tryAcquire(1000, TimeUnit.MILLISECONDS);
    verify(mockMsgProcessedListener).messageProcessed(immutableMessage.getId());
    verifyNoMoreInteractions(messagingStubMock);
}
Also used : Semaphore(java.util.concurrent.Semaphore) Mockito.anyString(org.mockito.Mockito.anyString) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) MessagingQos(io.joynr.messaging.MessagingQos) MutableMessage(joynr.MutableMessage) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ImmutableMessage(joynr.ImmutableMessage) Reply(joynr.Reply) Test(org.junit.Test)

Example 5 with ImmutableMessage

use of joynr.ImmutableMessage in project joynr by bmwcarit.

the class CcMessageRouterTest method testRetryForNoParticipantFound.

@Test
public void testRetryForNoParticipantFound() throws Exception {
    joynrMessage.setTtlMs(ExpiryDate.fromRelativeTtl(100000).getValue());
    joynrMessage.setTtlAbsolute(true);
    joynrMessage.setRecipient("I don't exist");
    ImmutableMessage immutableMessage = joynrMessage.getImmutableMessage();
    messageRouter.route(immutableMessage);
    Thread.sleep(100);
    verify(routingTable, atLeast(2)).containsKey("I don't exist");
    verify(addressManager, atLeast(2)).getAddresses(immutableMessage);
}
Also used : ImmutableMessage(joynr.ImmutableMessage) Test(org.junit.Test)

Aggregations

ImmutableMessage (joynr.ImmutableMessage)63 Test (org.junit.Test)42 Response (com.jayway.restassured.response.Response)12 FailureAction (io.joynr.messaging.FailureAction)11 SuccessAction (io.joynr.messaging.SuccessAction)11 InvocationOnMock (org.mockito.invocation.InvocationOnMock)10 EncodingException (io.joynr.smrf.EncodingException)9 MutableMessage (joynr.MutableMessage)9 JoynrRuntimeException (io.joynr.exceptions.JoynrRuntimeException)8 UnsuppportedVersionException (io.joynr.smrf.UnsuppportedVersionException)8 Semaphore (java.util.concurrent.Semaphore)7 JoynrDelayMessageException (io.joynr.exceptions.JoynrDelayMessageException)6 JoynrMessageNotSentException (io.joynr.exceptions.JoynrMessageNotSentException)6 MessagingQos (io.joynr.messaging.MessagingQos)6 Ignore (org.junit.Ignore)6 AbstractModule (com.google.inject.AbstractModule)5 JoynrMessageProcessor (io.joynr.messaging.JoynrMessageProcessor)5 Injector (com.google.inject.Injector)4 Module (com.google.inject.Module)4 CcMessageRouter (io.joynr.messaging.routing.CcMessageRouter)4