Search in sources :

Example 1 with EmitterProcessor

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();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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) FluxProcessor(reactor.core.publisher.FluxProcessor) 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) EmitterProcessor(reactor.core.publisher.EmitterProcessor) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Schedulers(reactor.core.scheduler.Schedulers) Iterator(java.util.Iterator) MonoProcessor(reactor.core.publisher.MonoProcessor) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) ReplayProcessor(reactor.core.publisher.ReplayProcessor) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) List(java.util.List) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Assert(org.junit.Assert) Collections(java.util.Collections) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 2 with EmitterProcessor

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);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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) FluxProcessor(reactor.core.publisher.FluxProcessor) 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) EmitterProcessor(reactor.core.publisher.EmitterProcessor) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Schedulers(reactor.core.scheduler.Schedulers) Iterator(java.util.Iterator) MonoProcessor(reactor.core.publisher.MonoProcessor) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) ReplayProcessor(reactor.core.publisher.ReplayProcessor) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) List(java.util.List) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Assert(org.junit.Assert) Collections(java.util.Collections) LongAdder(java.util.concurrent.atomic.LongAdder) Test(org.junit.Test)

Example 3 with EmitterProcessor

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());
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Processor(org.reactivestreams.Processor) Tuples(reactor.util.function.Tuples) TimeoutException(java.util.concurrent.TimeoutException) Random(java.util.Random) Timer(java.util.Timer) 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) Assertions(org.assertj.core.api.Assertions) IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Signal(reactor.core.publisher.Signal) KeyEvent(java.awt.event.KeyEvent) Instant(java.time.Instant) SignalType(reactor.core.publisher.SignalType) Collectors(java.util.stream.Collectors) LockSupport(java.util.concurrent.locks.LockSupport) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stream(java.util.stream.Stream) Exceptions(reactor.core.Exceptions) IntStream(java.util.stream.IntStream) Disposable(reactor.core.Disposable) TopicProcessor(reactor.core.publisher.TopicProcessor) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Tuple2(reactor.util.function.Tuple2) FluxProcessor(reactor.core.publisher.FluxProcessor) Scheduler(reactor.core.scheduler.Scheduler) OrderingComparison.lessThan(org.hamcrest.number.OrderingComparison.lessThan) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) EmitterProcessor(reactor.core.publisher.EmitterProcessor) Schedulers(reactor.core.scheduler.Schedulers) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Semaphore(java.util.concurrent.Semaphore) Publisher(org.reactivestreams.Publisher) MonoProcessor(reactor.core.publisher.MonoProcessor) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) ReplayProcessor(reactor.core.publisher.ReplayProcessor) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Flux(reactor.core.publisher.Flux) Ignore(org.junit.Ignore) Phaser(java.util.concurrent.Phaser) Matcher(org.hamcrest.Matcher) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Comparator(java.util.Comparator) Assert(org.junit.Assert) Collections(java.util.Collections) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 4 with EmitterProcessor

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Processor(org.reactivestreams.Processor) Tuples(reactor.util.function.Tuples) TimeoutException(java.util.concurrent.TimeoutException) Random(java.util.Random) Timer(java.util.Timer) 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) Assertions(org.assertj.core.api.Assertions) IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Signal(reactor.core.publisher.Signal) KeyEvent(java.awt.event.KeyEvent) Instant(java.time.Instant) SignalType(reactor.core.publisher.SignalType) Collectors(java.util.stream.Collectors) LockSupport(java.util.concurrent.locks.LockSupport) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stream(java.util.stream.Stream) Exceptions(reactor.core.Exceptions) IntStream(java.util.stream.IntStream) Disposable(reactor.core.Disposable) TopicProcessor(reactor.core.publisher.TopicProcessor) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Tuple2(reactor.util.function.Tuple2) FluxProcessor(reactor.core.publisher.FluxProcessor) Scheduler(reactor.core.scheduler.Scheduler) OrderingComparison.lessThan(org.hamcrest.number.OrderingComparison.lessThan) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) EmitterProcessor(reactor.core.publisher.EmitterProcessor) Schedulers(reactor.core.scheduler.Schedulers) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Semaphore(java.util.concurrent.Semaphore) Publisher(org.reactivestreams.Publisher) MonoProcessor(reactor.core.publisher.MonoProcessor) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) ReplayProcessor(reactor.core.publisher.ReplayProcessor) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Flux(reactor.core.publisher.Flux) Ignore(org.junit.Ignore) Phaser(java.util.concurrent.Phaser) Matcher(org.hamcrest.Matcher) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Comparator(java.util.Comparator) Assert(org.junit.Assert) Collections(java.util.Collections) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 5 with EmitterProcessor

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);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Processor(org.reactivestreams.Processor) Tuples(reactor.util.function.Tuples) TimeoutException(java.util.concurrent.TimeoutException) Random(java.util.Random) Timer(java.util.Timer) 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) Assertions(org.assertj.core.api.Assertions) IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Signal(reactor.core.publisher.Signal) KeyEvent(java.awt.event.KeyEvent) Instant(java.time.Instant) SignalType(reactor.core.publisher.SignalType) Collectors(java.util.stream.Collectors) LockSupport(java.util.concurrent.locks.LockSupport) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stream(java.util.stream.Stream) Exceptions(reactor.core.Exceptions) IntStream(java.util.stream.IntStream) Disposable(reactor.core.Disposable) TopicProcessor(reactor.core.publisher.TopicProcessor) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Tuple2(reactor.util.function.Tuple2) FluxProcessor(reactor.core.publisher.FluxProcessor) Scheduler(reactor.core.scheduler.Scheduler) OrderingComparison.lessThan(org.hamcrest.number.OrderingComparison.lessThan) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) EmitterProcessor(reactor.core.publisher.EmitterProcessor) Schedulers(reactor.core.scheduler.Schedulers) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Semaphore(java.util.concurrent.Semaphore) Publisher(org.reactivestreams.Publisher) MonoProcessor(reactor.core.publisher.MonoProcessor) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) ReplayProcessor(reactor.core.publisher.ReplayProcessor) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Flux(reactor.core.publisher.Flux) Ignore(org.junit.Ignore) Phaser(java.util.concurrent.Phaser) Matcher(org.hamcrest.Matcher) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Comparator(java.util.Comparator) Assert(org.junit.Assert) Collections(java.util.Collections) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

Duration (java.time.Duration)13 List (java.util.List)13 CountDownLatch (java.util.concurrent.CountDownLatch)13 TimeUnit (java.util.concurrent.TimeUnit)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 Test (org.junit.Test)13 EmitterProcessor (reactor.core.publisher.EmitterProcessor)13 Flux (reactor.core.publisher.Flux)13 Mono (reactor.core.publisher.Mono)13 Schedulers (reactor.core.scheduler.Schedulers)13 StepVerifier (reactor.test.StepVerifier)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)11 MonoProcessor (reactor.core.publisher.MonoProcessor)11 ArrayList (java.util.ArrayList)10 Arrays (java.util.Arrays)10 Collections (java.util.Collections)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 Consumer (java.util.function.Consumer)10 Assert (org.junit.Assert)10 FluxProcessor (reactor.core.publisher.FluxProcessor)10