use of reactor.core.publisher.EmitterProcessor 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"
EmitterProcessor<Integer> source = EmitterProcessor.create();
Flux<Integer> filtered = source.filter(it -> it % 2 == 0);
// when: "the source accepts an even value"
AtomicReference<Integer> value = new AtomicReference<>();
filtered.subscribe(value::set);
source.onNext(2);
// then: "it passes through"
assertThat(value.get()).isEqualTo(2);
// when: "the source accepts an odd value"
source.onNext(3);
// then: "it is blocked by the filter"
assertThat(value.get()).isEqualTo(2);
// when: "simple filter"
EmitterProcessor<Boolean> anotherSource = EmitterProcessor.create();
AtomicBoolean tap = new AtomicBoolean();
anotherSource.filter(it -> it).subscribe(tap::set);
anotherSource.onNext(true);
// then: "it is accepted by the filter"
assertThat(tap.get()).isTrue();
// when: "simple filter nominal case"
anotherSource = EmitterProcessor.create();
anotherSource.filter(it -> it).subscribe(tap::set);
anotherSource.onNext(false);
// then: "it is not accepted by the filter (previous value held)"
assertThat(tap.get()).isTrue();
}
use of reactor.core.publisher.EmitterProcessor 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"
EmitterProcessor<Integer> composable = EmitterProcessor.create();
LongAdder errors = new LongAdder();
composable.doOnError(e -> errors.increment()).subscribe();
// when: "A RuntimeException is accepted"
composable.onError(new RuntimeException());
// 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);
}
use of reactor.core.publisher.EmitterProcessor in project reactor-core by reactor.
the class FluxTests method parallelBufferedTimeoutTest.
private void parallelBufferedTimeoutTest(int iterations) throws InterruptedException {
System.out.println("Buffered Stream: " + iterations);
final CountDownLatch latch = new CountDownLatch(iterations);
EmitterProcessor<String> deferred = EmitterProcessor.create();
deferred.publishOn(asyncGroup).parallel(8).groups().subscribe(stream -> stream.publishOn(asyncGroup).bufferTimeout(1000 / 8, Duration.ofSeconds(1)).subscribe(batch -> {
for (int j = 0; j < batch.size(); j++) {
latch.countDown();
}
}));
String[] data = new String[iterations];
for (int i = 0; i < iterations; i++) {
data[i] = Integer.toString(i);
}
long start = System.currentTimeMillis();
for (String i : data) {
deferred.onNext(i);
}
if (!latch.await(10, TimeUnit.SECONDS)) {
throw new RuntimeException(latch.getCount() + " ");
}
long stop = System.currentTimeMillis() - start;
stop = stop > 0 ? stop : 1;
System.out.println("Time spent: " + stop + "ms");
System.out.println("ev/ms: " + iterations / stop);
System.out.println("ev/s: " + iterations / stop * 1000);
System.out.println("");
assertEquals(0, latch.getCount());
}
use of reactor.core.publisher.EmitterProcessor in project reactor-core by reactor.
the class FluxTests method shouldCorrectlyDispatchComplexFlow.
@Test
public void shouldCorrectlyDispatchComplexFlow() throws InterruptedException {
EmitterProcessor<Integer> globalFeed = EmitterProcessor.create();
CountDownLatch afterSubscribe = new CountDownLatch(1);
CountDownLatch latch = new CountDownLatch(4);
Flux<Integer> s = Flux.just("2222").map(Integer::parseInt).flatMap(l -> Flux.merge(globalFeed.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.onNext(2223);
globalFeed.onNext(2224);
latch.await(5, TimeUnit.SECONDS);
assertEquals("Must have counted 4 elements", 0, latch.getCount());
}
use of reactor.core.publisher.EmitterProcessor 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 EmitterProcessor<Integer> streamBatcher = EmitterProcessor.create();
streamBatcher.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.onNext(12);
streamBatcher.onNext(123);
streamBatcher.onNext(42);
streamBatcher.onNext(666);
boolean finished = latch.await(2, TimeUnit.SECONDS);
if (!finished) {
throw new RuntimeException(latch.getCount() + "");
} else {
assertEquals("Must have correct latch number : " + latch.getCount(), latch.getCount(), 0);
}
}
Aggregations