2424import org .apache .shardingsphere .infra .metadata .database .resource .unit .StorageUnit ;
2525import org .apache .shardingsphere .mode .manager .ContextManager ;
2626import org .apache .shardingsphere .test .natived .commons .TestShardingService ;
27- import org .awaitility .Awaitility ;
2827import org .junit .jupiter .api .AfterEach ;
29- import org .junit .jupiter .api .BeforeEach ;
3028import org .junit .jupiter .api .Test ;
3129import org .junit .jupiter .api .condition .EnabledInNativeImage ;
32- import org .testcontainers .containers .GenericContainer ;
33- import org .testcontainers .junit .jupiter .Container ;
30+ import org .testcontainers .jdbc .ContainerDatabaseDriver ;
3431import org .testcontainers .junit .jupiter .Testcontainers ;
3532
3633import javax .sql .DataSource ;
3734import java .sql .Connection ;
38- import java .sql .DriverManager ;
3935import java .sql .SQLException ;
40- import java .sql .Statement ;
41- import java .time .Duration ;
42- import java .util .Properties ;
43- import java .util .stream .Stream ;
4436
45- import static org .hamcrest .MatcherAssert .assertThat ;
46- import static org .hamcrest .Matchers .is ;
47- import static org .hamcrest .Matchers .nullValue ;
48-
49- /**
50- * Cannot use testcontainers-java style jdbcURL for Clickhouse Server due to unresolved
51- * <a href="https://github.com/testcontainers/testcontainers-java/issues/8736">testcontainers/testcontainers-java#8736</a>.
52- */
53- @ SuppressWarnings ({"SqlNoDataSourceInspection" , "resource" })
5437@ EnabledInNativeImage
5538@ Testcontainers
5639class ClickHouseTest {
5740
58- @ Container
59- private final GenericContainer <?> container = new GenericContainer <>("clickhouse/clickhouse-server:24.11.1.2557" )
60- .withExposedPorts (8123 );
61-
62- private final String systemPropKeyPrefix = "fixture.test-native.yaml.database.clickhouse." ;
63-
6441 private DataSource logicDataSource ;
6542
66- private String jdbcUrlPrefix ;
67-
68- @ BeforeEach
69- void beforeEach () {
70- assertThat (System .getProperty (systemPropKeyPrefix + "ds0.jdbc-url" ), is (nullValue ()));
71- assertThat (System .getProperty (systemPropKeyPrefix + "ds1.jdbc-url" ), is (nullValue ()));
72- assertThat (System .getProperty (systemPropKeyPrefix + "ds2.jdbc-url" ), is (nullValue ()));
73- }
74-
7543 @ AfterEach
7644 void afterEach () throws SQLException {
7745 try (Connection connection = logicDataSource .getConnection ()) {
@@ -81,92 +49,16 @@ void afterEach() throws SQLException {
8149 }
8250 contextManager .close ();
8351 }
84- System .clearProperty (systemPropKeyPrefix + "ds0.jdbc-url" );
85- System .clearProperty (systemPropKeyPrefix + "ds1.jdbc-url" );
86- System .clearProperty (systemPropKeyPrefix + "ds2.jdbc-url" );
52+ ContainerDatabaseDriver .killContainers ();
8753 }
8854
8955 @ Test
9056 void assertShardingInLocalTransactions () throws SQLException {
91- jdbcUrlPrefix = "jdbc:ch://localhost:" + container .getMappedPort (8123 ) + "/" ;
92- logicDataSource = createDataSource ();
93- TestShardingService testShardingService = new TestShardingService (logicDataSource );
94- testShardingService .processSuccessInClickHouse ();
95- }
96-
97- private Connection openConnection (final String databaseName ) throws SQLException {
98- Properties props = new Properties ();
99- props .setProperty ("user" , "default" );
100- props .setProperty ("password" , "" );
101- return DriverManager .getConnection (jdbcUrlPrefix + databaseName , props );
102- }
103-
104- private DataSource createDataSource () throws SQLException {
105- Awaitility .await ().atMost (Duration .ofMinutes (1L )).ignoreExceptions ().until (() -> {
106- openConnection ("default" ).close ();
107- return true ;
108- });
109- try (
110- Connection connection = openConnection ("default" );
111- Statement statement = connection .createStatement ()) {
112- statement .executeUpdate ("CREATE DATABASE demo_ds_0" );
113- statement .executeUpdate ("CREATE DATABASE demo_ds_1" );
114- statement .executeUpdate ("CREATE DATABASE demo_ds_2" );
115- }
116- Stream .of ("demo_ds_0" , "demo_ds_1" , "demo_ds_2" ).parallel ().forEach (this ::initTable );
11757 HikariConfig config = new HikariConfig ();
11858 config .setDriverClassName ("org.apache.shardingsphere.driver.ShardingSphereDriver" );
119- config .setJdbcUrl ("jdbc:shardingsphere:classpath:test-native/yaml/jdbc/databases/clickhouse.yaml?placeholder-type=system_props" );
120- System .setProperty (systemPropKeyPrefix + "ds0.jdbc-url" , jdbcUrlPrefix + "demo_ds_0" );
121- System .setProperty (systemPropKeyPrefix + "ds1.jdbc-url" , jdbcUrlPrefix + "demo_ds_1" );
122- System .setProperty (systemPropKeyPrefix + "ds2.jdbc-url" , jdbcUrlPrefix + "demo_ds_2" );
123- return new HikariDataSource (config );
124- }
125-
126- /**
127- * ClickHouse does not support `AUTO_INCREMENT`,
128- * refer to <a href="https://github.com/ClickHouse/ClickHouse/issues/56228">ClickHouse/ClickHouse#56228</a> .
129- * TODO The {@code shardingsphere-parser-sql-clickhouse} module needs to be fixed to use SQL like `create table`,
130- * `truncate table` and `drop table`.
131- *
132- * @param databaseName database name
133- * @throws RuntimeException SQL exception
134- */
135- private void initTable (final String databaseName ) {
136- try (
137- Connection connection = openConnection (databaseName );
138- Statement statement = connection .createStatement ()) {
139- statement .execute ("create table IF NOT EXISTS t_order\n "
140- + "(\n "
141- + " order_id Int64 NOT NULL,\n "
142- + " order_type Int32,\n "
143- + " user_id Int32 NOT NULL,\n "
144- + " address_id Int64 NOT NULL,\n "
145- + " status VARCHAR(50)\n "
146- + ") engine = MergeTree\n "
147- + " primary key (order_id)\n "
148- + " order by (order_id)" );
149- statement .execute ("create table IF NOT EXISTS t_order_item\n "
150- + "(\n "
151- + " order_item_id Int64 NOT NULL,\n "
152- + " order_id Int64 NOT NULL,\n "
153- + " user_id Int32 NOT NULL,\n "
154- + " phone VARCHAR(50),\n "
155- + " status VARCHAR(50)\n "
156- + ") engine = MergeTree\n "
157- + " primary key (order_item_id)\n "
158- + " order by (order_item_id)" );
159- statement .execute ("CREATE TABLE IF NOT EXISTS t_address\n "
160- + "(\n "
161- + " address_id BIGINT NOT NULL,\n "
162- + " address_name VARCHAR(100) NOT NULL,\n "
163- + " PRIMARY KEY (address_id)\n "
164- + ")" );
165- statement .execute ("TRUNCATE TABLE t_order" );
166- statement .execute ("TRUNCATE TABLE t_order_item" );
167- statement .execute ("TRUNCATE TABLE t_address" );
168- } catch (final SQLException exception ) {
169- throw new RuntimeException (exception );
170- }
59+ config .setJdbcUrl ("jdbc:shardingsphere:classpath:test-native/yaml/jdbc/databases/clickhouse.yaml" );
60+ logicDataSource = new HikariDataSource (config );
61+ TestShardingService testShardingService = new TestShardingService (logicDataSource );
62+ testShardingService .processSuccessInClickHouse ();
17163 }
17264}
0 commit comments