use of com.urbanairship.connect.client.model.request.StartPosition in project connect-java-library by urbanairship.
the class StreamConnectionTest method testCloseBeforeRead.
@Test
public void testCloseBeforeRead() throws Exception {
stream = new StreamConnection(descriptor(), http, connectionRetryStrategy, consumer, url);
stream.close();
final CountDownLatch latch = new CountDownLatch(1);
ExecutorService thread = Executors.newSingleThreadExecutor();
try {
thread.submit(new Runnable() {
@Override
public void run() {
try {
stream.read(Optional.<StartPosition>absent());
latch.countDown();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
});
assertTrue(latch.await(1, TimeUnit.SECONDS));
} finally {
thread.shutdownNow();
}
}
use of com.urbanairship.connect.client.model.request.StartPosition 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 com.urbanairship.connect.client.model.request.StartPosition 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 com.urbanairship.connect.client.model.request.StartPosition 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());
}
use of com.urbanairship.connect.client.model.request.StartPosition in project connect-java-library by urbanairship.
the class StreamConsumeTaskTest method testStopPreventsFurtherStreamRead.
@Test
public void testStopPreventsFurtherStreamRead() throws Exception {
task = task(descriptor(), new LinkedBlockingQueue<String>());
final CountDownLatch streamRequested = new CountDownLatch(1);
final CountDownLatch stopped = new CountDownLatch(1);
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 {
streamRequested.countDown();
stopped.await();
return stream;
}
});
Future<?> future = readThread.submit(task);
assertTrue(streamRequested.await(10, TimeUnit.SECONDS));
task.stop();
stopped.countDown();
future.get(10, TimeUnit.SECONDS);
verify(stream, never()).read(Matchers.<Optional<StartPosition>>any());
}
Aggregations