Search in sources :

Example 51 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.jupiter.api.Test)

Example 52 with Disposable

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

the class MonoCacheTimeTest method totalCancelDoesntCancelSource.

@Test
public void totalCancelDoesntCancelSource() {
    AtomicInteger cancelled = new AtomicInteger();
    Mono<Object> cached = Mono.never().doOnCancel(cancelled::incrementAndGet).cache(Duration.ofMillis(200));
    Disposable d1 = cached.subscribe();
    Disposable d2 = cached.subscribe();
    d1.dispose();
    d2.dispose();
    assertThat(cancelled).hasValue(0);
}
Also used : Disposable(reactor.core.Disposable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MonoOperatorTest(reactor.test.publisher.MonoOperatorTest) Test(org.junit.jupiter.api.Test)

Example 53 with Disposable

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

the class MonoCacheInvalidateWhenTest method cancellingAllSubscribersBeforeOnNextInvalidates.

@Test
void cancellingAllSubscribersBeforeOnNextInvalidates() {
    TestPublisher<Integer> source = TestPublisher.create();
    Mono<Integer> cached = source.mono().cacheInvalidateWhen(i -> Mono.never());
    Disposable sub1 = cached.subscribe();
    Disposable sub2 = cached.subscribe();
    Disposable sub3 = cached.subscribe();
    source.assertWasSubscribed();
    source.assertNotCancelled();
    sub1.dispose();
    source.assertNotCancelled();
    sub2.dispose();
    source.assertNotCancelled();
    sub3.dispose();
    source.assertCancelled(1);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Disposable(reactor.core.Disposable) Test(org.junit.jupiter.api.Test)

Example 54 with Disposable

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

the class MonoCacheInvalidateIfTest method cancellingAllSubscribersBeforeOnNextInvalidates.

@Test
void cancellingAllSubscribersBeforeOnNextInvalidates() {
    TestPublisher<Integer> source = TestPublisher.create();
    Mono<Integer> cached = source.mono().cacheInvalidateIf(i -> false);
    Disposable sub1 = cached.subscribe();
    Disposable sub2 = cached.subscribe();
    Disposable sub3 = cached.subscribe();
    source.assertWasSubscribed();
    source.assertNotCancelled();
    sub1.dispose();
    source.assertNotCancelled();
    sub2.dispose();
    source.assertNotCancelled();
    sub3.dispose();
    source.assertCancelled(1);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Disposable(reactor.core.Disposable) Test(org.junit.jupiter.api.Test)

Example 55 with Disposable

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

the class MonoCacheTimeTest method totalCancelCanResubscribe.

@Test
public void totalCancelCanResubscribe() {
    AtomicInteger cancelled = new AtomicInteger();
    AtomicInteger subscribed = new AtomicInteger();
    TestPublisher<Integer> source = TestPublisher.create();
    Mono<Integer> cached = source.mono().doOnSubscribe(s -> subscribed.incrementAndGet()).doOnCancel(cancelled::incrementAndGet).cache(Duration.ofMillis(200));
    Disposable d1 = cached.subscribe();
    Disposable d2 = cached.subscribe();
    d1.dispose();
    d2.dispose();
    assertThat(cancelled).hasValue(0);
    assertThat(subscribed).hasValue(1);
    StepVerifier.create(cached).then(() -> source.emit(100)).expectNext(100).verifyComplete();
    assertThat(cancelled).hasValue(0);
    assertThat(subscribed).hasValue(1);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Disposable(reactor.core.Disposable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MonoOperatorTest(reactor.test.publisher.MonoOperatorTest) Test(org.junit.jupiter.api.Test)

Aggregations

Disposable (reactor.core.Disposable)118 Test (org.junit.jupiter.api.Test)99 CountDownLatch (java.util.concurrent.CountDownLatch)33 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)32 StepVerifier (reactor.test.StepVerifier)29 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)25 AtomicReference (java.util.concurrent.atomic.AtomicReference)18 Duration (java.time.Duration)15 List (java.util.List)15 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)15 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)15 Subscription (org.reactivestreams.Subscription)15 TimeUnit (java.util.concurrent.TimeUnit)14 Timeout (org.junit.jupiter.api.Timeout)13 CoreSubscriber (reactor.core.CoreSubscriber)12 ArrayList (java.util.ArrayList)11 Arrays (java.util.Arrays)11 Disabled (org.junit.jupiter.api.Disabled)11 Scannable (reactor.core.Scannable)11 Fuseable (reactor.core.Fuseable)10