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();
}
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));
}
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);
}
}
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);
}
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();
}
Aggregations