Search in sources :

Example 21 with Disposable

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

the class NextProcessorTest method shareCanDisconnectAndReconnect.

@Test
void shareCanDisconnectAndReconnect() {
    AtomicInteger connectionCount = new AtomicInteger();
    AtomicInteger disconnectionCount = new AtomicInteger();
    Mono<Object> shared = Mono.never().doFirst(() -> connectionCount.incrementAndGet()).doOnCancel(() -> disconnectionCount.incrementAndGet()).share();
    Disposable sub1 = shared.subscribe();
    assertThat(connectionCount).as("sub1 connectionCount").hasValue(1);
    assertThat(disconnectionCount).as("sub1 disconnectionCount before dispose").hasValue(0);
    sub1.dispose();
    assertThat(disconnectionCount).as("sub1 disconnectionCount after dispose").hasValue(1);
    Disposable sub2 = shared.subscribe();
    assertThat(connectionCount).as("sub2 connectionCount").hasValue(2);
    assertThat(disconnectionCount).as("sub2 disconnectionCount before dispose").hasValue(1);
    sub2.dispose();
    assertThat(disconnectionCount).as("sub2 disconnectionCount after dispose").hasValue(2);
}
Also used : Disposable(reactor.core.Disposable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test)

Example 22 with Disposable

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

the class NextProcessorTest method monoShareSubscribeNoArgsCanBeIndividuallyDisposed.

@Test
void monoShareSubscribeNoArgsCanBeIndividuallyDisposed() {
    AtomicInteger cancelledCount = new AtomicInteger();
    Mono<Object> mono = Mono.never().doOnCancel(cancelledCount::incrementAndGet).share();
    Disposable sub1 = mono.subscribe();
    Disposable sub2 = mono.subscribe();
    assertThat(sub1).as("subscription are differentiated").isNotSameAs(sub2);
    sub1.dispose();
    assertThat(cancelledCount).as("not cancelled when 1+ subscriber").hasValue(0);
    AtomicBoolean sub3Terminated = new AtomicBoolean();
    Disposable sub3 = mono.doOnTerminate(() -> sub3Terminated.set(true)).subscribe();
    assertThat(sub3).as("3rd subscribe differentiated").isNotSameAs(sub2);
    assertThat(sub3Terminated).as("subscription still possible once sub disposed").isFalse();
    sub2.dispose();
    sub3.dispose();
    Scannable.from(mono).inners().forEach(System.out::println);
    assertThat(cancelledCount).as("cancelled once 0 subscriber").hasValue(1);
    AtomicBoolean sub4Terminated = new AtomicBoolean();
    Disposable sub4 = mono.doOnTerminate(() -> sub4Terminated.set(true)).subscribe();
    assertThat(sub4Terminated).as("subscription still possible once all subs disposed").isFalse();
}
Also used : Disposable(reactor.core.Disposable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test)

Example 23 with Disposable

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

the class SinkManyBestEffortTest method innersReflectsSubscribers.

@Test
void innersReflectsSubscribers() {
    SinkManyBestEffort<Integer> sink = SinkManyBestEffort.createBestEffort();
    assertThat(sink.inners()).as("before subscribers").isEmpty();
    Disposable sub1 = sink.subscribe();
    Disposable sub2 = sink.subscribe();
    assertThat(sink.inners()).hasSize(2);
    assertThat(sink.inners().map(inner -> inner.scanUnsafe(Scannable.Attr.ACTUAL))).containsExactly(sub1, sub2);
}
Also used : Disposable(reactor.core.Disposable) Test(org.junit.jupiter.api.Test)

Example 24 with Disposable

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

the class UnicastProcessorTest method emitNextWithNoSubscriberAndBoundedQueueAndHandlerHandlesValueAndKeepsSinkOpen.

// TODO that onOverflow API isn't exposed via Sinks. But maybe it should be generalized?
@Test
public void emitNextWithNoSubscriberAndBoundedQueueAndHandlerHandlesValueAndKeepsSinkOpen() {
    Disposable sinkDisposed = Disposables.single();
    List<Integer> discarded = new CopyOnWriteArrayList<>();
    UnicastProcessor<Integer> unicastProcessor = UnicastProcessor.create(Queues.<Integer>one().get(), discarded::add, sinkDisposed);
    // fill the buffer
    unicastProcessor.tryEmitNext(1);
    // this "overflows" but keeps the sink open
    unicastProcessor.emitNext(2, FAIL_FAST);
    assertThat(discarded).containsExactly(2);
    assertThat(sinkDisposed.isDisposed()).as("sinkDisposed").isFalse();
    unicastProcessor.emitComplete(FAIL_FAST);
    // let's verify we get the buffer's content
    StepVerifier.create(unicastProcessor).expectNext(// from the buffer
    1).verifyComplete();
}
Also used : Disposable(reactor.core.Disposable) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.jupiter.api.Test)

Example 25 with Disposable

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

the class UnicastProcessorTest method createOverrideQueueOnTerminate.

@Test
public void createOverrideQueueOnTerminate() {
    Disposable onTerminate = () -> {
    };
    Queue<Integer> queue = Queues.<Integer>get(10).get();
    UnicastProcessor<Integer> processor = UnicastProcessor.create(queue, onTerminate);
    assertProcessor(processor, queue, null, onTerminate);
}
Also used : Disposable(reactor.core.Disposable) 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