Java Code Examples for org.reactivestreams.Processor

The following examples show how to use org.reactivestreams.Processor. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: smallrye-mutiny   Source File: SerializedProcessorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test(invocationCount = 50)
public void verifyOnErrorThreadSafety() {
    Exception failure = new Exception("boom");
    final Processor<Integer, Integer> processor = UnicastProcessor.<Integer> create().serialized();
    MultiAssertSubscriber<Integer> subscriber = MultiAssertSubscriber.create(100);
    processor.subscribe(subscriber);

    Runnable r1 = () -> processor.onError(failure);
    Runnable r2 = () -> processor.onError(failure);

    new Thread(r1).start();
    new Thread(r2).start();

    subscriber
            .await()
            .assertSubscribed()
            .assertHasFailedWith(Exception.class, "boom");
}
 
Example 2
Source Project: smallrye-mutiny   Source File: SerializedProcessorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test(invocationCount = 20)
public void verifyOnNextOnCompleteThreadSafety() {
    final Processor<Integer, Integer> processor = UnicastProcessor.<Integer> create().serialized();
    MultiAssertSubscriber<Integer> subscriber = MultiAssertSubscriber.create(100);
    processor.subscribe(subscriber);

    Runnable r1 = () -> {
        processor.onNext(1);
        processor.onComplete();
    };
    Runnable r2 = processor::onComplete;

    new Thread(r1).start();
    new Thread(r2).start();

    subscriber.await();
    subscriber
            .assertSubscribed()
            .assertCompletedSuccessfully();

    if (subscriber.items().size() != 0) {
        assertThat(subscriber.items()).containsExactly(1);
    }
}
 
Example 3
Source Project: smallrye-mutiny   Source File: SerializedProcessorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test(invocationCount = 20)
public void verifyOnSubscribeOnCompleteThreadSafety() {
    final Processor<Integer, Integer> processor = UnicastProcessor.<Integer> create().serialized();
    MultiAssertSubscriber<Integer> subscriber = MultiAssertSubscriber.create(100);
    processor.subscribe(subscriber);

    Runnable r1 = () -> {
        processor.onNext(1);
        processor.onComplete();
    };
    Runnable r2 = () -> processor.onSubscribe(new Subscriptions.EmptySubscription());

    new Thread(r1).start();
    new Thread(r2).start();

    subscriber.await();
    subscriber
            .assertSubscribed()
            .assertCompletedSuccessfully();

    if (subscriber.items().size() != 0) {
        assertThat(subscriber.items()).containsExactly(1);
    }
}
 
Example 4
Source Project: wurmloch-crdt   Source File: GSetTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldHandleAddCommands() {
    // given:
    final Processor<GSet.AddCommand<String>, GSet.AddCommand<String>> inputStream = ReplayProcessor.create();
    final TestSubscriber<CrdtCommand> subscriber = TestSubscriber.create();
    final GSet<String> set = new GSet<>("ID_1");
    set.subscribeTo(inputStream);
    set.subscribe(subscriber);

    final GSet.AddCommand<String> command1 = new GSet.AddCommand<>(set.getCrdtId(), "1");
    final GSet.AddCommand<String> command2 = new GSet.AddCommand<>(set.getCrdtId(), "2");
    final GSet.AddCommand<String> command3 = new GSet.AddCommand<>(set.getCrdtId(), "1");

    // when:
    inputStream.onNext(command1);
    inputStream.onNext(command2);
    inputStream.onNext(command3);

    // then:
    assertThat(set, hasSize(2));
    assertThat(subscriber.valueCount(), is(2));
    subscriber.assertNotComplete();
    subscriber.assertNoErrors();
}
 
Example 5
Source Project: wurmloch-crdt   Source File: TwoPSetTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldHandleRemoveCommandArrivesBeforeAddCommand() {
    // given:
    final Processor<TwoPSet.TwoPSetCommand<String>, TwoPSet.TwoPSetCommand<String>> inputStream = ReplayProcessor.create();
    final TestSubscriber<CrdtCommand> subscriber = TestSubscriber.create();
    final TwoPSet<String> set = new TwoPSet<>("ID_1");
    set.subscribeTo(inputStream);
    set.subscribe(subscriber);

    final TwoPSet.RemoveCommand<String> command1 = new TwoPSet.RemoveCommand<>(set.getCrdtId(), "1");
    final TwoPSet.AddCommand<String> command2 = new TwoPSet.AddCommand<>(set.getCrdtId(), "1");
    final TwoPSet.AddCommand<String> command3 = new TwoPSet.AddCommand<>(set.getCrdtId(), "1");

    // when:
    inputStream.onNext(command1);
    inputStream.onNext(command2);
    inputStream.onNext(command3);

    // then:
    assertThat(set, empty());
    assertThat(subscriber.valueCount(), is(1));
    subscriber.assertNotComplete();
    subscriber.assertNoErrors();
}
 
Example 6
Source Project: wurmloch-crdt   Source File: TwoPSetTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldHandleAddCommands() {
    // given:
    final Processor<TwoPSet.TwoPSetCommand<String>, TwoPSet.TwoPSetCommand<String>> inputStream = ReplayProcessor.create();
    final TestSubscriber<CrdtCommand> subscriber = TestSubscriber.create();
    final TwoPSet<String> set = new TwoPSet<>("ID_1");
    set.subscribeTo(inputStream);
    set.subscribe(subscriber);

    final TwoPSet.AddCommand<String> command1 = new TwoPSet.AddCommand<>(set.getCrdtId(), "1");
    final TwoPSet.AddCommand<String> command2 = new TwoPSet.AddCommand<>(set.getCrdtId(), "2");
    final TwoPSet.AddCommand<String> command3 = new TwoPSet.AddCommand<>(set.getCrdtId(), "1");

    // when:
    inputStream.onNext(command1);
    inputStream.onNext(command2);
    inputStream.onNext(command3);

    // then:
    assertThat(set, hasSize(2));
    assertThat(subscriber.valueCount(), is(2));
    subscriber.assertNotComplete();
    subscriber.assertNoErrors();
}
 
Example 7
@Override
public <T, R> SubscriberWithCompletionStage<T, R> buildSubscriber(Graph graph) {
    Processor<T, T> processor = new ConnectableProcessor<>();
    Flowable<T> flowable = Flowable.fromPublisher(processor);
    for (Stage stage : graph.getStages()) {
        Operator operator = Stages.lookup(stage);
        if (operator instanceof ProcessorOperator) {
            flowable = applyProcessors(flowable, stage, (ProcessorOperator) operator);
        } else if (operator instanceof TerminalOperator) {
            CompletionStage<R> result = applySubscriber(Transformer.apply(flowable), stage,
                    (TerminalOperator) operator);
            return new DefaultSubscriberWithCompletionStage<>(processor, result);
        } else {
            throw new UnsupportedStageException(stage);
        }
    }

    throw new IllegalArgumentException("The graph does not have a valid final stage");
}
 
Example 8
Source Project: RxBus2   Source File: RxBusSenderBuilder.java    License: Apache License 2.0 6 votes vote down vote up
private boolean sendToKeyBoundBus(RxQueueKey key, Object event)
{
    RxQueueKey keyToUse = key.clone();
    boolean send = false;
    Processor processor;
    if (mKey instanceof String)
        keyToUse.withId((String)mKey);
    else if (mKey instanceof Integer)
        keyToUse.withId((Integer)mKey);
    processor = RxBus.getInstance().getProcessor(keyToUse, false);

    // only send event, if processor exists => this means someone has at least once subscribed to it
    if (processor != null)
    {
        if (mCast == null)
            processor.onNext(event);
        else
            processor.onNext(mCast.cast(event));
        send = true;
    }
    return send;
}
 
Example 9
Source Project: reactor-core   Source File: FluxWindow.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void onError(Throwable t) {
	if (done) {
		Operators.onErrorDropped(t, actual.currentContext());
		return;
	}
	done = true;

	for (Processor<T, T> w : this) {
		w.onError(t);
	}
	clear();

	error = t;
	drain();
}
 
Example 10
Source Project: wurmloch-crdt   Source File: GSetTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldHandleDuplicateCommands() {
    // given:
    final Processor<GSet.AddCommand<String>, GSet.AddCommand<String>> inputStream = ReplayProcessor.create();
    final TestSubscriber<CrdtCommand> subscriber = TestSubscriber.create();
    final GSet<String> set = new GSet<>("ID_1");
    set.subscribeTo(inputStream);
    set.subscribe(subscriber);

    final GSet.AddCommand<String> command = new GSet.AddCommand<>(set.getCrdtId(), "1");

    // when:
    inputStream.onNext(command);
    inputStream.onNext(command);

    // then:
    assertThat(set, hasSize(1));
    assertThat(subscriber.valueCount(), is(1));
    subscriber.assertNotComplete();
    subscriber.assertNoErrors();
}
 
Example 11
Source Project: wurmloch-crdt   Source File: USetTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldHandleRemoveCommands() {
    // given:
    final UUID uuid1 = UUID.randomUUID();
    final Processor<USet.USetCommand<UUID>, USet.USetCommand<UUID>> inputStream = ReplayProcessor.create();
    final TestSubscriber<CrdtCommand> subscriber = TestSubscriber.create();
    final USet<UUID> set = new USet<>("ID_1");
    set.subscribeTo(inputStream);
    set.subscribe(subscriber);

    final USet.AddCommand<UUID> command1 = new USet.AddCommand<>(set.getCrdtId(), uuid1);
    final USet.RemoveCommand<UUID> command2 = new USet.RemoveCommand<>(set.getCrdtId(), uuid1);

    // when:
    inputStream.onNext(command1);
    inputStream.onNext(command2);

    // then:
    assertThat(set, empty());
    assertThat(subscriber.valueCount(), is(2));
    subscriber.assertNotComplete();
    subscriber.assertNoErrors();
}
 
Example 12
Source Project: RxBus2   Source File: RxBusSenderBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private boolean sendToUnboundBus(RxQueueKey key, Object event)
{
    boolean send = false;
    Processor processor = RxBus.getInstance().getProcessor(key, false);
    // only send event, if processor exists => this means someone has at least once subscribed to it
    if (processor != null)
    {
        if (mCast == null)
            processor.onNext(event);
        else
            processor.onNext(mCast.cast(event));
        send = true;
    }
    return send;
}
 
Example 13
Source Project: smallrye-mutiny   Source File: MultiWindowOp.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onFailure(Throwable failure) {
    Subscription subscription = upstream.getAndSet(CANCELLED);
    if (subscription != CANCELLED) {
        Processor<T, T> proc = processor;
        if (proc != null) {
            processor = null;
            proc.onError(failure);
        }
        downstream.onFailure(failure);
    }
}
 
Example 14
Source Project: smallrye-mutiny   Source File: MultiWindowOp.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onCompletion() {
    Subscription subscription = upstream.getAndSet(CANCELLED);
    if (subscription != CANCELLED) {
        Processor<T, T> proc = processor;
        if (proc != null) {
            processor = null;
            proc.onComplete();
        }

        downstream.onCompletion();
    }
}
 
Example 15
Source Project: smallrye-mutiny   Source File: SerializedProcessorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testWithMultipleItems() {
    Processor<Integer, Integer> processor = UnicastProcessor.<Integer> create().serialized();
    MultiAssertSubscriber<Integer> subscriber = MultiAssertSubscriber.create(10);
    processor.subscribe(subscriber);

    Multi.createFrom().range(1, 11).subscribe(processor);

    subscriber
            .assertReceived(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
            .assertCompletedSuccessfully();

    processor.onNext(11);
    processor.onComplete();
}
 
Example 16
Source Project: micrometer   Source File: StatsdMeterRegistryTest.java    License: Apache License 2.0 5 votes vote down vote up
private Consumer<String> toSink(Processor<String, String> lines, int numLines) {
    AtomicInteger latch = new AtomicInteger(numLines);
    return l -> {
        lines.onNext(l);
        if (latch.decrementAndGet() == 0) {
            lines.onComplete();
        }
    };
}
 
Example 17
Source Project: smallrye-mutiny   Source File: SerializedProcessorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testSubscriptionAfterTerminalEvent() {
    final Processor<Integer, Integer> processor = UnicastProcessor.<Integer> create().serialized();
    processor.onComplete();
    Subscription subscription = mock(Subscription.class);
    processor.onSubscribe(subscription);
    verify(subscription).cancel();
}
 
Example 18
@Incoming("count")
@Outgoing("sink")
public Processor<Integer, String> process() {
    return ReactiveStreams.<Integer> builder()
            .map(i -> i + 1)
            .flatMapRsPublisher(i -> Flowable.just(i, i))
            .map(i -> Integer.toString(i))
            .buildRs();
}
 
Example 19
Source Project: reactive-streams-commons   Source File: PublisherWindow.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onError(Throwable t) {
    if (done) {
        UnsignalledExceptions.onErrorDropped(t);
        return;
    }
    Processor<T, T> w = window;
    if (w != null) {
        window = null;
        w.onError(t);
    }
    
    actual.onError(t);
}
 
Example 20
@Incoming(PRE_ACKNOWLEDGMENT)
@Acknowledgment(Acknowledgment.Strategy.PRE_PROCESSING)
@Outgoing("sink-" + PRE_ACKNOWLEDGMENT)
public Processor<Message<String>, Message<String>> processorWithPreAck() {
    return ReactiveStreams.<Message<String>> builder()
            .flatMap(m -> ReactiveStreams.of(Message.of(m.getPayload()), Message.of(m.getPayload())))
            .peek(m -> processed(PRE_ACKNOWLEDGMENT, m))
            .buildRs();
}
 
Example 21
Source Project: quarkus   Source File: ProcessorSignatureTest.java    License: Apache License 2.0 5 votes vote down vote up
@Incoming("A")
@Outgoing("AA")
public Processor<Message<Integer>, Message<String>> process() {
    return ReactiveStreams.<Message<Integer>> builder()
            .map(m -> Message.of(Integer.toString(m.getPayload())))
            .buildRs();
}
 
Example 22
@Incoming("count")
@Outgoing("sink")
public Processor<Message<Integer>, Message<String>> process() {
    return ReactiveStreams.<Message<Integer>> builder()
            .map(Message::getPayload)
            .map(i -> i + 1)
            .flatMapRsPublisher(i -> Flowable.just(i, i))
            .map(i -> Integer.toString(i))
            .map(Message::of)
            .buildRs();
}
 
Example 23
Source Project: RHub   Source File: RxJava2ProcProxy.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected <T> Flowable<T> filter(Processor processor, final Class<T> filterClass) {
    return ((Flowable) processor).filter(new Predicate() {
        @Override
        public boolean test(Object o) {
            return filterClass.isAssignableFrom(o.getClass());
        }
    });
}
 
Example 24
Source Project: micrometer   Source File: StatsdMeterRegistryTest.java    License: Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@EnumSource(StatsdFlavor.class)
void gaugeLineProtocol(StatsdFlavor flavor) {
    final AtomicInteger n = new AtomicInteger(2);
    final StatsdConfig config = configWithFlavor(flavor);

    String line = null;
    switch (flavor) {
        case ETSY:
            line = "myGauge.myTag.val.statistic.value:2|g";
            break;
        case DATADOG:
            line = "my.gauge:2|g|#statistic:value,my.tag:val";
            break;
        case TELEGRAF:
            line = "my_gauge,statistic=value,my_tag=val:2|g";
            break;
        case SYSDIG:
            line = "my.gauge#statistic=value,my.tag=val:2|g";
            break;
        default:
            fail("Unexpected flavor");
    }

    StepVerifier
            .withVirtualTime(() -> {
                final Processor<String, String> lines = lineProcessor();
                registry = StatsdMeterRegistry.builder(config)
                        .clock(clock)
                        .lineSink(toSink(lines))
                        .build();

                registry.gauge("my.gauge", Tags.of("my.tag", "val"), n);
                return lines;
            })
            .then(() -> clock.add(config.step()))
            .thenAwait(config.step())
            .expectNext(line)
            .verifyComplete();
}
 
Example 25
public DefaultWebSocketHttpResponse(HttpVersion version, HttpResponseStatus status, boolean validateHeaders,
                                    Processor<WebSocketFrame, WebSocketFrame> processor,
                                    WebSocketServerHandshakerFactory handshakerFactory) {
    super(version, status, validateHeaders);
    this.processor = processor;
    this.handshakerFactory = handshakerFactory;
}
 
Example 26
@Override
public <I, O> ProcessingStage<I, O> create(Engine engine, Stage.ProcessorStage stage) {
    Processor<I, O> processor = Casts.cast(Objects.requireNonNull(
            Objects.requireNonNull(stage).getRsProcessor()));

    return source -> Flowable.defer(() -> {
        Flowable<O> flowable = Flowable.fromPublisher(processor);
        source.safeSubscribe(processor);
        return flowable;
    });
}
 
Example 27
Source Project: RHub   Source File: AkkaProcProxy.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected Publisher hide(Processor processor) {
    return (Publisher) Source
            .fromPublisher(processor)
            .runWith(Sink.asPublisher(AsPublisher.WITH_FANOUT), mat);
}
 
Example 28
@Incoming(DEFAULT_ACKNOWLEDGMENT)
@Outgoing("sink-" + DEFAULT_ACKNOWLEDGMENT)
public Processor<Message<String>, Message<String>> processorWithAutoAck() {
    return ReactiveStreams.<Message<String>> builder()
            .flatMap(m -> ReactiveStreams.of(Message.of(m.getPayload()), Message.of(m.getPayload())).onComplete(
                    m::ack))
            .peek(m -> processed(DEFAULT_ACKNOWLEDGMENT, m))
            .buildRs();
}
 
Example 29
@Incoming(MANUAL_ACKNOWLEDGMENT)
@Acknowledgment(Acknowledgment.Strategy.MANUAL)
@Outgoing("sink-" + MANUAL_ACKNOWLEDGMENT)
public Processor<Message<String>, Message<String>> processorWithAck() {
    return ReactiveStreams.<Message<String>> builder()
            .flatMapCompletionStage(m -> m.ack().thenApply(x -> m))
            .flatMap(m -> ReactiveStreams.of(Message.of(m.getPayload()), Message.of(m.getPayload())))
            .peek(m -> processed(MANUAL_ACKNOWLEDGMENT, m))
            .buildRs();
}
 
Example 30
Source Project: netty-reactive-streams   Source File: AkkaStreamsUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static <In, Out> Processor<In, Out> flowToProcessor(Flow<In, Out, ?> flow, Materializer materializer) {
    Pair<Subscriber<In>, Publisher<Out>> pair =
            Source.<In>asSubscriber()
                    .via(flow)
                    .toMat(Sink.<Out>asPublisher(AsPublisher.WITH_FANOUT), Keep.<Subscriber<In>, Publisher<Out>>both())
                    .run(materializer);

    return new DelegateProcessor<>(pair.first(), pair.second());
}