Search in sources :

Example 21 with Scheduler

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

the class EmitterProcessorTest method test.

@Test
@Ignore
public void test() {
    Scheduler asyncGroup = Schedulers.single();
    FluxProcessor<String, String> emitter = EmitterProcessor.create();
    CountDownLatch requestReceived = new CountDownLatch(1);
    AtomicLong demand = new AtomicLong(0);
    Publisher<String> publisher = s -> s.onSubscribe(new Subscription() {

        @Override
        public void request(long n) {
            System.out.println("request: " + n + " " + s);
            demand.addAndGet(n);
            requestReceived.countDown();
        }

        @Override
        public void cancel() {
            System.out.println("cancel");
        }
    });
    Flux.from(publisher).subscribeOn(asyncGroup).subscribe(emitter);
    AssertSubscriber<String> subscriber = AssertSubscriber.create();
    emitter.subscribe(subscriber);
    int i = 0;
    for (; ; ) {
        if (getAndSub(demand, 1) != 0) {
            emitter.onNext("" + (i++));
        } else {
            System.out.println("NO REQUESTED: " + emitter);
            LockSupport.parkNanos(100_000_000);
        }
    }
}
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) AtomicLong(java.util.concurrent.atomic.AtomicLong) Scheduler(reactor.core.scheduler.Scheduler) CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(org.reactivestreams.Subscription) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 22 with Scheduler

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

the class FluxProcessorTest method testSubmitSession.

@Test
public void testSubmitSession() throws Exception {
    FluxProcessor<Integer, Integer> processor = EmitterProcessor.create();
    AtomicInteger count = new AtomicInteger();
    CountDownLatch latch = new CountDownLatch(1);
    Scheduler scheduler = Schedulers.parallel();
    processor.publishOn(scheduler).delaySubscription(Duration.ofMillis(1000)).limitRate(1).subscribe(d -> {
        count.incrementAndGet();
        latch.countDown();
    });
    FluxSink<Integer> session = processor.sink();
    session.next(1);
    // System.out.println(emission);
    session.complete();
    latch.await(5, TimeUnit.SECONDS);
    Assert.assertTrue("latch : " + count, count.get() == 1);
    scheduler.dispose();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Scheduler(reactor.core.scheduler.Scheduler) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 23 with Scheduler

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

the class FluxProcessorTest method testEmitter2.

@Test
public void testEmitter2() throws Throwable {
    FluxProcessor<Integer, Integer> processor = EmitterProcessor.create();
    int n = 100_000;
    int subs = 4;
    final CountDownLatch latch = new CountDownLatch((n + 1) * subs);
    Scheduler c = Schedulers.single();
    for (int i = 0; i < subs; i++) {
        processor.publishOn(c).doOnComplete(latch::countDown).doOnNext(d -> latch.countDown()).subscribe();
    }
    FluxSink<Integer> session = processor.sink();
    for (int i = 0; i < n; i++) {
        while (session.requestedFromDownstream() == 0) {
        }
        session.next(i);
    }
    session.complete();
    boolean waited = latch.await(5, TimeUnit.SECONDS);
    Assert.assertTrue("latch : " + latch.getCount(), waited);
    c.dispose();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StepVerifier(reactor.test.StepVerifier) Scannable(reactor.core.Scannable) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AssertionsForClassTypes(org.assertj.core.api.AssertionsForClassTypes) Test(org.junit.Test) Scheduler(reactor.core.scheduler.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Schedulers(reactor.core.scheduler.Schedulers) Assert(org.junit.Assert) Subscriber(org.reactivestreams.Subscriber) Scheduler(reactor.core.scheduler.Scheduler) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 24 with Scheduler

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

the class FluxWindowTimeoutTest method scanMainSubscriber.

@Test
public void scanMainSubscriber() {
    Scheduler scheduler = new MyScheduler();
    CoreSubscriber<Flux<Integer>> actual = new LambdaSubscriber<>(null, e -> {
    }, null, null);
    FluxWindowTimeout.WindowTimeoutSubscriber<Integer> test = new FluxWindowTimeout.WindowTimeoutSubscriber<>(actual, 123, Long.MAX_VALUE, scheduler);
    Subscription parent = Operators.emptySubscription();
    test.onSubscribe(parent);
    Assertions.assertThat(test.scan(Scannable.Attr.RUN_ON)).isSameAs(scheduler.createWorker());
    Assertions.assertThat(test.scan(Scannable.Attr.PARENT)).isSameAs(parent);
    Assertions.assertThat(test.scan(Scannable.Attr.ACTUAL)).isSameAs(actual);
    Assertions.assertThat(test.scan(Scannable.Attr.CAPACITY)).isEqualTo(123);
    test.requested = 35;
    Assertions.assertThat(test.scan(Scannable.Attr.REQUESTED_FROM_DOWNSTREAM)).isEqualTo(35);
    Assertions.assertThat(test.scan(Scannable.Attr.BUFFERED)).isEqualTo(0);
    test.onNext(1);
    test.onNext(2);
    Assertions.assertThat(test.inners().findFirst().get().scan(Scannable.Attr.BUFFERED)).isEqualTo(2);
    Assertions.assertThat(test.inners().findFirst().get().scan(Scannable.Attr.CANCELLED)).isEqualTo(false);
    Assertions.assertThat(test.scan(Scannable.Attr.TERMINATED)).isFalse();
    test.onComplete();
    Assertions.assertThat(test.scan(Scannable.Attr.TERMINATED)).isTrue();
    Assertions.assertThat(test.scan(Scannable.Attr.CANCELLED)).isFalse();
    test.cancel();
    Assertions.assertThat(test.scan(Scannable.Attr.CANCELLED)).isTrue();
}
Also used : Scheduler(reactor.core.scheduler.Scheduler) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 25 with Scheduler

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

the class FluxSubscribeOnTest method gh507.

@Test
public void gh507() {
    Scheduler s = Schedulers.newSingle("subscribe");
    Scheduler s2 = Schedulers.newParallel("receive");
    AtomicBoolean interrupted = new AtomicBoolean();
    AtomicBoolean timedOut = new AtomicBoolean();
    try {
        Flux.from((Publisher<String>) subscriber -> {
            subscriber.onSubscribe(new Subscription() {

                private int totalCount;

                @Override
                public void request(long n) {
                    for (int i = 0; i < n; i++) {
                        if (totalCount++ < 317) {
                            subscriber.onNext(String.valueOf(totalCount));
                        } else {
                            subscriber.onComplete();
                        }
                    }
                }

                @Override
                public void cancel() {
                }
            });
        }).subscribeOn(s).limitRate(10).doOnNext(d -> {
            CountDownLatch latch = new CountDownLatch(1);
            Mono.fromCallable(() -> d).subscribeOn(s2).doFinally(it -> latch.countDown()).subscribe();
            try {
                if (!latch.await(5, TimeUnit.SECONDS)) {
                    timedOut.set(true);
                }
            } catch (InterruptedException e) {
                interrupted.set(true);
            }
        }).blockLast(Duration.ofSeconds(2));
        assertThat(interrupted).as("interrupted").isFalse();
        assertThat(timedOut).as("latch timeout").isFalse();
    } finally {
        s.dispose();
        s2.dispose();
    }
}
Also used : StepVerifier(reactor.test.StepVerifier) Scannable(reactor.core.Scannable) Publisher(org.reactivestreams.Publisher) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Scheduler(reactor.core.scheduler.Scheduler) Function(java.util.function.Function) Queues(reactor.util.concurrent.Queues) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) CoreSubscriber(reactor.core.CoreSubscriber) DROP(reactor.core.publisher.FluxSink.OverflowStrategy.DROP) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ForkJoinPool(java.util.concurrent.ForkJoinPool) Duration(java.time.Duration) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Schedulers(reactor.core.scheduler.Schedulers) Assert(org.junit.Assert) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Scheduler(reactor.core.scheduler.Scheduler) Subscription(org.reactivestreams.Subscription) 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