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