use of com.urbanairship.connect.client.model.request.StartPosition in project connect-java-library by urbanairship.
the class StreamTest method testStartPositionPropogated.
@Test
public void testStartPositionPropogated() throws Exception {
final AtomicReference<Consumer<String>> consumer = hookStream(connSupplier, conn);
final List<String> events = events(1);
final CountDownLatch stop = new CountDownLatch(1);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
for (String event : events) {
consumer.get().accept(event);
}
stop.await();
return null;
}
}).when(conn).read(Matchers.<Optional<StartPosition>>any());
StartPosition pos = StartPosition.relative(StartPosition.RelativePosition.EARLIEST);
List<String> received = new ArrayList<>();
try (Stream stream = new Stream(descriptor(), Optional.of(pos), Optional.of(connSupplier))) {
if (stream.hasNext()) {
received.add(stream.next());
}
} finally {
stop.countDown();
}
assertEquals(events, received);
verify(conn).read(Optional.of(pos));
}
use of com.urbanairship.connect.client.model.request.StartPosition 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();
}
}
use of com.urbanairship.connect.client.model.request.StartPosition in project connect-java-library by urbanairship.
the class StreamConsumeTaskTest method testSpecifiedStartPosition.
@Test
public void testSpecifiedStartPosition() throws Exception {
StartPosition position = StartPosition.offset(RandomStringUtils.randomAlphanumeric(32));
task = StreamConsumeTask.newBuilder().setStreamQueryDescriptor(descriptor()).setStreamConnectionSupplier(supplier).setTargetQueue(new LinkedBlockingQueue<String>()).setStartingPosition(position).build();
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch verified = new CountDownLatch(1);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
latch.countDown();
verified.await();
return null;
}
}).when(stream).read(Matchers.<Optional<StartPosition>>any());
readThread.submit(task);
try {
assertTrue(latch.await(10, TimeUnit.SECONDS));
verify(stream).read(Optional.of(position));
} finally {
verified.countDown();
}
}
use of com.urbanairship.connect.client.model.request.StartPosition in project connect-java-library by urbanairship.
the class StreamConsumeTaskTest method testRetries.
@Test
public void testRetries() throws Exception {
final List<TestEvent> batch1 = events(2);
final List<TestEvent> batch2 = events(3);
final List<TestEvent> batch3 = events(1);
final AtomicReference<Consumer<String>> hook = hookStream();
final CountDownLatch iterationsDone = new CountDownLatch(1);
final CountDownLatch assertionDone = new CountDownLatch(1);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
consume(hook.get(), batch1);
return null;
}
}).doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
consume(hook.get(), batch2);
throw new RuntimeException("Boom!");
}
}).doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
consume(hook.get(), batch3);
iterationsDone.countDown();
assertionDone.await();
return null;
}
}).doNothing().when(stream).read(Matchers.<Optional<StartPosition>>any());
BlockingQueue<String> queue = new LinkedBlockingQueue<>();
task = task(descriptor(), queue);
readThread.submit(task);
try {
assertTrue(iterationsDone.await(10, TimeUnit.SECONDS));
assertEquals(ImmutableList.builder().addAll(reduce(batch1)).addAll(reduce(batch2)).addAll(reduce(batch3)).build(), ImmutableList.copyOf(queue));
verify(stream, atLeastOnce()).read(positionCaptor.capture());
assertEquals(ImmutableList.builder().add(Optional.<Long>absent()).add(Optional.of(StartPosition.offset(Iterables.getLast(batch1).offset))).add(Optional.of(StartPosition.offset(Iterables.getLast(batch2).offset))).build(), positionCaptor.getAllValues().subList(0, 3));
} finally {
assertionDone.countDown();
}
}
use of com.urbanairship.connect.client.model.request.StartPosition in project connect-java-library by urbanairship.
the class StreamTest method testStream.
@Test
public void testStream() throws Exception {
final AtomicReference<Consumer<String>> consumer = hookStream(connSupplier, conn);
final List<String> events = events(20);
final CountDownLatch stop = new CountDownLatch(1);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
for (String event : events) {
consumer.get().accept(event);
}
stop.await();
return null;
}
}).when(conn).read(Matchers.<Optional<StartPosition>>any());
List<String> received = new ArrayList<>();
try (Stream stream = new Stream(descriptor(), Optional.<StartPosition>absent(), Optional.of(connSupplier))) {
while (stream.hasNext()) {
received.add(stream.next());
if (received.size() == 20) {
break;
}
}
} finally {
stop.countDown();
}
assertEquals(events, received);
verify(conn, atLeastOnce()).close();
}
Aggregations