Search in sources :

Example 1 with BQRuntime

use of io.bootique.BQRuntime in project bootique-jdbc by bootique.

the class HikariCPInstrumentedModuleIT method testMetrics_TurnedOn.

@Test
public void testMetrics_TurnedOn() {
    BQRuntime runtime = TEST_FACTORY.app("-c", "classpath:io/bootique/jdbc/instrumented/hikaricp/hikaricp-ds-health.yml").autoLoadModules().createRuntime();
    DataSourceFactory factory = runtime.getInstance(DataSourceFactory.class);
    DataSource ds = factory.forName("derby1");
    assertNotNull(ds);
    assertTrue(ds instanceof HikariDataSource);
    HikariDataSource hikariDS = (HikariDataSource) ds;
    String poolName = hikariDS.getPoolName();
    MetricRegistry metricRegistry = runtime.getInstance(MetricRegistry.class);
    assertEquals(metricRegistry.getTimers().size(), 1);
    assertEquals(metricRegistry.getTimers().firstKey(), MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_WAIT));
    assertEquals(metricRegistry.getGauges().size(), 4);
    assertEquals(metricRegistry.getGauges().keySet(), new HashSet<String>() {

        {
            add(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_TOTAL_CONNECTIONS));
            add(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_IDLE_CONNECTIONS));
            add(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_ACTIVE_CONNECTIONS));
            add(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_PENDING_CONNECTIONS));
        }
    });
}
Also used : DataSourceFactory(io.bootique.jdbc.DataSourceFactory) HikariDataSource(com.zaxxer.hikari.HikariDataSource) BQRuntime(io.bootique.BQRuntime) MetricRegistry(com.codahale.metrics.MetricRegistry) HikariDataSource(com.zaxxer.hikari.HikariDataSource) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 2 with BQRuntime

use of io.bootique.BQRuntime in project bootique-jdbc by bootique.

the class HikariCPInstrumentedModuleIT method testHealthChecks_HealthNoParam_ChecksTurnedOff.

@Test
public void testHealthChecks_HealthNoParam_ChecksTurnedOff() {
    BQRuntime runtime = TEST_FACTORY.app("-c", "classpath:io/bootique/jdbc/instrumented/hikaricp/dummy-ds.yml").autoLoadModules().createRuntime();
    DataSourceFactory factory = runtime.getInstance(DataSourceFactory.class);
    String dataSourceName = "DerbyDatabaseIT";
    HikariDataSource dataSource = (HikariDataSource) factory.forName(dataSourceName);
    assertNotNull(dataSource);
    HealthCheckRegistry registry = runtime.getInstance(HealthCheckRegistry.class);
    /**
     * embedded health check {@link io.bootique.jdbc.instrumented.healthcheck.DataSourceHealthCheck}
     */
    assertTrue(registry.containsHealthCheck(DataSourceHealthCheck.healthCheckName(dataSourceName)));
    Map<String, HealthCheckOutcome> results = registry.runHealthChecks();
    assertEquals(results.size(), 1);
}
Also used : DataSourceFactory(io.bootique.jdbc.DataSourceFactory) HikariDataSource(com.zaxxer.hikari.HikariDataSource) HealthCheckOutcome(io.bootique.metrics.health.HealthCheckOutcome) BQRuntime(io.bootique.BQRuntime) HealthCheckRegistry(io.bootique.metrics.health.HealthCheckRegistry) Test(org.junit.Test)

Example 3 with BQRuntime

use of io.bootique.BQRuntime in project bootique-jdbc by bootique.

the class HikariCPDerbyIT method testDerbyDataSource.

@Test
public void testDerbyDataSource() throws SQLException {
    BQRuntime runtime = testFactory.app("-c", "classpath:HikariCPDerbyIT.yml").autoLoadModules().createRuntime();
    DataSource ds4 = runtime.getInstance(DataSourceFactory.class).forName("derby4");
    assertNotNull(ds4);
    assertTrue(ds4 instanceof HikariDataSource);
    HikariDataSource hikariDS = (HikariDataSource) ds4;
    assertEquals("org.apache.derby.jdbc.EmbeddedDataSource", hikariDS.getDataSourceClassName());
    HikariPool pool = (HikariPool) hikariDS.getHikariPoolMXBean();
    assertTrue(pool.getUnwrappedDataSource().getClass().isAssignableFrom(EmbeddedDataSource.class));
    try (Connection c = hikariDS.getConnection()) {
        assertEquals("jdbc:derby:target/derby4", c.getMetaData().getURL());
    }
}
Also used : DataSourceFactory(io.bootique.jdbc.DataSourceFactory) HikariDataSource(com.zaxxer.hikari.HikariDataSource) HikariPool(com.zaxxer.hikari.pool.HikariPool) BQRuntime(io.bootique.BQRuntime) Connection(java.sql.Connection) EmbeddedDataSource(org.apache.derby.jdbc.EmbeddedDataSource) DriverDataSource(com.zaxxer.hikari.util.DriverDataSource) EmbeddedDataSource(org.apache.derby.jdbc.EmbeddedDataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 4 with BQRuntime

use of io.bootique.BQRuntime in project bootique-jdbc by bootique.

the class HikariCPDerbyIT method testDerbyDataSource_ConnectionAttributes.

@Test
public void testDerbyDataSource_ConnectionAttributes() throws SQLException {
    BQRuntime runtime = testFactory.app("-c", "classpath:HikariCPDerbyIT_connAttrs.yml").autoLoadModules().createRuntime();
    DataSource ds6 = runtime.getInstance(DataSourceFactory.class).forName("derby6");
    assertNotNull(ds6);
    HikariDataSource hikariDS = (HikariDataSource) ds6;
    try (Connection c = hikariDS.getConnection()) {
        assertEquals("jdbc:derby:", c.getMetaData().getURL());
    }
}
Also used : DataSourceFactory(io.bootique.jdbc.DataSourceFactory) HikariDataSource(com.zaxxer.hikari.HikariDataSource) BQRuntime(io.bootique.BQRuntime) Connection(java.sql.Connection) DriverDataSource(com.zaxxer.hikari.util.DriverDataSource) EmbeddedDataSource(org.apache.derby.jdbc.EmbeddedDataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 5 with BQRuntime

use of io.bootique.BQRuntime in project bootique-jdbc by bootique.

the class HikariCPModuleIT method testDataSource_FullConfig.

@Test
public void testDataSource_FullConfig() throws SQLException {
    BQRuntime runtime = testFactory.app("-c", "classpath:HikariCPModuleIT_full.yml").autoLoadModules().createRuntime();
    DataSource ds = runtime.getInstance(DataSourceFactory.class).forName("ds1");
    assertNotNull(ds);
    assertTrue(ds instanceof HikariDataSource);
    HikariDataSource hikariDS = (HikariDataSource) ds;
    assertEquals(250, hikariDS.getConnectionTimeout());
    assertEquals(250, hikariDS.getValidationTimeout());
    assertEquals(20000, hikariDS.getIdleTimeout());
    assertEquals(20000, hikariDS.getLeakDetectionThreshold());
    assertEquals(50000, hikariDS.getMaxLifetime());
    assertEquals(3, hikariDS.getMaximumPoolSize());
    assertEquals(1, hikariDS.getMinimumIdle());
    assertEquals("x", hikariDS.getUsername());
    assertEquals("x", hikariDS.getPassword());
    assertEquals(100, hikariDS.getInitializationFailTimeout());
    assertEquals("test-catalog", hikariDS.getCatalog());
    assertEquals("org.apache.derby.jdbc.EmbeddedDataSource", hikariDS.getDataSourceClassName());
    assertEquals("jdbc:derby:;", hikariDS.getJdbcUrl());
    assertEquals("test-pool", hikariDS.getPoolName());
    assertEquals("TRANSACTION_SERIALIZABLE", hikariDS.getTransactionIsolation());
    assertTrue(hikariDS.isAutoCommit());
    assertFalse(hikariDS.isReadOnly());
    assertFalse(hikariDS.isIsolateInternalQueries());
    assertFalse(hikariDS.isRegisterMbeans());
    assertFalse(hikariDS.isAllowPoolSuspension());
    try (Connection c = hikariDS.getConnection()) {
        assertEquals("jdbc:derby:target/ds1", c.getMetaData().getURL());
    }
}
Also used : DataSourceFactory(io.bootique.jdbc.DataSourceFactory) HikariDataSource(com.zaxxer.hikari.HikariDataSource) BQRuntime(io.bootique.BQRuntime) Connection(java.sql.Connection) HikariDataSource(com.zaxxer.hikari.HikariDataSource) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Aggregations

BQRuntime (io.bootique.BQRuntime)45 Test (org.junit.Test)39 DataSource (javax.sql.DataSource)23 DataSourceFactory (io.bootique.jdbc.DataSourceFactory)22 HikariDataSource (com.zaxxer.hikari.HikariDataSource)12 ProvisionException (com.google.inject.ProvisionException)10 BootiqueException (io.bootique.BootiqueException)10 BQTestFactory (io.bootique.test.junit.BQTestFactory)10 JsonTypeName (com.fasterxml.jackson.annotation.JsonTypeName)9 Injector (com.google.inject.Injector)9 Map (java.util.Map)9 Optional (java.util.Optional)9 Assert.assertNotNull (org.junit.Assert.assertNotNull)9 Assert.assertTrue (org.junit.Assert.assertTrue)9 Assert.fail (org.junit.Assert.fail)9 Rule (org.junit.Rule)9 Mockito.mock (org.mockito.Mockito.mock)9 JdbcModule (io.bootique.jdbc.JdbcModule)8 Connection (java.sql.Connection)7 ConfigurationFactory (io.bootique.config.ConfigurationFactory)6