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));
}
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();
}
Aggregations