Search in sources :

Example 31 with Disposable

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

the class OldBoundedElasticScheduler 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)

Example 32 with Disposable

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

the class FluxWindowPredicateTest method windowWhileNotPropagatingCancelToSource_withConcat.

// see https://github.com/reactor/reactor-core/issues/1452
@Test
public void windowWhileNotPropagatingCancelToSource_withConcat() {
    // Similar to windowWhileNotPropagatingCancelToSource_disposeOuterFirst
    final AtomicBoolean beforeWindowWhileStageCancelled = new AtomicBoolean();
    final AtomicBoolean afterWindowWhileStageCancelled = new AtomicBoolean();
    final Flux<String> sourceFlux = Flux.<String>create(fluxSink -> fluxSink.next("0").next("#")).doOnCancel(() -> beforeWindowWhileStageCancelled.set(true));
    final Disposable disposable = sourceFlux.windowWhile(s -> !"#".equals(s)).doOnCancel(() -> afterWindowWhileStageCancelled.set(true)).as(Flux::concat).subscribe();
    disposable.dispose();
    assertThat(afterWindowWhileStageCancelled).as("afterWindowWhileStageCancelled cancelled").isTrue();
    assertThat(beforeWindowWhileStageCancelled).as("beforeWindowWhileStageCancelled cancelled").isTrue();
}
Also used : TestPublisher(reactor.test.publisher.TestPublisher) Arrays(java.util.Arrays) Disposable(reactor.core.Disposable) StepVerifier(reactor.test.StepVerifier) Scannable(reactor.core.Scannable) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MemoryUtils(reactor.test.MemoryUtils) FluxOperatorTest(reactor.test.publisher.FluxOperatorTest) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Queues(reactor.util.concurrent.Queues) CoreSubscriber(reactor.core.CoreSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) Disposables(reactor.core.Disposables) StepVerifierOptions(reactor.test.StepVerifierOptions) Mode(reactor.core.publisher.FluxBufferPredicate.Mode) Awaitility.await(org.awaitility.Awaitility.await) Predicate(java.util.function.Predicate) Context(reactor.util.context.Context) SignalType(reactor.core.publisher.SignalType) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Fuseable(reactor.core.Fuseable) Assertions.fail(org.assertj.core.api.Assertions.fail) FAIL_FAST(reactor.core.publisher.Sinks.EmitFailureHandler.FAIL_FAST) Subscription(org.reactivestreams.Subscription) Collections(java.util.Collections) Duration.ofMillis(java.time.Duration.ofMillis) Disposable(reactor.core.Disposable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FluxOperatorTest(reactor.test.publisher.FluxOperatorTest) Test(org.junit.jupiter.api.Test)

Example 33 with Disposable

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

the class FluxWindowPredicateTest method windowWhilePropagatingCancelToSource_disposeOuterFirst.

// see https://github.com/reactor/reactor-core/issues/1452
@Test
public void windowWhilePropagatingCancelToSource_disposeOuterFirst() {
    final AtomicBoolean beforeWindowWhileStageCancelled = new AtomicBoolean();
    final AtomicBoolean afterWindowWhileStageCancelled = new AtomicBoolean();
    TestPublisher<String> testPublisher = TestPublisher.create();
    final Flux<String> sourceFlux = testPublisher.flux().doOnCancel(() -> beforeWindowWhileStageCancelled.set(true));
    final AtomicInteger windowCounter = new AtomicInteger();
    final Disposable.Swap innerDisposable = Disposables.swap();
    final Disposable outerDisposable = sourceFlux.windowWhile(s -> !"#".equals(s)).doOnCancel(() -> afterWindowWhileStageCancelled.set(true)).subscribe(next -> {
        final int windowId = windowCounter.getAndIncrement();
        innerDisposable.update(next.subscribe());
    });
    testPublisher.next("1");
    // Dispose outer subscription; we should see cancellation at stage after windowWhile, but not before
    outerDisposable.dispose();
    assertThat(afterWindowWhileStageCancelled).as("afterWindowWhileStageCancelled cancelled when outer is disposed").isTrue();
    assertThat(beforeWindowWhileStageCancelled).as("beforeWindowWhileStageCancelled cancelled when outer is disposed").isFalse();
    // Dispose inner subscription; we should see cancellation propagates all the way up
    innerDisposable.dispose();
    assertThat(afterWindowWhileStageCancelled).as("afterWindowWhileStageCancelled cancelled when inner is disposed").isTrue();
    assertThat(beforeWindowWhileStageCancelled).as("beforeWindowWhileStageCancelled cancelled when inner is disposed").isTrue();
}
Also used : Disposable(reactor.core.Disposable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FluxOperatorTest(reactor.test.publisher.FluxOperatorTest) Test(org.junit.jupiter.api.Test)

Example 34 with Disposable

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

the class FluxRefCountTest method subscribersComeAndGoBelowThreshold.

@Test
public void subscribersComeAndGoBelowThreshold() {
    Flux<Integer> p = Flux.range(1, 5).publish().refCount(2);
    Disposable r = p.subscribe();
    r.dispose();
    p.subscribe().dispose();
    p.subscribe().dispose();
    p.subscribe().dispose();
    p.subscribe().dispose();
    AssertSubscriber<Integer> ts1 = AssertSubscriber.create();
    p.subscribe(ts1);
    ts1.assertValueCount(0);
    AssertSubscriber<Integer> ts2 = AssertSubscriber.create();
    p.subscribe(ts2);
    ts1.assertValues(1, 2, 3, 4, 5);
    ts2.assertValues(1, 2, 3, 4, 5);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Disposable(reactor.core.Disposable) Test(org.junit.jupiter.api.Test)

Example 35 with Disposable

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

the class FluxRefCountTest method reconnectsAfterRefCountZero.

@Test
public void reconnectsAfterRefCountZero() {
    AtomicLong subscriptionCount = new AtomicLong();
    AtomicReference<SignalType> termination = new AtomicReference<>();
    Flux<Integer> source = Flux.range(1, 50).delayElements(Duration.ofMillis(100)).doFinally(termination::set).doOnSubscribe(s -> subscriptionCount.incrementAndGet());
    Flux<Integer> refCounted = source.publish().refCount(2);
    Disposable sub1 = refCounted.subscribe();
    assertThat(subscriptionCount.get()).isZero();
    assertThat(termination.get()).isNull();
    Disposable sub2 = refCounted.subscribe();
    assertThat(subscriptionCount).hasValue(1);
    assertThat(termination.get()).isNull();
    sub1.dispose();
    assertThat(subscriptionCount).hasValue(1);
    assertThat(termination.get()).isNull();
    sub2.dispose();
    assertThat(subscriptionCount).hasValue(1);
    assertThat(termination).hasValue(SignalType.CANCEL);
    try {
        sub1 = refCounted.subscribe();
        sub2 = refCounted.subscribe();
        assertThat(subscriptionCount).hasValue(2);
    } finally {
        sub1.dispose();
        sub2.dispose();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Disposable(reactor.core.Disposable) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Aggregations

Disposable (reactor.core.Disposable)120 Test (org.junit.jupiter.api.Test)101 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