Search in sources :

Example 11 with Consumer

use of com.urbanairship.connect.java8.Consumer 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 12 with Consumer

use of com.urbanairship.connect.java8.Consumer in project connect-java-library by urbanairship.

the class StreamTest method hookStream.

@SuppressWarnings("unchecked")
private AtomicReference<Consumer<String>> hookStream(StreamConnectionSupplier supplier, final StreamConnection conn) {
    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 conn;
        }
    });
    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 13 with Consumer

use of com.urbanairship.connect.java8.Consumer 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)

Example 14 with Consumer

use of com.urbanairship.connect.java8.Consumer in project components by Talend.

the class ReaderDataProviderTest method testReaderDataProviderWithLimitTo0.

@Test
public void testReaderDataProviderWithLimitTo0() throws IOException {
    Reader<String> reader = spy(new OneTwoReader());
    Consumer consumer = mock(Consumer.class);
    ReaderDataProvider<String> readerDataProvider = new ReaderDataProvider<>(reader, 0, consumer);
    readerDataProvider.retrieveData();
    verify(consumer, never()).accept(any());
    verify(reader, times(1)).close();
}
Also used : Consumer(org.talend.daikon.java8.Consumer) Test(org.junit.Test)

Example 15 with Consumer

use of com.urbanairship.connect.java8.Consumer in project components by Talend.

the class ReaderDataProviderTest method testReaderDataProviderWithException.

@Test
public void testReaderDataProviderWithException() throws IOException {
    Reader<String> reader = mock(Reader.class);
    Consumer consumer = mock(Consumer.class);
    ReaderDataProvider<String> readerDataProvider = new ReaderDataProvider<>(reader, 100, consumer);
    // reader start throws an IOE
    when(reader.start()).thenThrow(new IOException());
    try {
        readerDataProvider.retrieveData();
        fail("the code above should have thrown an exception");
    } catch (TalendRuntimeException tre) {
        // expected exception
        verify(reader, times(1)).close();
    }
    // reader getCurrent throws an IOE
    reset(reader);
    when(reader.start()).thenReturn(true);
    when(reader.getCurrent()).thenThrow(new NoSuchElementException());
    try {
        readerDataProvider.retrieveData();
        fail("the code above should have thrown an exception");
    } catch (TalendRuntimeException tre) {
        // expected exception
        verify(reader, times(1)).close();
    }
    // reader close throws an IOE
    reset(reader);
    when(reader.start()).thenReturn(false);
    doThrow(new IOException()).when(reader).close();
    try {
        readerDataProvider.retrieveData();
        fail("the code above should have thrown an exception");
    } catch (TalendRuntimeException tre) {
    // expected exception
    }
}
Also used : TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException) Consumer(org.talend.daikon.java8.Consumer) IOException(java.io.IOException) NoSuchElementException(java.util.NoSuchElementException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)13 Consumer (org.talend.daikon.java8.Consumer)10 Consumer (com.urbanairship.connect.java8.Consumer)8 InvocationOnMock (org.mockito.invocation.InvocationOnMock)8 StartPosition (com.urbanairship.connect.client.model.request.StartPosition)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 Mockito.doAnswer (org.mockito.Mockito.doAnswer)6 Answer (org.mockito.stubbing.Answer)6 JsonObject (com.google.gson.JsonObject)5 IndexedRecord (org.apache.avro.generic.IndexedRecord)5 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)4 ArrayList (java.util.ArrayList)3 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