Java Code Examples for reactor.core.publisher.Flux#hide()

The following examples show how to use reactor.core.publisher.Flux#hide() . 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
@Test
public void manyToOne() {
    ReactorGreeterGrpc.ReactorGreeterStub stub = ReactorGreeterGrpc.newReactorStub(channel);
    Flux<HelloRequest> requestFlux = Flux.just(HelloRequest.getDefaultInstance());

    if (!expectFusion) {
        requestFlux = requestFlux.hide();
    }

    Mono<HelloResponse> resp = requestFlux.as(stub::sayHelloReqStream);

    StepVerifier.Step<HelloResponse> stepVerifier = StepVerifier.create(resp);

    if (expectFusion) {
        stepVerifier = ((StepVerifier.FirstStep<HelloResponse>) stepVerifier).expectFusion();
    }

    stepVerifier
            .verifyErrorMatches(t -> t instanceof StatusRuntimeException && ((StatusRuntimeException)t).getStatus() == Status.INTERNAL);
}
 
Example 2
@Test
public void manyToMany() {
    ReactorGreeterGrpc.ReactorGreeterStub stub = ReactorGreeterGrpc.newReactorStub(channel);
    Flux<HelloRequest> requestFlux = Flux.just(HelloRequest.getDefaultInstance());

    if (!expectFusion) {
        requestFlux = requestFlux.hide();
    }

    Flux<HelloResponse> resp = requestFlux.transform(stub::sayHelloBothStream);

    StepVerifier.Step<HelloResponse> stepVerifier = StepVerifier.create(resp);

    if (expectFusion) {
        stepVerifier = ((StepVerifier.FirstStep<HelloResponse>) stepVerifier).expectFusion();
    }

    stepVerifier
            .verifyErrorMatches(t -> t instanceof StatusRuntimeException && ((StatusRuntimeException)t).getStatus() == Status.INTERNAL);
}
 
Example 3
@Test
public void manyToOne() throws Exception {
    ReactorGreeterGrpc.ReactorGreeterStub stub = ReactorGreeterGrpc.newReactorStub(channel);
    Flux<HelloRequest> req = Flux.just(
            HelloRequest.newBuilder().setName("a").build(),
            HelloRequest.newBuilder().setName("b").build(),
            HelloRequest.newBuilder().setName("c").build());

    if (!expectFusion) {
        req = req.hide();
    }

    Mono<HelloResponse> resp = req.as(stub::sayHelloReqStream);

    StepVerifier.Step<String> stepVerifier = StepVerifier.create(resp.map(HelloResponse::getMessage));

    if (expectFusion) {
        stepVerifier = ((StepVerifier.FirstStep<String>) stepVerifier).expectFusion();
    }

    stepVerifier
            .expectNext("Hello a and b and c")
            .verifyComplete();
}
 
Example 4
@Test
public void manyToMany() {
    ReactorGreeterGrpc.ReactorGreeterStub stub = ReactorGreeterGrpc.newReactorStub(channel);
    Flux<HelloRequest> req = Flux.just(
            HelloRequest.newBuilder().setName("a").build(),
            HelloRequest.newBuilder().setName("b").build(),
            HelloRequest.newBuilder().setName("c").build(),
            HelloRequest.newBuilder().setName("d").build(),
            HelloRequest.newBuilder().setName("e").build());

    if (!expectFusion) {
        req = req.hide();
    }

    Flux<HelloResponse> resp = req.transform(stub::sayHelloBothStream);

    AtomicReference<String> clientThreadName = new AtomicReference<>();

    StepVerifier.Step<String> stepVerifier = StepVerifier.create(
        resp
            .map(HelloResponse::getMessage)
            .doOnNext(x -> clientThreadName.set(Thread.currentThread().getName()))
    );

    if (expectFusion) {
        stepVerifier = ((StepVerifier.FirstStep<String>) stepVerifier).expectFusion();
    }

    stepVerifier
            .expectNext("Hello a and b", "Hello c and d", "Hello e")
            .verifyComplete();

    assertThat(clientThreadName.get()).isEqualTo("TheGrpcClient");
    assertThat(service.serverThreadName.get()).isEqualTo("TheGrpcServer");
}
 
Example 5
@Test
public void clientCanCancelServerStreamExplicitly() throws InterruptedException {
    serverRule.getServiceRegistry().addService(service);

    AtomicInteger lastNumberConsumed = new AtomicInteger(Integer.MAX_VALUE);
    ReactorNumbersGrpc.ReactorNumbersStub stub = ReactorNumbersGrpc.newReactorStub(serverRule.getChannel());
    Mono<Empty> request = Mono.just(Empty.getDefaultInstance());

    if (!expectFusion) {
        request = request.hide();
    }

    Flux<NumberProto.Number> test =
            request
                .as(stub::responsePressure)
                .doOnNext(number -> {lastNumberConsumed.set(number.getNumber(0)); System.out.println("C: " + number.getNumber(0));})
                .doOnError(throwable -> System.out.println(throwable.getMessage()))
                .doOnComplete(() -> System.out.println("Completed"))
                .doOnCancel(() -> System.out.println("Client canceled"));

    if (!expectFusion) {
        test = test.hide();
    }

    Disposable subscription = test.publish().connect();

    Thread.sleep(1000);
    subscription.dispose();
    Thread.sleep(1000);

    // Cancellation may or may not deliver the last generated message due to delays in the gRPC processing thread
    assertThat(Math.abs(lastNumberConsumed.get() - service.getLastNumberProduced())).isLessThanOrEqualTo(3);
    assertThat(service.wasCanceled()).isTrue();
}
 
Example 6
@Test
public void clientCanCancelServerStreamImplicitly() throws InterruptedException {
    serverRule.getServiceRegistry().addService(service);

    ReactorNumbersGrpc.ReactorNumbersStub stub = ReactorNumbersGrpc.newReactorStub(serverRule.getChannel());
    Mono<Empty> request = Mono.just(Empty.getDefaultInstance());

    if (!expectFusion) {
        request = request.hide();
    }

    Flux<NumberProto.Number> test =
            request
                .as(stub::responsePressure)
                .doOnNext(number -> System.out.println(number.getNumber(0)))
                .doOnError(throwable -> System.out.println(throwable.getMessage()))
                .doOnComplete(() -> System.out.println("Completed"))
                .doOnCancel(() -> System.out.println("Client canceled"))
                .take(10);

    if (!expectFusion) {
        test = test.hide();
    }

    Disposable subscription = test.publish().connect();

    Thread.sleep(1000);

    assertThat(service.wasCanceled()).isTrue();
}
 
Example 7
@Test
public void clientToServerBackpressure() {
    serverRule.getServiceRegistry().addService(service);

    ReactorNumbersGrpc.ReactorNumbersStub stub = ReactorNumbersGrpc.newReactorStub(serverRule.getChannel());

    Flux<NumberProto.Number> reactorRequest = Flux
            .fromIterable(IntStream.range(0, NUMBER_OF_STREAM_ELEMENTS)::iterator)
            .doOnNext(i -> System.out.println(i + " --> "))
            .doOnNext(i -> updateNumberOfWaits(lastValueTime, numberOfWaits))
            .map(BackpressureIntegrationTest::protoNum);

    if (!expectFusion) {
        reactorRequest = reactorRequest.hide();
    }

    Mono<NumberProto.Number> reactorResponse = reactorRequest.as(stub::requestPressure);

    StepVerifier.Step<NumberProto.Number> stepVerifier = StepVerifier.create(reactorResponse);

    if (expectFusion) {
        stepVerifier = ((StepVerifier.FirstStep<NumberProto.Number>) stepVerifier).expectFusion();
    }

    stepVerifier
                .expectNextMatches(v -> v.getNumber(0) == NUMBER_OF_STREAM_ELEMENTS - 1)
                .expectComplete()
                .verify(Duration.ofSeconds(15));

    assertThat(numberOfWaits.get()).isGreaterThan(0L);
}
 
Example 8
@Test
public void bidiResponseBackpressure() {
    serverRule.getServiceRegistry().addService(service);

    ReactorNumbersGrpc.ReactorNumbersStub stub = ReactorNumbersGrpc.newReactorStub(serverRule.getChannel());

    Flux<NumberProto.Number> reactorRequest = Flux.empty();

    if (!expectFusion) {
        reactorRequest = reactorRequest.hide();
    }

    Flux<NumberProto.Number> reactorResponse = reactorRequest.transform(stub::twoWayResponsePressure)
            .doOnNext(n -> System.out.println(n.getNumber(0) + "  <--"))
            .doOnNext(n -> waitIfValuesAreEqual(n.getNumber(0), 3));

    StepVerifier.Step<NumberProto.Number> stepVerifier = StepVerifier.create(reactorResponse);

    if (expectFusion) {
        stepVerifier = ((StepVerifier.FirstStep<NumberProto.Number>) stepVerifier).expectFusion();
    }

    stepVerifier
            .expectNextCount(NUMBER_OF_STREAM_ELEMENTS)
            .expectComplete()
            .verify(Duration.ofSeconds(15));

    assertThat(numberOfWaits.get()).isGreaterThan(0L);
}
 
Example 9
@Test
public void bidiRequestBackpressure() {
    serverRule.getServiceRegistry().addService(service);

    ReactorNumbersGrpc.ReactorNumbersStub stub = ReactorNumbersGrpc.newReactorStub(serverRule.getChannel());

    Flux<NumberProto.Number> reactorRequest = Flux
            .fromIterable(IntStream.range(0, NUMBER_OF_STREAM_ELEMENTS)::iterator)
            .doOnNext(i -> System.out.println(i + " --> "))
            .doOnNext(i -> updateNumberOfWaits(lastValueTime, numberOfWaits))
            .map(BackpressureIntegrationTest::protoNum);

    if (!expectFusion) {
        reactorRequest = reactorRequest.hide();
    }

    Flux<NumberProto.Number> reactorResponse = reactorRequest.transform(stub::twoWayRequestPressure);

    StepVerifier.Step<NumberProto.Number> stepVerifier = StepVerifier.create(reactorResponse);

    if (expectFusion) {
        stepVerifier = ((StepVerifier.FirstStep<NumberProto.Number>) stepVerifier).expectFusion();
    }

    stepVerifier
            .expectNextMatches(v -> v.getNumber(0) == NUMBER_OF_STREAM_ELEMENTS - 1)
            .expectComplete()
            .verify(Duration.ofSeconds(15));

    assertThat(numberOfWaits.get()).isGreaterThan(0L);
}
 
Example 10
@Test
public void manyToMany() throws Exception {
    ReactorGreeterGrpc.ReactorGreeterStub stub = ReactorGreeterGrpc.newReactorStub(channel);
    Flux<HelloRequest> req = Flux.just(
            HelloRequest.newBuilder().setName("a").build(),
            HelloRequest.newBuilder().setName("b").build(),
            HelloRequest.newBuilder().setName("c").build(),
            HelloRequest.newBuilder().setName("d").build(),
            HelloRequest.newBuilder().setName("e").build());

    if (!expectFusion) {
        req = req.hide();
    }

    Flux<HelloResponse> resp = req.transform(stub::sayHelloBothStream);



    StepVerifier.Step<String> stepVerifier = StepVerifier.create(resp.map(HelloResponse::getMessage));

    if (expectFusion) {
        stepVerifier = ((StepVerifier.FirstStep<String>) stepVerifier).expectFusion();
    }

    stepVerifier
            .expectNext("Hello a and b", "Hello c and d", "Hello e")
            .verifyComplete();
}
 
Example 11
@Test
public void serverErrorSignalsUpstreamCancellationManyToOne() {
    serverRule.getServiceRegistry().addService(service);
    ReactorNumbersGrpc.ReactorNumbersStub stub = ReactorNumbersGrpc.newReactorStub(serverRule.getChannel());

    AtomicBoolean upstreamCancel = new AtomicBoolean(false);

    Flux<NumberProto.Number> requestFlux = Flux.range(0, Integer.MAX_VALUE)
                                               .map(this::protoNum)
                                               .doOnCancel(() -> upstreamCancel.set(true));

    if (!expectFusion) {
        requestFlux = requestFlux.hide();
    }

    Mono<NumberProto.Number> observer = requestFlux
                                            .as(stub::requestPressure)
                                            .doOnError(System.out::println)
                                            .doOnSuccess(i -> System.out.println(i.getNumber(0)));

    StepVerifier.Step<NumberProto.Number> stepVerifier = StepVerifier.create(observer);

    if (expectFusion) {
        stepVerifier = ((StepVerifier.FirstStep<NumberProto.Number>) stepVerifier).expectFusion();
    }

    stepVerifier
            .verifyError(StatusRuntimeException.class);

    assertThat(upstreamCancel.get()).isTrue();
}
 
Example 12
@Test
public void serverErrorSignalsUpstreamCancellationBidi() {
    serverRule.getServiceRegistry().addService(service);
    ReactorNumbersGrpc.ReactorNumbersStub stub = ReactorNumbersGrpc.newReactorStub(serverRule.getChannel());

    AtomicBoolean upstreamCancel = new AtomicBoolean(false);

    Flux<NumberProto.Number> requestFlux = Flux.range(0, Integer.MAX_VALUE)
                                               .map(this::protoNum)
                                               .doOnCancel(() -> upstreamCancel.set(true));

    if (!expectFusion) {
        requestFlux = requestFlux.hide();
    }

    Flux<NumberProto.Number> subscriber = requestFlux
                                              .transform(stub::twoWayPressure)
                                              .doOnNext(i -> System.out.println(i.getNumber(0)));

    StepVerifier.Step<NumberProto.Number> stepVerifier = StepVerifier.create(subscriber);

    if (expectFusion) {
        stepVerifier = ((StepVerifier.FirstStep<NumberProto.Number>) stepVerifier).expectFusion();
    }

    stepVerifier
            .verifyError(StatusRuntimeException.class);

    assertThat(upstreamCancel.get()).isTrue();
}
 
Example 13
@Test
public void serverCanCancelClientStreamImplicitly() {
    serverRule.getServiceRegistry().addService(service);

    ReactorNumbersGrpc.ReactorNumbersStub stub = ReactorNumbersGrpc.newReactorStub(serverRule.getChannel());

    service.setExplicitCancel(false);

    AtomicBoolean requestWasCanceled = new AtomicBoolean(false);
    AtomicBoolean requestDidProduce = new AtomicBoolean(false);

    Flux<NumberProto.Number> request = Flux
            .fromIterable(IntStream.range(0, NUMBER_OF_STREAM_ELEMENTS)::iterator)
            .delayElements(Duration.ofMillis(SEQUENCE_DELAY_MILLIS))
            .map(CancellationPropagationIntegrationTest::protoNum)
            .doOnNext(x -> {
                requestDidProduce.set(true);
                System.out.println("Produced: " + x.getNumber(0));
            })
            .doOnCancel(() -> {
                requestWasCanceled.set(true);
                System.out.println("Client canceled");
            });

    if (!expectFusion) {
        request = request.hide();
    }

    Mono<NumberProto.Number> observer = request.as(stub::requestPressure)
            .doOnSuccess(number -> System.out.println(number.getNumber(0)))
            .doOnError(throwable -> System.out.println(throwable.getMessage()));

    StepVerifier.Step<NumberProto.Number> stepVerifier = StepVerifier.create(observer);

    if (expectFusion) {
        stepVerifier = ((StepVerifier.FirstStep<NumberProto.Number>) stepVerifier).expectFusion();
    }

    stepVerifier
            .expectNext(protoNum(9))
            .verifyComplete();

    await().atMost(org.awaitility.Duration.FIVE_HUNDRED_MILLISECONDS).untilTrue(requestWasCanceled);

    assertThat(requestWasCanceled.get()).isTrue();
    assertThat(requestDidProduce.get()).isTrue();
}
 
Example 14
@Test
public void serverCanCancelClientStreamExplicitly() {
    serverRule.getServiceRegistry().addService(service);

    ReactorNumbersGrpc.ReactorNumbersStub stub = ReactorNumbersGrpc.newReactorStub(serverRule.getChannel());

    service.setExplicitCancel(true);

    AtomicBoolean requestWasCanceled = new AtomicBoolean(false);
    AtomicBoolean requestDidProduce = new AtomicBoolean(false);

    Flux<NumberProto.Number> request = Flux
            .fromIterable(IntStream.range(0, NUMBER_OF_STREAM_ELEMENTS)::iterator)
            .delayElements(Duration.ofMillis(SEQUENCE_DELAY_MILLIS))
            .map(CancellationPropagationIntegrationTest::protoNum)
            .doOnNext(n -> {
                requestDidProduce.set(true);
                System.out.println("P: " + n.getNumber(0));
            })
            .doOnCancel(() -> {
                requestWasCanceled.set(true);
                System.out.println("Client canceled");
            });

    if (!expectFusion) {
        request = request.hide();
    }

    Mono<NumberProto.Number> observer = request.as(stub::requestPressure)
            .doOnSuccess(number -> System.out.println(number.getNumber(0)))
            .doOnError(throwable -> System.out.println(throwable.getMessage()));

    StepVerifier.Step<NumberProto.Number> stepVerifier = StepVerifier.create(observer);

    if (expectFusion) {
        stepVerifier = ((StepVerifier.FirstStep<NumberProto.Number>) stepVerifier).expectFusion();
    }

    stepVerifier
            .expectNext(protoNum(-1))
            .verifyComplete();

    await().atMost(org.awaitility.Duration.FIVE_HUNDRED_MILLISECONDS).untilTrue(requestWasCanceled);

    assertThat(requestWasCanceled.get()).isTrue();
    assertThat(requestDidProduce.get()).isTrue();
}
 
Example 15
@Test
public void serverCanCancelClientStreamImplicitlyBidi() {
    serverRule.getServiceRegistry().addService(service);

    ReactorNumbersGrpc.ReactorNumbersStub stub = ReactorNumbersGrpc.newReactorStub(serverRule.getChannel());

    service.setExplicitCancel(false);

    AtomicBoolean requestWasCanceled = new AtomicBoolean(false);
    AtomicBoolean requestDidProduce = new AtomicBoolean(false);

    Flux<NumberProto.Number> request = Flux
            .fromIterable(IntStream.range(0, NUMBER_OF_STREAM_ELEMENTS)::iterator)
            .delayElements(Duration.ofMillis(SEQUENCE_DELAY_MILLIS))
            .map(CancellationPropagationIntegrationTest::protoNum)
            .doOnNext(x -> {
                requestDidProduce.set(true);
                System.out.println("Produced: " + x.getNumber(0));
            })
            .doOnCancel(() -> {
                requestWasCanceled.set(true);
                System.out.println("Client canceled");
            });

    if (!expectFusion) {
        request = request.hide();
    }

    Flux<NumberProto.Number> observer = request.transform(stub::twoWayPressure)
            .doOnNext(number -> System.out.println(number.getNumber(0)))
            .doOnError(throwable -> System.out.println(throwable.getMessage()));

    StepVerifier.Step<NumberProto.Number> stepVerifier = StepVerifier.create(observer);

    if (expectFusion) {
        stepVerifier = ((StepVerifier.FirstStep<NumberProto.Number>) stepVerifier).expectFusion();
    }

    stepVerifier
            .expectNext(protoNum(9))
            .verifyComplete();

    await().atMost(org.awaitility.Duration.FIVE_HUNDRED_MILLISECONDS).untilTrue(requestWasCanceled);

    assertThat(requestWasCanceled.get()).isTrue();
    assertThat(requestDidProduce.get()).isTrue();
}
 
Example 16
@Test
public void serverCanCancelClientStreamExplicitlyBidi() {
    serverRule.getServiceRegistry().addService(service);

    ReactorNumbersGrpc.ReactorNumbersStub stub = ReactorNumbersGrpc.newReactorStub(serverRule.getChannel());

    service.setExplicitCancel(true);

    AtomicBoolean requestWasCanceled = new AtomicBoolean(false);
    AtomicBoolean requestDidProduce = new AtomicBoolean(false);

    Flux<NumberProto.Number> request = Flux
            .fromIterable(IntStream.range(0, NUMBER_OF_STREAM_ELEMENTS)::iterator)
            .delayElements(Duration.ofMillis(SEQUENCE_DELAY_MILLIS))
            .map(CancellationPropagationIntegrationTest::protoNum)
            .doOnNext(n -> {
                requestDidProduce.set(true);
                System.out.println("P: " + n.getNumber(0));
            })
            .doOnCancel(() -> {
                requestWasCanceled.set(true);
                System.out.println("Client canceled");
            });

    if (!expectFusion) {
        request = request.hide();
    }

    Flux<NumberProto.Number> observer = request.transform(stub::twoWayPressure)
            .doOnNext(number -> System.out.println(number.getNumber(0)))
            .doOnError(throwable -> System.out.println(throwable.getMessage()));

    StepVerifier.Step<NumberProto.Number> stepVerifier = StepVerifier.create(observer);

    if (expectFusion) {
        stepVerifier = ((StepVerifier.FirstStep<NumberProto.Number>) stepVerifier).expectFusion();
    }

    stepVerifier
            .expectNext(protoNum(-1))
            .verifyComplete();

    await().atMost(org.awaitility.Duration.FIVE_HUNDRED_MILLISECONDS).untilTrue(requestWasCanceled);

    assertThat(requestWasCanceled.get()).isTrue();
    assertThat(requestDidProduce.get()).isTrue();
}
 
Example 17
Source Project: reactor-core   File: ReduceOperatorTest.java    License: Apache License 2.0 4 votes vote down vote up
@Override
final Flux<I> hide(Flux<I> input) {
	return input.hide();
}
 
Example 18
Source Project: reactor-core   File: FluxOperatorTest.java    License: Apache License 2.0 4 votes vote down vote up
@Override
Flux<I> hide(Flux<I> input) {
	return input.hide();
}