Skip to content

Commit

Permalink
Add tests for TC_DAEMON parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
inikolaev committed Jun 8, 2017
1 parent 27d7ef7 commit d95abae
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.testcontainers.containers.JdbcDatabaseContainer;

import java.sql.Connection;
import java.sql.ResultSet;
Expand All @@ -18,6 +19,8 @@
import static java.util.Arrays.asList;
import static org.junit.Assume.assumeFalse;
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;
import static org.rnorth.visibleassertions.VisibleAssertions.assertNotNull;
import static org.rnorth.visibleassertions.VisibleAssertions.assertNull;
import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue;

@RunWith(Parameterized.class)
Expand All @@ -32,6 +35,7 @@ public class JDBCDriverTest {
@Parameter(3)
public boolean performTestForCustomIniFile;


@Parameterized.Parameters(name = "{index} - {0}")
public static Iterable<Object[]> data() {
return asList(
Expand All @@ -48,7 +52,7 @@ public static Iterable<Object[]> data() {
{"jdbc:tc:mariadb://hostname/databasename?TC_INITSCRIPT=somepath/init_mariadb.sql", true, false, false},
{"jdbc:tc:mariadb://hostname/databasename?TC_INITFUNCTION=org.testcontainers.jdbc.JDBCDriverTest::sampleInitFunction", true, false, false},
{"jdbc:tc:mysql:5.6://hostname/databasename?TC_MY_CNF=somepath/mysql_conf_override", false, false, true},
{"jdbc:tc:mariadb:10.1.16://hostname/databasename?TC_MY_CNF=somepath/mariadb_conf_override", false, false, true}
{"jdbc:tc:mariadb:10.1.16://hostname/databasename?TC_MY_CNF=somepath/mariadb_conf_override", false, false, true},
});
}

Expand Down Expand Up @@ -84,6 +88,27 @@ public void test() throws SQLException {
}
}

@Test
public void shouldStopContainerWhenAllConnectionsClosed() throws SQLException {
final String jdbcUrl = "jdbc:tc:postgresql://hostname/databasename";

performSimpleTest(jdbcUrl);

JdbcDatabaseContainer<?> container = ContainerDatabaseDriver.getContainer(jdbcUrl);
assertNull("Database container instance is null as expected", container);
}

@Test
public void shouldNotStopDaemonContainerWhenAllConnectionsClosed() throws SQLException {
final String jdbcUrl = "jdbc:tc:postgresql://hostname/databasename?TC_DAEMON=true";

performSimpleTest(jdbcUrl);

JdbcDatabaseContainer<?> container = ContainerDatabaseDriver.getContainer(jdbcUrl);
assertNotNull("Database container instance is not null as expected", container);
assertTrue("Database container is running as expected", container.isRunning());
}

private void performSimpleTest(String jdbcUrl) throws SQLException {
try (HikariDataSource dataSource = getDataSource(jdbcUrl, 1)) {
boolean result = new QueryRunner(dataSource).query("SELECT 1", rs -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.testcontainers.jdbc;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -317,4 +318,16 @@ public static void killContainer(String jdbcUrl) {
}
}
}

/**
* Utility method to get an instance of a database container given its JDBC URL.
* @param jdbcUrl the JDBC URL of the container instance to get
* @return an instance of database container or <code>null</code> if no container associated with JDBC URL
*/
@VisibleForTesting
public static JdbcDatabaseContainer getContainer(String jdbcUrl) {
synchronized (jdbcUrlContainerCache) {
return jdbcUrlContainerCache.get(jdbcUrl);
}
}
}

0 comments on commit d95abae

Please sign in to comment.