Search in sources :

Example 1 with CollectSubscriber

use of reactor.core.publisher.MonoCollect.CollectSubscriber in project reactor-core by reactor.

the class MonoCollectTest method discardCancelNextRace.

@Test
public void discardCancelNextRace() {
    lessVerboseLogs(Operators.class);
    AtomicInteger doubleDiscardCounter = new AtomicInteger();
    Context discardingContext = Operators.enableOnDiscard(null, o -> {
        AtomicBoolean ab = (AtomicBoolean) o;
        if (ab.getAndSet(true)) {
            doubleDiscardCounter.incrementAndGet();
        }
    });
    for (int i = 0; i < 100_000; i++) {
        AssertSubscriber<List<AtomicBoolean>> testSubscriber = new AssertSubscriber<>(discardingContext);
        CollectSubscriber<AtomicBoolean, List<AtomicBoolean>> subscriber = new CollectSubscriber<>(testSubscriber, List::add, new ArrayList<>());
        subscriber.onSubscribe(Operators.emptySubscription());
        AtomicBoolean extraneous = new AtomicBoolean(false);
        RaceTestUtils.race(subscriber::cancel, () -> subscriber.onNext(extraneous));
        testSubscriber.assertNoValues();
        if (!extraneous.get()) {
            LOGGER.info("" + subscriber.container);
        }
        assertThat(extraneous).as("released %d", i).isTrue();
    }
    LOGGER.info("discarded twice or more: {}", doubleDiscardCounter.get());
}
Also used : Context(reactor.util.context.Context) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) ArrayList(java.util.ArrayList) List(java.util.List) CollectSubscriber(reactor.core.publisher.MonoCollect.CollectSubscriber) Test(org.junit.jupiter.api.Test)

Example 2 with CollectSubscriber

use of reactor.core.publisher.MonoCollect.CollectSubscriber in project reactor-core by reactor.

the class MonoCollectTest method scanSubscriber.

@Test
public void scanSubscriber() {
    CoreSubscriber<List<String>> actual = new LambdaMonoSubscriber<>(null, e -> {
    }, null, null);
    CollectSubscriber<String, List<String>> test = new CollectSubscriber<>(actual, (l, v) -> l.add(v), new ArrayList<>());
    Subscription parent = Operators.emptySubscription();
    test.onSubscribe(parent);
    assertThat(test.scan(Scannable.Attr.PREFETCH)).isEqualTo(Integer.MAX_VALUE);
    assertThat(test.scan(Scannable.Attr.PARENT)).isSameAs(parent);
    assertThat(test.scan(Scannable.Attr.ACTUAL)).isSameAs(actual);
    assertThat(test.scan(Scannable.Attr.RUN_STYLE)).isSameAs(Scannable.Attr.RunStyle.SYNC);
    assertThat(test.scan(Scannable.Attr.TERMINATED)).isFalse();
    test.onError(new IllegalStateException("boom"));
    assertThat(test.scan(Scannable.Attr.TERMINATED)).isTrue();
    assertThat(test.scan(Scannable.Attr.CANCELLED)).isFalse();
    test.cancel();
    assertThat(test.scan(Scannable.Attr.CANCELLED)).isTrue();
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) CollectSubscriber(reactor.core.publisher.MonoCollect.CollectSubscriber) Subscription(org.reactivestreams.Subscription) Test(org.junit.jupiter.api.Test)

Example 3 with CollectSubscriber

use of reactor.core.publisher.MonoCollect.CollectSubscriber in project reactor-core by reactor.

the class MonoCollectTest method discardCancelCompleteRace.

@Test
public void discardCancelCompleteRace() {
    lessVerboseLogs(Operators.class);
    AtomicInteger doubleDiscardCounter = new AtomicInteger();
    Context discardingContext = Operators.enableOnDiscard(null, o -> {
        AtomicBoolean ab = (AtomicBoolean) o;
        if (ab.getAndSet(true)) {
            doubleDiscardCounter.incrementAndGet();
        }
    });
    for (int i = 0; i < 100_000; i++) {
        AssertSubscriber<List<AtomicBoolean>> testSubscriber = new AssertSubscriber<>(discardingContext);
        CollectSubscriber<AtomicBoolean, List<AtomicBoolean>> subscriber = new CollectSubscriber<>(testSubscriber, List::add, new ArrayList<>());
        subscriber.onSubscribe(Operators.emptySubscription());
        AtomicBoolean resource = new AtomicBoolean(false);
        subscriber.onNext(resource);
        RaceTestUtils.race(subscriber::cancel, subscriber::onComplete);
        if (testSubscriber.values().isEmpty()) {
            assertThat(resource).as("not completed and released %d", i).isTrue();
        }
    }
    LOGGER.info("discarded twice or more: {}", doubleDiscardCounter.get());
}
Also used : Context(reactor.util.context.Context) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) ArrayList(java.util.ArrayList) List(java.util.List) CollectSubscriber(reactor.core.publisher.MonoCollect.CollectSubscriber) Test(org.junit.jupiter.api.Test)

Aggregations

ArrayList (java.util.ArrayList)3 List (java.util.List)3 Test (org.junit.jupiter.api.Test)3 CollectSubscriber (reactor.core.publisher.MonoCollect.CollectSubscriber)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AssertSubscriber (reactor.test.subscriber.AssertSubscriber)2 Context (reactor.util.context.Context)2 Subscription (org.reactivestreams.Subscription)1