Search in sources :

Example 1 with FAIL_FAST

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

the class FluxPublishTest method prematureOnComplete.

@Test
public void prematureOnComplete() {
    Sinks.Many<Flux<String>> sink = Sinks.unsafe().many().multicast().onBackpressureBuffer(Queues.SMALL_BUFFER_SIZE, false);
    Flux.just("ALPHA", "BRAVO", "CHARLIE", "DELTA", "ALPHA", "BRAVO", "CHARLIE", "DELTA", "ALPHA", "BRAVO", "CHARLIE", "DELTA").log("stream.incoming").windowWhile(s -> !"DELTA".equals(s), 1).subscribe(v -> sink.emitNext(v, FAIL_FAST), e -> sink.emitError(e, FAIL_FAST), () -> sink.emitComplete(FAIL_FAST));
    AtomicInteger windowIndex = new AtomicInteger(0);
    AtomicInteger nextIndex = new AtomicInteger(0);
    sink.asFlux().next().flatMapMany(flux -> flux.takeWhile(s -> !"CHARLIE".equals(s)).log(String.format("stream.window.%d", windowIndex.getAndIncrement()))).log(String.format("stream.next.%d", nextIndex.getAndIncrement())).as(StepVerifier::create).expectNextCount(2).verifyComplete();
    sink.asFlux().next().flatMapMany(flux -> flux.takeWhile(s -> !"CHARLIE".equals(s)).log(String.format("stream.window.%d", windowIndex.getAndIncrement()))).log(String.format("stream.next.%d", nextIndex.getAndIncrement())).as(StepVerifier::create).expectNextCount(2).verifyComplete();
    sink.asFlux().next().flatMapMany(flux -> flux.takeWhile(s -> !"CHARLIE".equals(s)).log(String.format("stream.window.%d", windowIndex.getAndIncrement()))).log(String.format("stream.next.%d", nextIndex.getAndIncrement())).as(StepVerifier::create).expectNextCount(2).verifyComplete();
}
Also used : TestPublisher(reactor.test.publisher.TestPublisher) Arrays(java.util.Arrays) Disposable(reactor.core.Disposable) StepVerifier(reactor.test.StepVerifier) CancellationException(java.util.concurrent.CancellationException) Scannable(reactor.core.Scannable) ContextView(reactor.util.context.ContextView) Context(reactor.util.context.Context) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FluxOperatorTest(reactor.test.publisher.FluxOperatorTest) AtomicReference(java.util.concurrent.atomic.AtomicReference) Queues(reactor.util.concurrent.Queues) Test(org.junit.jupiter.api.Test) List(java.util.List) Stream(java.util.stream.Stream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FAIL_FAST(reactor.core.publisher.Sinks.EmitFailureHandler.FAIL_FAST) Duration(java.time.Duration) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Schedulers(reactor.core.scheduler.Schedulers) Timeout(org.junit.jupiter.api.Timeout) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StepVerifier(reactor.test.StepVerifier) FluxOperatorTest(reactor.test.publisher.FluxOperatorTest) Test(org.junit.jupiter.api.Test)

Example 2 with FAIL_FAST

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

the class FluxPublishMulticastTest method scenarios_errorFromUpstreamFailure.

@Override
protected List<Scenario<String, String>> scenarios_errorFromUpstreamFailure() {
    return Arrays.asList(scenario(f -> f.publish(p -> p)), scenario(f -> f.publish(p -> {
        // TODO double check this Sink usage
        Sinks.Many<String> sink = Sinks.unsafe().many().unicast().onBackpressureBuffer();
        p.subscribe(v -> sink.emitNext(v, FAIL_FAST), e -> sink.emitError(e, FAIL_FAST), () -> sink.emitComplete(FAIL_FAST));
        return sink.asFlux();
    })).fusionMode(Fuseable.ASYNC), scenario(f -> f.publish(p -> p)).shouldHitDropNextHookAfterTerminate(false), scenario(f -> Flux.never().publish(p -> {
        Disposable d = p.subscribe();
        Disposable d2 = p.subscribe();
        d.dispose();
        d2.dispose();
        return f;
    })).shouldHitDropErrorHookAfterTerminate(false).shouldHitDropNextHookAfterTerminate(false));
}
Also used : Arrays(java.util.Arrays) Flux.range(reactor.core.publisher.Flux.range) Disposable(reactor.core.Disposable) StepVerifier(reactor.test.StepVerifier) Scannable(reactor.core.Scannable) Context(reactor.util.context.Context) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Tuples(reactor.util.function.Tuples) Tuple2(reactor.util.function.Tuple2) FluxOperatorTest(reactor.test.publisher.FluxOperatorTest) Function(java.util.function.Function) Fail.fail(org.assertj.core.api.Fail.fail) Queues(reactor.util.concurrent.Queues) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) CoreSubscriber(reactor.core.CoreSubscriber) Fuseable(reactor.core.Fuseable) FAIL_FAST(reactor.core.publisher.Sinks.EmitFailureHandler.FAIL_FAST) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Flux.zip(reactor.core.publisher.Flux.zip) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Disposable(reactor.core.Disposable)

Example 3 with FAIL_FAST

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

the class FluxTests method shouldCorrectlyDispatchBatchedTimeout.

@Test
public void shouldCorrectlyDispatchBatchedTimeout() throws InterruptedException {
    long timeout = 100;
    final int batchsize = 4;
    int parallelStreams = 16;
    CountDownLatch latch = new CountDownLatch(1);
    final Sinks.Many<Integer> streamBatcher = Sinks.many().multicast().onBackpressureBuffer();
    streamBatcher.asFlux().publishOn(asyncGroup).bufferTimeout(batchsize, Duration.ofSeconds(timeout)).log("batched").parallel(parallelStreams).groups().log("batched-inner").subscribe(innerStream -> innerStream.publishOn(asyncGroup).doOnError(Throwable::printStackTrace).subscribe(i -> latch.countDown()));
    streamBatcher.emitNext(12, FAIL_FAST);
    streamBatcher.emitNext(123, FAIL_FAST);
    streamBatcher.emitNext(42, FAIL_FAST);
    streamBatcher.emitNext(666, FAIL_FAST);
    boolean finished = latch.await(2, TimeUnit.SECONDS);
    if (!finished) {
        throw new RuntimeException(latch.getCount() + "");
    } else {
        assertThat(latch.getCount()).as("latch count").isEqualTo(0);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 4 with FAIL_FAST

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

the class FluxTests method shouldCorrectlyDispatchComplexFlow.

@Test
public void shouldCorrectlyDispatchComplexFlow() throws InterruptedException {
    Sinks.Many<Integer> globalFeed = Sinks.many().multicast().onBackpressureBuffer();
    CountDownLatch afterSubscribe = new CountDownLatch(1);
    CountDownLatch latch = new CountDownLatch(4);
    Flux<Integer> s = Flux.just("2222").map(Integer::parseInt).flatMap(l -> Flux.merge(globalFeed.asFlux().publishOn(asyncGroup), Flux.just(1111, l, 3333, 4444, 5555, 6666)).log("merged").publishOn(asyncGroup).log("dispatched").doOnSubscribe(x -> afterSubscribe.countDown()).filter(nearbyLoc -> 3333 >= nearbyLoc).filter(nearbyLoc -> 2222 <= nearbyLoc));
    /*Disposable action = */
    s.limitRate(1).subscribe(integer -> {
        latch.countDown();
        System.out.println(integer);
    });
    afterSubscribe.await(5, TimeUnit.SECONDS);
    globalFeed.emitNext(2223, FAIL_FAST);
    globalFeed.emitNext(2224, FAIL_FAST);
    latch.await(5, TimeUnit.SECONDS);
    assertThat(latch.getCount()).as("latch count").isEqualTo(0);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 5 with FAIL_FAST

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

the class FluxSpecTests method fluxValuesCanBeFiltered.

@Test
public void fluxValuesCanBeFiltered() {
    // "A Flux"s values can be filtered"
    // given: "a source composable with a filter that rejects odd values"
    Sinks.Many<Integer> source = Sinks.many().multicast().onBackpressureBuffer();
    Flux<Integer> filtered = source.asFlux().filter(it -> it % 2 == 0);
    // when: "the source accepts an even value"
    AtomicReference<Integer> value = new AtomicReference<>();
    filtered.subscribe(value::set);
    source.emitNext(2, FAIL_FAST);
    // then: "it passes through"
    assertThat(value).hasValue(2);
    // when: "the source accepts an odd value"
    source.emitNext(3, FAIL_FAST);
    // then: "it is blocked by the filter"
    assertThat(value).hasValue(2);
    // when: "simple filter"
    Sinks.Many<Boolean> anotherSource = Sinks.many().multicast().onBackpressureBuffer();
    AtomicBoolean tap = new AtomicBoolean();
    anotherSource.asFlux().filter(it -> it).subscribe(tap::set);
    anotherSource.emitNext(true, FAIL_FAST);
    // then: "it is accepted by the filter"
    assertThat(tap.get()).isTrue();
    // when: "simple filter nominal case"
    anotherSource = Sinks.many().multicast().onBackpressureBuffer();
    anotherSource.asFlux().filter(it -> it).subscribe(tap::set);
    anotherSource.emitNext(false, FAIL_FAST);
    // then: "it is not accepted by the filter (previous value held)"
    assertThat(tap.get()).isTrue();
}
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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Sinks(reactor.core.publisher.Sinks) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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