Skip to content

Commit

Permalink
WIP Adjusting more JDBC tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Paultagoras committed Jul 24, 2024
1 parent 38cbeed commit 3b9d006
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class ClickHouseServerForTest {
// ignore
}

database = "clickhouse_java_test_" + System.currentTimeMillis();
database = "clickhouse_http_test_" + System.currentTimeMillis();
String proxy = ClickHouseUtils.getProperty("proxyAddress", properties);
if (!ClickHouseChecker.isNullOrEmpty(proxy)) { // use external proxy
int index = proxy.indexOf(':');
Expand Down Expand Up @@ -340,7 +340,15 @@ public static void beforeSuite() {
success = false;
LOGGER.error("Failed to create database for testing", e);
}
} while(!success && retries++ < 3);

if (!success) {
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
LOGGER.error("Failed to sleep", e);
}
}
} while(!success && retries++ < 5);

if (!success) {
throw new IllegalStateException("Failed to create database for testing");
Expand Down Expand Up @@ -394,7 +402,15 @@ public static void afterSuite() {
success = false;
LOGGER.error("Failed to drop database after testing", e);
}
} while(!success && retries++ < 3);

if (!success) {
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
LOGGER.error("Failed to sleep", e);
}
}
} while(!success && retries++ < 5);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public class AccessManagementTest extends JdbcIntegrationTest {
@Test(groups = "integration", dataProvider = "setRolesArgsForTestSetRole")
public void testSetRoleDifferentConnections(String[] roles, String setRoleExpr, String[] activeRoles,
String connectionProvider) throws SQLException {
if (isCloud()) return;

String httpEndpoint = "http://" + getServerAddress(ClickHouseProtocol.HTTP) + "/";
String url = String.format("jdbc:ch:%s", httpEndpoint);
String url = String.format("jdbc:ch:%s", getEndpointString());
Properties properties = new Properties();
properties.setProperty(ClickHouseHttpOption.REMEMBER_LAST_SET_ROLES.getKey(), "true");
properties.setProperty(ClickHouseHttpOption.CONNECTION_PROVIDER.getKey(), connectionProvider);
Expand Down Expand Up @@ -105,8 +105,7 @@ private String getServerVersion(Connection connection) {

@Test
public void testSetRolesAccessingTableRows() throws SQLException {
String httpEndpoint = "http://" + getServerAddress(ClickHouseProtocol.HTTP) + "/";
String url = String.format("jdbc:ch:%s", httpEndpoint);
String url = String.format("jdbc:ch:%s", getEndpointString());
Properties properties = new Properties();
properties.setProperty(ClickHouseHttpOption.REMEMBER_LAST_SET_ROLES.getKey(), "true");
ClickHouseDataSource dataSource = new ClickHouseDataSource(url, properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public ClickHouseConnection newConnection(Properties properties) throws SQLExcep

@Test(groups = "integration")
public void testCentralizedConfiguration() throws SQLException {
if (isCloud()) return;

Properties props = new Properties();
props.setProperty("custom_settings", "max_result_rows=1");
try (ClickHouseConnection conn = newConnection(props); Statement stmt = conn.createStatement()) {
Expand Down Expand Up @@ -126,6 +128,8 @@ public void testCreateArray() throws SQLException {

@Test(groups = "integration")
public void testAutoCommitMode() throws SQLException {
if (isCloud()) return;

Properties props = new Properties();
props.setProperty("transactionSupport", "true");

Expand Down Expand Up @@ -198,6 +202,7 @@ public void testNonExistDatabase() throws SQLException {

@Test(groups = "integration")
public void testReadOnly() throws SQLException {
if (isCloud()) return;
Properties props = new Properties();
props.setProperty("user", "dba");
props.setProperty("password", "dba");
Expand Down Expand Up @@ -304,6 +309,8 @@ public void testReadOnly() throws SQLException {

@Test(groups = "integration")
public void testAutoCommit() throws SQLException {
if (isCloud()) return;

Properties props = new Properties();
props.setProperty("transactionSupport", "true");
String tableName = "test_jdbc_tx_auto_commit";
Expand Down Expand Up @@ -399,6 +406,8 @@ public void testAutoCommit() throws SQLException {

@Test(groups = "integration")
public void testManualTxApi() throws SQLException {
if (isCloud()) return;

Properties props = new Properties();
props.setProperty("autoCommit", "false");
Properties txProps = new Properties();
Expand Down Expand Up @@ -515,6 +524,8 @@ public void testManualTxApi() throws SQLException {

@Test(groups = "integration")
public void testManualTxTcl() throws SQLException {
if (isCloud()) return;

Properties props = new Properties();
props.setProperty("autoCommit", "false");
Properties txProps = new Properties();
Expand Down Expand Up @@ -662,6 +673,8 @@ public void testManualTxTcl() throws SQLException {

@Test(groups = "integration")
public void testNestedTransactions() throws SQLException {
if (isCloud()) return;

Properties props = new Properties();
props.setProperty("autoCommit", "false");
props.setProperty("transactionSupport", "true");
Expand Down Expand Up @@ -704,6 +717,8 @@ public void testNestedTransactions() throws SQLException {

@Test(groups = "integration")
public void testParallelTransactions() throws SQLException {
if (isCloud()) return;

Properties props = new Properties();
props.setProperty("autoCommit", "false");
props.setProperty("transactionSupport", "true");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.sql.Statement;
import java.util.Properties;

import com.clickhouse.client.ClickHouseServerForTest;
import org.testng.Assert;
import org.testng.annotations.Test;

Expand All @@ -18,7 +19,8 @@
public class ClickHouseDataSourceTest extends JdbcIntegrationTest {
@Test(groups = "integration")
public void testHighAvailabilityConfig() throws SQLException {
String httpEndpoint = "http://" + getServerAddress(ClickHouseProtocol.HTTP) + "/";
if (isCloud()) return;
String httpEndpoint = getEndpointString();
String grpcEndpoint = "grpc://" + getServerAddress(ClickHouseProtocol.GRPC) + "/";
String tcpEndpoint = "tcp://" + getServerAddress(ClickHouseProtocol.TCP) + "/";

Expand All @@ -35,7 +37,8 @@ public void testHighAvailabilityConfig() throws SQLException {

@Test // (groups = "integration")
public void testMultiEndpoints() throws SQLException {
String httpEndpoint = "http://" + getServerAddress(ClickHouseProtocol.HTTP) + "/";
if (isCloud()) return;
String httpEndpoint = getEndpointString();
String grpcEndpoint = "grpc://" + getServerAddress(ClickHouseProtocol.GRPC) + "/";
String tcpEndpoint = "tcp://" + getServerAddress(ClickHouseProtocol.TCP) + "/";

Expand All @@ -58,16 +61,20 @@ public void testMultiEndpoints() throws SQLException {

@Test(groups = "integration")
public void testGetConnection() throws SQLException {
String url = "jdbc:ch:" + DEFAULT_PROTOCOL.name() + "://" + getServerAddress(DEFAULT_PROTOCOL);
String urlWithCredentials = "jdbc:ch:" + DEFAULT_PROTOCOL.name() + "://default@"
+ getServerAddress(DEFAULT_PROTOCOL);
String url = "jdbc:ch:" + getEndpointString();
// String urlWithCredentials = "jdbc:ch:" + (isCloud() ? "https" : DEFAULT_PROTOCOL.name()) + "://default@"
// + getServerAddress(DEFAULT_PROTOCOL);
String urlWithCredentials = "jdbc:ch:" + DEFAULT_PROTOCOL.name() + "://default@" + getServerAddress(DEFAULT_PROTOCOL);
if (isCloud()) {
urlWithCredentials = "jdbc:ch:https://default/" + getPassword() + "@" + getServerAddress(DEFAULT_PROTOCOL);
}
String clientName = "client1";
int maxExecuteTime = 1234;
boolean continueBatchOnError = true;

Properties properties = new Properties();
properties.setProperty(ClickHouseDefaults.USER.getKey(), "default");
properties.setProperty(ClickHouseDefaults.PASSWORD.getKey(), "");
properties.setProperty(ClickHouseDefaults.PASSWORD.getKey(), getPassword());
properties.setProperty(ClickHouseClientOption.CLIENT_NAME.getKey(), clientName);
properties.setProperty(ClickHouseClientOption.MAX_EXECUTION_TIME.getKey(), Integer.toString(maxExecuteTime));
properties.setProperty(JdbcConfig.PROP_CONTINUE_BATCH, Boolean.toString(continueBatchOnError));
Expand All @@ -82,7 +89,7 @@ public void testGetConnection() throws SQLException {
}) {
for (ClickHouseConnection connection : new ClickHouseConnection[] {
ds.getConnection(),
ds.getConnection("default", ""),
ds.getConnection("default", getPassword()),
new ClickHouseDriver().connect(url, properties),
new ClickHouseDriver().connect(urlWithCredentials, properties),
new ClickHouseDriver().connect(url + params, new Properties()),
Expand All @@ -91,8 +98,8 @@ public void testGetConnection() throws SQLException {
(ClickHouseConnection) DriverManager.getConnection(urlWithCredentials, properties),
(ClickHouseConnection) DriverManager.getConnection(url + params),
(ClickHouseConnection) DriverManager.getConnection(urlWithCredentials + params),
(ClickHouseConnection) DriverManager.getConnection(url + params, "default", ""),
(ClickHouseConnection) DriverManager.getConnection(urlWithCredentials + params, "default", ""),
(ClickHouseConnection) DriverManager.getConnection(url + params, "default", getPassword()),
(ClickHouseConnection) DriverManager.getConnection(urlWithCredentials + params, "default", getPassword()),
}) {
try (ClickHouseConnection conn = connection; Statement stmt = conn.createStatement()) {
Assert.assertEquals(conn.getClientInfo(ClickHouseConnection.PROP_APPLICATION_NAME), clientName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public void testAcceptUrl() throws SQLException {

@Test(groups = "integration")
public void testConnect() throws SQLException {
if (isCloud()) return;
String address = getServerAddress(ClickHouseProtocol.HTTP, true);
ClickHouseDriver driver = new ClickHouseDriver();
ClickHouseConnection conn = driver.connect("jdbc:clickhouse://" + address, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public void testReadWriteBinaryString() throws SQLException {
Statement s = conn.createStatement()) {
s.execute("drop table if exists test_binary_string; "
+ "create table test_binary_string(id Int32, "
+ "f0 FixedString(3), f1 Nullable(FixedString(3)), s0 String, s1 Nullable(String))engine=Memory");
+ "f0 FixedString(3), f1 Nullable(FixedString(3)), s0 String, s1 Nullable(String)) engine=MergeTree ORDER BY id");
}

byte[] bytes = new byte[256];
Expand Down Expand Up @@ -307,8 +307,8 @@ public void testReadWriteBinaryString() throws SQLException {

try (ClickHouseConnection conn = newConnection(props);
PreparedStatement ps = conn
.prepareStatement(
"select distinct * except(id) from test_binary_string where f0 = ? order by id")) {
.prepareStatement("SELECT DISTINCT * EXCEPT(id) FROM test_binary_string" +
" WHERE f0 = ? ORDER BY id" + (isCloud() ? " SETTINGS select_sequential_consistency=1" : ""))) {
ps.setBytes(1, bytes);
ResultSet rs = ps.executeQuery();
Assert.assertTrue(rs.next(), "Should have at least one row");
Expand Down Expand Up @@ -703,7 +703,8 @@ public void testBatchDdl() throws SQLException {
if (!conn.getServerVersion().check("[22.8,)")) {
throw new SkipException("Skip due to error 'unknown key zookeeper_load_balancing'");
}
try (PreparedStatement stmt = conn.prepareStatement(
try (PreparedStatement stmt = conn.prepareStatement(isCloud() ?
"drop table if exists test_batch_dll" :
"drop table if exists test_batch_dll_on_cluster on cluster single_node_cluster_localhost")) {
stmt.addBatch();
stmt.addBatch();
Expand Down Expand Up @@ -1297,17 +1298,16 @@ public void testInsertDefaultValue() throws SQLException {
try (ClickHouseConnection conn = newConnection(props);
Statement s = conn.createStatement();
PreparedStatement ps = conn.prepareStatement(
"insert into test_insert_default_value select id, name from input('id UInt32, name Nullable(String)')")) {
s.execute("drop table if exists test_insert_default_value;"
+ "create table test_insert_default_value(n Int32, s String DEFAULT 'secret') engine=Memory");
"INSERT INTO test_insert_default_value select id, name from input('id UInt32, name Nullable(String)')")) {
s.execute("DROP TABLE IF EXISTS test_insert_default_value; CREATE TABLE test_insert_default_value(n Int32, s String DEFAULT 'secret') engine=MergeTree ORDER BY n");
ps.setInt(1, 1);
ps.setString(2, null);
ps.addBatch();
ps.setInt(1, -1);
ps.setNull(2, Types.ARRAY);
ps.addBatch();
ps.executeBatch();
try (ResultSet rs = s.executeQuery("select * from test_insert_default_value order by n")) {
try (ResultSet rs = s.executeQuery(String.format("SELECT * FROM test_insert_default_value ORDER BY n %s", isCloud() ? "SETTINGS select_sequential_consistency=1" : ""))) {
Assert.assertTrue(rs.next());
Assert.assertEquals(rs.getInt(1), -1);
Assert.assertEquals(rs.getString(2), "secret");
Expand Down Expand Up @@ -1390,7 +1390,7 @@ public void testInsertDefaultValue(String columnType, String defaultExpr, String
throw new SkipException("Skip test when ClickHouse is older than 21.8");
}
s.execute(String.format("drop table if exists %s; ", tableName)
+ String.format("create table %s(id Int8, v %s DEFAULT %s)engine=Memory", tableName, columnType,
+ String.format("CREATE TABLE %s(id Int8, v %s DEFAULT %s) engine=MergeTree ORDER BY id", tableName, columnType,
defaultExpr));
s.executeUpdate(String.format("insert into %s values(1, null)", tableName));
try (PreparedStatement stmt = conn
Expand All @@ -1404,7 +1404,7 @@ public void testInsertDefaultValue(String columnType, String defaultExpr, String
}

int rowCount = 0;
try (ResultSet rs = s.executeQuery(String.format("select * from %s order by id", tableName))) {
try (ResultSet rs = s.executeQuery(String.format("select * from %s order by id %s", tableName, isCloud() ? "SETTINGS select_sequential_consistency=1" : ""))) {
Assert.assertTrue(rs.next());
Assert.assertEquals(rs.getInt(1), 1);
Assert.assertEquals(rs.getString(2), defaultValue);
Expand Down Expand Up @@ -1913,7 +1913,7 @@ public void testInsertWithFormat() throws SQLException {
}

s.execute("drop table if exists test_insert_with_format; "
+ "CREATE TABLE test_insert_with_format(i Int32, s String) ENGINE=Memory");
+ "CREATE TABLE test_insert_with_format(i Int32, s String) ENGINE=MergeTree ORDER BY i");
try (PreparedStatement ps = conn.prepareStatement("INSERT INTO test_insert_with_format format CSV")) {
Assert.assertTrue(ps instanceof StreamBasedPreparedStatement);
Assert.assertEquals(ps.getParameterMetaData().getParameterCount(), 1);
Expand All @@ -1922,7 +1922,7 @@ public void testInsertWithFormat() throws SQLException {
Assert.assertEquals(ps.executeUpdate(), 2);
}

try (ResultSet rs = s.executeQuery("select * from test_insert_with_format order by i")) {
try (ResultSet rs = s.executeQuery("SELECT * FROM test_insert_with_format order by i" + (isCloud() ? " SETTINGS select_sequential_consistency=1" : ""))) {
Assert.assertTrue(rs.next());
Assert.assertEquals(rs.getInt(1), 1);
Assert.assertEquals(rs.getString(2), "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ private Object[][] getConnectionProperties() {

@Test(groups = "integration")
public void testBatchUpdate() throws SQLException {
if (isCloud()) return;
Properties props = new Properties();
try (ClickHouseConnection conn = newConnection(props); ClickHouseStatement stmt = conn.createStatement()) {
if (!conn.getServerVersion().check("[22.8,)")) {
Expand Down
Loading

0 comments on commit 3b9d006

Please sign in to comment.