Search in sources :

Example 1 with ScheduledExecutorServiceMock

use of org.jowidgets.util.mock.ScheduledExecutorServiceMock 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());
}
Also used : HttpRequest(org.apache.http.HttpRequest) ScheduledExecutorServiceMock(org.jowidgets.util.mock.ScheduledExecutorServiceMock) AtomicReference(java.util.concurrent.atomic.AtomicReference) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) JUnitLogger(org.jowidgets.logging.tools.JUnitLogger) IMessageReceiver(org.jowidgets.message.api.IMessageReceiver) IExceptionCallback(org.jowidgets.message.api.IExceptionCallback) Test(org.junit.Test)

Example 2 with ScheduledExecutorServiceMock

use of org.jowidgets.util.mock.ScheduledExecutorServiceMock in project jo-client-platform by jo-source.

the class MessageBrokerTest method testSendMessages.

@Test
public void testSendMessages() throws ClientProtocolException, IOException, InterruptedException {
    final String expectedResultMessage = "RESULT_MESSAGE";
    final MessageStub requestMessage = new MessageStub(expectedResultMessage);
    final int messageCount = 5;
    final HttpClientStub httpClient = new HttpClientStub(messageCount, 0);
    final IHttpRequestInitializer httpRequestInitializer = Mockito.mock(IHttpRequestInitializer.class);
    final ScheduledExecutorServiceMock executor = new ScheduledExecutorServiceMock();
    final MessageBrokerBuilder builder = new MessageBrokerBuilder(BROKER_ID);
    builder.setUrl(DEFAULT_URL);
    builder.setHttpClient(httpClient);
    builder.setHttpRequestInitializer(httpRequestInitializer);
    builder.setIncommingMessageExecutor(executor);
    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);
    }
    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();
    final int postCount = httpClient.getPostInvocationCount();
    final int getCount = httpClient.getGetInvocationCount();
    Assert.assertEquals(messageCount, postCount);
    // get count must be 1 initial get and at least one get request read the result messages
    Assert.assertTrue(getCount >= 2);
    Mockito.verify(httpRequestInitializer, Mockito.times(postCount + getCount)).initialize(Mockito.any(HttpRequest.class));
}
Also used : HttpRequest(org.apache.http.HttpRequest) ScheduledExecutorServiceMock(org.jowidgets.util.mock.ScheduledExecutorServiceMock) IMessageReceiver(org.jowidgets.message.api.IMessageReceiver) Test(org.junit.Test)

Example 3 with ScheduledExecutorServiceMock

use of org.jowidgets.util.mock.ScheduledExecutorServiceMock 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);
}
Also used : HttpRequest(org.apache.http.HttpRequest) ScheduledExecutorServiceMock(org.jowidgets.util.mock.ScheduledExecutorServiceMock) IMessageReceiver(org.jowidgets.message.api.IMessageReceiver) AtomicReference(java.util.concurrent.atomic.AtomicReference) JUnitLogger(org.jowidgets.logging.tools.JUnitLogger) Test(org.junit.Test)

Example 4 with ScheduledExecutorServiceMock

use of org.jowidgets.util.mock.ScheduledExecutorServiceMock 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);
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpPost(org.apache.http.client.methods.HttpPost) ScheduledExecutorServiceMock(org.jowidgets.util.mock.ScheduledExecutorServiceMock) AtomicReference(java.util.concurrent.atomic.AtomicReference) JUnitLogger(org.jowidgets.logging.tools.JUnitLogger) AtomicLong(java.util.concurrent.atomic.AtomicLong) IMessageReceiver(org.jowidgets.message.api.IMessageReceiver) Test(org.junit.Test)

Aggregations

HttpRequest (org.apache.http.HttpRequest)4 IMessageReceiver (org.jowidgets.message.api.IMessageReceiver)4 ScheduledExecutorServiceMock (org.jowidgets.util.mock.ScheduledExecutorServiceMock)4 Test (org.junit.Test)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 JUnitLogger (org.jowidgets.logging.tools.JUnitLogger)3 IOException (java.io.IOException)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 ClientProtocolException (org.apache.http.client.ClientProtocolException)1 HttpPost (org.apache.http.client.methods.HttpPost)1 IExceptionCallback (org.jowidgets.message.api.IExceptionCallback)1