Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.apache.calcite.avatica.server.AvaticaProtobufHandler;
import org.apache.calcite.avatica.server.HttpServer;
import org.apache.calcite.avatica.server.Main;

import org.apache.hc.client5.http.ConnectTimeoutException;
import org.apache.hc.client5.http.HttpHostConnectException;
import org.apache.hc.core5.util.Timeout;
Expand All @@ -36,6 +35,8 @@
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
Expand All @@ -61,6 +62,7 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;

public class ConnectionPropertiesHATest {
private static final AvaticaServersForTest SERVERS = new AvaticaServersForTest();
Expand Down Expand Up @@ -250,24 +252,38 @@ public void testConnectionPropertiesHALBFailover() throws Exception {
@Test
public void testConnectionPropertiesHAHttpConnectionTimeout5Sec() throws Exception {
// Skip the test for Windows.
Assume.assumeFalse(OS_NAME_LOWERCASE.startsWith(WINDOWS_OS_PREFIX));
Assume.assumeFalse("Skipping on Windows.", OS_NAME_LOWERCASE.startsWith(WINDOWS_OS_PREFIX));
Properties properties = new Properties();

properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
properties.put(BuiltInConnectionProperty.HTTP_CONNECTION_TIMEOUT.name(), "5000");
properties.put(BuiltInConnectionProperty.LB_CONNECTION_FAILOVER_RETRIES.name(), "0");
// 240.0.0.1 is special URL which should result in connection timeout.
// (Except on Windows)
properties.put(BuiltInConnectionProperty.LB_URLS.name(), "http://240.0.0.1:" + 9000);
String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
long startTime = System.currentTimeMillis();
try {
DriverManager.getConnection(url, properties);
} catch (RuntimeException re) {
assumeFalse(
"Got HttpHostConnectException, probably running in WSL / Docker Desktop on Windows.",
re.getCause() instanceof HttpHostConnectException);
long endTime = System.currentTimeMillis();
long elapsedTime = endTime - startTime;
Assert.assertTrue(elapsedTime < Timeout.ofMinutes(3).toMilliseconds());
Assert.assertTrue(elapsedTime >= 5000);
Assert.assertTrue(re.getCause() instanceof ConnectTimeoutException);
String stackTrace = "";
try (StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw)) {
re.printStackTrace(pw);
stackTrace = sw.toString();
}
Assert.assertTrue(
"Expected RuntimeException with ConnectTimeoutException cause, got:\n" + stackTrace,
re.getCause() instanceof ConnectTimeoutException);
Assert.assertTrue("Elapsed time: " + elapsedTime + " ms, expected less than 3 minutes",
elapsedTime < Timeout.ofMinutes(3).toMilliseconds());
Assert.assertTrue("Elapsed time: " + elapsedTime + " ms, expected at least 5000 ms",
elapsedTime >= 5000);
}
}

Expand Down
Loading