Search in sources :

Example 1 with StartPosition

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();
    }
}
Also used : ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) StartPosition(com.urbanairship.connect.client.model.request.StartPosition) Test(org.junit.Test)

Example 2 with StartPosition

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();
    }
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Consumer(com.urbanairship.connect.java8.Consumer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) StreamQueryDescriptor(com.urbanairship.connect.client.model.StreamQueryDescriptor) StartPosition(com.urbanairship.connect.client.model.request.StartPosition) Test(org.junit.Test)

Example 3 with StartPosition

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();
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) StartPosition(com.urbanairship.connect.client.model.request.StartPosition) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Consumer(com.urbanairship.connect.java8.Consumer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) JsonObject(com.google.gson.JsonObject) Test(org.junit.Test)

Example 4 with StartPosition

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());
}
Also used : LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) StartPosition(com.urbanairship.connect.client.model.request.StartPosition) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Consumer(com.urbanairship.connect.java8.Consumer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) JsonObject(com.google.gson.JsonObject) Test(org.junit.Test)

Example 5 with StartPosition

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());
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) StartPosition(com.urbanairship.connect.client.model.request.StartPosition) Test(org.junit.Test)

Aggregations

StartPosition (com.urbanairship.connect.client.model.request.StartPosition)10 CountDownLatch (java.util.concurrent.CountDownLatch)10 Test (org.junit.Test)10 InvocationOnMock (org.mockito.invocation.InvocationOnMock)9 Mockito.doAnswer (org.mockito.Mockito.doAnswer)8 Answer (org.mockito.stubbing.Answer)8 JsonObject (com.google.gson.JsonObject)6 Consumer (com.urbanairship.connect.java8.Consumer)6 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)6 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)2 ArrayList (java.util.ArrayList)2 ExecutorService (java.util.concurrent.ExecutorService)2 HttpExchange (com.sun.net.httpserver.HttpExchange)1 ConnectionRetryStrategy (com.urbanairship.connect.client.consume.ConnectionRetryStrategy)1 StreamQueryDescriptor (com.urbanairship.connect.client.model.StreamQueryDescriptor)1