|
36 | 36 | import org.junit.BeforeClass; |
37 | 37 | import org.junit.Test; |
38 | 38 |
|
| 39 | +import java.io.PrintWriter; |
| 40 | +import java.io.StringWriter; |
39 | 41 | import java.lang.reflect.Field; |
40 | 42 | import java.lang.reflect.InvocationTargetException; |
41 | 43 | import java.net.URI; |
|
61 | 63 | import static org.junit.Assert.assertNotNull; |
62 | 64 | import static org.junit.Assert.assertTrue; |
63 | 65 | import static org.junit.Assert.fail; |
| 66 | +import static org.junit.Assume.assumeFalse; |
64 | 67 |
|
65 | 68 | public class ConnectionPropertiesHATest { |
66 | 69 | private static final AvaticaServersForTest SERVERS = new AvaticaServersForTest(); |
@@ -250,24 +253,38 @@ public void testConnectionPropertiesHALBFailover() throws Exception { |
250 | 253 | @Test |
251 | 254 | public void testConnectionPropertiesHAHttpConnectionTimeout5Sec() throws Exception { |
252 | 255 | // Skip the test for Windows. |
253 | | - Assume.assumeFalse(OS_NAME_LOWERCASE.startsWith(WINDOWS_OS_PREFIX)); |
| 256 | + Assume.assumeFalse("Skipping on Windows.", OS_NAME_LOWERCASE.startsWith(WINDOWS_OS_PREFIX)); |
254 | 257 | Properties properties = new Properties(); |
255 | 258 |
|
256 | 259 | properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true"); |
257 | 260 | properties.put(BuiltInConnectionProperty.HTTP_CONNECTION_TIMEOUT.name(), "5000"); |
258 | 261 | properties.put(BuiltInConnectionProperty.LB_CONNECTION_FAILOVER_RETRIES.name(), "0"); |
259 | 262 | // 240.0.0.1 is special URL which should result in connection timeout. |
| 263 | + // (Except on Windows) |
260 | 264 | properties.put(BuiltInConnectionProperty.LB_URLS.name(), "http://240.0.0.1:" + 9000); |
261 | 265 | String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF); |
262 | 266 | long startTime = System.currentTimeMillis(); |
263 | 267 | try { |
264 | 268 | DriverManager.getConnection(url, properties); |
265 | 269 | } catch (RuntimeException re) { |
| 270 | + assumeFalse( |
| 271 | + "Got HttpHostConnectException, probably running in WSL / Docker Desktop on Windows.", |
| 272 | + re.getCause() instanceof HttpHostConnectException); |
266 | 273 | long endTime = System.currentTimeMillis(); |
267 | 274 | long elapsedTime = endTime - startTime; |
268 | | - Assert.assertTrue(elapsedTime < Timeout.ofMinutes(3).toMilliseconds()); |
269 | | - Assert.assertTrue(elapsedTime >= 5000); |
270 | | - Assert.assertTrue(re.getCause() instanceof ConnectTimeoutException); |
| 275 | + String stackTrace = ""; |
| 276 | + try (StringWriter sw = new StringWriter(); |
| 277 | + PrintWriter pw = new PrintWriter(sw)) { |
| 278 | + re.printStackTrace(pw); |
| 279 | + stackTrace = sw.toString(); |
| 280 | + } |
| 281 | + Assert.assertTrue( |
| 282 | + "Expected RuntimeException with ConnectTimeoutException cause, got:\n" + stackTrace, |
| 283 | + re.getCause() instanceof ConnectTimeoutException); |
| 284 | + Assert.assertTrue("Elapsed time: " + elapsedTime + " ms, expected less than 3 minutes", |
| 285 | + elapsedTime < Timeout.ofMinutes(3).toMilliseconds()); |
| 286 | + Assert.assertTrue("Elapsed time: " + elapsedTime + " ms, expected at least 5000 ms", |
| 287 | + elapsedTime >= 5000); |
271 | 288 | } |
272 | 289 | } |
273 | 290 |
|
|
0 commit comments