Search in sources :

Example 1 with Disposable

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

the class FluxDelayUntilTest method immediateCancel.

@Test
public void immediateCancel() {
    AtomicReference<String> value = new AtomicReference<>();
    AtomicReference<Throwable> error = new AtomicReference<>();
    Disposable s = Flux.just("foo", "bar").delayUntil(v -> Mono.just(1)).subscribe(value::set, error::set, () -> {
    }, Subscription::cancel);
    assertThat(value.get()).isNull();
    // would be a NPE if trigger array wasn't pre-initialized
    assertThat(error.get()).isNull();
}
Also used : Disposable(reactor.core.Disposable) Disposable(reactor.core.Disposable) StepVerifier(reactor.test.StepVerifier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Publisher(org.reactivestreams.Publisher) Subscription(org.reactivestreams.Subscription) 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) AtomicReference(java.util.concurrent.atomic.AtomicReference) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 2 with Disposable

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

the class EmitterProcessorTest method scanMain.

@Test
public void scanMain() {
    EmitterProcessor<Integer> test = EmitterProcessor.create(123);
    assertThat(test.scan(BUFFERED)).isEqualTo(0);
    assertThat(test.scan(CANCELLED)).isFalse();
    assertThat(test.scan(PREFETCH)).isEqualTo(123);
    assertThat(test.scan(CAPACITY)).isEqualTo(123);
    Disposable d1 = test.subscribe();
    FluxSink<Integer> sink = test.sink();
    sink.next(2);
    sink.next(3);
    sink.next(4);
    assertThat(test.scan(BUFFERED)).isEqualTo(0);
    AtomicReference<Subscription> d2 = new AtomicReference<>();
    test.subscribe(new CoreSubscriber<Integer>() {

        @Override
        public void onSubscribe(Subscription s) {
            d2.set(s);
        }

        @Override
        public void onNext(Integer integer) {
        }

        @Override
        public void onError(Throwable t) {
        }

        @Override
        public void onComplete() {
        }
    });
    sink.next(5);
    sink.next(6);
    sink.next(7);
    assertThat(test.scan(BUFFERED)).isEqualTo(3);
    assertThat(test.scan(TERMINATED)).isFalse();
    sink.complete();
    assertThat(test.scan(TERMINATED)).isFalse();
    d1.dispose();
    d2.get().cancel();
    assertThat(test.scan(TERMINATED)).isTrue();
    // other values
    assertThat(test.scan(Scannable.Attr.PARENT)).isNotNull();
    assertThat(test.scan(Attr.ERROR)).isNull();
}
Also used : Disposable(reactor.core.Disposable) AtomicReference(java.util.concurrent.atomic.AtomicReference) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 3 with Disposable

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

the class EmitterProcessorTest method state.

@Test
public void state() {
    EmitterProcessor<Integer> tp = EmitterProcessor.create();
    assertThat(tp.getPending()).isEqualTo(0);
    assertThat(tp.getBufferSize()).isEqualTo(Queues.SMALL_BUFFER_SIZE);
    assertThat(tp.isCancelled()).isFalse();
    assertThat(tp.inners()).isEmpty();
    Disposable d1 = tp.subscribe();
    assertThat(tp.inners()).hasSize(1);
    FluxSink<Integer> s = tp.sink();
    s.next(2);
    s.next(3);
    s.next(4);
    assertThat(tp.getPending()).isEqualTo(0);
    AtomicReference<Subscription> d2 = new AtomicReference<>();
    tp.subscribe(new CoreSubscriber<Integer>() {

        @Override
        public void onSubscribe(Subscription s) {
            d2.set(s);
        }

        @Override
        public void onNext(Integer integer) {
        }

        @Override
        public void onError(Throwable t) {
        }

        @Override
        public void onComplete() {
        }
    });
    s.next(5);
    s.next(6);
    s.next(7);
    assertThat(tp.scan(BUFFERED)).isEqualTo(3);
    assertThat(tp.isTerminated()).isFalse();
    s.complete();
    assertThat(tp.isTerminated()).isFalse();
    d1.dispose();
    d2.get().cancel();
    assertThat(tp.isTerminated()).isTrue();
    StepVerifier.create(tp).verifyComplete();
    // noop
    tp.onNext(8);
    EmitterProcessor<Void> empty = EmitterProcessor.create();
    empty.onComplete();
    assertThat(empty.isTerminated()).isTrue();
}
Also used : Disposable(reactor.core.Disposable) AtomicReference(java.util.concurrent.atomic.AtomicReference) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 4 with Disposable

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

the class PublisherProbeTest method assertWasNotCancelledMono.

@Test
public void assertWasNotCancelledMono() {
    PublisherProbe<Void> probe = PublisherProbe.of(Mono.never());
    Disposable d = probe.mono().subscribe();
    probe.assertWasNotCancelled();
    d.dispose();
    assertThatExceptionOfType(AssertionError.class).isThrownBy(probe::assertWasNotCancelled).withMessage("PublisherProbe should not have been cancelled but it was");
}
Also used : Disposable(reactor.core.Disposable) Test(org.junit.Test)

Example 5 with Disposable

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

the class PublisherProbeTest method wasCancelledMono.

@Test
public void wasCancelledMono() {
    PublisherProbe<Void> probe = PublisherProbe.of(Mono.never());
    Disposable d = probe.mono().subscribe();
    assertThat(probe.wasCancelled()).isFalse();
    d.dispose();
    assertThat(probe.wasCancelled()).isTrue();
}
Also used : Disposable(reactor.core.Disposable) Test(org.junit.Test)

Aggregations

Disposable (reactor.core.Disposable)77 Test (org.junit.Test)52 StepVerifier (reactor.test.StepVerifier)24 CountDownLatch (java.util.concurrent.CountDownLatch)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 Test (org.junit.jupiter.api.Test)18 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)15 Subscription (org.reactivestreams.Subscription)11 List (java.util.List)10 TimeUnit (java.util.concurrent.TimeUnit)9 Duration (java.time.Duration)8 Arrays (java.util.Arrays)8 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 ArrayList (java.util.ArrayList)7 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)7 Function (java.util.function.Function)7 IOException (java.io.IOException)6 Instant (java.time.Instant)6