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

The following examples show how to use reactor.core.publisher.Flux#as() . 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: ChannelSendOperatorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void errorAfterMultipleItems() throws Exception {
	IllegalStateException error = new IllegalStateException("boo");
	Flux<String> publisher = Flux.generate(() -> 0, (idx , subscriber) -> {
		int i = ++idx;
		subscriber.next(String.valueOf(i));
		if (i == 3) {
			subscriber.error(error);
		}
		return i;
	});
	Mono<Void> completion = publisher.as(this::sendOperator);
	Signal<Void> signal = completion.materialize().block();

	assertNotNull(signal);
	assertSame("Unexpected signal: " + signal, error, signal.getThrowable());

	assertEquals(3, this.writer.items.size());
	assertEquals("1", this.writer.items.get(0));
	assertEquals("2", this.writer.items.get(1));
	assertEquals("3", this.writer.items.get(2));
	assertSame(error, this.writer.error);
}
 
Example 2
Source File: ChannelSendOperatorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void errorAfterMultipleItems() throws Exception {
	IllegalStateException error = new IllegalStateException("boo");
	Flux<String> publisher = Flux.generate(() -> 0, (idx , subscriber) -> {
		int i = ++idx;
		subscriber.next(String.valueOf(i));
		if (i == 3) {
			subscriber.error(error);
		}
		return i;
	});
	Mono<Void> completion = publisher.as(this::sendOperator);
	Signal<Void> signal = completion.materialize().block();

	assertNotNull(signal);
	assertSame("Unexpected signal: " + signal, error, signal.getThrowable());

	assertEquals(3, this.writer.items.size());
	assertEquals("1", this.writer.items.get(0));
	assertEquals("2", this.writer.items.get(1));
	assertEquals("3", this.writer.items.get(2));
	assertSame(error, this.writer.error);
}
 
Example 3
Source File: ServerErrorIntegrationTest.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@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 4
Source File: UnaryZeroMessageResponseIntegrationTest.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void zeroMessageResponseManyToOne() {
    serverRule.getServiceRegistry().addService(new MissingUnaryResponseService());

    ReactorGreeterGrpc.ReactorGreeterStub stub = ReactorGreeterGrpc.newReactorStub(serverRule.getChannel());
    Flux<HelloRequest> req = Flux.just(
            HelloRequest.newBuilder().setName("a").build(),
            HelloRequest.newBuilder().setName("b").build(),
            HelloRequest.newBuilder().setName("c").build());

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

    StepVerifier.create(resp).verifyErrorMatches(t ->
            t instanceof StatusRuntimeException &&
            ((StatusRuntimeException) t).getStatus().getCode() == Status.Code.CANCELLED);
}
 
Example 5
Source File: UnaryZeroMessageResponseIntegrationTest.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void reactorZeroMessageResponseManyToOne() {
    serverRule.getServiceRegistry().addService(new ReactorMissingUnaryResponseService());

    ReactorGreeterGrpc.ReactorGreeterStub stub = ReactorGreeterGrpc.newReactorStub(serverRule.getChannel());
    Flux<HelloRequest> req = Flux.just(
            HelloRequest.newBuilder().setName("a").build(),
            HelloRequest.newBuilder().setName("b").build(),
            HelloRequest.newBuilder().setName("c").build());

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

    StepVerifier.create(resp).verifyErrorMatches(t ->
            t instanceof StatusRuntimeException &&
                    ((StatusRuntimeException) t).getStatus().getCode() == Status.Code.CANCELLED);
}
 
Example 6
Source File: EndToEndIntegrationTest.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@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 7
Source File: UnexpectedServerErrorIntegrationTest.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void manyToOne() {
    ReactorGreeterGrpc.ReactorGreeterStub stub = ReactorGreeterGrpc.newReactorStub(channel);
    Flux<HelloRequest> req = Flux.just(HelloRequest.getDefaultInstance());
    Mono<HelloResponse> resp = req.as(stub::sayHelloReqStream);

    StepVerifier.create(resp)
            .verifyErrorMatches(t -> t instanceof StatusRuntimeException && ((StatusRuntimeException)t).getStatus().getCode() == Status.Code.INTERNAL);
}
 
Example 8
Source File: BackpressureIntegrationTest.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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);
}