org.eclipse.microprofile.reactive.streams.operators.spi.Stage Java Examples

The following examples show how to use org.eclipse.microprofile.reactive.streams.operators.spi.Stage. 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 File: Engine.java    From smallrye-reactive-streams-operators with Apache License 2.0 6 votes vote down vote up
@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 #2
Source File: Engine.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@Override
public <T> CompletionStage<T> buildCompletion(Graph graph) {
    Multi<?> flowable = null;
    for (Stage stage : graph.getStages()) {
        Operator operator = Stages.lookup(stage);
        if (operator instanceof PublisherOperator) {
            flowable = createPublisher(stage, (PublisherOperator) operator);
        } else if (operator instanceof ProcessorOperator) {
            flowable = applyProcessors(flowable, stage, (ProcessorOperator) operator);
        } else {
            return applySubscriber(flowable, stage, (TerminalOperator) operator);
        }
    }

    throw new IllegalArgumentException("Graph did not have terminal stage");
}
 
Example #3
Source File: CancelStageFactoryTest.java    From smallrye-reactive-streams-operators with Apache License 2.0 6 votes vote down vote up
@Test
public void create() throws ExecutionException, InterruptedException {
    TerminalStage<Long, Void> terminal = factory.create(null, new Stage.Cancel() {
    });
    AtomicBoolean cancelled = new AtomicBoolean();
    List<Long> list = new ArrayList<>();
    Flowable<Long> flowable = Flowable.interval(1000, TimeUnit.MILLISECONDS)
            .observeOn(Schedulers.io())
            .doOnNext(list::add)
            .doOnCancel(() -> cancelled.set(true));
    CompletionStage<Void> stage = terminal.apply(flowable);
    stage.toCompletableFuture().get();

    await().untilAtomic(cancelled, is(true));
    assertThat(list).isEmpty();
    assertThat(cancelled).isTrue();
}
 
Example #4
Source File: CancelStageFactoryTest.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@Test
public void create() throws ExecutionException, InterruptedException {
    TerminalStage<Long, Void> terminal = factory.create(null, new Stage.Cancel() {
    });
    AtomicBoolean cancelled = new AtomicBoolean();
    List<Long> list = new ArrayList<>();
    Multi<Long> publisher = Multi.createFrom().ticks().every(Duration.ofMillis(1000))
            .emitOn(Infrastructure.getDefaultExecutor())
            .onItem().invoke(list::add)
            .on().cancellation(() -> cancelled.set(true));
    CompletionStage<Void> stage = terminal.apply(publisher);
    stage.toCompletableFuture().get();

    await().untilAtomic(cancelled, is(true));
    assertThat(list).isEmpty();
    assertThat(cancelled).isTrue();
}
 
Example #5
Source File: ConcatStageFactoryTest.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@Test(expected = NullPointerException.class)
public void testWithoutEngine() {
    Graph g1 = () -> Collections.singletonList((Stage.Of) () -> Arrays.asList(1, 2, 3));
    Graph g2 = () -> Collections.singletonList((Stage.Of) () -> Arrays.asList(1, 2, 3));
    factory.create(null, new Stage.Concat() {
        @Override
        public Graph getFirst() {
            return g1;
        }

        @Override
        public Graph getSecond() {
            return g2;
        }
    });
}
 
Example #6
Source File: Engine.java    From smallrye-reactive-streams-operators with Apache License 2.0 6 votes vote down vote up
@Override
public <T> Publisher<T> buildPublisher(Graph graph) {
    Flowable<T> flowable = null;
    for (Stage stage : graph.getStages()) {
        Operator operator = Stages.lookup(stage);
        if (flowable == null) {
            if (operator instanceof PublisherOperator) {
                flowable = createPublisher(stage, (PublisherOperator) operator);
            } else {
                throw new IllegalArgumentException("Expecting a publisher stage, got a " + stage);
            }
        } else {
            if (operator instanceof ProcessorOperator) {
                flowable = applyProcessors(flowable, stage, (ProcessorOperator) operator);
            } else {
                throw new IllegalArgumentException("Expecting a processor stage, got a " + stage);
            }
        }
    }
    return flowable;
}
 
Example #7
Source File: ConcatStageFactoryTest.java    From smallrye-reactive-streams-operators with Apache License 2.0 6 votes vote down vote up
@Test(expected = NullPointerException.class)
public void testWithoutEngine() {
    Graph g1 = () -> Collections.singletonList((Stage.Of) () -> Arrays.asList(1, 2, 3));
    Graph g2 = () -> Collections.singletonList((Stage.Of) () -> Arrays.asList(1, 2, 3));
    factory.create(null, new Stage.Concat() {
        @Override
        public Graph getFirst() {
            return g1;
        }

        @Override
        public Graph getSecond() {
            return g2;
        }
    });
}
 
Example #8
Source File: Engine.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@Override
public <T> Publisher<T> buildPublisher(Graph graph) {
    Multi<T> publisher = null;
    for (Stage stage : graph.getStages()) {
        Operator operator = Stages.lookup(stage);
        if (publisher == null) {
            if (operator instanceof PublisherOperator) {
                publisher = createPublisher(stage, (PublisherOperator) operator);
            } else {
                throw new IllegalArgumentException("Expecting a publisher stage, got a " + stage);
            }
        } else {
            if (operator instanceof ProcessorOperator) {
                publisher = applyProcessors(publisher, stage, (ProcessorOperator) operator);
            } else {
                throw new IllegalArgumentException("Expecting a processor stage, got a " + stage);
            }
        }
    }
    return publisher;
}
 
Example #9
Source File: EngineTest.java    From smallrye-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidPublisher() {
    engine = new Engine();
    List<Stage> stages = new ArrayList<>();
    stages.add((Stage.Of) () -> Arrays.asList(1, 2, 3));
    stages.add((Stage.Map) () -> i -> (int) i + 1);
    Graph graph = () -> stages;
    assertThat(engine.buildPublisher(graph)).isNotNull();
}
 
Example #10
Source File: PublisherBuilderVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test
public void collectComponents() {
    Supplier supplier = () -> null;
    BiConsumer accumulator = (a, b) -> {};
    Graph graph = graphFor(builder().collect(supplier, accumulator));
    Collector collector = getAddedStage(Stage.Collect.class, graph).getCollector();
    assertSame(collector.supplier(), supplier);
    assertSame(collector.accumulator(), accumulator);
    // Finisher must be identity function
    Object myObject = new Object();
    assertSame(collector.finisher().apply(myObject), myObject);
}
 
Example #11
Source File: ProcessorBuilderVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test
public void toList() {
    Graph graph = graphFor(builder().toList());
    Collector collector = getAddedStage(Stage.Collect.class, graph).getCollector();
    Object container = collector.supplier().get();
    collector.accumulator().accept(container, 1);
    collector.accumulator().accept(container, 2);
    collector.accumulator().accept(container, 3);
    assertEquals(collector.finisher().apply(container), Arrays.asList(1, 2, 3));
}
 
Example #12
Source File: OnTerminateStageFactory.java    From smallrye-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <I, O> ProcessingStage<I, O> create(Engine engine, Stage.OnTerminate stage) {
    Runnable runnable = Objects.requireNonNull(stage).getAction();
    Objects.requireNonNull(runnable);
    // Interesting issue when using onTerminate, the TCK fails because the issue is reported twice
    // First, the onComplete "part" is called, throws an exception, and then call the doOnError part
    // which throws another exception.
    // Anyway, we should also configure the cancellation callback.
    return source -> (Flowable<O>) source
            .doOnError(t -> runnable.run())
            .doOnComplete(runnable::run)
            .doOnCancel(runnable::run);
}
 
Example #13
Source File: ProcessorBuilderVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test
public void toBuilderFromDifferentReactiveStreamsImplementation() {
    Graph graph = graphFor(builder().to(Mocks.SUBSCRIBER_BUILDER));
    assertEquals(graph.getStages().size(), 3);
    Iterator<Stage> stages = graph.getStages().iterator();
    assertTrue(stages.next() instanceof Stage.Map);
    assertTrue(stages.next() instanceof Stage.Distinct);
    assertTrue(stages.next() instanceof Stage.Cancel);
}
 
Example #14
Source File: PublisherBuilderVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test
public void onErrorResumeWith() {
    Graph graph = graphFor(builder().onErrorResumeWith(t -> rs.empty()));
    Graph resumeWith = getAddedStage(Stage.OnErrorResumeWith.class, graph).getFunction().apply(new RuntimeException());
    assertEquals(resumeWith.getStages().size(), 1);
    assertEmptyStage(resumeWith.getStages().iterator().next());
}
 
Example #15
Source File: ReactiveStreamsVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test
public void iterate() {
    Graph graph = graphFor(rs.iterate(1, i -> i + 1));
    Iterator iter = getStage(Stage.Of.class, graph).getElements().iterator();
    assertTrue(iter.hasNext());
    assertEquals(iter.next(), 1);
    assertTrue(iter.hasNext());
    assertEquals(iter.next(), 2);
    assertTrue(iter.hasNext());
    assertEquals(iter.next(), 3);
}
 
Example #16
Source File: FilterStageFactory.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <I, O> ProcessingStage<I, O> create(Engine engine, Stage.Filter stage) {
    Objects.requireNonNull(stage);
    Predicate predicate = Objects.requireNonNull(stage.getPredicate());
    return source -> (Multi<O>) source.transform().byFilteringItemsWith(predicate);
}
 
Example #17
Source File: FilterStageFactory.java    From smallrye-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <I, O> ProcessingStage<I, O> create(Engine engine, Stage.Filter stage) {
    Objects.requireNonNull(stage);
    Predicate predicate = Objects.requireNonNull(stage.getPredicate());
    return source -> (Flowable<O>) source.filter(predicate::test);
}
 
Example #18
Source File: ProcessorBuilderVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test
public void collectComponents() {
    Supplier supplier = () -> null;
    BiConsumer accumulator = (a, b) -> {};
    Graph graph = graphFor(builder().collect(supplier, accumulator));
    Collector collector = getAddedStage(Stage.Collect.class, graph).getCollector();
    assertSame(collector.supplier(), supplier);
    assertSame(collector.accumulator(), accumulator);
    // Finisher must be identity function
    Object myObject = new Object();
    assertSame(collector.finisher().apply(myObject), myObject);
}
 
Example #19
Source File: PublisherBuilderVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test
public void flatMapRsPublisher() {
    Graph graph = graphFor(builder().flatMapRsPublisher(i -> Mocks.PUBLISHER));
    Function flatMap = getAddedStage(Stage.FlatMap.class, graph).getMapper();
    Object result = flatMap.apply(1);
    assertTrue(result instanceof Graph);
    Graph innerGraph = (Graph) result;
    assertEquals(innerGraph.getStages().size(), 1);
    Stage inner = innerGraph.getStages().iterator().next();
    assertTrue(inner instanceof Stage.PublisherStage);
    assertEquals(((Stage.PublisherStage) inner).getRsPublisher(), Mocks.PUBLISHER);
}
 
Example #20
Source File: ProcessorBuilderVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test
public void onErrorResumeWith() {
    Graph graph = graphFor(builder().onErrorResumeWith(t -> rs.empty()));
    Graph resumeWith = getAddedStage(Stage.OnErrorResumeWith.class, graph).getFunction().apply(new RuntimeException());
    assertEquals(resumeWith.getStages().size(), 1);
    assertEmptyStage(resumeWith.getStages().iterator().next());
}
 
Example #21
Source File: OnErrorResumeStageFactory.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <I, O> ProcessingStage<I, O> create(Engine engine, Stage.OnErrorResume stage) {
    Function<Throwable, I> function = (Function<Throwable, I>) Objects.requireNonNull(stage).getFunction();
    Objects.requireNonNull(function);
    return source -> (Multi<O>) source.onFailure().recoverWithItem(function);
}
 
Example #22
Source File: EngineTest.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@Test(expected = UnsupportedStageException.class)
public void testCompletionWithUnknownStage() {
    engine = new Engine();
    List<Stage> stages = new ArrayList<>();
    stages.add((Stage.PublisherStage) () -> Multi.createFrom().empty());
    stages.add((Stage.Map) () -> i -> (int) i + 1);
    stages.add(new Stage() {
        // Unknown stage.
    });
    stages.add(new Stage.FindFirst() {
    });
    Graph graph = () -> stages;
    assertThat(engine.buildCompletion(graph)).isNotNull();
}
 
Example #23
Source File: SubscriberStageFactory.java    From smallrye-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <I, O> TerminalStage<I, O> create(Engine engine, Stage.SubscriberStage stage) {
    Subscriber<I> subscriber = (Subscriber<I>) Objects.requireNonNull(stage).getRsSubscriber();
    Objects.requireNonNull(subscriber);
    return (TerminalStage<I, O>) new SubscriberStage<>(subscriber);
}
 
Example #24
Source File: EngineTest.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testInvalidPublisher() {
    engine = new Engine();
    List<Stage> stages = new ArrayList<>();
    stages.add((Stage.Map) () -> i -> (int) i + 1);
    stages.add(new Stage.FindFirst() {
    });
    // This graph is closed, invalid as publisher
    Graph graph = () -> stages;
    engine.buildPublisher(graph);
}
 
Example #25
Source File: EngineTest.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@Test(expected = UnsupportedStageException.class)
public void testCreatingPublisherWithUnknownStage() {
    engine = new Engine();
    List<Stage> stages = new ArrayList<>();
    stages.add(new Stage() {
        // Unknown stage.
    });
    stages.add((Stage.Map) () -> i -> (int) i + 1);
    Graph graph = () -> stages;
    engine.buildPublisher(graph);
}
 
Example #26
Source File: PublisherBuilderVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test
public void peek() {
    AtomicInteger peeked = new AtomicInteger();
    Graph graph = graphFor(builder().peek(peeked::set));
    ((Consumer) getAddedStage(Stage.Peek.class, graph).getConsumer()).accept(1);
    assertEquals(peeked.get(), 1);
}
 
Example #27
Source File: ProcessorBuilderVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test
public void peek() {
    AtomicInteger peeked = new AtomicInteger();
    Graph graph = graphFor(builder().peek(peeked::set));
    ((Consumer) getAddedStage(Stage.Peek.class, graph).getConsumer()).accept(1);
    assertEquals(peeked.get(), 1);
}
 
Example #28
Source File: EngineTest.java    From smallrye-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testInvalidPublisher() {
    engine = new Engine();
    List<Stage> stages = new ArrayList<>();
    stages.add((Stage.Map) () -> i -> (int) i + 1);
    stages.add(new Stage.FindFirst() {
    });
    // This graph is closed, invalid as publisher
    Graph graph = () -> stages;
    engine.buildPublisher(graph);
}
 
Example #29
Source File: ProcessorBuilderVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test
public void flatMapRsPublisher() {
    Graph graph = graphFor(builder().flatMapRsPublisher(i -> Mocks.PUBLISHER));
    Function flatMap = getAddedStage(Stage.FlatMap.class, graph).getMapper();
    Object result = flatMap.apply(1);
    assertTrue(result instanceof Graph);
    Graph innerGraph = (Graph) result;
    assertEquals(innerGraph.getStages().size(), 1);
    Stage inner = innerGraph.getStages().iterator().next();
    assertTrue(inner instanceof Stage.PublisherStage);
    assertEquals(((Stage.PublisherStage) inner).getRsPublisher(), Mocks.PUBLISHER);
}
 
Example #30
Source File: EngineTest.java    From smallrye-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test(expected = UnsupportedStageException.class)
public void testUnknownTerminalStage() {
    engine = new Engine();
    List<Stage> stages = new ArrayList<>();
    stages.add((Stage.Map) () -> i -> (int) i + 1);
    stages.add(new Stage() {
        // Unknown stage
    });
    Graph graph = () -> stages;
    engine.buildSubscriber(graph);
}