|
11 | 11 | import org.testng.annotations.AfterSuite; |
12 | 12 | import org.testng.annotations.BeforeSuite; |
13 | 13 |
|
| 14 | +import java.io.OutputStream; |
| 15 | +import java.net.HttpURLConnection; |
14 | 16 | import java.net.InetSocketAddress; |
| 17 | +import java.net.URL; |
| 18 | +import java.nio.charset.StandardCharsets; |
15 | 19 | import java.time.Duration; |
| 20 | +import java.util.Base64; |
16 | 21 | import java.util.Collections; |
17 | 22 | import java.util.Map; |
18 | 23 | import java.util.Properties; |
@@ -88,7 +93,7 @@ public class ClickHouseServerForTest { |
88 | 93 | isCloud = true; |
89 | 94 | imageTag = ""; |
90 | 95 | } |
91 | | - if (clickhouseServer != null) { // use external server |
| 96 | + if (clickhouseServer != null || isCloud) { // use external server |
92 | 97 | clickhouseVersion = imageTag == null |
93 | 98 | || ClickHouseVersionUtils.of(imageTag).getYear() == 0 ? "" : imageTag; |
94 | 99 | clickhouseContainer = null; |
@@ -385,29 +390,39 @@ public static boolean runQuery(String sql) { |
385 | 390 | } |
386 | 391 | } else { |
387 | 392 | //Create database for testing |
| 393 | + |
388 | 394 | ClickHouseNode server = getClickHouseNode(ClickHouseProtocol.HTTP, isCloud(), ClickHouseNode.builder().build()); |
389 | 395 |
|
390 | | - LOGGER.info("SQL: " + sql); |
391 | | - LOGGER.info("Server: " + server); |
| 396 | + String uri = server.getBaseUri(); |
392 | 397 |
|
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 | + } |
399 | 421 | } |
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); |
409 | 422 | } |
410 | | - } while(retries++ < 10); |
| 423 | + } catch (Exception e) { |
| 424 | + LOGGER.error("failed to run query", e); |
| 425 | + } |
411 | 426 |
|
412 | 427 | return false; |
413 | 428 | } |
|
0 commit comments