org.eclipse.microprofile.reactive.streams.operators.CompletionSubscriber Java Examples

The following examples show how to use org.eclipse.microprofile.reactive.streams.operators.CompletionSubscriber. 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: APITest.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@Test
public void testBuildingSubscriberFromSpec() throws ExecutionException, InterruptedException {
    Processor<ByteBuffer, MyDomainObject> parser = createParser();

    CompletionSubscriber<ByteBuffer, List<MyDomainObject>> subscriber = ReactiveStreams.<ByteBuffer> builder()
            .via(parser)
            .toList()
            .build();

    CompletionStage<List<MyDomainObject>> result = subscriber.getCompletion();

    List<MyDomainObject> domainObjects = Arrays.asList(new MyDomainObject("Clement", "Neo"),
            new MyDomainObject("Tintin", "Milou"));
    Publisher<ByteBuffer> publisher = ReactiveStreams.fromIterable(domainObjects)
            .map(obj -> String.format("%s,%s\n", obj.field1, obj.field2))
            .map(line -> ByteBuffer.wrap(line.getBytes()))
            .buildRs();

    publisher.subscribe(subscriber);
    List<MyDomainObject> objects = result.toCompletableFuture().get();
    assertThat(objects.toString()).contains("Clement => Neo", "Tintin => Milou");
}
 
Example #2
Source File: EngineTest.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidSubscriber() {
    engine = new Engine();
    CompletionSubscriber<Integer, Optional<Integer>> built = ReactiveStreams.<Integer> builder()
            .map(i -> i + 1)
            .findFirst()
            .build(engine);

    assertThat(built).isNotNull();

    ReactiveStreams.of(5, 4, 3).buildRs().subscribe(built);
    Optional<Integer> integer = built.getCompletion().toCompletableFuture().join();
    assertThat(integer).contains(6);
}
 
Example #3
Source File: CompletionSubscriberVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test
public void completionSubscriberShouldDelegateToSubscriber() {
    Deque<Object> calls = new ArrayDeque<>();
    CompletionSubscriber subscriber = CompletionSubscriber.of(new Subscriber() {
        @Override
        public void onSubscribe(Subscription s) {
            calls.add(s);
        }

        @Override
        public void onNext(Object o) {
            calls.add(o);
        }

        @Override
        public void onError(Throwable t) {
            calls.add(t);
        }

        @Override
        public void onComplete() {
            calls.add("onComplete");
        }
    }, new CompletableFuture<>());

    subscriber.onSubscribe(Mocks.SUBSCRIPTION);
    assertSame(calls.removeFirst(), Mocks.SUBSCRIPTION);
    subscriber.onNext("element");
    assertEquals(calls.removeFirst(), "element");
    Exception e = new Exception();
    subscriber.onError(e);
    assertSame(calls.removeFirst(), e);
    subscriber.onComplete();
    assertEquals(calls.removeFirst(),"onComplete");
    assertTrue(calls.isEmpty());
}
 
Example #4
Source File: CollectorOnly.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
@Incoming("sink")
public Subscriber<Message<String>> sink() {
    CompletionSubscriber<Message<String>, List<Message<String>>> subscriber = ReactiveStreams.<Message<String>> builder()
            .toList().build();
    subscriber.getCompletion().thenAccept(result::addAll);
    return subscriber;
}
 
Example #5
Source File: EngineTest.java    From smallrye-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidSubscriber() {
    engine = new Engine();
    CompletionSubscriber<Integer, Optional<Integer>> built = ReactiveStreams.<Integer> builder()
            .map(i -> i + 1)
            .findFirst()
            .build(engine);

    assertThat(built).isNotNull();

    ReactiveStreams.of(5, 4, 3).buildRs().subscribe(built);
    Optional<Integer> integer = built.getCompletion().toCompletableFuture().join();
    assertThat(integer).contains(6);
}
 
Example #6
Source File: DefaultSubscriberWithCompletionStage.java    From smallrye-mutiny with Apache License 2.0 4 votes vote down vote up
public DefaultSubscriberWithCompletionStage(Processor<T, T> processor, CompletionStage<R> result) {
    subscriber = CompletionSubscriber.of(processor, result);
}
 
Example #7
Source File: CompletionSubscriberVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 4 votes vote down vote up
@Test
public void completionSubscriberShouldReturnSameCompletionStage() {
    CompletableFuture future = new CompletableFuture();
    assertSame(CompletionSubscriber.of(Mocks.SUBSCRIBER, future).getCompletion(), future);
}
 
Example #8
Source File: CompletionSubscriberVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 4 votes vote down vote up
@Test(expectedExceptions = NullPointerException.class)
public void completionSubscriberShouldNotAcceptNullSubscriber() {
    CompletionSubscriber.of(null, new CompletableFuture<>());
}
 
Example #9
Source File: CompletionSubscriberVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 4 votes vote down vote up
@Test(expectedExceptions = NullPointerException.class)
public void completionSubscriberShouldNotAcceptNullCompletionStage() {
    CompletionSubscriber.of(Mocks.SUBSCRIBER, null);
}
 
Example #10
Source File: SubscriberBuilderImpl.java    From microprofile-reactive-streams-operators with Apache License 2.0 4 votes vote down vote up
@Override
public CompletionSubscriber<T, R> build() {
    return build(ReactiveStreamsEngineResolver.instance());
}
 
Example #11
Source File: SubscriberBuilderImpl.java    From microprofile-reactive-streams-operators with Apache License 2.0 4 votes vote down vote up
@Override
public CompletionSubscriber<T, R> build(ReactiveStreamsEngine engine) {
    Objects.requireNonNull(engine, "Engine must not be null");
    SubscriberWithCompletionStage<T, R> subscriberWithCompletionStage = engine.buildSubscriber(toGraph());
    return CompletionSubscriber.of(subscriberWithCompletionStage.getSubscriber(), subscriberWithCompletionStage.getCompletion());
}
 
Example #12
Source File: DefaultSubscriberWithCompletionStage.java    From smallrye-reactive-streams-operators with Apache License 2.0 4 votes vote down vote up
public DefaultSubscriberWithCompletionStage(Processor<T, T> processor, CompletionStage<R> result) {
    subscriber = CompletionSubscriber.of(processor, result);
}