-
Notifications
You must be signed in to change notification settings - Fork 543
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1686 from ClickHouse/fixing-proxy-support
Fixing proxy support and added a test to cover
- Loading branch information
Showing
3 changed files
with
122 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
client-v2/src/test/java/com/clickhouse/client/ProxyTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package com.clickhouse.client; | ||
|
||
import com.clickhouse.client.api.Client; | ||
import com.clickhouse.client.api.ClientException; | ||
import com.clickhouse.client.api.enums.Protocol; | ||
import com.clickhouse.client.api.enums.ProxyType; | ||
import com.clickhouse.client.api.insert.InsertResponse; | ||
import com.clickhouse.client.api.insert.InsertSettings; | ||
import com.clickhouse.client.api.metrics.ClientMetrics; | ||
import com.clickhouse.client.api.metrics.OperationMetrics; | ||
import com.clickhouse.client.api.metrics.ServerMetrics; | ||
import com.clickhouse.client.insert.SamplePOJO; | ||
import eu.rekawek.toxiproxy.Proxy; | ||
import eu.rekawek.toxiproxy.ToxiproxyClient; | ||
import org.testcontainers.containers.ToxiproxyContainer; | ||
import org.testng.annotations.AfterMethod; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertThrows; | ||
import static org.testng.Assert.assertTrue; | ||
|
||
public class ProxyTests extends BaseIntegrationTest{ | ||
private Client client; | ||
ToxiproxyContainer toxiproxy = null; | ||
ToxiproxyClient toxiproxyClient = null; | ||
Proxy proxy = null; | ||
|
||
@BeforeMethod(groups = { "integration" }, enabled = true) | ||
public void setUp() throws IOException { | ||
ClickHouseNode node = getServer(ClickHouseProtocol.HTTP); | ||
toxiproxy = new ToxiproxyContainer(ClickHouseServerForTest.getProxyImage()) | ||
.withNetwork(ClickHouseServerForTest.getNetwork()); | ||
toxiproxy.start(); | ||
|
||
toxiproxyClient = new ToxiproxyClient(toxiproxy.getHost(), toxiproxy.getControlPort()); | ||
proxy = toxiproxyClient.createProxy("clickhouse", "0.0.0.0:8666", | ||
ClickHouseServerForTest.hasClickHouseContainer() | ||
? "clickhouse:" + ClickHouseProtocol.HTTP.getDefaultPort() | ||
: ClickHouseServerForTest.getClickHouseAddress(ClickHouseProtocol.HTTP, true)); | ||
|
||
client = new Client.Builder() | ||
.addEndpoint(Protocol.HTTP, node.getHost(), node.getPort(), false) | ||
.setUsername("default") | ||
.setPassword("") | ||
.addProxy(ProxyType.HTTP, toxiproxy.getHost(), toxiproxy.getMappedPort(8666)) | ||
.build(); | ||
} | ||
|
||
@AfterMethod(groups = { "integration" }, enabled = true) | ||
public void teardown() { | ||
if (toxiproxy != null) { | ||
toxiproxy.stop(); | ||
} | ||
} | ||
|
||
private void createTable(String tableQuery) throws ClickHouseException { | ||
try (ClickHouseClient client = ClickHouseClient.builder().config(new ClickHouseConfig()) | ||
.nodeSelector(ClickHouseNodeSelector.of(ClickHouseProtocol.HTTP)) | ||
.build()) { | ||
client.read(getServer(ClickHouseProtocol.HTTP)).query(tableQuery).executeAndWait().close(); | ||
} | ||
} | ||
|
||
|
||
@Test(groups = { "integration" }, enabled = false) | ||
public void simpleProxyTest() throws Exception { | ||
String tableName = "simple_pojo_proxy_table"; | ||
String createSQL = SamplePOJO.generateTableCreateSQL(tableName); | ||
System.out.println(createSQL); | ||
createTable(createSQL); | ||
|
||
client.register(SamplePOJO.class, SamplePOJO.generateTableSchema(tableName)); | ||
List<Object> simplePOJOs = new ArrayList<>(); | ||
|
||
for (int i = 0; i < 1000; i++) { | ||
simplePOJOs.add(new SamplePOJO()); | ||
} | ||
proxy.enable(); | ||
InsertResponse response = client.insert(tableName, simplePOJOs).get(120, TimeUnit.SECONDS); | ||
|
||
OperationMetrics metrics = response.getMetrics(); | ||
assertEquals(simplePOJOs.size(), metrics.getMetric(ServerMetrics.NUM_ROWS_WRITTEN).getLong()); | ||
assertEquals(simplePOJOs.size(), response.getWrittenRows()); | ||
assertTrue(metrics.getMetric(ClientMetrics.OP_DURATION).getLong() > 0); | ||
assertTrue(metrics.getMetric(ClientMetrics.OP_SERIALIZATION).getLong() > 0); | ||
} | ||
|
||
@Test(groups = { "integration" }, enabled = true) | ||
public void simpleDisabledProxyTest() throws Exception { | ||
String tableName = "simple_pojo_proxy_table"; | ||
String createSQL = SamplePOJO.generateTableCreateSQL(tableName); | ||
System.out.println(createSQL); | ||
createTable(createSQL); | ||
|
||
client.register(SamplePOJO.class, SamplePOJO.generateTableSchema(tableName)); | ||
List<Object> simplePOJOs = new ArrayList<>(); | ||
|
||
for (int i = 0; i < 1000; i++) { | ||
simplePOJOs.add(new SamplePOJO()); | ||
} | ||
//proxy.disable(); | ||
try { | ||
InsertResponse response = client.insert(tableName, simplePOJOs).get(120, TimeUnit.SECONDS); | ||
} catch (Exception e) { | ||
//FOR NOW THIS WILL FAIL | ||
//IF IT SUCCEEDS (meaning the test fails), YAY PROXY WORKS WE SHOULD REIMPLEMENT THE TESTS! | ||
assertTrue(e.getMessage().contains("Operation has likely timed out.")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters