use of org.talend.daikon.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 org.talend.daikon.java8.Consumer in project connect-java-library by urbanairship.
the class StreamTest method testAirshipRequestClientStream.
@Test
public void testAirshipRequestClientStream() 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<>();
RequestClient requestClient = AirshipRequestClient.newBuilder().build();
Stream stream = Stream.newBuilder().setDescriptor(descriptor()).setRequestClient(requestClient).setConnectionSupplier(connSupplier).build();
try {
while (stream.hasNext()) {
received.add(stream.next());
if (received.size() == 20) {
break;
}
}
} finally {
stop.countDown();
}
assertEquals(events, received);
verify(conn, atLeastOnce()).close();
}
use of org.talend.daikon.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 org.talend.daikon.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();
}
}
Aggregations