Skip to content

Commit 1c601a8

Browse files
committed
[CALCITE-6855] Report elapsed time in ConnectionPropertiesHATest assert
1 parent c8e57f2 commit 1c601a8

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

server/src/test/java/org/apache/calcite/avatica/ha/ConnectionPropertiesHATest.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.apache.calcite.avatica.server.AvaticaProtobufHandler;
2727
import org.apache.calcite.avatica.server.HttpServer;
2828
import org.apache.calcite.avatica.server.Main;
29-
3029
import org.apache.hc.client5.http.ConnectTimeoutException;
3130
import org.apache.hc.client5.http.HttpHostConnectException;
3231
import org.apache.hc.core5.util.Timeout;
@@ -36,6 +35,8 @@
3635
import org.junit.BeforeClass;
3736
import org.junit.Test;
3837

38+
import java.io.PrintWriter;
39+
import java.io.StringWriter;
3940
import java.lang.reflect.Field;
4041
import java.lang.reflect.InvocationTargetException;
4142
import java.net.URI;
@@ -61,6 +62,7 @@
6162
import static org.junit.Assert.assertNotNull;
6263
import static org.junit.Assert.assertTrue;
6364
import static org.junit.Assert.fail;
65+
import static org.junit.Assume.assumeFalse;
6466

6567
public class ConnectionPropertiesHATest {
6668
private static final AvaticaServersForTest SERVERS = new AvaticaServersForTest();
@@ -250,24 +252,38 @@ public void testConnectionPropertiesHALBFailover() throws Exception {
250252
@Test
251253
public void testConnectionPropertiesHAHttpConnectionTimeout5Sec() throws Exception {
252254
// 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));
254256
Properties properties = new Properties();
255257

256258
properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
257259
properties.put(BuiltInConnectionProperty.HTTP_CONNECTION_TIMEOUT.name(), "5000");
258260
properties.put(BuiltInConnectionProperty.LB_CONNECTION_FAILOVER_RETRIES.name(), "0");
259261
// 240.0.0.1 is special URL which should result in connection timeout.
262+
// (Except on Windows)
260263
properties.put(BuiltInConnectionProperty.LB_URLS.name(), "http://240.0.0.1:" + 9000);
261264
String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
262265
long startTime = System.currentTimeMillis();
263266
try {
264267
DriverManager.getConnection(url, properties);
265268
} catch (RuntimeException re) {
269+
assumeFalse(
270+
"Got HttpHostConnectException, probably running in WSL / Docker Desktop on Windows.",
271+
re.getCause() instanceof HttpHostConnectException);
266272
long endTime = System.currentTimeMillis();
267273
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);
271287
}
272288
}
273289

0 commit comments

Comments
 (0)