Search in sources :

Example 11 with FAIL_FAST

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

the class FluxSpecTests method whenMappingFunctionThrowsMappedComposableAcceptsError.

@Test
public void whenMappingFunctionThrowsMappedComposableAcceptsError() {
    // "When a mapping function throws an exception, the mapped composable accepts the error"
    // given: "a source composable with a mapping function that throws an error"
    Sinks.Many<Integer> source = Sinks.many().multicast().onBackpressureBuffer();
    Flux<String> mapped = source.asFlux().map(it -> {
        if (it == 1) {
            throw new RuntimeException();
        } else {
            return "na";
        }
    });
    LongAdder errors = new LongAdder();
    mapped.doOnError(e -> errors.increment()).subscribe();
    // when: "the source accepts a value"
    source.emitNext(1, FAIL_FAST);
    // then: "the error is passed on"
    assertThat(errors.intValue()).isEqualTo(1);
}
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 12 with FAIL_FAST

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

the class FluxSpecTests method whenFilterFunctionThrowsFilteredComposableAcceptsError.

@Test
public void whenFilterFunctionThrowsFilteredComposableAcceptsError() {
    // "When a filter function throws an exception, the filtered composable accepts the error"
    // given: "a source composable with a filter function that throws an error"
    Sinks.Many<Integer> source = Sinks.many().multicast().onBackpressureBuffer();
    Flux<Integer> filtered = source.asFlux().filter(it -> {
        if (it == 1) {
            throw new RuntimeException();
        } else {
            return true;
        }
    });
    LongAdder errors = new LongAdder();
    filtered.doOnError(e -> errors.increment()).subscribe();
    // when: "the source accepts a value"
    source.emitNext(1, FAIL_FAST);
    // then: "the error is passed on"
    assertThat(errors.intValue()).isEqualTo(1);
}
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 13 with FAIL_FAST

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

the class FluxBufferTimeoutTest method requestedFromUpstreamShouldNotExceedDownstreamDemand.

@Test
public void requestedFromUpstreamShouldNotExceedDownstreamDemand() {
    Sinks.Many<String> sink = Sinks.many().multicast().onBackpressureBuffer();
    Flux<String> emitter = sink.asFlux();
    AtomicLong requestedOutstanding = new AtomicLong(0);
    VirtualTimeScheduler scheduler = VirtualTimeScheduler.create();
    Flux<List<String>> flux = emitter.doOnRequest(requestedOutstanding::addAndGet).bufferTimeout(5, Duration.ofMillis(100), scheduler).doOnNext(list -> requestedOutstanding.addAndGet(0 - list.size()));
    StepVerifier.withVirtualTime(() -> flux, () -> scheduler, 0).expectSubscription().then(() -> assertThat(requestedOutstanding).hasValue(0)).thenRequest(2).then(() -> assertThat(requestedOutstanding.get()).isEqualTo(10)).then(() -> sink.emitNext("a", FAIL_FAST)).thenAwait(Duration.ofMillis(100)).assertNext(s -> assertThat(s).containsExactly("a")).then(() -> assertThat(requestedOutstanding).hasValue(9)).thenRequest(1).then(() -> assertThat(requestedOutstanding).hasValue(10)).thenCancel().verify();
}
Also used : TestPublisher(reactor.test.publisher.TestPublisher) StepVerifier(reactor.test.StepVerifier) Scannable(reactor.core.Scannable) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Callable(java.util.concurrent.Callable) Scheduler(reactor.core.scheduler.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) CoreSubscriber(reactor.core.CoreSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Schedulers(reactor.core.scheduler.Schedulers) RaceTestUtils(reactor.test.util.RaceTestUtils) StepVerifierOptions(reactor.test.StepVerifierOptions) VirtualTimeScheduler(reactor.test.scheduler.VirtualTimeScheduler) Scannable.from(reactor.core.Scannable.from) Set(java.util.Set) 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) AfterEach(org.junit.jupiter.api.AfterEach) FAIL_FAST(reactor.core.publisher.Sinks.EmitFailureHandler.FAIL_FAST) Subscription(org.reactivestreams.Subscription) Exceptions(reactor.core.Exceptions) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) AtomicLong(java.util.concurrent.atomic.AtomicLong) VirtualTimeScheduler(reactor.test.scheduler.VirtualTimeScheduler) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.jupiter.api.Test)

Example 14 with FAIL_FAST

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

the class FluxMergeSequentialTest method testReentrantWork.

@Test
public void testReentrantWork() {
    final Sinks.Many<Integer> subject = Sinks.unsafe().many().multicast().directBestEffort();
    final AtomicBoolean once = new AtomicBoolean();
    subject.asFlux().flatMapSequential(Flux::just).doOnNext(t -> {
        if (once.compareAndSet(false, true)) {
            subject.emitNext(2, FAIL_FAST);
        }
    }).subscribe(ts);
    subject.emitNext(1, FAIL_FAST);
    ts.assertNoError();
    ts.assertNotComplete();
    ts.assertValues(1, 2);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeforeEach(org.junit.jupiter.api.BeforeEach) ErrorMode(reactor.core.publisher.FluxConcatMap.ErrorMode) 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) Scheduler(reactor.core.scheduler.Scheduler) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Queues(reactor.util.concurrent.Queues) CoreSubscriber(reactor.core.CoreSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Schedulers(reactor.core.scheduler.Schedulers) Subscriber(org.reactivestreams.Subscriber) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) Fuseable(reactor.core.Fuseable) FAIL_FAST(reactor.core.publisher.Sinks.EmitFailureHandler.FAIL_FAST) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Queue(java.util.Queue) Collections(java.util.Collections) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.jupiter.api.Test)

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