Search in sources :

Example 1 with SuccessAction

use of io.joynr.messaging.SuccessAction 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 SuccessAction

use of io.joynr.messaging.SuccessAction in project joynr by bmwcarit.

the class WebSocketTest method sendMessage.

private void sendMessage() throws Throwable {
    OneWayRequest request = new OneWayRequest("method", new Object[0], new Class<?>[0]);
    MessagingQos messagingQos = new MessagingQos(100000);
    ImmutableMessage msg = messageFactory.createOneWayRequest("fromID", "toID", request, messagingQos).getImmutableMessage();
    SuccessAction successAction = mock(SuccessAction.class);
    webSocketMessagingStub.transmit(msg, successAction, new FailureAction() {

        @Override
        public void execute(Throwable error) {
            Assert.fail(error.getMessage());
        }
    });
    Mockito.verify(messageRouterMock, Mockito.timeout(1000)).route(argThat(new SerializedDataOfImmutableMessageMatcher(msg)));
    Mockito.verify(successAction).execute();
}
Also used : OneWayRequest(joynr.OneWayRequest) MessagingQos(io.joynr.messaging.MessagingQos) SuccessAction(io.joynr.messaging.SuccessAction) FailureAction(io.joynr.messaging.FailureAction) ImmutableMessage(joynr.ImmutableMessage)

Aggregations

FailureAction (io.joynr.messaging.FailureAction)2 SuccessAction (io.joynr.messaging.SuccessAction)2 ImmutableMessage (joynr.ImmutableMessage)2 JoynrDelayMessageException (io.joynr.exceptions.JoynrDelayMessageException)1 JoynrMessageNotSentException (io.joynr.exceptions.JoynrMessageNotSentException)1 JoynrRuntimeException (io.joynr.exceptions.JoynrRuntimeException)1 MessagingQos (io.joynr.messaging.MessagingQos)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Semaphore (java.util.concurrent.Semaphore)1 OneWayRequest (joynr.OneWayRequest)1 Address (joynr.system.RoutingTypes.Address)1 ChannelAddress (joynr.system.RoutingTypes.ChannelAddress)1 Test (org.junit.Test)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1