Search in sources :

Example 1 with CoreSubscriber

use of reactor.core.CoreSubscriber in project reactor-core by reactor.

the class DefaultTestPublisherTests method normalIgnoresMultipleTerminations.

@Test
public void normalIgnoresMultipleTerminations() {
    TestPublisher<String> publisher = TestPublisher.create();
    AtomicLong count = new AtomicLong();
    Subscriber<String> subscriber = new CoreSubscriber<String>() {

        @Override
        public void onSubscribe(Subscription s) {
        }

        @Override
        public void onNext(String s) {
        }

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

        @Override
        public void onComplete() {
            count.incrementAndGet();
        }
    };
    publisher.subscribe(subscriber);
    publisher.complete().emit("A", "B", "C").error(new IllegalStateException("boom"));
    assertThat(count.get()).isEqualTo(1);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) CoreSubscriber(reactor.core.CoreSubscriber) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 2 with CoreSubscriber

use of reactor.core.CoreSubscriber in project reactor-core by reactor.

the class ParallelSourceTest method scanMainSubscriber.

@Test
public void scanMainSubscriber() {
    @SuppressWarnings("unchecked") CoreSubscriber<String>[] subs = new CoreSubscriber[1];
    subs[0] = new LambdaSubscriber<>(null, e -> {
    }, null, null);
    ParallelSource.ParallelSourceMain<String> test = new ParallelSource.ParallelSourceMain<>(subs, 123, Queues.one());
    Subscription parent = Operators.emptySubscription();
    test.onSubscribe(parent);
    assertThat(test.scan(Scannable.Attr.PARENT)).isSameAs(parent);
    assertThat(test.scan(Scannable.Attr.PREFETCH)).isEqualTo(123);
    assertThat(test.scan(Scannable.Attr.BUFFERED)).isZero();
    test.queue.offer("foo");
    assertThat(test.scan(Scannable.Attr.BUFFERED)).isEqualTo(1);
    assertThat(test.scan(Scannable.Attr.TERMINATED)).isFalse();
    assertThat(test.scan(Scannable.Attr.CANCELLED)).isFalse();
    assertThat(test.scan(Scannable.Attr.ERROR)).isNull();
    test.onError(new IllegalStateException("boom"));
    assertThat(test.scan(Scannable.Attr.TERMINATED)).isTrue();
    assertThat(test.scan(Scannable.Attr.CANCELLED)).isFalse();
    assertThat(test.scan(Scannable.Attr.ERROR)).hasMessage("boom");
    test.cancel();
    assertThat(test.scan(Scannable.Attr.CANCELLED)).isTrue();
}
Also used : CoreSubscriber(reactor.core.CoreSubscriber) Scannable(reactor.core.Scannable) Subscription(org.reactivestreams.Subscription) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Queues(reactor.util.concurrent.Queues) CoreSubscriber(reactor.core.CoreSubscriber) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 3 with CoreSubscriber

use of reactor.core.CoreSubscriber in project reactor-core by reactor.

the class FluxGroupByTest method twoGroupsConsumeWithSubscribePrefetchSmaller.

@Test
public void twoGroupsConsumeWithSubscribePrefetchSmaller() {
    ForkJoinPool forkJoinPool = new ForkJoinPool();
    AssertSubscriber<Integer> ts1 = AssertSubscriber.create();
    AssertSubscriber<Integer> ts2 = AssertSubscriber.create();
    AssertSubscriber<Integer> ts3 = AssertSubscriber.create();
    ts3.onSubscribe(Operators.emptySubscription());
    Flux.range(0, 1_000_000).groupBy(v -> v & 1).subscribe(new CoreSubscriber<GroupedFlux<Integer, Integer>>() {

        @Override
        public void onSubscribe(Subscription s) {
            s.request(Long.MAX_VALUE);
        }

        @Override
        public void onNext(GroupedFlux<Integer, Integer> t) {
            if (t.key() == 0) {
                t.publishOn(Schedulers.fromExecutorService(forkJoinPool), 32).subscribe(ts1);
            } else {
                t.publishOn(Schedulers.fromExecutorService(forkJoinPool), 32).subscribe(ts2);
            }
        }

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

        @Override
        public void onComplete() {
            ts3.onComplete();
        }
    });
    if (!ts1.await(Duration.ofSeconds(5)).isTerminated()) {
        Assert.fail("main subscriber timed out");
    }
    if (!ts2.await(Duration.ofSeconds(5)).isTerminated()) {
        Assert.fail("group 0 subscriber timed out");
    }
    if (!ts3.await(Duration.ofSeconds(5)).isTerminated()) {
        Assert.fail("group 1 subscriber timed out");
    }
    ts1.assertValueCount(500_000).assertNoError().assertComplete();
    ts2.assertValueCount(500_000).assertNoError().assertComplete();
    ts3.assertNoValues().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) FluxOperatorTest(reactor.test.publisher.FluxOperatorTest) Queues(reactor.util.concurrent.Queues) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) CoreSubscriber(reactor.core.CoreSubscriber) Fuseable(reactor.core.Fuseable) Ignore(org.junit.Ignore) 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) Subscription(org.reactivestreams.Subscription) ForkJoinPool(java.util.concurrent.ForkJoinPool) Test(org.junit.Test) FluxOperatorTest(reactor.test.publisher.FluxOperatorTest)

Example 4 with CoreSubscriber

use of reactor.core.CoreSubscriber in project reactor-core by reactor.

the class FluxGroupByTest method twoGroupsConsumeWithSubscribePrefetchBigger.

@Test
public void twoGroupsConsumeWithSubscribePrefetchBigger() {
    ForkJoinPool forkJoinPool = new ForkJoinPool();
    AssertSubscriber<Integer> ts1 = AssertSubscriber.create();
    AssertSubscriber<Integer> ts2 = AssertSubscriber.create();
    AssertSubscriber<Integer> ts3 = AssertSubscriber.create();
    ts3.onSubscribe(Operators.emptySubscription());
    Flux.range(0, 1_000_000).groupBy(v -> v & 1).subscribe(new CoreSubscriber<GroupedFlux<Integer, Integer>>() {

        @Override
        public void onSubscribe(Subscription s) {
            s.request(Long.MAX_VALUE);
        }

        @Override
        public void onNext(GroupedFlux<Integer, Integer> t) {
            if (t.key() == 0) {
                t.publishOn(Schedulers.fromExecutorService(forkJoinPool), 1024).subscribe(ts1);
            } else {
                t.publishOn(Schedulers.fromExecutorService(forkJoinPool), 1024).subscribe(ts2);
            }
        }

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

        @Override
        public void onComplete() {
            ts3.onComplete();
        }
    });
    if (!ts1.await(Duration.ofSeconds(5)).isTerminated()) {
        Assert.fail("main subscriber timed out");
    }
    if (!ts2.await(Duration.ofSeconds(5)).isTerminated()) {
        Assert.fail("group 0 subscriber timed out");
    }
    if (!ts3.await(Duration.ofSeconds(5)).isTerminated()) {
        Assert.fail("group 1 subscriber timed out");
    }
    ts1.assertValueCount(500_000).assertNoError().assertComplete();
    ts2.assertValueCount(500_000).assertNoError().assertComplete();
    ts3.assertNoValues().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) FluxOperatorTest(reactor.test.publisher.FluxOperatorTest) Queues(reactor.util.concurrent.Queues) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) CoreSubscriber(reactor.core.CoreSubscriber) Fuseable(reactor.core.Fuseable) Ignore(org.junit.Ignore) 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) Subscription(org.reactivestreams.Subscription) ForkJoinPool(java.util.concurrent.ForkJoinPool) Test(org.junit.Test) FluxOperatorTest(reactor.test.publisher.FluxOperatorTest)

Example 5 with CoreSubscriber

use of reactor.core.CoreSubscriber in project reactor-core by reactor.

the class FluxGroupByTest method twoGroupsFullAsyncFullHide.

@Test
@Ignore("temporarily disabled, see gh-1028")
public void twoGroupsFullAsyncFullHide() {
    ForkJoinPool forkJoinPool = new ForkJoinPool();
    AssertSubscriber<Integer> ts1 = AssertSubscriber.create();
    AssertSubscriber<Integer> ts2 = AssertSubscriber.create();
    AssertSubscriber<Integer> ts3 = AssertSubscriber.create();
    ts3.onSubscribe(Operators.emptySubscription());
    Flux.range(0, 1_000_000).hide().publishOn(Schedulers.fromExecutorService(forkJoinPool)).groupBy(v -> v & 1).subscribe(new CoreSubscriber<GroupedFlux<Integer, Integer>>() {

        @Override
        public void onSubscribe(Subscription s) {
            s.request(Long.MAX_VALUE);
        }

        @Override
        public void onNext(GroupedFlux<Integer, Integer> t) {
            if (t.key() == 0) {
                t.hide().publishOn(Schedulers.fromExecutorService(forkJoinPool)).subscribe(ts1);
            } else {
                t.hide().publishOn(Schedulers.fromExecutorService(forkJoinPool)).subscribe(ts2);
            }
        }

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

        @Override
        public void onComplete() {
            ts3.onComplete();
        }
    });
    ts1.await(Duration.ofSeconds(5));
    ts2.await(Duration.ofSeconds(5));
    ts3.await(Duration.ofSeconds(5));
    ts1.assertValueCount(500_000).assertNoError().assertComplete();
    ts2.assertValueCount(500_000).assertNoError().assertComplete();
    ts3.assertNoValues().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) FluxOperatorTest(reactor.test.publisher.FluxOperatorTest) Queues(reactor.util.concurrent.Queues) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) CoreSubscriber(reactor.core.CoreSubscriber) Fuseable(reactor.core.Fuseable) Ignore(org.junit.Ignore) 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) Subscription(org.reactivestreams.Subscription) ForkJoinPool(java.util.concurrent.ForkJoinPool) Ignore(org.junit.Ignore) Test(org.junit.Test) FluxOperatorTest(reactor.test.publisher.FluxOperatorTest)

Aggregations

CoreSubscriber (reactor.core.CoreSubscriber)10 Test (org.junit.Test)9 Subscription (org.reactivestreams.Subscription)9 AtomicLong (java.util.concurrent.atomic.AtomicLong)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 Scannable (reactor.core.Scannable)7 Queues (reactor.util.concurrent.Queues)7 Duration (java.time.Duration)6 Arrays (java.util.Arrays)6 List (java.util.List)6 ForkJoinPool (java.util.concurrent.ForkJoinPool)6 Assert (org.junit.Assert)6 Ignore (org.junit.Ignore)6 Fuseable (reactor.core.Fuseable)6 Schedulers (reactor.core.scheduler.Schedulers)6 StepVerifier (reactor.test.StepVerifier)6 FluxOperatorTest (reactor.test.publisher.FluxOperatorTest)6 AssertSubscriber (reactor.test.subscriber.AssertSubscriber)6 Worker (reactor.core.scheduler.Scheduler.Worker)1