Search in sources :

Example 1 with ConnectionRetryStrategy

use of com.urbanairship.connect.client.consume.ConnectionRetryStrategy in project connect-java-library by urbanairship.

the class StreamConnectionTest method testConnectionRetry.

@Test
public void testConnectionRetry() throws Exception {
    Answer errorAnswer = new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            HttpExchange exchange = (HttpExchange) invocation.getArguments()[0];
            exchange.sendResponseHeaders(500, 0L);
            exchange.close();
            return null;
        }
    };
    final CountDownLatch successAnswerLatch = new CountDownLatch(1);
    Answer successAnswer = new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            HttpExchange exchange = (HttpExchange) invocation.getArguments()[0];
            exchange.sendResponseHeaders(200, 0L);
            successAnswerLatch.countDown();
            return null;
        }
    };
    doAnswer(errorAnswer).doAnswer(errorAnswer).doAnswer(successAnswer).when(serverHandler).handle(Matchers.<HttpExchange>any());
    ConnectionRetryStrategy strat = mock(ConnectionRetryStrategy.class);
    when(strat.shouldRetry(anyInt())).thenAnswer(new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            Integer i = (Integer) invocation.getArguments()[0];
            return i < 3;
        }
    });
    when(strat.getPauseMillis(anyInt())).thenReturn(0L);
    ExecutorService thread = Executors.newSingleThreadExecutor();
    stream = new StreamConnection(descriptor(), http, strat, consumer, url);
    try {
        thread.submit(new Runnable() {

            @Override
            public void run() {
                try {
                    stream.read(Optional.<StartPosition>absent());
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        });
        assertTrue(successAnswerLatch.await(10, TimeUnit.SECONDS));
        ArgumentCaptor<Integer> captor = ArgumentCaptor.forClass(Integer.class);
        verify(strat, times(2)).shouldRetry(captor.capture());
        assertEquals(ImmutableList.of(1, 2), captor.getAllValues());
    } finally {
        stream.close();
        thread.shutdownNow();
    }
}
Also used : HttpExchange(com.sun.net.httpserver.HttpExchange) CountDownLatch(java.util.concurrent.CountDownLatch) StartPosition(com.urbanairship.connect.client.model.request.StartPosition) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ConnectionRetryStrategy(com.urbanairship.connect.client.consume.ConnectionRetryStrategy) Test(org.junit.Test)

Aggregations

ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)1 HttpExchange (com.sun.net.httpserver.HttpExchange)1 ConnectionRetryStrategy (com.urbanairship.connect.client.consume.ConnectionRetryStrategy)1 StartPosition (com.urbanairship.connect.client.model.request.StartPosition)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 Test (org.junit.Test)1 Mockito.doAnswer (org.mockito.Mockito.doAnswer)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1