Skip to content

Commit 6aa1640

Browse files
committed
#105 shutdown Testcontainers after test
1 parent 8d24e14 commit 6aa1640

File tree

6 files changed

+27
-5
lines changed

6 files changed

+27
-5
lines changed

embedded-database-spring-test/src/main/java/io/zonky/test/db/context/DefaultDatabaseContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,9 @@ public synchronized void destroy() {
221221
logger.trace("Closing database context bean - context={}", beanName);
222222
if (database != null) {
223223
try {
224-
awaitDatabase().close();
224+
awaitDatabase().shutdown();
225225
} catch (Throwable t) {
226-
// TODO: do nothing - consider logging the error
226+
logger.error("There was a error while shutting down database", t);
227227
}
228228
}
229229
}

embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/EmbeddedDatabase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ public interface EmbeddedDatabase extends DataSource, Closeable {
2525

2626
void close();
2727

28+
void shutdown();
2829
}

embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/postgres/DockerPostgresDatabaseProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ private EmbeddedDatabase getDatabase(ClientConfig config, String dbName) throws
257257
dataSource.setProperty(entry.getKey(), entry.getValue());
258258
}
259259

260-
return new BlockingDatabaseWrapper(new PostgresEmbeddedDatabase(dataSource, () -> dropDatabase(config, dbName)), semaphore);
260+
return new BlockingDatabaseWrapper(new PostgresEmbeddedDatabase(dataSource, () -> dropDatabase(config, dbName), container::close), semaphore);
261261
}
262262
}
263263

embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/postgres/PostgresEmbeddedDatabase.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@ public class PostgresEmbeddedDatabase extends AbstractEmbeddedDatabase {
2626

2727
private final PGSimpleDataSource dataSource;
2828

29-
public PostgresEmbeddedDatabase(PGSimpleDataSource dataSource, Runnable closeCallback) {
30-
super(closeCallback);
29+
public PostgresEmbeddedDatabase(PGSimpleDataSource dataSource, Runnable closeCallback, Runnable shutdownCallback) {
30+
super(closeCallback, shutdownCallback);
3131
this.dataSource = dataSource;
3232
}
3333

34+
public PostgresEmbeddedDatabase(PGSimpleDataSource dataSource, Runnable closeCallback) {
35+
this(dataSource, closeCallback, null);
36+
}
37+
3438
@Override
3539
protected DataSource getDataSource() {
3640
return dataSource;

embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/support/AbstractEmbeddedDatabase.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

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

5+
import javax.annotation.Nullable;
56
import javax.sql.DataSource;
67
import java.io.PrintWriter;
78
import java.sql.Connection;
@@ -12,9 +13,15 @@
1213
public abstract class AbstractEmbeddedDatabase implements EmbeddedDatabase {
1314

1415
private final Runnable closeCallback;
16+
private final Runnable shutdownCallback;
1517

1618
protected AbstractEmbeddedDatabase(Runnable closeCallback) {
19+
this(closeCallback, null);
20+
}
21+
22+
protected AbstractEmbeddedDatabase(Runnable closeCallback,Runnable shutdownCallback) {
1723
this.closeCallback = closeCallback;
24+
this.shutdownCallback = shutdownCallback;
1825
}
1926

2027
protected abstract DataSource getDataSource();
@@ -80,4 +87,9 @@ public Logger getParentLogger() throws SQLFeatureNotSupportedException {
8087
public synchronized void close() {
8188
closeCallback.run();
8289
}
90+
91+
@Override
92+
public void shutdown() {
93+
shutdownCallback.run();
94+
}
8395
}

embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/support/BlockingDatabaseWrapper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ public void close() {
120120
delegate.close();
121121
}
122122

123+
@Override
124+
public void shutdown() {
125+
delegate.shutdown();
126+
}
127+
123128
protected static class BlockingConnectionWrapper implements Connection {
124129

125130
private final Connection delegate;

0 commit comments

Comments
 (0)