Search in sources :

Example 1 with Schedulers

use of reactor.core.scheduler.Schedulers 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)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 LockSupport (java.util.concurrent.locks.LockSupport)1 Stream (java.util.stream.Stream)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assert (org.junit.Assert)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1 Processor (org.reactivestreams.Processor)1 Publisher (org.reactivestreams.Publisher)1 Subscriber (org.reactivestreams.Subscriber)1 Subscription (org.reactivestreams.Subscription)1 CoreSubscriber (reactor.core.CoreSubscriber)1 Disposable (reactor.core.Disposable)1