Search in sources :

Example 6 with FAIL_FAST

use of reactor.core.publisher.Sinks.EmitFailureHandler.FAIL_FAST in project reactor-core by reactor.

the class FluxSpecTests method acceptedErrorsArePassedToRegisteredConsumer.

@Test
public void acceptedErrorsArePassedToRegisteredConsumer() {
    // "Accepted errors are passed to a registered Consumer"
    // given: "a composable with a registered consumer of RuntimeExceptions"
    Sinks.Many<Integer> composable = Sinks.many().multicast().onBackpressureBuffer();
    LongAdder errors = new LongAdder();
    composable.asFlux().doOnError(e -> errors.increment()).subscribe();
    // when: "A RuntimeException is accepted"
    composable.emitError(new RuntimeException(), FAIL_FAST);
    // then: "it is passed to the consumer"
    assertThat(errors.intValue()).isEqualTo(1);
    // when: "A new error consumer is subscribed"
    Flux.error(new RuntimeException()).doOnError(e -> errors.increment()).subscribe();
    // then: "it is called since publisher is in error state"
    assertThat(errors.intValue()).isEqualTo(2);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Sinks(reactor.core.publisher.Sinks) LongAdder(java.util.concurrent.atomic.LongAdder) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) BiFunction(java.util.function.BiFunction) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Scheduler(reactor.core.scheduler.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Schedulers(reactor.core.scheduler.Schedulers) Iterator(java.util.Iterator) Mono(reactor.core.publisher.Mono) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) List(java.util.List) FAIL_FAST(reactor.core.publisher.Sinks.EmitFailureHandler.FAIL_FAST) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Collections(java.util.Collections) Timeout(org.junit.jupiter.api.Timeout) Sinks(reactor.core.publisher.Sinks) LongAdder(java.util.concurrent.atomic.LongAdder) Test(org.junit.jupiter.api.Test)

Example 7 with FAIL_FAST

use of reactor.core.publisher.Sinks.EmitFailureHandler.FAIL_FAST in project reactor-core by reactor.

the class FluxUsingTest method subscriberCancels.

@Test
public void subscriberCancels() {
    AssertSubscriber<Integer> ts = AssertSubscriber.create();
    AtomicInteger cleanup = new AtomicInteger();
    Sinks.Many<Integer> tp = Sinks.unsafe().many().multicast().directBestEffort();
    Flux.using(() -> 1, r -> tp.asFlux(), cleanup::set, true).subscribe(ts);
    assertThat(tp.currentSubscriberCount()).as("tp has subscriber").isPositive();
    tp.emitNext(1, FAIL_FAST);
    ts.assertValues(1).assertNotComplete().assertNoError();
    ts.cancel();
    tp.emitNext(2, FAIL_FAST);
    ts.assertValues(1).assertNotComplete().assertNoError();
    assertThat(tp.currentSubscriberCount()).as("tp has subscriber").isZero();
    assertThat(cleanup).hasValue(1);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Scannable(reactor.core.Scannable) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FluxOperatorTest(reactor.test.publisher.FluxOperatorTest) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) List(java.util.List) CoreSubscriber(reactor.core.CoreSubscriber) Fuseable(reactor.core.Fuseable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FAIL_FAST(reactor.core.publisher.Sinks.EmitFailureHandler.FAIL_FAST) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Condition(org.assertj.core.api.Condition) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Assertions(org.assertj.core.api.Assertions) MockUtils(reactor.test.MockUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FluxOperatorTest(reactor.test.publisher.FluxOperatorTest) Test(org.junit.jupiter.api.Test)

Example 8 with FAIL_FAST

use of reactor.core.publisher.Sinks.EmitFailureHandler.FAIL_FAST in project reactor-core by reactor.

the class FluxFlatMapTest method flatMapUnbounded.

@Test
public void flatMapUnbounded() {
    AssertSubscriber<Integer> ts = AssertSubscriber.create();
    AtomicInteger emission = new AtomicInteger();
    Flux<Integer> source = Flux.range(1, 1000).doOnNext(v -> emission.getAndIncrement());
    Sinks.Many<Integer> source1 = Sinks.many().multicast().onBackpressureBuffer();
    Sinks.Many<Integer> source2 = Sinks.many().multicast().onBackpressureBuffer();
    source.flatMap(v -> v == 1 ? source1.asFlux() : source2.asFlux(), Integer.MAX_VALUE, 32).subscribe(ts);
    assertThat(emission).hasValue(1000);
    ts.assertNoValues().assertNoError().assertNotComplete();
    assertThat(source1.currentSubscriberCount()).as("source1 has subscriber").isPositive();
    assertThat(source2.currentSubscriberCount()).as("source2 has subscriber").isPositive();
    source1.emitNext(1, FAIL_FAST);
    source1.emitComplete(FAIL_FAST);
    source2.emitNext(2, FAIL_FAST);
    source2.emitComplete(FAIL_FAST);
    ts.assertValueCount(1000).assertNoError().assertComplete();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestPublisher(reactor.test.publisher.TestPublisher) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Scannable(reactor.core.Scannable) TestLogger(reactor.test.util.TestLogger) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AssertQueueSubscription(reactor.core.publisher.FluxPeekFuseableTest.AssertQueueSubscription) Disabled(org.junit.jupiter.api.Disabled) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Queues(reactor.util.concurrent.Queues) CoreSubscriber(reactor.core.CoreSubscriber) Loggers(reactor.util.Loggers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Logger(reactor.util.Logger) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Schedulers(reactor.core.scheduler.Schedulers) RaceTestUtils(reactor.test.util.RaceTestUtils) Subscriber(org.reactivestreams.Subscriber) Publisher(org.reactivestreams.Publisher) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Fuseable(reactor.core.Fuseable) LoggerUtils(reactor.test.util.LoggerUtils) FAIL_FAST(reactor.core.publisher.Sinks.EmitFailureHandler.FAIL_FAST) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Exceptions(reactor.core.Exceptions) Awaitility(org.awaitility.Awaitility) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test)

Example 9 with FAIL_FAST

use of reactor.core.publisher.Sinks.EmitFailureHandler.FAIL_FAST in project reactor-core by reactor.

the class FluxFlatMapTest method singleSubscriberOnly.

@Test
public void singleSubscriberOnly() {
    AssertSubscriber<Integer> ts = AssertSubscriber.create();
    AtomicInteger emission = new AtomicInteger();
    Flux<Integer> source = Flux.range(1, 2).doOnNext(v -> emission.getAndIncrement());
    Sinks.Many<Integer> source1 = Sinks.many().multicast().onBackpressureBuffer();
    Sinks.Many<Integer> source2 = Sinks.many().multicast().onBackpressureBuffer();
    source.flatMap(v -> v == 1 ? source1.asFlux() : source2.asFlux(), 1, 32).subscribe(ts);
    assertThat(emission).hasValue(1);
    ts.assertNoValues().assertNoError().assertNotComplete();
    assertThat(source1.currentSubscriberCount()).as("source1 has subscriber").isPositive();
    assertThat(source2.currentSubscriberCount()).as("source2 has subscriber").isZero();
    source1.emitNext(1, FAIL_FAST);
    source2.emitNext(10, FAIL_FAST);
    source1.emitComplete(FAIL_FAST);
    source2.emitNext(2, FAIL_FAST);
    source2.emitComplete(FAIL_FAST);
    ts.assertValues(1, 10, 2).assertNoError().assertComplete();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestPublisher(reactor.test.publisher.TestPublisher) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Scannable(reactor.core.Scannable) TestLogger(reactor.test.util.TestLogger) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AssertQueueSubscription(reactor.core.publisher.FluxPeekFuseableTest.AssertQueueSubscription) Disabled(org.junit.jupiter.api.Disabled) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Queues(reactor.util.concurrent.Queues) CoreSubscriber(reactor.core.CoreSubscriber) Loggers(reactor.util.Loggers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Logger(reactor.util.Logger) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Schedulers(reactor.core.scheduler.Schedulers) RaceTestUtils(reactor.test.util.RaceTestUtils) Subscriber(org.reactivestreams.Subscriber) Publisher(org.reactivestreams.Publisher) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Fuseable(reactor.core.Fuseable) LoggerUtils(reactor.test.util.LoggerUtils) FAIL_FAST(reactor.core.publisher.Sinks.EmitFailureHandler.FAIL_FAST) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Exceptions(reactor.core.Exceptions) Awaitility(org.awaitility.Awaitility) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test)

Example 10 with FAIL_FAST

use of reactor.core.publisher.Sinks.EmitFailureHandler.FAIL_FAST in project reactor-core by reactor.

the class FluxTests method multiplexUsingDispatchersAndSplit.

/**
 * <pre>
 *                 forkStream
 *                 /        \      < - - - int
 *                v          v
 * persistenceStream        computationStream
 *                 \        /      < - - - List< String >
 *                  v      v
 *                 joinStream      < - - - String
 *                 splitStream
 *             observedSplitStream
 * </pre>
 */
@Test
@Timeout(10)
public void multiplexUsingDispatchersAndSplit() {
    final Sinks.Many<Integer> forkEmitterProcessor = Sinks.many().multicast().onBackpressureBuffer();
    final Sinks.Many<Integer> computationEmitterProcessor = Sinks.unsafe().many().multicast().onBackpressureBuffer(256, false);
    Scheduler computation = afterTest.autoDispose(Schedulers.newSingle("computation"));
    Scheduler persistence = afterTest.autoDispose(Schedulers.newSingle("persistence"));
    Scheduler forkJoin = afterTest.autoDispose(Schedulers.newParallel("forkJoin", 2));
    final Flux<List<String>> computationStream = computationEmitterProcessor.asFlux().publishOn(computation).map(i -> {
        final List<String> list = new ArrayList<>(i);
        for (int j = 0; j < i; j++) {
            list.add("i" + j);
        }
        return list;
    }).doOnNext(ls -> println("Computed: ", ls)).log("computation");
    final Sinks.Many<Integer> persistenceEmitterProcessor = Sinks.unsafe().many().multicast().onBackpressureBuffer(Queues.SMALL_BUFFER_SIZE, false);
    final Flux<List<String>> persistenceStream = persistenceEmitterProcessor.asFlux().publishOn(persistence).doOnNext(i -> println("Persisted: ", i)).map(i -> Collections.singletonList("done" + i)).log("persistence");
    Flux<Integer> forkStream = forkEmitterProcessor.asFlux().publishOn(forkJoin).log("fork");
    // from(sink) calls below should return same instance since both processor and standalone sink
    forkStream.subscribe(v -> computationEmitterProcessor.emitNext(v, FAIL_FAST), e -> computationEmitterProcessor.emitError(e, FAIL_FAST), () -> computationEmitterProcessor.emitComplete(FAIL_FAST));
    forkStream.subscribe(v -> persistenceEmitterProcessor.emitNext(v, FAIL_FAST), e -> persistenceEmitterProcessor.emitError(e, FAIL_FAST), () -> persistenceEmitterProcessor.emitComplete(FAIL_FAST));
    final Flux<List<String>> joinStream = Flux.zip(computationStream, persistenceStream, (a, b) -> Arrays.asList(a, b)).publishOn(forkJoin).map(listOfLists -> {
        listOfLists.get(0).addAll(listOfLists.get(1));
        return listOfLists.get(0);
    }).log("join");
    final Semaphore doneSemaphore = new Semaphore(0);
    StepVerifier.create(joinStream.flatMap(Flux::fromIterable).log("resultStream").collectList().doOnTerminate(doneSemaphore::release)).then(() -> {
        forkEmitterProcessor.emitNext(1, FAIL_FAST);
        forkEmitterProcessor.emitNext(2, FAIL_FAST);
        forkEmitterProcessor.emitNext(3, FAIL_FAST);
        forkEmitterProcessor.emitComplete(FAIL_FAST);
    }).assertNext(res -> assertThat(res).containsExactly("i0", "done1", "i0", "i1", "done2", "i0", "i1", "i2", "done3")).verifyComplete();
}
Also used : Sinks(reactor.core.publisher.Sinks) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) TestLogger(reactor.test.util.TestLogger) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Tuples(reactor.util.function.Tuples) TimeoutException(java.util.concurrent.TimeoutException) Hooks(reactor.core.publisher.Hooks) Timer(java.util.Timer) Disabled(org.junit.jupiter.api.Disabled) CoreSubscriber(reactor.core.CoreSubscriber) Loggers(reactor.util.Loggers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) Logger(reactor.util.Logger) Tag(org.junit.jupiter.api.Tag) AutoDisposingExtension(reactor.test.AutoDisposingExtension) IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Signal(reactor.core.publisher.Signal) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) LockSupport(java.util.concurrent.locks.LockSupport) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Fuseable(reactor.core.Fuseable) Assertions.fail(org.assertj.core.api.Assertions.fail) LoggerUtils(reactor.test.util.LoggerUtils) Exceptions(reactor.core.Exceptions) IntStream(java.util.stream.IntStream) Disposable(reactor.core.Disposable) Scannable(reactor.core.Scannable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Tuple2(reactor.util.function.Tuple2) Callable(java.util.concurrent.Callable) Scheduler(reactor.core.scheduler.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Queues(reactor.util.concurrent.Queues) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Schedulers(reactor.core.scheduler.Schedulers) Operators(reactor.core.publisher.Operators) Semaphore(java.util.concurrent.Semaphore) Publisher(org.reactivestreams.Publisher) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Flux(reactor.core.publisher.Flux) FAIL_FAST(reactor.core.publisher.Sinks.EmitFailureHandler.FAIL_FAST) Phaser(java.util.concurrent.Phaser) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Comparator(java.util.Comparator) Collections(java.util.Collections) Timeout(org.junit.jupiter.api.Timeout) Sinks(reactor.core.publisher.Sinks) Scheduler(reactor.core.scheduler.Scheduler) ArrayList(java.util.ArrayList) Semaphore(java.util.concurrent.Semaphore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Aggregations

List (java.util.List)14 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)14 Test (org.junit.jupiter.api.Test)14 FAIL_FAST (reactor.core.publisher.Sinks.EmitFailureHandler.FAIL_FAST)14 StepVerifier (reactor.test.StepVerifier)14 Arrays (java.util.Arrays)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)13 AssertSubscriber (reactor.test.subscriber.AssertSubscriber)13 Duration (java.time.Duration)12 Schedulers (reactor.core.scheduler.Schedulers)12 ArrayList (java.util.ArrayList)11 TimeUnit (java.util.concurrent.TimeUnit)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)11 CountDownLatch (java.util.concurrent.CountDownLatch)10 Subscription (org.reactivestreams.Subscription)10 Scannable (reactor.core.Scannable)10 CoreSubscriber (reactor.core.CoreSubscriber)9 Scheduler (reactor.core.scheduler.Scheduler)9