Search in sources :

Example 1 with FluxProcessor

use of reactor.core.publisher.FluxProcessor in project spring-framework by spring-projects.

the class ReactiveAdapterRegistryTests method getAdapterForReactiveSubType.

@Test
public void getAdapterForReactiveSubType() throws Exception {
    ReactiveAdapter adapter1 = getAdapter(Flux.class);
    ReactiveAdapter adapter2 = getAdapter(FluxProcessor.class);
    assertSame(adapter1, adapter2);
    this.registry.registerReactiveType(ReactiveTypeDescriptor.multiValue(FluxProcessor.class, FluxProcessor::empty), o -> (FluxProcessor<?, ?>) o, FluxProcessor::from);
    ReactiveAdapter adapter3 = getAdapter(FluxProcessor.class);
    assertNotNull(adapter3);
    assertNotSame(adapter1, adapter3);
}
Also used : FluxProcessor(reactor.core.publisher.FluxProcessor) ReactiveAdapter(org.springframework.core.ReactiveAdapter) Test(org.junit.Test)

Example 2 with FluxProcessor

use of reactor.core.publisher.FluxProcessor in project reactor-netty by reactor.

the class WebsocketTest method duplexEcho.

@Test
public void duplexEcho() throws Exception {
    int c = 10;
    CountDownLatch clientLatch = new CountDownLatch(c);
    CountDownLatch serverLatch = new CountDownLatch(c);
    FluxProcessor<String, String> server = ReplayProcessor.<String>create().serialize();
    FluxProcessor<String, String> client = ReplayProcessor.<String>create().serialize();
    server.log("server").subscribe(v -> serverLatch.countDown());
    client.log("client").subscribe(v -> clientLatch.countDown());
    httpServer = HttpServer.create(0).newHandler((in, out) -> out.sendWebsocket((i, o) -> o.sendString(i.receive().asString().take(c).subscribeWith(server)))).block(Duration.ofSeconds(30));
    Flux.interval(Duration.ofMillis(200)).map(Object::toString).subscribe(client::onNext);
    HttpClient.create(httpServer.address().getPort()).ws("/test").flatMap(in -> in.receiveWebsocket((i, o) -> o.options(opt -> opt.flushOnEach()).sendString(i.receive().asString().subscribeWith(client)))).subscribe();
    Assert.assertTrue(serverLatch.await(10, TimeUnit.SECONDS));
    Assert.assertTrue(clientLatch.await(10, TimeUnit.SECONDS));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) StepVerifier(reactor.test.StepVerifier) Test(org.junit.Test) FluxProcessor(reactor.core.publisher.FluxProcessor) Mono(reactor.core.publisher.Mono) WebSocketHandshakeException(io.netty.handler.codec.http.websocketx.WebSocketHandshakeException) PoolResources(reactor.ipc.netty.resources.PoolResources) AtomicReference(java.util.concurrent.atomic.AtomicReference) ReplayProcessor(reactor.core.publisher.ReplayProcessor) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) Duration(java.time.Duration) After(org.junit.After) NettyContext(reactor.ipc.netty.NettyContext) Assert(org.junit.Assert) HttpServer(reactor.ipc.netty.http.server.HttpServer) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with FluxProcessor

use of reactor.core.publisher.FluxProcessor in project reactor-core by reactor.

the class FluxTests method shouldNotFlushStreamOnTimeoutPrematurely.

/**
 * original from @oiavorskyl https://github.com/eventBus/eventBus/issues/358
 * @throws Exception for convenience
 */
@Test
public void shouldNotFlushStreamOnTimeoutPrematurely() throws Exception {
    final int NUM_MESSAGES = 100000;
    final int BATCH_SIZE = 1000;
    final int TIMEOUT = 100;
    final int PARALLEL_STREAMS = 2;
    /**
     * Relative tolerance, default to 90% of the batches, in an operative environment, random factors can impact
     * the fluxion latency, e.g. GC pause if system is under pressure.
     */
    final double TOLERANCE = 0.9;
    FluxProcessor<Integer, Integer> batchingStreamDef = EmitterProcessor.create();
    List<Integer> testDataset = createTestDataset(NUM_MESSAGES);
    final CountDownLatch latch = new CountDownLatch(NUM_MESSAGES);
    Map<Integer, Integer> batchesDistribution = new ConcurrentHashMap<>();
    batchingStreamDef.publishOn(asyncGroup).parallel(PARALLEL_STREAMS).groups().subscribe(substream -> substream.hide().publishOn(asyncGroup).bufferTimeout(BATCH_SIZE, Duration.ofMillis(TIMEOUT)).subscribe(items -> {
        batchesDistribution.compute(items.size(), (key, value) -> value == null ? 1 : value + 1);
        items.forEach(item -> latch.countDown());
    }));
    testDataset.forEach(batchingStreamDef::onNext);
    System.out.println(batchesDistribution);
    if (!latch.await(10, TimeUnit.SECONDS)) {
        throw new RuntimeException(latch.getCount() + " ");
    }
    int messagesProcessed = batchesDistribution.entrySet().stream().mapToInt(entry -> entry.getKey() * entry.getValue()).reduce(Integer::sum).getAsInt();
    assertEquals(NUM_MESSAGES, messagesProcessed);
    assertTrue("Less than 90% (" + NUM_MESSAGES / BATCH_SIZE * TOLERANCE + ") of the batches are matching the buffer size: " + batchesDistribution.get(BATCH_SIZE), NUM_MESSAGES / BATCH_SIZE * TOLERANCE >= batchesDistribution.get(BATCH_SIZE) * TOLERANCE);
}
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) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 FluxProcessor (reactor.core.publisher.FluxProcessor)3 Duration (java.time.Duration)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 TimeUnit (java.util.concurrent.TimeUnit)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 CoreMatchers.is (org.hamcrest.CoreMatchers.is)2 Assert (org.junit.Assert)2 Flux (reactor.core.publisher.Flux)2 Mono (reactor.core.publisher.Mono)2 ReplayProcessor (reactor.core.publisher.ReplayProcessor)2 StepVerifier (reactor.test.StepVerifier)2 WebSocketHandshakeException (io.netty.handler.codec.http.websocketx.WebSocketHandshakeException)1 KeyEvent (java.awt.event.KeyEvent)1 IOException (java.io.IOException)1 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1