use of com.urbanairship.connect.java8.Consumer 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.java8.Consumer in project connect-java-library by urbanairship.
the class StreamTest method hookStream.
@SuppressWarnings("unchecked")
private AtomicReference<Consumer<String>> hookStream(StreamConnectionSupplier supplier, final StreamConnection conn) {
final AtomicReference<Consumer<String>> hook = new AtomicReference<>();
final AtomicBoolean first = new AtomicBoolean(true);
when(supplier.get(Matchers.<StreamQueryDescriptor>any(), Matchers.<AsyncHttpClient>any(), Matchers.<Consumer<String>>any())).thenAnswer(new Answer<StreamConnection>() {
@Override
public StreamConnection answer(InvocationOnMock invocation) throws Throwable {
if (first.compareAndSet(true, false)) {
Consumer<String> consumer = (Consumer<String>) invocation.getArguments()[2];
hook.set(consumer);
}
return conn;
}
});
return hook;
}
use of com.urbanairship.connect.java8.Consumer 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();
}
use of com.urbanairship.connect.java8.Consumer in project components by Talend.
the class ReaderDataProviderTest method testReaderDataProviderWithLimitTo0.
@Test
public void testReaderDataProviderWithLimitTo0() throws IOException {
Reader<String> reader = spy(new OneTwoReader());
Consumer consumer = mock(Consumer.class);
ReaderDataProvider<String> readerDataProvider = new ReaderDataProvider<>(reader, 0, consumer);
readerDataProvider.retrieveData();
verify(consumer, never()).accept(any());
verify(reader, times(1)).close();
}
use of com.urbanairship.connect.java8.Consumer in project components by Talend.
the class ReaderDataProviderTest method testReaderDataProviderWithException.
@Test
public void testReaderDataProviderWithException() throws IOException {
Reader<String> reader = mock(Reader.class);
Consumer consumer = mock(Consumer.class);
ReaderDataProvider<String> readerDataProvider = new ReaderDataProvider<>(reader, 100, consumer);
// reader start throws an IOE
when(reader.start()).thenThrow(new IOException());
try {
readerDataProvider.retrieveData();
fail("the code above should have thrown an exception");
} catch (TalendRuntimeException tre) {
// expected exception
verify(reader, times(1)).close();
}
// reader getCurrent throws an IOE
reset(reader);
when(reader.start()).thenReturn(true);
when(reader.getCurrent()).thenThrow(new NoSuchElementException());
try {
readerDataProvider.retrieveData();
fail("the code above should have thrown an exception");
} catch (TalendRuntimeException tre) {
// expected exception
verify(reader, times(1)).close();
}
// reader close throws an IOE
reset(reader);
when(reader.start()).thenReturn(false);
doThrow(new IOException()).when(reader).close();
try {
readerDataProvider.retrieveData();
fail("the code above should have thrown an exception");
} catch (TalendRuntimeException tre) {
// expected exception
}
}
Aggregations