use of org.jowidgets.logging.tools.JUnitLogger in project jo-client-platform by jo-source.
the class MessageBrokerTest method testPostWithServerError.
@Test
public void testPostWithServerError() throws ClientProtocolException, IOException, InterruptedException {
final String expectedResultMessage = "RESULT_MESSAGE";
final MessageStub requestMessage = new MessageStub(expectedResultMessage);
final int postErrorCount = 5;
final int messageCount = 5;
final HttpClientStub httpClient = new HttpClientStub(messageCount, 0);
httpClient.setPostStatus(HttpClientStub.INTERNAL_SERVER_ERROR_STATUS_LINE);
final ScheduledExecutorServiceMock executor = new ScheduledExecutorServiceMock();
final AtomicReference<JUnitLogger> senderThreadLoggerRef = new AtomicReference<JUnitLogger>(null);
final AtomicReference<JUnitLogger> receiverThreadLoggerRef = new AtomicReference<JUnitLogger>(null);
final MessageBrokerBuilder builder = new MessageBrokerBuilder(BROKER_ID);
builder.setUrl(DEFAULT_URL);
builder.setHttpClient(httpClient);
builder.setIncommingMessageExecutor(executor);
builder.setSleepDurationMillisAfterIoException(0);
builder.setHttpRequestInitializer(new IHttpRequestInitializer() {
@Override
public void initialize(final HttpRequest httpRequest) {
if (Thread.currentThread().getName().contains("MessageSender")) {
prepareLoggerForThreadAndDisableConsole(senderThreadLoggerRef);
} else {
prepareLoggerForThreadAndDisableConsole(receiverThreadLoggerRef);
}
}
});
final IMessageBroker messageBroker = builder.build();
final IMessageReceiver receiver = Mockito.mock(IMessageReceiver.class);
messageBroker.setReceiver(receiver);
for (int i = 0; i < postErrorCount; i++) {
messageBroker.getChannel().send(requestMessage, null);
}
while (httpClient.getPostInvocationCount() < postErrorCount) {
// do nothing
}
final IExceptionCallback exceptionCallback = Mockito.mock(IExceptionCallback.class);
for (int i = 0; i < postErrorCount; i++) {
messageBroker.getChannel().send(requestMessage, exceptionCallback);
}
while (httpClient.getPostInvocationCount() < postErrorCount * 2) {
// do nothing
}
// now fix error and send some messages with success
httpClient.setPostStatus(HttpClientStub.OK_STATUS_LINE);
for (int i = 0; i < messageCount; i++) {
messageBroker.getChannel().send(requestMessage, exceptionCallback);
}
Assert.assertTrue(httpClient.awaitMessagesConsumed(SYNC_TIMEOUT, TimeUnit.MILLISECONDS));
executor.executeEvents();
Mockito.verify(receiver, Mockito.times(messageCount)).onMessage(expectedResultMessage, messageBroker.getChannel());
Assert.assertTrue(messageBroker.shutdown(SYNC_TIMEOUT));
Mockito.verify(httpClient.getConnectionManager(), Mockito.times(1)).shutdown();
Assert.assertEquals(2 * postErrorCount + messageCount, httpClient.getPostInvocationCount());
Assert.assertEquals(postErrorCount, senderThreadLoggerRef.get().getMessageCount());
Mockito.verify(exceptionCallback, Mockito.times(postErrorCount)).exception(Mockito.any(Exception.class));
Assert.assertEquals(0, receiverThreadLoggerRef.get().getMessageCount());
}
use of org.jowidgets.logging.tools.JUnitLogger in project jo-client-platform by jo-source.
the class MessageBrokerTest method testGetWithServerError.
@Test
public void testGetWithServerError() throws ClientProtocolException, IOException, InterruptedException {
final int errorCount = 5;
final HttpClientStub httpClient = new HttpClientStub(0, errorCount);
httpClient.setGetStatus(HttpClientStub.INTERNAL_SERVER_ERROR_STATUS_LINE);
final ScheduledExecutorServiceMock executor = new ScheduledExecutorServiceMock();
final AtomicReference<JUnitLogger> loggerRef = new AtomicReference<JUnitLogger>(null);
final MessageBrokerBuilder builder = new MessageBrokerBuilder(BROKER_ID);
builder.setUrl(DEFAULT_URL);
builder.setHttpClient(httpClient);
builder.setIncommingMessageExecutor(executor);
builder.setSleepDurationMillisAfterIoException(0);
builder.setHttpRequestInitializer(new IHttpRequestInitializer() {
@Override
public void initialize(final HttpRequest httpRequest) {
if (Thread.currentThread().getName().contains("MessageReceiver")) {
prepareLoggerForThreadAndDisableConsole(loggerRef);
}
}
});
final IMessageBroker messageBroker = builder.build();
final IMessageReceiver receiver = Mockito.mock(IMessageReceiver.class);
messageBroker.setReceiver(receiver);
Assert.assertTrue(httpClient.awaitErrorsConsumed(SYNC_TIMEOUT, TimeUnit.MILLISECONDS));
executor.executeEvents();
Mockito.verify(receiver, Mockito.never()).onMessage(Mockito.any(), Mockito.eq(messageBroker.getChannel()));
Assert.assertTrue(messageBroker.shutdown(SYNC_TIMEOUT));
Mockito.verify(httpClient.getConnectionManager(), Mockito.times(1)).shutdown();
Assert.assertEquals(0, httpClient.getPostInvocationCount());
Assert.assertTrue(httpClient.getGetInvocationCount() > errorCount);
Assert.assertTrue(loggerRef.get().getMessageCount() >= errorCount);
}
use of org.jowidgets.logging.tools.JUnitLogger in project jo-client-platform by jo-source.
the class MessageBrokerTest method testSucessfulGetRequiredBeforePost.
@Test
public void testSucessfulGetRequiredBeforePost() throws ClientProtocolException, IOException, InterruptedException {
final String expectedResultMessage = "RESULT_MESSAGE";
final MessageStub requestMessage = new MessageStub(expectedResultMessage);
final int minGetBeforeFixError = 100;
final int messageCount = 5;
final HttpClientStub httpClient = new HttpClientStub(messageCount, 0);
httpClient.setFirstGetStatus(HttpClientStub.INTERNAL_SERVER_ERROR_STATUS_LINE);
final ScheduledExecutorServiceMock executor = new ScheduledExecutorServiceMock();
final AtomicReference<JUnitLogger> senderThreadLoggerRef = new AtomicReference<JUnitLogger>(null);
final AtomicReference<JUnitLogger> receiverThreadLoggerRef = new AtomicReference<JUnitLogger>(null);
final AtomicLong postCount = new AtomicLong();
final MessageBrokerBuilder builder = new MessageBrokerBuilder(BROKER_ID);
builder.setUrl(DEFAULT_URL);
builder.setHttpClient(httpClient);
builder.setIncommingMessageExecutor(executor);
builder.setSleepDurationMillisAfterIoException(0);
builder.setHttpRequestInitializer(new IHttpRequestInitializer() {
@Override
public void initialize(final HttpRequest httpRequest) {
if (httpRequest instanceof HttpPost) {
postCount.incrementAndGet();
}
if (Thread.currentThread().getName().contains("MessageSender")) {
prepareLoggerForThreadAndDisableConsole(senderThreadLoggerRef);
} else {
prepareLoggerForThreadAndDisableConsole(receiverThreadLoggerRef);
}
}
});
final IMessageBroker messageBroker = builder.build();
final IMessageReceiver receiver = Mockito.mock(IMessageReceiver.class);
messageBroker.setReceiver(receiver);
for (int i = 0; i < messageCount; i++) {
messageBroker.getChannel().send(requestMessage, null);
}
while (httpClient.getGetInvocationCount() < minGetBeforeFixError) {
// wait for some get() failures
}
// no posts has been send until first get() is successful
executor.executeEvents();
Assert.assertEquals(0, httpClient.getPostInvocationCount());
Mockito.verify(receiver, Mockito.never()).onMessage(expectedResultMessage, messageBroker.getChannel());
// now fix the fist get
httpClient.setFirstGetStatus(HttpClientStub.OK_STATUS_LINE);
// and wait that previous send messages has been consumed
Assert.assertTrue(httpClient.awaitMessagesConsumed(SYNC_TIMEOUT, TimeUnit.MILLISECONDS));
executor.executeEvents();
// now all messages has been received
Mockito.verify(receiver, Mockito.times(messageCount)).onMessage(expectedResultMessage, messageBroker.getChannel());
Assert.assertTrue(messageBroker.shutdown(SYNC_TIMEOUT));
Mockito.verify(httpClient.getConnectionManager(), Mockito.times(1)).shutdown();
Assert.assertEquals(messageCount, httpClient.getPostInvocationCount());
Assert.assertEquals(0, senderThreadLoggerRef.get().getMessageCount());
Assert.assertTrue(receiverThreadLoggerRef.get().getMessageCount() >= minGetBeforeFixError);
}
use of org.jowidgets.logging.tools.JUnitLogger 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();
}
Aggregations