Skip to content

Commit b4920c6

Browse files
StefanHufschmidtbsideup
authored andcommitted
Added builder for timeouts in JdbcDatabaseContainer (#715) (#748)
* Added builder for timeouts in JdbcDatabaseContainer (#715) * Adjusted Oracle and SQL Server to use other default timeouts (#715)
1 parent 7936267 commit b4920c6

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

modules/jdbc/src/main/java/org/testcontainers/containers/JdbcDatabaseContainer.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public abstract class JdbcDatabaseContainer<SELF extends JdbcDatabaseContainer<S
3333
.withConstantThroughput()
3434
.build();
3535

36+
private int startupTimeoutSeconds = 120;
37+
private int connectTimeoutSeconds = 120;
38+
3639
public JdbcDatabaseContainer(@NonNull final String dockerImageName) {
3740
super(dockerImageName);
3841
}
@@ -86,6 +89,28 @@ public SELF withDatabaseName(String dbName) {
8689

8790
}
8891

92+
/**
93+
* Set startup time to allow, including image pull time, in seconds.
94+
*
95+
* @param startupTimeoutSeconds startup time to allow, including image pull time, in seconds
96+
* @return self
97+
*/
98+
public SELF withStartupTimeoutSeconds(int startupTimeoutSeconds) {
99+
this.startupTimeoutSeconds = startupTimeoutSeconds;
100+
return self();
101+
}
102+
103+
/**
104+
* Set time to allow for the database to start and establish an initial connection, in seconds.
105+
*
106+
* @param connectTimeoutSeconds time to allow for the database to start and establish an initial connection in seconds
107+
* @return self
108+
*/
109+
public SELF withConnectTimeoutSeconds(int connectTimeoutSeconds) {
110+
this.connectTimeoutSeconds = connectTimeoutSeconds;
111+
return self();
112+
}
113+
89114
@Override
90115
protected void waitUntilContainerStarted() {
91116
// Repeatedly try and open a connection to the DB and execute a test query
@@ -188,15 +213,19 @@ public void addParameter(String paramName, String value) {
188213

189214
/**
190215
* @return startup time to allow, including image pull time, in seconds
216+
* @deprecated should not be overridden anymore, use {@link #withStartupTimeoutSeconds(int)} in constructor instead
191217
*/
218+
@Deprecated
192219
protected int getStartupTimeoutSeconds() {
193-
return 120;
220+
return startupTimeoutSeconds;
194221
}
195222

196223
/**
197224
* @return time to allow for the database to start and establish an initial connection, in seconds
225+
* @deprecated should not be overridden anymore, use {@link #withConnectTimeoutSeconds(int)} in constructor instead
198226
*/
227+
@Deprecated
199228
protected int getConnectTimeoutSeconds() {
200-
return 120;
229+
return connectTimeoutSeconds;
201230
}
202231
}

modules/mssqlserver/src/main/java/org/testcontainers/containers/MSSQLServerContainer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@ public class MSSQLServerContainer<SELF extends MSSQLServerContainer<SELF>> exten
1414
private String username = "SA";
1515
private String password = "A_Str0ng_Required_Password";
1616

17+
private static final int DEFAULT_STARTUP_TIMEOUT_SECONDS = 240;
18+
private static final int DEFAULT_CONNECT_TIMEOUT_SECONDS = 240;
19+
1720
public MSSQLServerContainer() {
1821
this(IMAGE + ":" + DEFAULT_TAG);
1922
}
2023

2124
public MSSQLServerContainer(final String dockerImageName) {
2225
super(dockerImageName);
26+
withStartupTimeoutSeconds(DEFAULT_STARTUP_TIMEOUT_SECONDS);
27+
withConnectTimeoutSeconds(DEFAULT_CONNECT_TIMEOUT_SECONDS);
2328
}
2429

2530
@Override

modules/oracle-xe/src/main/java/org/testcontainers/containers/OracleContainer.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ public class OracleContainer extends JdbcDatabaseContainer {
1414
private static final int ORACLE_PORT = 1521;
1515
private static final int APEX_HTTP_PORT = 8080;
1616

17+
private static final int DEFAULT_STARTUP_TIMEOUT_SECONDS = 240;
18+
private static final int DEFAULT_CONNECT_TIMEOUT_SECONDS = 120;
19+
1720
private static String resolveImageName() {
1821
String image = TestcontainersConfiguration.getInstance()
1922
.getProperties().getProperty("oracle.container.image");
@@ -27,15 +30,19 @@ private static String resolveImageName() {
2730
}
2831

2932
public OracleContainer() {
30-
super(resolveImageName());
33+
this(resolveImageName());
3134
}
3235

3336
public OracleContainer(String dockerImageName) {
3437
super(dockerImageName);
38+
withStartupTimeoutSeconds(DEFAULT_STARTUP_TIMEOUT_SECONDS);
39+
withConnectTimeoutSeconds(DEFAULT_CONNECT_TIMEOUT_SECONDS);
3540
}
3641

3742
public OracleContainer(Future<String> dockerImageName) {
3843
super(dockerImageName);
44+
withStartupTimeoutSeconds(DEFAULT_STARTUP_TIMEOUT_SECONDS);
45+
withConnectTimeoutSeconds(DEFAULT_CONNECT_TIMEOUT_SECONDS);
3946
}
4047

4148
@Override
@@ -45,7 +52,6 @@ protected Integer getLivenessCheckPort() {
4552

4653
@Override
4754
protected void configure() {
48-
4955
addExposedPorts(ORACLE_PORT, APEX_HTTP_PORT);
5056
}
5157

@@ -87,9 +93,4 @@ public Integer getWebPort() {
8793
public String getTestQueryString() {
8894
return "SELECT 1 FROM DUAL";
8995
}
90-
91-
@Override
92-
protected int getStartupTimeoutSeconds() {
93-
return 240;
94-
}
9596
}

0 commit comments

Comments
 (0)