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

The following examples show how to use reactor.core.publisher.Flux#blockFirst() . 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: TestcontainersR2DBCConnectionFactoryTest.java    From testcontainers-java with MIT License 7 votes vote down vote up
@Test
public void reusesUntilConnectionFactoryIsClosed() {
    String url = "r2dbc:tc:postgresql:///db?TC_IMAGE_TAG=10-alpine";
    ConnectionFactory connectionFactory = ConnectionFactories.get(url);

    Integer updated = Flux
        .usingWhen(
            connectionFactory.create(),
            connection -> {
                return Mono
                    .from(connection.createStatement("CREATE TABLE test(id integer PRIMARY KEY)").execute())
                    .thenMany(connection.createStatement("INSERT INTO test(id) VALUES(123)").execute())
                    .flatMap(Result::getRowsUpdated);
            },
            Connection::close
        )
        .blockFirst();

    assertThat(updated).isEqualTo(1);

    Flux<Long> select = Flux
        .usingWhen(
            Flux.defer(connectionFactory::create),
            connection -> {
                return Flux
                    .from(connection.createStatement("SELECT COUNT(*) FROM test").execute())
                    .flatMap(it -> it.map((row, meta) -> (Long) row.get(0)));
            },
            Connection::close
        );

    Long rows = select.blockFirst();

    assertThat(rows).isEqualTo(1);

    close(connectionFactory);

    Assertions
        .assertThatThrownBy(select::blockFirst)
        .isInstanceOf(PostgresqlException.class)
        // relation "X" does not exists
        // https://github.com/postgres/postgres/blob/REL_10_0/src/backend/utils/errcodes.txt#L349
        .returns("42P01", e -> ((PostgresqlException) e).getErrorDetails().getCode());
}
 
Example 2
Source File: MultiConvertToTest.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testCreatingAFluxWithFailure() {
    Flux<Integer> flux = Multi.createFrom().<Integer> failure(new IOException("boom")).convert()
            .with(MultiReactorConverters.toFlux());
    assertThat(flux).isNotNull();
    try {
        flux.blockFirst();
        fail("Exception expected");
    } catch (Exception e) {
        assertThat(e).isInstanceOf(RuntimeException.class).hasCauseInstanceOf(IOException.class);
    }
}
 
Example 3
Source File: UniConvertToTest.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreatingAFluxWithFailure() {
    Flux<Integer> flux = Uni.createFrom().<Integer> failure(new IOException("boom")).convert()
            .with(UniReactorConverters.toFlux());
    assertThat(flux).isNotNull();
    try {
        flux.blockFirst();
        fail("Exception expected");
    } catch (Exception e) {
        assertThat(e).isInstanceOf(RuntimeException.class).hasCauseInstanceOf(IOException.class);
    }
}
 
Example 4
Source File: ExpressionReactiveAccessDecisionManager.java    From spring-security-reactive with Apache License 2.0 5 votes vote down vote up
@Override
public Mono<Boolean> decide(Authentication authentication, ServerWebExchange object, Flux<ConfigAttribute> configAttributes) {
	ConfigAttribute attribute = configAttributes.blockFirst();
	EvaluationContext context = handler.createEvaluationContext(authentication, object);
	Expression expression = handler.getExpressionParser().parseExpression(attribute.getAttribute());
	return Mono.just(ExpressionUtils.evaluateAsBoolean(expression, context));
}
 
Example 5
Source File: SpringFunctionAdapterInitializerTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void functionApp() {
	this.initializer = new AbstractSpringFunctionAdapterInitializer<Object>(FluxFunctionApp.class) {

	};
	this.initializer.initialize(null);
	Flux<?> result = Flux.from(this.initializer.apply(Flux.just(new Foo())));
	Object o = result.blockFirst();
	assertThat(result.blockFirst()).isInstanceOf(Bar.class);
}
 
Example 6
Source File: SleuthSpanCreatorAspectFluxTests.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnNewSpanFromTraceContext() {
	Flux<Long> flux = this.testBean.newSpanInTraceContext();
	Long newSpanId = flux.blockFirst();

	Awaitility.await().untilAsserted(() -> {
		then(this.spans).hasSize(1);
		then(this.spans.get(0).name()).isEqualTo("span-in-trace-context");
		then(this.spans.get(0).id()).isEqualTo(toHexString(newSpanId));
		then(this.tracer.currentSpan()).isNull();
	});
}
 
Example 7
Source File: SleuthSpanCreatorAspectFluxTests.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnNewSpanFromSubscriberContext() {
	Flux<Long> flux = this.testBean.newSpanInSubscriberContext();
	Long newSpanId = flux.blockFirst();

	Awaitility.await().untilAsserted(() -> {
		then(this.spans).hasSize(1);
		then(this.spans.get(0).name()).isEqualTo("span-in-subscriber-context");
		then(this.spans.get(0).id()).isEqualTo(toHexString(newSpanId));
		then(this.tracer.currentSpan()).isNull();
	});
}
 
Example 8
Source File: FluxFromRSPublisherTest.java    From smallrye-reactive-streams-operators with Apache License 2.0 4 votes vote down vote up
@Override
protected String getOne(Flux instance) {
    return (String) instance.blockFirst();
}
 
Example 9
Source File: FluxFromCompletionStageTest.java    From smallrye-reactive-streams-operators with Apache License 2.0 4 votes vote down vote up
@Override
protected String getOne(Flux instance) {
    return (String) instance.blockFirst();
}