Search in sources :

Example 81 with Disposable

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

the class MonoCacheTimeTest method partialCancelDoesntCancelSource.

@Test
public void partialCancelDoesntCancelSource() {
    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();
    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 82 with Disposable

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

the class MonoCacheTimeTest method noTtlCancelDoesntCancelSource.

@Test
public void noTtlCancelDoesntCancelSource() {
    AtomicInteger cancelled = new AtomicInteger();
    Mono<Object> cached = new MonoCacheTime<>(Mono.never().doOnCancel(cancelled::incrementAndGet));
    Disposable d1 = cached.subscribe();
    Disposable d2 = cached.subscribe();
    d1.dispose();
    assertThat(cancelled.get()).as("when cancelling d1").isEqualTo(0);
    d2.dispose();
    assertThat(cancelled.get()).as("when both cancelled").isEqualTo(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 83 with Disposable

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

the class FluxRefCount method cancel.

void cancel(RefCountMonitor rc) {
    Disposable dispose = null;
    synchronized (this) {
        if (rc.terminated) {
            return;
        }
        long c = rc.subscribers - 1;
        rc.subscribers = c;
        if (c != 0L || !rc.connected) {
            return;
        }
        if (rc == connection) {
            dispose = RefCountMonitor.DISCONNECT.getAndSet(rc, Disposables.disposed());
            connection = null;
        }
    }
    if (dispose != null) {
        dispose.dispose();
    }
}
Also used : Disposable(reactor.core.Disposable)

Example 84 with Disposable

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

the class BaseSubscriberTest method disposeCancels.

@Test
public void disposeCancels() throws InterruptedException {
    AtomicReference<SignalType> onFinally = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    BaseSubscriber<Long> sub = new BaseSubscriber<Long>() {

        @Override
        protected void hookOnSubscribe(Subscription subscription) {
            requestUnbounded();
        }

        @Override
        protected void hookOnNext(Long value) {
            fail("delay was not cancelled");
        }

        @Override
        protected void hookFinally(SignalType type) {
            onFinally.set(type);
            latch.countDown();
        }
    };
    Disposable d = Mono.delay(Duration.ofSeconds(1)).subscribeWith(sub);
    d.dispose();
    assertThat(latch.await(1500, TimeUnit.MILLISECONDS)).as("delay should be skipped by cancel").isTrue();
    assertThat(onFinally).hasValue(SignalType.CANCEL);
}
Also used : Disposable(reactor.core.Disposable) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(org.reactivestreams.Subscription) Test(org.junit.jupiter.api.Test)

Example 85 with Disposable

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

the class BoundedElasticScheduler method schedulePeriodically.

@Override
public Disposable schedulePeriodically(Runnable task, long initialDelay, long period, TimeUnit unit) {
    final BoundedState picked = BOUNDED_SERVICES.get(this).pick();
    Disposable scheduledTask = Schedulers.directSchedulePeriodically(picked.executor, task, initialDelay, period, unit);
    // (ie decreases its usage by one)
    return Disposables.composite(scheduledTask, picked);
}
Also used : Disposable(reactor.core.Disposable)

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