Skip to content

Commit 778faea

Browse files
committed
reimplemented sending simple requests via URL connection
1 parent e17466a commit 778faea

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

clickhouse-client/src/test/java/com/clickhouse/client/ClickHouseServerForTest.java

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@
1111
import org.testng.annotations.AfterSuite;
1212
import org.testng.annotations.BeforeSuite;
1313

14+
import java.io.OutputStream;
15+
import java.net.HttpURLConnection;
1416
import java.net.InetSocketAddress;
17+
import java.net.URL;
18+
import java.nio.charset.StandardCharsets;
1519
import java.time.Duration;
20+
import java.util.Base64;
1621
import java.util.Collections;
1722
import java.util.Map;
1823
import java.util.Properties;
@@ -88,7 +93,7 @@ public class ClickHouseServerForTest {
8893
isCloud = true;
8994
imageTag = "";
9095
}
91-
if (clickhouseServer != null) { // use external server
96+
if (clickhouseServer != null || isCloud) { // use external server
9297
clickhouseVersion = imageTag == null
9398
|| ClickHouseVersionUtils.of(imageTag).getYear() == 0 ? "" : imageTag;
9499
clickhouseContainer = null;
@@ -385,29 +390,39 @@ public static boolean runQuery(String sql) {
385390
}
386391
} else {
387392
//Create database for testing
393+
388394
ClickHouseNode server = getClickHouseNode(ClickHouseProtocol.HTTP, isCloud(), ClickHouseNode.builder().build());
389395

390-
LOGGER.info("SQL: " + sql);
391-
LOGGER.info("Server: " + server);
396+
String uri = server.getBaseUri();
392397

393-
int retries = 0;
394-
do {
395-
try (ClickHouseClient client = ClickHouseClient.builder().nodeSelector(ClickHouseNodeSelector.of(ClickHouseProtocol.HTTP)).build();
396-
ClickHouseResponse response = client.read(server).query(sql).executeAndWait()) {
397-
if (response.getSummary() != null && response.getSummary().getWrittenRows() > -1) {
398-
return true;
398+
try {
399+
URL serverURL = new URL(uri);
400+
LOGGER.info("sending request to {} (uri={})", serverURL, uri);
401+
402+
for (int attempts = 0; attempts < 10; attempts++) {
403+
HttpURLConnection httpConn = (HttpURLConnection) serverURL.openConnection();
404+
try {
405+
httpConn.setRequestMethod("POST");
406+
httpConn.setDoOutput(true);
407+
httpConn.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString(("default:" + getPassword()).getBytes()));
408+
409+
try (OutputStream out = httpConn.getOutputStream()) {
410+
out.write(sql.getBytes(StandardCharsets.UTF_8));
411+
out.flush();
412+
}
413+
414+
if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
415+
return true;
416+
}
417+
} finally {
418+
if (httpConn != null) {
419+
httpConn.disconnect();
420+
}
399421
}
400-
} catch (Exception e) {
401-
LOGGER.warn("Failed to run query for testing...", e);
402-
}
403-
404-
try {
405-
Thread.sleep(15000);
406-
} catch (InterruptedException e) {
407-
LOGGER.error("Failed to sleep", e);
408-
throw new RuntimeException(e);
409422
}
410-
} while(retries++ < 10);
423+
} catch (Exception e) {
424+
LOGGER.error("failed to run query", e);
425+
}
411426

412427
return false;
413428
}

0 commit comments

Comments
 (0)