Skip to content

Commit

Permalink
#105 shutdown Testcontainers after test
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreKoepke committed Dec 3, 2024
1 parent 8d24e14 commit 6aa1640
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ public synchronized void destroy() {
logger.trace("Closing database context bean - context={}", beanName);
if (database != null) {
try {
awaitDatabase().close();
awaitDatabase().shutdown();
} catch (Throwable t) {
// TODO: do nothing - consider logging the error
logger.error("There was a error while shutting down database", t);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ public interface EmbeddedDatabase extends DataSource, Closeable {

void close();

void shutdown();
}
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ private EmbeddedDatabase getDatabase(ClientConfig config, String dbName) throws
dataSource.setProperty(entry.getKey(), entry.getValue());
}

return new BlockingDatabaseWrapper(new PostgresEmbeddedDatabase(dataSource, () -> dropDatabase(config, dbName)), semaphore);
return new BlockingDatabaseWrapper(new PostgresEmbeddedDatabase(dataSource, () -> dropDatabase(config, dbName), container::close), semaphore);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ public class PostgresEmbeddedDatabase extends AbstractEmbeddedDatabase {

private final PGSimpleDataSource dataSource;

public PostgresEmbeddedDatabase(PGSimpleDataSource dataSource, Runnable closeCallback) {
super(closeCallback);
public PostgresEmbeddedDatabase(PGSimpleDataSource dataSource, Runnable closeCallback, Runnable shutdownCallback) {
super(closeCallback, shutdownCallback);
this.dataSource = dataSource;
}

public PostgresEmbeddedDatabase(PGSimpleDataSource dataSource, Runnable closeCallback) {
this(dataSource, closeCallback, null);
}

@Override
protected DataSource getDataSource() {
return dataSource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.zonky.test.db.provider.EmbeddedDatabase;

import javax.annotation.Nullable;
import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
Expand All @@ -12,9 +13,15 @@
public abstract class AbstractEmbeddedDatabase implements EmbeddedDatabase {

private final Runnable closeCallback;
private final Runnable shutdownCallback;

protected AbstractEmbeddedDatabase(Runnable closeCallback) {
this(closeCallback, null);
}

protected AbstractEmbeddedDatabase(Runnable closeCallback,Runnable shutdownCallback) {
this.closeCallback = closeCallback;
this.shutdownCallback = shutdownCallback;
}

protected abstract DataSource getDataSource();
Expand Down Expand Up @@ -80,4 +87,9 @@ public Logger getParentLogger() throws SQLFeatureNotSupportedException {
public synchronized void close() {
closeCallback.run();
}

@Override
public void shutdown() {
shutdownCallback.run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ public void close() {
delegate.close();
}

@Override
public void shutdown() {
delegate.shutdown();
}

protected static class BlockingConnectionWrapper implements Connection {

private final Connection delegate;
Expand Down

0 comments on commit 6aa1640

Please sign in to comment.