Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#105 shutdown Testcontainers after test #289

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This must be Nullsafe.

}
}
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
Loading