Skip to content

Commit

Permalink
Consider reusability when stopping Postgres testcontainer
Browse files Browse the repository at this point in the history
Signed-off-by: nscuro <[email protected]>
  • Loading branch information
nscuro committed Feb 13, 2024
1 parent 54aa0c0 commit 95467c2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
8 changes: 3 additions & 5 deletions src/test/java/org/dependencytrack/PersistenceCapableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.junit.Rule;
import org.junit.contrib.java.lang.system.EnvironmentVariables;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.utility.TestcontainersConfiguration;

import javax.jdo.JDOHelper;
import java.sql.Connection;
Expand All @@ -49,7 +48,7 @@ public abstract class PersistenceCapableTest {
protected QueryManager qm;

@BeforeClass
public static void init() throws Exception {
public static void init() {
Config.enableUnitTests();

postgresContainer = new PostgresTestContainer();
Expand Down Expand Up @@ -84,9 +83,8 @@ public void after() {

@AfterClass
public static void tearDownClass() {
final boolean canReuseContainers = TestcontainersConfiguration.getInstance().environmentSupportsReuse();
if (postgresContainer != null && (!canReuseContainers || !postgresContainer.isShouldBeReused())) {
postgresContainer.stop();
if (postgresContainer != null) {
postgresContainer.stopWhenNotReusing();
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/test/java/org/dependencytrack/PostgresTestContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.postgresql.ds.PGSimpleDataSource;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.TestcontainersConfiguration;

public class PostgresTestContainer extends PostgreSQLContainer<PostgresTestContainer> {

Expand Down Expand Up @@ -42,4 +43,11 @@ protected void containerIsStarted(final InspectContainerResponse containerInfo,
throw new RuntimeException(e);
}
}

public void stopWhenNotReusing() {
if (!TestcontainersConfiguration.getInstance().environmentSupportsReuse() || !isShouldBeReused()) {
stop();
}
}

}
2 changes: 1 addition & 1 deletion src/test/java/org/dependencytrack/ResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public void after() {
@AfterClass
public static void tearDownClass() {
if (postgresContainer != null) {
postgresContainer.stop();
postgresContainer.stopWhenNotReusing();
}
}

Expand Down

0 comments on commit 95467c2

Please sign in to comment.