Search in sources :

Example 6 with Consumer

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));
}
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 Consumer

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;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Consumer(com.urbanairship.connect.java8.Consumer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 8 with Consumer

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();
    }
}
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 9 with Consumer

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();
    }
}
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 10 with Consumer

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());
}
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)

Aggregations

Test (org.junit.Test)14 Consumer (org.talend.daikon.java8.Consumer)10 Consumer (com.urbanairship.connect.java8.Consumer)9 InvocationOnMock (org.mockito.invocation.InvocationOnMock)9 StartPosition (com.urbanairship.connect.client.model.request.StartPosition)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 Mockito.doAnswer (org.mockito.Mockito.doAnswer)7 Answer (org.mockito.stubbing.Answer)7 JsonObject (com.google.gson.JsonObject)6 IndexedRecord (org.apache.avro.generic.IndexedRecord)5 ArrayList (java.util.ArrayList)4 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)4 JDBCDatasetRuntime (org.talend.components.jdbc.runtime.dataprep.JDBCDatasetRuntime)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 StreamQueryDescriptor (com.urbanairship.connect.client.model.StreamQueryDescriptor)1 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1 List (java.util.List)1 NoSuchElementException (java.util.NoSuchElementException)1