Skip to content

Commit 35ad5cd

Browse files
committed
Fix intermittent failure of inMemoryDerbyIsShutdown
1 parent 419f92d commit 35ad5cd

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/DevToolsPooledDataSourceAutoConfigurationTests.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import javax.sql.DataSource;
2424

25+
import com.zaxxer.hikari.HikariDataSource;
26+
import com.zaxxer.hikari.HikariPoolMXBean;
2527
import org.apache.derby.jdbc.EmbeddedDriver;
2628
import org.junit.After;
2729
import org.junit.Before;
@@ -122,8 +124,23 @@ public void inMemoryDerbyIsShutdown() throws SQLException {
122124
ConfigurableApplicationContext context = createContext("org.apache.derby.jdbc.EmbeddedDriver",
123125
"jdbc:derby:memory:test;create=true", DataSourceAutoConfiguration.class,
124126
DataSourceSpyConfiguration.class);
125-
JdbcTemplate jdbc = new JdbcTemplate(context.getBean(DataSource.class));
127+
HikariDataSource dataSource = context.getBean(HikariDataSource.class);
128+
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
126129
jdbc.execute("SELECT 1 FROM SYSIBM.SYSDUMMY1");
130+
HikariPoolMXBean pool = dataSource.getHikariPoolMXBean();
131+
// Prevent a race between Hikari's initialization and Derby shutdown
132+
long end = System.currentTimeMillis() + 30000;
133+
while (pool.getIdleConnections() != dataSource.getMinimumIdle()) {
134+
if (System.currentTimeMillis() >= end) {
135+
throw new IllegalStateException("DataSource did not become idle within 30 seconds");
136+
}
137+
try {
138+
Thread.sleep(100);
139+
}
140+
catch (InterruptedException ex) {
141+
Thread.currentThread().interrupt();
142+
}
143+
}
127144
context.close();
128145
// Connect should fail as DB no longer exists
129146
assertThatExceptionOfType(SQLException.class)

0 commit comments

Comments
 (0)