@@ -62,10 +62,23 @@ public class ClickHouseServerForTest {
6262 private static boolean isCloud = false ;
6363
6464 private static final String database ;
65+ private static final boolean localDatabase ;
6566
6667 static {
6768 properties = new Properties (System .getProperties ());
68- database = "clickhouse_java_" + UUID .randomUUID ().toString ().substring (0 , 8 ) + "_test_" + System .currentTimeMillis ();
69+ String externalDatabase = System .getenv ("TEST_DB_NAME" ); // see build.yaml workflow
70+ if (externalDatabase != null && !externalDatabase .trim ().isEmpty ()) {
71+ if (!externalDatabase .startsWith ("clickhouse_java_test_" )) {
72+ throw new RuntimeException ("external database for tests should start with 'clickhouse_java_test_'" );
73+ }
74+ localDatabase = false ;
75+ database = externalDatabase ;
76+ } else {
77+ localDatabase = true ;
78+ database = "clickhouse_java_" + UUID .randomUUID ().toString ().substring (0 , 8 ) + "_test_" + System .currentTimeMillis ();
79+ }
80+
81+ LOGGER .info ("Local database: {}" , localDatabase );
6982
7083 String proxy = properties .getProperty ("proxyAddress" );
7184 if (proxy != null && !proxy .isEmpty ()) { // use external proxy
@@ -320,10 +333,11 @@ public static boolean isCloud() {
320333 @ BeforeSuite (groups = {"integration" })
321334 public static void beforeSuite () {
322335 if (isCloud ) {
323- if (!runQuery ("CREATE DATABASE IF NOT EXISTS " + database )) {
324- throw new RuntimeException ("Failed to create database for testing." );
336+ if (localDatabase ) {
337+ if (!runQuery ("CREATE DATABASE IF NOT EXISTS " + database )) {
338+ throw new RuntimeException ("Failed to create database for testing." );
339+ }
325340 }
326-
327341 return ;
328342 }
329343
@@ -358,8 +372,10 @@ public static void afterSuite() {
358372 }
359373
360374 if (isCloud ) {
361- if (!runQuery ("DROP DATABASE IF EXISTS `" + database + "`" )) {
362- LOGGER .warn ("Failed to drop database for testing." );
375+ if (localDatabase ) {
376+ if (!runQuery ("DROP DATABASE IF EXISTS `" + database + "`" )) {
377+ LOGGER .warn ("Failed to drop database for testing." );
378+ }
363379 }
364380 }
365381 }
@@ -391,16 +407,17 @@ public static boolean runQuery(String sql) {
391407 try {
392408 URL serverURL = new URL (uri );
393409 LOGGER .info ("sending request to {} (uri={})" , serverURL , uri );
394-
410+ byte [] postData = sql . getBytes ( StandardCharsets . UTF_8 );
395411 for (int attempts = 0 ; attempts < 10 ; attempts ++) {
396412 HttpURLConnection httpConn = (HttpURLConnection ) serverURL .openConnection ();
397413 try {
398414 httpConn .setRequestMethod ("POST" );
399415 httpConn .setDoOutput (true );
400416 httpConn .setRequestProperty ("Authorization" , "Basic " + Base64 .getEncoder ().encodeToString (("default:" + getPassword ()).getBytes ()));
417+ httpConn .setFixedLengthStreamingMode (postData .length );
401418
402419 try (OutputStream out = httpConn .getOutputStream ()) {
403- out .write (sql . getBytes ( StandardCharsets . UTF_8 ) );
420+ out .write (postData , 0 , postData . length );
404421 out .flush ();
405422 }
406423
0 commit comments