From 35ad5cd011d7ee29c1a59c8485028156c4f762e8 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 13 Sep 2019 20:12:22 +0100 Subject: [PATCH] Fix intermittent failure of inMemoryDerbyIsShutdown --- ...ooledDataSourceAutoConfigurationTests.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/DevToolsPooledDataSourceAutoConfigurationTests.java b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/DevToolsPooledDataSourceAutoConfigurationTests.java index 5e3dee05c62..3d767beca17 100644 --- a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/DevToolsPooledDataSourceAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/DevToolsPooledDataSourceAutoConfigurationTests.java @@ -22,6 +22,8 @@ import java.util.Properties; import javax.sql.DataSource; +import com.zaxxer.hikari.HikariDataSource; +import com.zaxxer.hikari.HikariPoolMXBean; import org.apache.derby.jdbc.EmbeddedDriver; import org.junit.After; import org.junit.Before; @@ -122,8 +124,23 @@ public class DevToolsPooledDataSourceAutoConfigurationTests extends AbstractDevT ConfigurableApplicationContext context = createContext("org.apache.derby.jdbc.EmbeddedDriver", "jdbc:derby:memory:test;create=true", DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class); - JdbcTemplate jdbc = new JdbcTemplate(context.getBean(DataSource.class)); + HikariDataSource dataSource = context.getBean(HikariDataSource.class); + JdbcTemplate jdbc = new JdbcTemplate(dataSource); jdbc.execute("SELECT 1 FROM SYSIBM.SYSDUMMY1"); + HikariPoolMXBean pool = dataSource.getHikariPoolMXBean(); + // Prevent a race between Hikari's initialization and Derby shutdown + long end = System.currentTimeMillis() + 30000; + while (pool.getIdleConnections() != dataSource.getMinimumIdle()) { + if (System.currentTimeMillis() >= end) { + throw new IllegalStateException("DataSource did not become idle within 30 seconds"); + } + try { + Thread.sleep(100); + } + catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + } context.close(); // Connect should fail as DB no longer exists assertThatExceptionOfType(SQLException.class)