Search in sources :

Example 6 with StartPosition

use of com.urbanairship.connect.client.model.request.StartPosition 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));
}
Also used : ArrayList(java.util.ArrayList) 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 7 with StartPosition

use of com.urbanairship.connect.client.model.request.StartPosition in project connect-java-library by urbanairship.

the class StreamConnectionTest method testConnectionRetry.

@Test
public void testConnectionRetry() throws Exception {
    Answer errorAnswer = new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            HttpExchange exchange = (HttpExchange) invocation.getArguments()[0];
            exchange.sendResponseHeaders(500, 0L);
            exchange.close();
            return null;
        }
    };
    final CountDownLatch successAnswerLatch = new CountDownLatch(1);
    Answer successAnswer = new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            HttpExchange exchange = (HttpExchange) invocation.getArguments()[0];
            exchange.sendResponseHeaders(200, 0L);
            successAnswerLatch.countDown();
            return null;
        }
    };
    doAnswer(errorAnswer).doAnswer(errorAnswer).doAnswer(successAnswer).when(serverHandler).handle(Matchers.<HttpExchange>any());
    ConnectionRetryStrategy strat = mock(ConnectionRetryStrategy.class);
    when(strat.shouldRetry(anyInt())).thenAnswer(new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            Integer i = (Integer) invocation.getArguments()[0];
            return i < 3;
        }
    });
    when(strat.getPauseMillis(anyInt())).thenReturn(0L);
    ExecutorService thread = Executors.newSingleThreadExecutor();
    stream = new StreamConnection(descriptor(), http, strat, consumer, url);
    try {
        thread.submit(new Runnable() {

            @Override
            public void run() {
                try {
                    stream.read(Optional.<StartPosition>absent());
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        });
        assertTrue(successAnswerLatch.await(10, TimeUnit.SECONDS));
        ArgumentCaptor<Integer> captor = ArgumentCaptor.forClass(Integer.class);
        verify(strat, times(2)).shouldRetry(captor.capture());
        assertEquals(ImmutableList.of(1, 2), captor.getAllValues());
    } finally {
        stream.close();
        thread.shutdownNow();
    }
}
Also used : HttpExchange(com.sun.net.httpserver.HttpExchange) CountDownLatch(java.util.concurrent.CountDownLatch) StartPosition(com.urbanairship.connect.client.model.request.StartPosition) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ConnectionRetryStrategy(com.urbanairship.connect.client.consume.ConnectionRetryStrategy) Test(org.junit.Test)

Example 8 with StartPosition

use of com.urbanairship.connect.client.model.request.StartPosition in project connect-java-library by urbanairship.

the class StreamConsumeTaskTest method testSpecifiedStartPosition.

@Test
public void testSpecifiedStartPosition() throws Exception {
    StartPosition position = StartPosition.offset(RandomStringUtils.randomAlphanumeric(32));
    task = StreamConsumeTask.newBuilder().setStreamQueryDescriptor(descriptor()).setStreamConnectionSupplier(supplier).setTargetQueue(new LinkedBlockingQueue<String>()).setStartingPosition(position).build();
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch verified = new CountDownLatch(1);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            latch.countDown();
            verified.await();
            return null;
        }
    }).when(stream).read(Matchers.<Optional<StartPosition>>any());
    readThread.submit(task);
    try {
        assertTrue(latch.await(10, TimeUnit.SECONDS));
        verify(stream).read(Optional.of(position));
    } finally {
        verified.countDown();
    }
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) JsonObject(com.google.gson.JsonObject) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) StartPosition(com.urbanairship.connect.client.model.request.StartPosition) Test(org.junit.Test)

Example 9 with StartPosition

use of com.urbanairship.connect.client.model.request.StartPosition 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();
    }
}
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 10 with StartPosition

use of com.urbanairship.connect.client.model.request.StartPosition 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();
}
Also used : ArrayList(java.util.ArrayList) 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)

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