Search in sources :

Example 1 with Scheduler

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

the class EmitterProcessorTest method testThreadAffinity.

@Test
public void testThreadAffinity() throws InterruptedException {
    int count = 10;
    Scheduler[] schedulers = new Scheduler[4];
    CountDownLatch[] latches = new CountDownLatch[schedulers.length];
    for (int i = 0; i < schedulers.length; i++) {
        schedulers[i] = Schedulers.newSingle("scheduler" + i + '-');
        int expectedCount = i == 1 ? count * 2 : count;
        latches[i] = new CountDownLatch(expectedCount);
    }
    EmitterProcessor<Integer> processor = EmitterProcessor.create();
    processor.publishOn(schedulers[0]).share();
    processor.publishOn(schedulers[1]).subscribe(i -> {
        assertThat(Thread.currentThread().getName().contains("scheduler1")).isTrue();
        latches[1].countDown();
    });
    for (int i = 0; i < count; i++) processor.onNext(i);
    processor.publishOn(schedulers[2]).map(i -> {
        assertThat(Thread.currentThread().getName().contains("scheduler2")).isTrue();
        latches[2].countDown();
        return i;
    }).publishOn(schedulers[3]).doOnNext(i -> {
        assertThat(Thread.currentThread().getName().contains("scheduler3")).isTrue();
        latches[3].countDown();
    }).subscribe();
    for (int i = 0; i < count; i++) processor.onNext(i);
    processor.onComplete();
    for (int i = 1; i < latches.length; i++) assertThat(latches[i].await(5, TimeUnit.SECONDS)).isTrue();
    assertThat(latches[0].getCount()).isEqualTo(count);
}
Also used : Disposable(reactor.core.Disposable) StepVerifier(reactor.test.StepVerifier) Scannable(reactor.core.Scannable) Processor(org.reactivestreams.Processor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Scheduler(reactor.core.scheduler.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) Nullable(reactor.util.annotation.Nullable) ArrayList(java.util.ArrayList) Queues(reactor.util.concurrent.Queues) CoreSubscriber(reactor.core.CoreSubscriber) Attr(reactor.core.Scannable.Attr) CANCELLED(reactor.core.Scannable.Attr.CANCELLED) Schedulers(reactor.core.scheduler.Schedulers) Subscriber(org.reactivestreams.Subscriber) CyclicBarrier(java.util.concurrent.CyclicBarrier) Publisher(org.reactivestreams.Publisher) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) LockSupport(java.util.concurrent.locks.LockSupport) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Stream(java.util.stream.Stream) Ignore(org.junit.Ignore) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) TERMINATED(reactor.core.Scannable.Attr.TERMINATED) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) Scheduler(reactor.core.scheduler.Scheduler) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with Scheduler

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

the class StepVerifierTests method assertNextWithSubscribeOnDirectProcessor.

// see https://github.com/reactor/reactor-core/issues/959
@Test
public void assertNextWithSubscribeOnDirectProcessor() {
    Scheduler scheduler = Schedulers.newElastic("test");
    DirectProcessor<Integer> processor = DirectProcessor.create();
    Mono<Integer> doAction = Mono.fromSupplier(() -> 22).doOnNext(processor::onNext).subscribeOn(scheduler);
    assertThatExceptionOfType(AssertionError.class).isThrownBy(StepVerifier.create(processor).then(doAction::subscribe).assertNext(v -> assertThat(v).isEqualTo(23)).thenCancel()::verify);
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) UnicastProcessor(reactor.core.publisher.UnicastProcessor) TestPublisher(reactor.test.publisher.TestPublisher) Arrays(java.util.Arrays) Date(java.util.Date) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Scheduler(reactor.core.scheduler.Scheduler) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) Duration(java.time.Duration) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) ExecutorService(java.util.concurrent.ExecutorService) DirectProcessor(reactor.core.publisher.DirectProcessor) VirtualTimeScheduler(reactor.test.scheduler.VirtualTimeScheduler) Collections.emptyList(java.util.Collections.emptyList) Context(reactor.util.context.Context) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) LockSupport(java.util.concurrent.locks.LockSupport) Flux(reactor.core.publisher.Flux) List(java.util.List) Fuseable(reactor.core.Fuseable) REQUEST_OVERFLOW(reactor.test.publisher.TestPublisher.Violation.REQUEST_OVERFLOW) Assert(org.junit.Assert) Collections(java.util.Collections) Scheduler(reactor.core.scheduler.Scheduler) VirtualTimeScheduler(reactor.test.scheduler.VirtualTimeScheduler) Test(org.junit.Test)

Example 3 with Scheduler

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

the class TopicProcessorTest method testBufferSize1Shared.

// see https://github.com/reactor/reactor-core/issues/445
@Test(timeout = 5_000)
public void testBufferSize1Shared() throws Exception {
    TopicProcessor<String> broadcast = TopicProcessor.<String>builder().name("share-name").bufferSize(1).autoCancel(true).share(true).build();
    int simultaneousSubscribers = 3000;
    CountDownLatch latch = new CountDownLatch(simultaneousSubscribers);
    Scheduler scheduler = Schedulers.single();
    FluxSink<String> sink = broadcast.sink();
    Flux<String> flux = broadcast.filter(Objects::nonNull).publishOn(scheduler).cache(1);
    for (int i = 0; i < simultaneousSubscribers; i++) {
        flux.subscribe(s -> latch.countDown());
    }
    sink.next("data");
    assertThat(latch.await(4, TimeUnit.SECONDS)).overridingErrorMessage("Data not received").isTrue();
}
Also used : Scheduler(reactor.core.scheduler.Scheduler) Objects(java.util.Objects) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with Scheduler

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

the class TopicProcessorTest method testBufferSize1Created.

// see https://github.com/reactor/reactor-core/issues/445
@Test(timeout = 5_000)
public void testBufferSize1Created() throws Exception {
    TopicProcessor<String> broadcast = TopicProcessor.<String>builder().name("share-name").bufferSize(1).autoCancel(true).build();
    int simultaneousSubscribers = 3000;
    CountDownLatch latch = new CountDownLatch(simultaneousSubscribers);
    Scheduler scheduler = Schedulers.single();
    FluxSink<String> sink = broadcast.sink();
    Flux<String> flux = broadcast.filter(Objects::nonNull).publishOn(scheduler).cache(1);
    for (int i = 0; i < simultaneousSubscribers; i++) {
        flux.subscribe(s -> latch.countDown());
    }
    sink.next("data");
    assertThat(latch.await(4, TimeUnit.SECONDS)).overridingErrorMessage("Data not received").isTrue();
}
Also used : Scheduler(reactor.core.scheduler.Scheduler) Objects(java.util.Objects) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 5 with Scheduler

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

the class FluxTests method consistentMultithreadingWithPartition.

@Test
public void consistentMultithreadingWithPartition() throws InterruptedException {
    Scheduler supplier1 = Schedulers.newParallel("groupByPool", 2);
    Scheduler supplier2 = Schedulers.newParallel("partitionPool", 5);
    CountDownLatch latch = new CountDownLatch(10);
    /*Disposable c = */
    Flux.range(1, 10).groupBy(n -> n % 2 == 0).flatMap(stream -> stream.publishOn(supplier1).log("groupBy-" + stream.key())).parallel(5).runOn(supplier2).sequential().publishOn(asyncGroup).log("join").subscribe(t -> {
        latch.countDown();
    });
    latch.await(30, TimeUnit.SECONDS);
    assertThat("Not totally dispatched: " + latch.getCount(), latch.getCount() == 0);
    supplier1.dispose();
    supplier2.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) CountDownLatch(java.util.concurrent.CountDownLatch) 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