Search in sources :

Example 6 with Scheduler

use of reactor.core.scheduler.Scheduler in project reactor-core by reactor.

the class FluxTests method testParallelWithJava8StreamsInput.

/**
 * https://gist.github.com/nithril/444d8373ce67f0a8b853 Contribution by Nicolas Labrot
 * @throws InterruptedException on interrupt
 */
@Test
public void testParallelWithJava8StreamsInput() throws InterruptedException {
    Scheduler supplier = Schedulers.newParallel("test-p", 2);
    int max = ThreadLocalRandom.current().nextInt(100, 300);
    CountDownLatch countDownLatch = new CountDownLatch(max);
    Flux<Integer> worker = Flux.range(0, max).publishOn(asyncGroup);
    worker.parallel(2).runOn(supplier).map(v -> v).subscribe(v -> countDownLatch.countDown());
    countDownLatch.await(10, TimeUnit.SECONDS);
    Assert.assertEquals(0, countDownLatch.getCount());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Processor(org.reactivestreams.Processor) Tuples(reactor.util.function.Tuples) TimeoutException(java.util.concurrent.TimeoutException) Random(java.util.Random) Timer(java.util.Timer) CoreSubscriber(reactor.core.CoreSubscriber) Loggers(reactor.util.Loggers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) Logger(reactor.util.Logger) Assertions(org.assertj.core.api.Assertions) IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Signal(reactor.core.publisher.Signal) KeyEvent(java.awt.event.KeyEvent) Instant(java.time.Instant) SignalType(reactor.core.publisher.SignalType) Collectors(java.util.stream.Collectors) LockSupport(java.util.concurrent.locks.LockSupport) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stream(java.util.stream.Stream) Exceptions(reactor.core.Exceptions) IntStream(java.util.stream.IntStream) Disposable(reactor.core.Disposable) TopicProcessor(reactor.core.publisher.TopicProcessor) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Tuple2(reactor.util.function.Tuple2) FluxProcessor(reactor.core.publisher.FluxProcessor) Scheduler(reactor.core.scheduler.Scheduler) OrderingComparison.lessThan(org.hamcrest.number.OrderingComparison.lessThan) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) EmitterProcessor(reactor.core.publisher.EmitterProcessor) Schedulers(reactor.core.scheduler.Schedulers) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Semaphore(java.util.concurrent.Semaphore) Publisher(org.reactivestreams.Publisher) MonoProcessor(reactor.core.publisher.MonoProcessor) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) ReplayProcessor(reactor.core.publisher.ReplayProcessor) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Flux(reactor.core.publisher.Flux) Ignore(org.junit.Ignore) Phaser(java.util.concurrent.Phaser) Matcher(org.hamcrest.Matcher) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Comparator(java.util.Comparator) Assert(org.junit.Assert) Collections(java.util.Collections) Scheduler(reactor.core.scheduler.Scheduler) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 7 with Scheduler

use of reactor.core.scheduler.Scheduler in project reactor-core by reactor.

the class FluxBlackboxProcessorVerification method transformFlux.

@Override
Flux<Integer> transformFlux(Flux<Integer> f) {
    Flux<String> otherStream = Flux.just("test", "test2", "test3");
    // System.out.println("Providing new downstream");
    Scheduler asyncGroup = Schedulers.newParallel("flux-p-tck", 2);
    BiFunction<Integer, String, Integer> combinator = (t1, t2) -> t1;
    return f.publishOn(sharedGroup).parallel(2).groups().flatMap(stream -> stream.publishOn(asyncGroup).doOnNext(this::monitorThreadUse).scan((prev, next) -> next).map(integer -> -integer).filter(integer -> integer <= 0).map(integer -> -integer).bufferTimeout(batch, Duration.ofMillis(50)).flatMap(Flux::fromIterable).flatMap(i -> Flux.zip(Flux.just(i), otherStream, combinator))).publishOn(sharedGroup).doAfterTerminate(asyncGroup::dispose).doOnError(Throwable::printStackTrace);
}
Also used : Flux(reactor.core.publisher.Flux) Duration(java.time.Duration) BiFunction(java.util.function.BiFunction) BeforeMethod(org.testng.annotations.BeforeMethod) Schedulers(reactor.core.scheduler.Schedulers) AfterMethod(org.testng.annotations.AfterMethod) Scheduler(reactor.core.scheduler.Scheduler) Scheduler(reactor.core.scheduler.Scheduler)

Example 8 with Scheduler

use of reactor.core.scheduler.Scheduler in project reactor-core by reactor.

the class ParallelReduceSeedTest method collectAsync3Take.

@Test
public void collectAsync3Take() {
    Scheduler s = Schedulers.newParallel("test", 4);
    Supplier<List<Integer>> as = () -> new ArrayList<>();
    AssertSubscriber<Integer> ts = AssertSubscriber.create();
    Flux.range(1, 100000).publishOn(s).parallel(3).runOn(s).collect(as, (a, b) -> a.add(b)).doOnNext(v -> System.out.println(v.size())).groups().flatMap(v -> v).reduce(0, (a, b) -> b.size() + a).subscribe(ts);
    ts.await(Duration.ofSeconds(5));
    ts.assertValues(100_000).assertNoError().assertComplete();
}
Also used : Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Scannable(reactor.core.Scannable) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Scheduler(reactor.core.scheduler.Scheduler) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) List(java.util.List) CoreSubscriber(reactor.core.CoreSubscriber) ParallelOperatorTest(reactor.test.publisher.ParallelOperatorTest) Duration(java.time.Duration) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Schedulers(reactor.core.scheduler.Schedulers) Scheduler(reactor.core.scheduler.Scheduler) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test) ParallelOperatorTest(reactor.test.publisher.ParallelOperatorTest)

Example 9 with Scheduler

use of reactor.core.scheduler.Scheduler in project reactor-core by reactor.

the class ParallelReduceSeedTest method collectAsync3.

@Test
public void collectAsync3() {
    Scheduler s = Schedulers.newParallel("test", 3);
    Supplier<List<Integer>> as = () -> new ArrayList<>();
    AssertSubscriber<Integer> ts = AssertSubscriber.create();
    Flux.range(1, 100000).hide().publishOn(s).parallel(3).runOn(s).filter(t -> true).collect(as, (a, b) -> a.add(b)).doOnNext(v -> System.out.println(v.size())).groups().flatMap(v -> v).reduce(0, (a, b) -> b.size() + a).subscribe(ts);
    ts.await(Duration.ofSeconds(5));
    ts.assertValues(100_000).assertNoError().assertComplete();
}
Also used : Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Scannable(reactor.core.Scannable) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Scheduler(reactor.core.scheduler.Scheduler) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) List(java.util.List) CoreSubscriber(reactor.core.CoreSubscriber) ParallelOperatorTest(reactor.test.publisher.ParallelOperatorTest) Duration(java.time.Duration) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Schedulers(reactor.core.scheduler.Schedulers) Scheduler(reactor.core.scheduler.Scheduler) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test) ParallelOperatorTest(reactor.test.publisher.ParallelOperatorTest)

Example 10 with Scheduler

use of reactor.core.scheduler.Scheduler in project reactor-core by reactor.

the class ParallelReduceSeedTest method collectAsyncFused.

@Test
public void collectAsyncFused() {
    AssertSubscriber<Integer> ts = AssertSubscriber.create();
    Scheduler scheduler = Schedulers.newParallel("test", 3);
    Flux.range(1, 100000).parallel(3).runOn(scheduler).collect(ArrayList::new, ArrayList::add).sequential().reduce(0, (a, b) -> a + b.size()).subscribe(ts);
    ts.await(Duration.ofSeconds(5));
    ts.assertValues(100_000).assertNoError().assertComplete();
}
Also used : Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Scannable(reactor.core.Scannable) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Scheduler(reactor.core.scheduler.Scheduler) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) List(java.util.List) CoreSubscriber(reactor.core.CoreSubscriber) ParallelOperatorTest(reactor.test.publisher.ParallelOperatorTest) Duration(java.time.Duration) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Schedulers(reactor.core.scheduler.Schedulers) Scheduler(reactor.core.scheduler.Scheduler) Test(org.junit.Test) ParallelOperatorTest(reactor.test.publisher.ParallelOperatorTest)

Aggregations

Scheduler (reactor.core.scheduler.Scheduler)36 Test (org.junit.Test)35 Schedulers (reactor.core.scheduler.Schedulers)19 Duration (java.time.Duration)17 List (java.util.List)16 CountDownLatch (java.util.concurrent.CountDownLatch)16 StepVerifier (reactor.test.StepVerifier)16 ArrayList (java.util.ArrayList)15 AssertSubscriber (reactor.test.subscriber.AssertSubscriber)15 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)13 CoreSubscriber (reactor.core.CoreSubscriber)13 Arrays (java.util.Arrays)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 TimeUnit (java.util.concurrent.TimeUnit)11 Subscription (org.reactivestreams.Subscription)11 Scannable (reactor.core.Scannable)11 Assert (org.junit.Assert)10 Publisher (org.reactivestreams.Publisher)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 Disposable (reactor.core.Disposable)7