11package com .clickhouse .client ;
22
33import com .clickhouse .client .api .Client ;
4- import com .clickhouse .client .api .ClientException ;
54import com .clickhouse .client .api .enums .Protocol ;
65import com .clickhouse .client .api .enums .ProxyType ;
76import com .clickhouse .client .api .insert .InsertResponse ;
8- import com .clickhouse .client .api .insert .InsertSettings ;
9- import com .clickhouse .client .api .metrics .ClientMetrics ;
10- import com .clickhouse .client .api .metrics .OperationMetrics ;
11- import com .clickhouse .client .api .metrics .ServerMetrics ;
7+ import com .clickhouse .client .api .query .GenericRecord ;
128import com .clickhouse .client .insert .SamplePOJO ;
13- import eu .rekawek .toxiproxy .Proxy ;
14- import eu .rekawek .toxiproxy .ToxiproxyClient ;
15- import org .testcontainers .containers .ToxiproxyContainer ;
9+ import com .github .tomakehurst .wiremock .WireMockServer ;
10+ import org .testng .Assert ;
1611import org .testng .annotations .AfterMethod ;
1712import org .testng .annotations .BeforeMethod ;
1813import org .testng .annotations .Test ;
2217import java .util .List ;
2318import java .util .concurrent .TimeUnit ;
2419
25- import static org . testng . Assert . assertEquals ;
26- import static org . testng . Assert . assertThrows ;
27- import static org . testng . Assert . assertTrue ;
20+ import static com . github . tomakehurst . wiremock . client . WireMock . aResponse ;
21+ import static com . github . tomakehurst . wiremock . client . WireMock . post ;
22+ import static com . github . tomakehurst . wiremock . client . WireMock . urlMatching ;
2823import static org .testng .Assert .fail ;
2924
3025public class ProxyTests extends BaseIntegrationTest {
3126 private Client client ;
32- ToxiproxyContainer toxiproxy = null ;
33- ToxiproxyClient toxiproxyClient = null ;
34- Proxy proxy = null ;
3527
36- @ BeforeMethod (groups = { "integration" }, enabled = false )
28+ private WireMockServer proxy = new WireMockServer (8666 );
29+ @ BeforeMethod (groups = { "integration" })
3730 public void setUp () throws IOException {
31+ proxy .start ();
3832 ClickHouseNode node = getServer (ClickHouseProtocol .HTTP );
39- toxiproxy = new ToxiproxyContainer (ClickHouseServerForTest .getProxyImage ())
40- .withNetwork (ClickHouseServerForTest .getNetwork ());
41- toxiproxy .start ();
42-
43- toxiproxyClient = new ToxiproxyClient (toxiproxy .getHost (), toxiproxy .getControlPort ());
44- proxy = toxiproxyClient .createProxy ("clickhouse" , "0.0.0.0:8666" ,
45- ClickHouseServerForTest .hasClickHouseContainer ()
46- ? "clickhouse:" + ClickHouseProtocol .HTTP .getDefaultPort ()
47- : ClickHouseServerForTest .getClickHouseAddress (ClickHouseProtocol .HTTP , true ));
48-
33+ proxy .addStubMapping (post (urlMatching ("/.*" ))
34+ .willReturn (aResponse ().proxiedFrom ("http://localhost:" + node .getPort ())).build ());
4935 client = new Client .Builder ()
50- .addEndpoint (Protocol .HTTP , node . getHost (), node . getPort () , false )
36+ .addEndpoint (Protocol .HTTP , "clickhouse" , 8123 , false )
5137 .setUsername ("default" )
5238 .setPassword ("" )
53- .addProxy (ProxyType .HTTP , toxiproxy .getHost (), toxiproxy .getMappedPort (8666 ))
39+ // .useNewImplementation(true)
40+ .addProxy (ProxyType .HTTP , "localhost" , 8666 )
5441 .build ();
5542 }
5643
57- @ AfterMethod (groups = { "integration" }, enabled = false )
44+ @ AfterMethod (groups = { "integration" })
5845 public void teardown () {
59- if (toxiproxy != null ) {
60- toxiproxy .stop ();
61- }
46+ proxy .stop ();
6247 }
6348
6449 private void createTable (String tableQuery ) throws ClickHouseException {
@@ -68,33 +53,15 @@ private void createTable(String tableQuery) throws ClickHouseException {
6853 client .read (getServer (ClickHouseProtocol .HTTP )).query (tableQuery ).executeAndWait ().close ();
6954 }
7055 }
71-
72-
73- @ Test (groups = { "integration" }, enabled = false )
74- public void simpleProxyTest () throws Exception {
75- String tableName = "simple_pojo_enable_proxy_table" ;
76- String createSQL = SamplePOJO .generateTableCreateSQL (tableName );
77- System .out .println (createSQL );
78- createTable (createSQL );
79-
80- client .register (SamplePOJO .class , client .getTableSchema (tableName , "default" ));
81- List <Object > simplePOJOs = new ArrayList <>();
82-
83- for (int i = 0 ; i < 1000 ; i ++) {
84- simplePOJOs .add (new SamplePOJO ());
85- }
86- proxy .enable ();
87- InsertResponse response = client .insert (tableName , simplePOJOs ).get (120 , TimeUnit .SECONDS );
88-
89- OperationMetrics metrics = response .getMetrics ();
90- assertEquals (simplePOJOs .size (), metrics .getMetric (ServerMetrics .NUM_ROWS_WRITTEN ).getLong ());
91- assertEquals (simplePOJOs .size (), response .getWrittenRows ());
92- assertTrue (metrics .getMetric (ClientMetrics .OP_DURATION ).getLong () > 0 );
93- assertTrue (metrics .getMetric (ClientMetrics .OP_SERIALIZATION ).getLong () > 0 );
56+
57+ @ Test (groups = { "integration" })
58+ public void testSimpleQuery () throws Exception {
59+ List <GenericRecord > records = client .queryAll ("select timezone()" );
60+ Assert .assertEquals (records .stream ().findFirst ().get ().getString (1 ), "UTC" );
9461 }
9562
96- @ Test (groups = { "integration" }, enabled = false )
97- public void simpleDisabledProxyTest () throws Exception {
63+ @ Test (groups = { "integration" })
64+ public void testInsert () throws Exception {
9865 String tableName = "simple_pojo_disable_proxy_table" ;
9966 String createSQL = SamplePOJO .generateTableCreateSQL (tableName );
10067 System .out .println (createSQL );
@@ -106,12 +73,12 @@ public void simpleDisabledProxyTest() throws Exception {
10673 for (int i = 0 ; i < 1000 ; i ++) {
10774 simplePOJOs .add (new SamplePOJO ());
10875 }
109- proxy . disable ();
76+
11077 try {
11178 InsertResponse response = client .insert (tableName , simplePOJOs ).get (120 , TimeUnit .SECONDS );
112- fail ( "Should have thrown exception." );
79+ Assert . assertEquals ( response . getWrittenRows (), 1000 );
11380 } catch (Exception e ) {
114- assertTrue ( e instanceof ClientException );
81+ fail ( "Should not have thrown exception." , e );
11582 }
11683 }
11784}
0 commit comments