use of org.talend.daikon.java8.Consumer 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 org.talend.daikon.java8.Consumer in project connect-java-library by urbanairship.
the class StreamConsumeTaskTest method hookStream.
@SuppressWarnings("unchecked")
private AtomicReference<Consumer<String>> hookStream() {
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 stream;
}
});
return hook;
}
use of org.talend.daikon.java8.Consumer in project connect-java-library by urbanairship.
the class StreamConsumeTaskTest method testAlreadySeenEventAfterReconnectIsIgnored.
@Test
public void testAlreadySeenEventAfterReconnectIsIgnored() throws Exception {
final List<TestEvent> batch1 = events(2);
final List<TestEvent> batch2 = events(3);
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);
throw new RuntimeException();
}
}).doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
consume(hook.get(), ImmutableList.of(Iterables.getLast(batch1)));
consume(hook.get(), batch2);
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 {
iterationsDone.await();
assertTrue(iterationsDone.await(10, TimeUnit.SECONDS));
assertEquals(ImmutableList.builder().addAll(reduce(batch1)).addAll(reduce(batch2)).build(), ImmutableList.copyOf(queue));
} finally {
assertionDone.countDown();
}
}
use of org.talend.daikon.java8.Consumer in project connect-java-library by urbanairship.
the class StreamConsumeTaskTest method testRun.
@Test
public void testRun() throws Exception {
final AtomicReference<Consumer<String>> hook = hookStream();
StreamQueryDescriptor descriptor = descriptor();
BlockingQueue<String> queue = new LinkedBlockingQueue<>();
task = task(descriptor, queue);
final List<TestEvent> events = events(10);
final CountDownLatch readDone = new CountDownLatch(1);
final CountDownLatch assertionDone = new CountDownLatch(1);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
consume(hook.get(), events);
readDone.countDown();
assertionDone.await();
return null;
}
}).doNothing().when(stream).read(Matchers.<Optional<StartPosition>>any());
readThread.submit(task);
try {
assertTrue(readDone.await(10, TimeUnit.SECONDS));
assertEquals(reduce(events), ImmutableList.copyOf(queue));
verify(supplier).get(eq(descriptor), Matchers.<AsyncHttpClient>any(), Matchers.<Consumer<String>>any());
verify(stream).read(Optional.<StartPosition>absent());
} finally {
assertionDone.countDown();
}
}
use of org.talend.daikon.java8.Consumer in project connect-java-library by urbanairship.
the class StreamConsumeTaskTest method testTaskExistWhenConsumingBlockedOnFullQueue.
@Test
public void testTaskExistWhenConsumingBlockedOnFullQueue() throws Exception {
BlockingQueue<String> queue = new LinkedBlockingQueue<>(1);
task = task(descriptor(), queue);
final AtomicReference<Consumer<String>> hook = hookStream();
final List<TestEvent> events = events(5);
final CountDownLatch consumeDone = new CountDownLatch(1);
final CountDownLatch ignoredDone = new CountDownLatch(1);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
consume(hook.get(), events.subList(0, 1));
consumeDone.countDown();
consume(hook.get(), events.subList(1, 5));
ignoredDone.countDown();
return null;
}
}).when(stream).read(Matchers.<Optional<StartPosition>>any());
Future<?> future = readThread.submit(task);
assertTrue(consumeDone.await(10, TimeUnit.SECONDS));
task.stop();
assertTrue(ignoredDone.await(10, TimeUnit.SECONDS));
future.get(10, TimeUnit.SECONDS);
assertEquals(1, queue.size());
assertEquals(events.get(0).json, queue.remove());
}
Aggregations