Search in sources :

Example 31 with Scheduler

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

the class FluxSpecTests method whenProcessorIsStreamed.

@Test
public void whenProcessorIsStreamed() {
    // "When a processor is streamed"
    // given: "a source composable and a async downstream"
    ReplayProcessor<Integer> source = ReplayProcessor.create();
    Scheduler scheduler = Schedulers.newParallel("test", 2);
    try {
        Mono<List<Integer>> res = source.subscribeOn(scheduler).delaySubscription(Duration.ofMillis(1L)).log("streamed").map(it -> it * 2).buffer().publishNext();
        res.subscribe();
        // when: "the source accepts a value"
        source.onNext(1);
        source.onNext(2);
        source.onNext(3);
        source.onNext(4);
        source.onComplete();
        // then: "the res is passed on"
        assertThat(res.block()).containsExactly(2, 4, 6, 8);
    } finally {
        scheduler.dispose();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Scheduler(reactor.core.scheduler.Scheduler) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 32 with Scheduler

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

the class FluxTests method multiplexUsingDispatchersAndSplit.

/**
 * <pre>
 *                 forkStream
 *                 /        \      < - - - int
 *                v          v
 * persistenceStream        computationStream
 *                 \        /      < - - - List< String >
 *                  v      v
 *                 joinStream      < - - - String
 *                 splitStream
 *             observedSplitStream
 * </pre>
 * @throws Exception for convenience
 */
@Test(timeout = TIMEOUT)
public void multiplexUsingDispatchersAndSplit() throws Exception {
    final EmitterProcessor<Integer> forkEmitterProcessor = EmitterProcessor.create();
    final EmitterProcessor<Integer> computationEmitterProcessor = EmitterProcessor.create(false);
    Scheduler computation = Schedulers.newSingle("computation");
    Scheduler persistence = Schedulers.newSingle("persistence");
    Scheduler forkJoin = Schedulers.newParallel("forkJoin", 2);
    final Flux<List<String>> computationStream = computationEmitterProcessor.publishOn(computation).map(i -> {
        final List<String> list = new ArrayList<>(i);
        for (int j = 0; j < i; j++) {
            list.add("i" + j);
        }
        return list;
    }).doOnNext(ls -> println("Computed: ", ls)).log("computation");
    final EmitterProcessor<Integer> persistenceEmitterProcessor = EmitterProcessor.create(false);
    final Flux<List<String>> persistenceStream = persistenceEmitterProcessor.publishOn(persistence).doOnNext(i -> println("Persisted: ", i)).map(i -> Collections.singletonList("done" + i)).log("persistence");
    Flux<Integer> forkStream = forkEmitterProcessor.publishOn(forkJoin).log("fork");
    forkStream.subscribe(computationEmitterProcessor);
    forkStream.subscribe(persistenceEmitterProcessor);
    final Flux<List<String>> joinStream = Flux.zip(computationStream, persistenceStream, (a, b) -> Arrays.asList(a, b)).publishOn(forkJoin).map(listOfLists -> {
        listOfLists.get(0).addAll(listOfLists.get(1));
        return listOfLists.get(0);
    }).log("join");
    final Semaphore doneSemaphore = new Semaphore(0);
    final MonoProcessor<List<String>> listPromise = joinStream.flatMap(Flux::fromIterable).log("resultStream").collectList().doOnTerminate(doneSemaphore::release).toProcessor();
    listPromise.subscribe();
    forkEmitterProcessor.onNext(1);
    forkEmitterProcessor.onNext(2);
    forkEmitterProcessor.onNext(3);
    forkEmitterProcessor.onComplete();
    List<String> res = listPromise.block(Duration.ofSeconds(5));
    assertEquals(Arrays.asList("i0", "done1", "i0", "i1", "done2", "i0", "i1", "i2", "done3"), res);
    forkJoin.dispose();
    persistence.dispose();
    computation.dispose();
}
Also used : 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) ArrayList(java.util.ArrayList) Flux(reactor.core.publisher.Flux) Semaphore(java.util.concurrent.Semaphore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 33 with Scheduler

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

the class WorkQueueProcessorTest method highRate.

@Test
public void highRate() throws Exception {
    WorkQueueProcessor<String> queueProcessor = WorkQueueProcessor.<String>builder().share(true).name("Processor").bufferSize(256).waitStrategy(liteBlocking()).build();
    Scheduler timer = Schedulers.newSingle("Timer");
    queueProcessor.bufferTimeout(32, Duration.ofMillis(2), timer).subscribe(new CoreSubscriber<List<String>>() {

        int counter;

        @Override
        public void onComplete() {
            System.out.println("Consumed in total: " + counter);
        }

        @Override
        public void onError(Throwable t) {
            t.printStackTrace();
        }

        @Override
        public void onNext(List<String> strings) {
            int size = strings.size();
            counter += size;
            if (strings.contains(s)) {
                synchronized (s) {
                    // logger.debug("Synchronizer!");
                    s.notifyAll();
                }
            }
        }

        @Override
        public void onSubscribe(Subscription s) {
            s.request(Long.MAX_VALUE);
        }
    });
    FluxSink<String> emitter = queueProcessor.sink();
    try {
        submitInCurrentThread(emitter);
    } finally {
        logger.debug("Finishing");
        emitter.complete();
        timer.dispose();
    }
    TimeUnit.SECONDS.sleep(1);
}
Also used : Scheduler(reactor.core.scheduler.Scheduler) List(java.util.List) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 34 with Scheduler

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

the class ParallelFluxTest method parallelModeFused.

@Test
public void parallelModeFused() {
    Flux<Integer> source = Flux.range(1, 1_000_000);
    int ncpu = Math.max(8, Runtime.getRuntime().availableProcessors());
    for (int i = 1; i < ncpu + 1; i++) {
        Scheduler scheduler = Schedulers.newParallel("test", i);
        try {
            Flux<Integer> result = ParallelFlux.from(source, i).runOn(scheduler).map(v -> v + 1).sequential();
            AssertSubscriber<Integer> ts = AssertSubscriber.create();
            result.subscribe(ts);
            ts.await(Duration.ofSeconds(10));
            ts.assertSubscribed().assertValueCount(1_000_000).assertComplete().assertNoError();
        } finally {
            scheduler.dispose();
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LongAdder(java.util.concurrent.atomic.LongAdder) TestPublisher(reactor.test.publisher.TestPublisher) Arrays(java.util.Arrays) Disposable(reactor.core.Disposable) StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Scheduler(reactor.core.scheduler.Scheduler) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Queues(reactor.util.concurrent.Queues) HashSet(java.util.HashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) Subscriber(org.reactivestreams.Subscriber) Publisher(org.reactivestreams.Publisher) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) 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) Test(org.junit.Test)

Example 35 with Scheduler

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

the class ParallelMergeOrderedTest method reorderingByIndex.

@Test
public void reorderingByIndex() {
    final int LOOPS = 100;
    final int PARALLELISM = 2;
    final List<Integer> ordered = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    int notShuffled = 0;
    for (int i = 0; i < LOOPS; i++) {
        final Scheduler SCHEDULER = Schedulers.newParallel("test", PARALLELISM);
        final List<Integer> disordered = Collections.synchronizedList(new ArrayList<>());
        List<Integer> reordered = Flux.fromIterable(ordered).hide().index().parallel(PARALLELISM).runOn(SCHEDULER).doOnNext(t2 -> disordered.add(t2.getT2())).ordered(Comparator.comparing(Tuple2::getT1)).map(Tuple2::getT2).collectList().block();
        SCHEDULER.dispose();
        assertThat(reordered).containsExactlyElementsOf(ordered);
        assertThat(disordered).containsExactlyInAnyOrderElementsOf(ordered);
        try {
            assertThat(disordered).doesNotContainSequence(ordered);
            System.out.println("parallel shuffled the collection into " + disordered);
            break;
        } catch (AssertionError e) {
            notShuffled++;
        }
    }
    if (notShuffled > 0) {
        System.out.println("not shuffled loops: " + notShuffled);
    }
    assertThat(LOOPS - notShuffled).as("at least one run shuffled").isGreaterThan(0);
}
Also used : Scheduler(reactor.core.scheduler.Scheduler) Tuple2(reactor.util.function.Tuple2) Test(org.junit.Test)

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