Search in sources :

Example 1 with PartitionMapperImpl

use of org.talend.sdk.component.runtime.input.PartitionMapperImpl in project component-runtime by Talend.

the class ComponentExtensionTest method sourceCollectorParallel.

@Test
void sourceCollectorParallel() {
    final CountDownLatch latch = new CountDownLatch(1);
    final Mapper mapper = new PartitionMapperImpl() {

        @Override
        public long assess() {
            return 2;
        }

        @Override
        public List<Mapper> split(final long desiredSize) {
            assertEquals(1, desiredSize);
            return asList(this, this);
        }

        @Override
        public Input create() {
            return new InputImpl() {

                private final AtomicBoolean done = new AtomicBoolean();

                @Override
                public Object next() {
                    try {
                        latch.await(1, MINUTES);
                    } catch (final InterruptedException e) {
                        Thread.interrupted();
                        fail();
                    }
                    return done.compareAndSet(false, true) ? Thread.currentThread().getName() : null;
                }

                @Override
                protected Stream<Method> findMethods(final Class<? extends Annotation> marker) {
                    return Stream.empty();
                }
            };
        }

        @Override
        protected Stream<Method> findMethods(final Class<? extends Annotation> marker) {
            return Stream.empty();
        }
    };
    latch.countDown();
    final Stream<String> collect = handler.collect(String.class, mapper, 2, 2);
    final List<String> threads = collect.collect(toList());
    assertEquals(threads.toString(), 2, threads.size());
    threads.forEach(n -> assertTrue(n, n.startsWith("ComponentExtension-pool-")));
}
Also used : Mapper(org.talend.sdk.component.runtime.input.Mapper) InputImpl(org.talend.sdk.component.runtime.input.InputImpl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PartitionMapperImpl(org.talend.sdk.component.runtime.input.PartitionMapperImpl) Method(java.lang.reflect.Method) CountDownLatch(java.util.concurrent.CountDownLatch) Annotation(java.lang.annotation.Annotation) Test(org.junit.jupiter.api.Test)

Example 2 with PartitionMapperImpl

use of org.talend.sdk.component.runtime.input.PartitionMapperImpl in project component-runtime by Talend.

the class SimpleComponentRuleTest method sourceCollectorParallel.

@Test
public void sourceCollectorParallel() {
    final CountDownLatch latch = new CountDownLatch(1);
    final Mapper mapper = new PartitionMapperImpl() {

        @Override
        public long assess() {
            return 2;
        }

        @Override
        public List<Mapper> split(final long desiredSize) {
            assertEquals(1, desiredSize);
            return asList(this, this);
        }

        @Override
        public Input create() {
            return new InputImpl() {

                private final AtomicBoolean done = new AtomicBoolean();

                @Override
                public Object next() {
                    try {
                        latch.await(1, MINUTES);
                    } catch (final InterruptedException e) {
                        Thread.interrupted();
                        fail();
                    }
                    return done.compareAndSet(false, true) ? Thread.currentThread().getName() : null;
                }

                @Override
                protected Stream<Method> findMethods(final Class<? extends Annotation> marker) {
                    return Stream.empty();
                }
            };
        }

        @Override
        protected Stream<Method> findMethods(final Class<? extends Annotation> marker) {
            return Stream.empty();
        }
    };
    latch.countDown();
    final Stream<String> collect = COMPONENT_FACTORY.collect(String.class, mapper, 2, 2);
    final List<String> threads = collect.collect(toList());
    assertEquals(threads.toString(), 2, threads.size());
    threads.forEach(n -> assertTrue(n, n.startsWith("SimpleComponentRule-pool-")));
}
Also used : Mapper(org.talend.sdk.component.runtime.input.Mapper) InputImpl(org.talend.sdk.component.runtime.input.InputImpl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PartitionMapperImpl(org.talend.sdk.component.runtime.input.PartitionMapperImpl) Method(java.lang.reflect.Method) CountDownLatch(java.util.concurrent.CountDownLatch) Annotation(java.lang.annotation.Annotation) Test(org.junit.Test)

Aggregations

Annotation (java.lang.annotation.Annotation)2 Method (java.lang.reflect.Method)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 InputImpl (org.talend.sdk.component.runtime.input.InputImpl)2 Mapper (org.talend.sdk.component.runtime.input.Mapper)2 PartitionMapperImpl (org.talend.sdk.component.runtime.input.PartitionMapperImpl)2 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1