Search in sources :

Example 46 with AssertSubscriber

use of reactor.test.subscriber.AssertSubscriber in project reactor-core by reactor.

the class FluxDoOnEachTest method nextCallbackBubbleError.

@Test
public void nextCallbackBubbleError() {
    AssertSubscriber<Integer> ts = AssertSubscriber.create();
    LongAdder state = new LongAdder();
    Throwable err = new Exception("test");
    try {
        Flux.just(1).doOnEach(s -> {
            if (s.isOnNext()) {
                state.increment();
                throw Exceptions.bubble(err);
            }
        }).subscribe(ts);
        fail();
    } catch (Exception e) {
        Assert.assertTrue(Exceptions.unwrap(e) == err);
        Assert.assertEquals(1, state.intValue());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LongAdder(java.util.concurrent.atomic.LongAdder) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Scannable(reactor.core.Scannable) Context(reactor.util.context.Context) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) List(java.util.List) CoreSubscriber(reactor.core.CoreSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Schedulers(reactor.core.scheduler.Schedulers) Assert.fail(org.junit.Assert.fail) Assert(org.junit.Assert) Exceptions(reactor.core.Exceptions) StepVerifierOptions(reactor.test.StepVerifierOptions) LongAdder(java.util.concurrent.atomic.LongAdder) Test(org.junit.Test)

Example 47 with AssertSubscriber

use of reactor.test.subscriber.AssertSubscriber 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 48 with AssertSubscriber

use of reactor.test.subscriber.AssertSubscriber in project reactor-core by reactor.

the class EmitterProcessorTest method testPerformance.

@Test
@Ignore
public void testPerformance() {
    FluxProcessor<String, String> emitter = EmitterProcessor.create();
    CountDownLatch requestReceived = new CountDownLatch(1);
    AtomicLong maxDelay = new AtomicLong(0);
    AtomicLong demand = new AtomicLong(0);
    Publisher<String> publisher = new Publisher<String>() {

        long lastTimeRequestReceivedNs = -1;

        @Override
        public void subscribe(Subscriber<? super String> s) {
            s.onSubscribe(new Subscription() {

                @Override
                public void request(long n) {
                    requestReceived.countDown();
                    long now = System.nanoTime();
                    if (lastTimeRequestReceivedNs > 0) {
                        maxDelay.set(now - lastTimeRequestReceivedNs);
                    }
                    lastTimeRequestReceivedNs = now;
                    demand.addAndGet(n);
                }

                @Override
                public void cancel() {
                    System.out.println("cancel");
                }
            });
        }
    };
    publisher.subscribe(emitter);
    AssertSubscriber<String> subscriber = AssertSubscriber.create();
    emitter.subscribe(subscriber);
    String buffer = "Hello";
    int i = 0;
    for (; ; ) {
        if (getAndSub(demand, 1) > 0) {
            emitter.onNext(buffer);
        }
        if (i++ % 1000000 == 0) {
            System.out.println("maxDelay: " + TimeUnit.MICROSECONDS.toMillis(maxDelay.get()) + " µs");
        }
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) CoreSubscriber(reactor.core.CoreSubscriber) Subscriber(org.reactivestreams.Subscriber) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Publisher(org.reactivestreams.Publisher) CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(org.reactivestreams.Subscription) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 49 with AssertSubscriber

use of reactor.test.subscriber.AssertSubscriber in project reactor-core by reactor.

the class FluxPeekFuseableTest method resumeFuseable.

@Test
public void resumeFuseable() {
    RuntimeException nextError = new IllegalStateException("next");
    List<Throwable> resumedErrors = new ArrayList<>();
    List<Object> resumedValues = new ArrayList<>();
    Context context = Context.of(OnNextFailureStrategy.KEY_ON_NEXT_ERROR_STRATEGY, OnNextFailureStrategy.resume((t, s) -> {
        resumedErrors.add(t);
        resumedValues.add(s);
    }));
    AssertSubscriber<Integer> actual = new AssertSubscriber<>(context, 0);
    SignalPeekThrowNext<Integer> peekParent = new SignalPeekThrowNext<>(nextError);
    AssertQueueSubscription<Integer> qs = new AssertQueueSubscription<>();
    PeekFuseableSubscriber<Integer> test = new PeekFuseableSubscriber<>(actual, peekParent);
    test.onSubscribe(qs);
    test.onNext(1);
    actual.assertNoValues();
    assertThat(qs.requested).as("onNext requested more").isEqualTo(1);
    qs.offer(3);
    Integer polled = test.poll();
    assertThat(polled).as("poll skips").isNull();
    test.onComplete();
    actual.assertNoValues();
    actual.assertNoError();
    actual.assertComplete();
    assertThat(resumedErrors).containsExactly(nextError, nextError);
    assertThat(resumedValues).containsExactly(1, 3);
}
Also used : Context(reactor.util.context.Context) PeekConditionalSubscriber(reactor.core.publisher.FluxPeekFuseable.PeekConditionalSubscriber) CoreMatchers(org.hamcrest.CoreMatchers) StepVerifier(reactor.test.StepVerifier) Scannable(reactor.core.Scannable) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Queues(reactor.util.concurrent.Queues) CoreSubscriber(reactor.core.CoreSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Schedulers(reactor.core.scheduler.Schedulers) Thread.sleep(java.lang.Thread.sleep) MockUtils(reactor.test.MockUtils) Nullable(javax.annotation.Nullable) PeekFuseableSubscriber(reactor.core.publisher.FluxPeekFuseable.PeekFuseableSubscriber) Context(reactor.util.context.Context) Test(org.junit.Test) Schedulers.parallel(reactor.core.scheduler.Schedulers.parallel) LongConsumer(java.util.function.LongConsumer) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Mockito(org.mockito.Mockito) List(java.util.List) Fuseable(reactor.core.Fuseable) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Queue(java.util.Queue) Assert(org.junit.Assert) Exceptions(reactor.core.Exceptions) Collections(java.util.Collections) PeekFuseableConditionalSubscriber(reactor.core.publisher.FluxPeekFuseable.PeekFuseableConditionalSubscriber) ArrayList(java.util.ArrayList) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PeekFuseableSubscriber(reactor.core.publisher.FluxPeekFuseable.PeekFuseableSubscriber) Test(org.junit.Test)

Example 50 with AssertSubscriber

use of reactor.test.subscriber.AssertSubscriber in project reactor-core by reactor.

the class FluxPeekFuseableTest method asyncFusionAvailable.

@Test
public void asyncFusionAvailable() {
    AssertSubscriber<Integer> ts = AssertSubscriber.create();
    UnicastProcessor.create(Queues.<Integer>get(2).get()).doOnNext(v -> {
    }).subscribe(ts);
    Subscription s = ts.upstream();
    Assert.assertTrue("Non-fuseable upstream" + s, s instanceof Fuseable.QueueSubscription);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PeekConditionalSubscriber(reactor.core.publisher.FluxPeekFuseable.PeekConditionalSubscriber) CoreMatchers(org.hamcrest.CoreMatchers) StepVerifier(reactor.test.StepVerifier) Scannable(reactor.core.Scannable) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Queues(reactor.util.concurrent.Queues) CoreSubscriber(reactor.core.CoreSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Schedulers(reactor.core.scheduler.Schedulers) Thread.sleep(java.lang.Thread.sleep) MockUtils(reactor.test.MockUtils) Nullable(javax.annotation.Nullable) PeekFuseableSubscriber(reactor.core.publisher.FluxPeekFuseable.PeekFuseableSubscriber) Context(reactor.util.context.Context) Test(org.junit.Test) Schedulers.parallel(reactor.core.scheduler.Schedulers.parallel) LongConsumer(java.util.function.LongConsumer) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Mockito(org.mockito.Mockito) List(java.util.List) Fuseable(reactor.core.Fuseable) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Queue(java.util.Queue) Assert(org.junit.Assert) Exceptions(reactor.core.Exceptions) Collections(java.util.Collections) PeekFuseableConditionalSubscriber(reactor.core.publisher.FluxPeekFuseable.PeekFuseableConditionalSubscriber) Subscription(org.reactivestreams.Subscription) Fuseable(reactor.core.Fuseable) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)65 AssertSubscriber (reactor.test.subscriber.AssertSubscriber)65 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)52 StepVerifier (reactor.test.StepVerifier)51 List (java.util.List)49 CoreSubscriber (reactor.core.CoreSubscriber)46 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)44 Assert (org.junit.Assert)43 Scannable (reactor.core.Scannable)41 Arrays (java.util.Arrays)40 Subscription (org.reactivestreams.Subscription)37 ArrayList (java.util.ArrayList)33 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)33 Schedulers (reactor.core.scheduler.Schedulers)32 AtomicReference (java.util.concurrent.atomic.AtomicReference)27 Duration (java.time.Duration)21 Fuseable (reactor.core.Fuseable)21 FluxOperatorTest (reactor.test.publisher.FluxOperatorTest)20 Exceptions (reactor.core.Exceptions)19 Queues (reactor.util.concurrent.Queues)19