use of io.joynr.messaging.FailureAction 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.FailureAction in project joynr by bmwcarit.
the class AbstractMessageRouter method createFailureAction.
private FailureAction createFailureAction(final ImmutableMessage message, final int retriesCount) {
final FailureAction failureAction = new FailureAction() {
final String messageId = message.getId();
private boolean failureActionExecutedOnce = false;
@Override
public void execute(Throwable error) {
synchronized (this) {
if (failureActionExecutedOnce) {
logger.trace("Failure action for message with id {} already executed once. Ignoring further call.", messageId);
return;
}
failureActionExecutedOnce = true;
}
if (error instanceof JoynrShutdownException) {
logger.warn("{}", error.getMessage());
return;
} else if (error instanceof JoynrMessageNotSentException) {
logger.error(" ERROR SENDING: aborting send of messageId: {}. Error: {}", new Object[] { messageId, error.getMessage() });
callMessageProcessedListeners(messageId);
return;
}
logger.warn("PROBLEM SENDING, will retry. messageId: {}. Error: {} Message: {}", new Object[] { messageId, error.getClass().getName(), error.getMessage() });
long delayMs;
if (error instanceof JoynrDelayMessageException) {
delayMs = ((JoynrDelayMessageException) error).getDelayMs();
} else {
delayMs = createDelayWithExponentialBackoff(sendMsgRetryIntervalMs, retriesCount);
}
logger.error("Rescheduling messageId: {} with delay {} ms, TTL is: {}", messageId, delayMs, dateFormatter.format(message.getTtlMs()));
try {
routeInternal(message, delayMs, retriesCount + 1);
} catch (Exception e) {
logger.warn("Rescheduling of message failed (messageId {})", messageId);
callMessageProcessedListeners(messageId);
}
return;
}
};
return failureAction;
}
use of io.joynr.messaging.FailureAction 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();
}
use of io.joynr.messaging.FailureAction in project joynr by bmwcarit.
the class MqttPahoClient method messageArrived.
@Override
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
logger.debug("MQTT message received: id {}, topic {}, payload\n{}", mqttMessage.getId(), topic, new String(mqttMessage.getPayload(), Charsets.UTF_8));
if (messagingSkeleton == null) {
logger.error("MQTT message not processed: messagingSkeleton has not been set yet");
return;
}
messagingSkeleton.transmit(mqttMessage.getPayload(), new FailureAction() {
@Override
public void execute(Throwable error) {
logger.error("MQTT message not processed");
}
});
}
use of io.joynr.messaging.FailureAction in project joynr by bmwcarit.
the class WebSocketJettyClient method onWebSocketBinary.
@Override
public void onWebSocketBinary(byte[] payload, int offset, int len) {
logger.trace(this.getClass().getSimpleName() + ": Received message: " + new String(payload, CHARSET));
messageListener.transmit(Arrays.copyOfRange(payload, offset, offset + len), new FailureAction() {
@Override
public void execute(Throwable error) {
logger.error("WebSocket message not processed: {}", error.getMessage());
}
});
}
Aggregations