Search in sources :

Example 1 with ConnectionPool

use of io.r2dbc.pool.ConnectionPool in project spring-boot by spring-projects.

the class ConnectionPoolMetricsTests method connectionFactoryIsInstrumented.

@Test
void connectionFactoryIsInstrumented() {
    SimpleMeterRegistry registry = new SimpleMeterRegistry();
    ConnectionPool connectionPool = new ConnectionPool(ConnectionPoolConfiguration.builder(this.connectionFactory).initialSize(3).maxSize(7).build());
    ConnectionPoolMetrics metrics = new ConnectionPoolMetrics(connectionPool, "test-pool", Tags.of(testTag, regionTag));
    metrics.bindTo(registry);
    // acquire two connections
    connectionPool.create().as(StepVerifier::create).expectNextCount(1).verifyComplete();
    connectionPool.create().as(StepVerifier::create).expectNextCount(1).verifyComplete();
    assertGauge(registry, "r2dbc.pool.acquired", 2);
    assertGauge(registry, "r2dbc.pool.allocated", 3);
    assertGauge(registry, "r2dbc.pool.idle", 1);
    assertGauge(registry, "r2dbc.pool.pending", 0);
    assertGauge(registry, "r2dbc.pool.max.allocated", 7);
    assertGauge(registry, "r2dbc.pool.max.pending", Integer.MAX_VALUE);
}
Also used : ConnectionPool(io.r2dbc.pool.ConnectionPool) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 2 with ConnectionPool

use of io.r2dbc.pool.ConnectionPool in project spring-boot by spring-projects.

the class R2dbcAutoConfigurationTests method configureWithUrlAndDefaultDoNotOverrideDefaultTimeouts.

@Test
void configureWithUrlAndDefaultDoNotOverrideDefaultTimeouts() {
    this.contextRunner.withPropertyValues("spring.r2dbc.url:r2dbc:h2:mem:///" + randomDatabaseName()).run((context) -> {
        assertThat(context).hasSingleBean(ConnectionFactory.class).hasSingleBean(ConnectionPool.class).hasSingleBean(R2dbcProperties.class);
        ConnectionPool connectionPool = context.getBean(ConnectionPool.class);
        assertThat(connectionPool).hasFieldOrPropertyWithValue("maxAcquireTime", Duration.ofNanos(-1));
    });
}
Also used : ConnectionPool(io.r2dbc.pool.ConnectionPool) Test(org.junit.jupiter.api.Test)

Example 3 with ConnectionPool

use of io.r2dbc.pool.ConnectionPool in project spring-boot by spring-projects.

the class R2dbcAutoConfigurationTests method configureWithUrlPoolAndNoPoolPropertiesCreatesPool.

@Test
void configureWithUrlPoolAndNoPoolPropertiesCreatesPool() {
    this.contextRunner.withPropertyValues("spring.r2dbc.url:r2dbc:pool:h2:mem:///" + randomDatabaseName() + "?maxSize=12").run((context) -> {
        assertThat(context).hasSingleBean(ConnectionFactory.class).hasSingleBean(ConnectionPool.class);
        ConnectionPool connectionPool = context.getBean(ConnectionPool.class);
        assertThat(connectionPool.getMetrics().get().getMaxAllocatedSize()).isEqualTo(12);
    });
}
Also used : ConnectionPool(io.r2dbc.pool.ConnectionPool) ConnectionFactory(io.r2dbc.spi.ConnectionFactory) SimpleTestConnectionFactory(org.springframework.boot.autoconfigure.r2dbc.SimpleConnectionFactoryProvider.SimpleTestConnectionFactory) OptionsCapableConnectionFactory(org.springframework.boot.r2dbc.OptionsCapableConnectionFactory) H2ConnectionFactory(io.r2dbc.h2.H2ConnectionFactory) Test(org.junit.jupiter.api.Test)

Example 4 with ConnectionPool

use of io.r2dbc.pool.ConnectionPool in project spring-boot by spring-projects.

the class R2dbcAutoConfigurationTests method configureWithPoolInvokeOptionCustomizer.

@Test
void configureWithPoolInvokeOptionCustomizer() {
    this.contextRunner.withPropertyValues("spring.r2dbc.url:r2dbc:simple://host/database").withUserConfiguration(CustomizerConfiguration.class).run((context) -> {
        assertThat(context).hasSingleBean(ConnectionFactory.class).hasSingleBean(ConnectionPool.class);
        ConnectionFactory pool = context.getBean(ConnectionFactory.class);
        ConnectionFactory connectionFactory = ((ConnectionPool) pool).unwrap();
        assertThat(connectionFactory).asInstanceOf(type(OptionsCapableConnectionFactory.class)).extracting(OptionsCapableConnectionFactory::getOptions).satisfies((options) -> assertThat(options.getRequiredValue(Option.valueOf("customized"))).isEqualTo(Boolean.TRUE));
    });
}
Also used : ConnectionPool(io.r2dbc.pool.ConnectionPool) ConnectionFactory(io.r2dbc.spi.ConnectionFactory) SimpleTestConnectionFactory(org.springframework.boot.autoconfigure.r2dbc.SimpleConnectionFactoryProvider.SimpleTestConnectionFactory) OptionsCapableConnectionFactory(org.springframework.boot.r2dbc.OptionsCapableConnectionFactory) H2ConnectionFactory(io.r2dbc.h2.H2ConnectionFactory) OptionsCapableConnectionFactory(org.springframework.boot.r2dbc.OptionsCapableConnectionFactory) Test(org.junit.jupiter.api.Test)

Example 5 with ConnectionPool

use of io.r2dbc.pool.ConnectionPool in project spring-boot by spring-projects.

the class ConnectionFactoryBuilderTests method buildWhenDerivedFromPoolReturnsNewNonPooledConnectionFactory.

@Test
void buildWhenDerivedFromPoolReturnsNewNonPooledConnectionFactory() {
    ConnectionFactory connectionFactory = ConnectionFactoryBuilder.withUrl(EmbeddedDatabaseConnection.H2.getUrl(UUID.randomUUID().toString())).build();
    ConnectionFactoryOptions initialOptions = ((OptionsCapableConnectionFactory) connectionFactory).getOptions();
    ConnectionPoolConfiguration poolConfiguration = ConnectionPoolConfiguration.builder(connectionFactory).build();
    ConnectionPool pool = new ConnectionPool(poolConfiguration);
    ConnectionFactory derived = ConnectionFactoryBuilder.derivedFrom(pool).username("admin").password("secret").build();
    assertThat(derived).isNotInstanceOf(ConnectionPool.class).isInstanceOf(OptionsCapableConnectionFactory.class);
    ConnectionFactoryOptions derivedOptions = ((OptionsCapableConnectionFactory) derived).getOptions();
    assertThat(derivedOptions.getRequiredValue(ConnectionFactoryOptions.USER)).isEqualTo("admin");
    assertThat(derivedOptions.getRequiredValue(ConnectionFactoryOptions.PASSWORD)).isEqualTo("secret");
    assertMatchingOptions(derivedOptions, initialOptions, ConnectionFactoryOptions.CONNECT_TIMEOUT, ConnectionFactoryOptions.DATABASE, ConnectionFactoryOptions.DRIVER, ConnectionFactoryOptions.HOST, ConnectionFactoryOptions.PORT, ConnectionFactoryOptions.PROTOCOL, ConnectionFactoryOptions.SSL);
}
Also used : ConnectionPool(io.r2dbc.pool.ConnectionPool) ConnectionFactory(io.r2dbc.spi.ConnectionFactory) ConnectionFactoryOptions(io.r2dbc.spi.ConnectionFactoryOptions) ConnectionPoolConfiguration(io.r2dbc.pool.ConnectionPoolConfiguration) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

ConnectionPool (io.r2dbc.pool.ConnectionPool)6 Test (org.junit.jupiter.api.Test)6 ConnectionFactory (io.r2dbc.spi.ConnectionFactory)3 H2ConnectionFactory (io.r2dbc.h2.H2ConnectionFactory)2 SimpleTestConnectionFactory (org.springframework.boot.autoconfigure.r2dbc.SimpleConnectionFactoryProvider.SimpleTestConnectionFactory)2 OptionsCapableConnectionFactory (org.springframework.boot.r2dbc.OptionsCapableConnectionFactory)2 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)1 ConnectionPoolConfiguration (io.r2dbc.pool.ConnectionPoolConfiguration)1 PoolMetrics (io.r2dbc.pool.PoolMetrics)1 ConnectionFactoryOptions (io.r2dbc.spi.ConnectionFactoryOptions)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 StepVerifier (reactor.test.StepVerifier)1