Search in sources :

Example 76 with Disposable

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

the class OperatorDisposablesTest method trySet.

@Test
public void trySet() {
    TestDisposable r = new TestDisposable();
    Disposable d1 = Disposables.single();
    assertThat(OperatorDisposables.trySet(DISPOSABLE_UPDATER, r, d1)).isTrue();
    Disposable d2 = Disposables.single();
    assertThat(OperatorDisposables.trySet(DISPOSABLE_UPDATER, r, d2)).isFalse();
    assertThat(d1.isDisposed()).isFalse();
    assertThat(d2.isDisposed()).isFalse();
    OperatorDisposables.dispose(DISPOSABLE_UPDATER, r);
    Disposable d3 = Disposables.single();
    assertThat(OperatorDisposables.trySet(DISPOSABLE_UPDATER, r, d3)).isFalse();
    assertThat(d3.isDisposed()).isTrue();
}
Also used : Disposable(reactor.core.Disposable) Test(org.junit.jupiter.api.Test) RepeatedTest(org.junit.jupiter.api.RepeatedTest)

Example 77 with Disposable

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

the class FluxWindowPredicateTest method windowWhileNotPropagatingCancelToSource_disposeInnerFirst.

// see https://github.com/reactor/reactor-core/issues/1452
@Test
public void windowWhileNotPropagatingCancelToSource_disposeInnerFirst() {
    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 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());
    });
    // Dispose inner subscription, outer Flux at before/after the windowWhile stage should not be cancelled yet
    innerDisposable.dispose();
    assertThat(afterWindowWhileStageCancelled).as("afterWindowWhileStageCancelled cancelled when inner is disposed").isFalse();
    assertThat(beforeWindowWhileStageCancelled).as("beforeWindowWhileStageCancelled cancelled when inner is disposed").isFalse();
    // Dispose outer subscription; we should see cancellation propagates all the way up
    outerDisposable.dispose();
    assertThat(afterWindowWhileStageCancelled).as("afterWindowWhileStageCancelled cancelled when outer is disposed").isTrue();
    assertThat(beforeWindowWhileStageCancelled).as("beforeWindowWhileStageCancelled cancelled when outer is disposed").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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FluxOperatorTest(reactor.test.publisher.FluxOperatorTest) Test(org.junit.jupiter.api.Test)

Example 78 with Disposable

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

the class FluxUsingWhenTest method blockOnNeverResourceCanBeCancelled.

@Test
public void blockOnNeverResourceCanBeCancelled() throws InterruptedException {
    CountDownLatch latch = new CountDownLatch(1);
    Disposable disposable = Flux.usingWhen(Flux.<String>never(), Flux::just, Flux::just, (res, err) -> Flux.just(res), Flux::just).doFinally(f -> latch.countDown()).subscribe();
    assertThat(latch.await(500, TimeUnit.MILLISECONDS)).as("hangs before dispose").isFalse();
    disposable.dispose();
    assertThat(latch.await(100, TimeUnit.MILLISECONDS)).as("terminates after dispose").isTrue();
}
Also used : Disposable(reactor.core.Disposable) LongAdder(java.util.concurrent.atomic.LongAdder) TestPublisher(reactor.test.publisher.TestPublisher) Disposable(reactor.core.Disposable) StepVerifier(reactor.test.StepVerifier) TestLogger(reactor.test.util.TestLogger) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UsingWhenSubscriber(reactor.core.publisher.FluxUsingWhen.UsingWhenSubscriber) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Tuple2(reactor.util.function.Tuple2) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Nullable(reactor.util.annotation.Nullable) Level(java.util.logging.Level) CoreSubscriber(reactor.core.CoreSubscriber) Loggers(reactor.util.Loggers) Attr(reactor.core.Scannable.Attr) ResourceSubscriber(reactor.core.publisher.FluxUsingWhen.ResourceSubscriber) Duration(java.time.Duration) Arguments.arguments(org.junit.jupiter.params.provider.Arguments.arguments) MethodSource(org.junit.jupiter.params.provider.MethodSource) ParameterizedTestWithName(reactor.test.ParameterizedTestWithName) Assertions.assertThatNullPointerException(org.assertj.core.api.Assertions.assertThatNullPointerException) Publisher(org.reactivestreams.Publisher) Context(reactor.util.context.Context) Arguments(org.junit.jupiter.params.provider.Arguments) PublisherProbe(reactor.test.publisher.PublisherProbe) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) Fuseable(reactor.core.Fuseable) Stream(java.util.stream.Stream) Subscription(org.reactivestreams.Subscription) Named.named(org.junit.jupiter.api.Named.named) Awaitility(org.awaitility.Awaitility) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 79 with Disposable

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

the class MonoCollectListTest method cancelPropagatesEvenOnEmptySource.

@Test
// See https://github.com/reactor/reactor-core/issues/2519
void cancelPropagatesEvenOnEmptySource() {
    AtomicBoolean cancel1 = new AtomicBoolean();
    AtomicBoolean cancel2 = new AtomicBoolean();
    Flux<?> publisher = Flux.never().hide().doOnCancel(() -> cancel1.set(true)).collectList().doOnCancel(() -> cancel2.set(true)).flatMapIterable(Function.identity());
    Disposable d = publisher.subscribe();
    d.dispose();
    SoftAssertions.assertSoftly(softly -> {
        softly.assertThat(cancel1).as("cancel1").isTrue();
        softly.assertThat(cancel2).as("cancel2").isTrue();
    });
}
Also used : Disposable(reactor.core.Disposable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.jupiter.api.Test)

Example 80 with Disposable

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

the class MonoCollectTest method cancelPropagatesEvenOnEmptySource.

@Test
// See https://github.com/reactor/reactor-core/issues/2519
void cancelPropagatesEvenOnEmptySource() {
    AtomicBoolean cancel1 = new AtomicBoolean();
    AtomicBoolean cancel2 = new AtomicBoolean();
    Flux<?> publisher = Flux.never().hide().doOnCancel(() -> cancel1.set(true)).collectMultimap(Function.identity()).doOnCancel(() -> cancel2.set(true)).flatMapIterable(Map::entrySet);
    Disposable d = publisher.subscribe();
    d.dispose();
    SoftAssertions.assertSoftly(softly -> {
        softly.assertThat(cancel1).as("cancel1").isTrue();
        softly.assertThat(cancel2).as("cancel2").isTrue();
    });
}
Also used : Disposable(reactor.core.Disposable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Map(java.util.Map) 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