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