|
| 1 | +package com.clickhouse.client; |
| 2 | + |
| 3 | +import com.clickhouse.client.api.Client; |
| 4 | +import com.clickhouse.client.api.ClientException; |
| 5 | +import com.clickhouse.client.api.enums.Protocol; |
| 6 | +import com.clickhouse.client.api.enums.ProxyType; |
| 7 | +import 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; |
| 12 | +import com.clickhouse.client.insert.SamplePOJO; |
| 13 | +import eu.rekawek.toxiproxy.Proxy; |
| 14 | +import eu.rekawek.toxiproxy.ToxiproxyClient; |
| 15 | +import org.testcontainers.containers.ToxiproxyContainer; |
| 16 | +import org.testng.annotations.AfterMethod; |
| 17 | +import org.testng.annotations.BeforeMethod; |
| 18 | +import org.testng.annotations.Test; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | + |
| 25 | +import static org.testng.Assert.assertEquals; |
| 26 | +import static org.testng.Assert.assertThrows; |
| 27 | +import static org.testng.Assert.assertTrue; |
| 28 | + |
| 29 | +public class ProxyTests extends BaseIntegrationTest{ |
| 30 | + private Client client; |
| 31 | + ToxiproxyContainer toxiproxy = null; |
| 32 | + ToxiproxyClient toxiproxyClient = null; |
| 33 | + Proxy proxy = null; |
| 34 | + |
| 35 | + @BeforeMethod(groups = { "integration" }, enabled = true) |
| 36 | + public void setUp() throws IOException { |
| 37 | + ClickHouseNode node = getServer(ClickHouseProtocol.HTTP); |
| 38 | + toxiproxy = new ToxiproxyContainer(ClickHouseServerForTest.getProxyImage()) |
| 39 | + .withNetwork(ClickHouseServerForTest.getNetwork()); |
| 40 | + toxiproxy.start(); |
| 41 | + |
| 42 | + toxiproxyClient = new ToxiproxyClient(toxiproxy.getHost(), toxiproxy.getControlPort()); |
| 43 | + proxy = toxiproxyClient.createProxy("clickhouse", "0.0.0.0:8666", |
| 44 | + ClickHouseServerForTest.hasClickHouseContainer() |
| 45 | + ? "clickhouse:" + ClickHouseProtocol.HTTP.getDefaultPort() |
| 46 | + : ClickHouseServerForTest.getClickHouseAddress(ClickHouseProtocol.HTTP, true)); |
| 47 | + |
| 48 | + client = new Client.Builder() |
| 49 | + .addEndpoint(Protocol.HTTP, node.getHost(), node.getPort(), false) |
| 50 | + .setUsername("default") |
| 51 | + .setPassword("") |
| 52 | + .addProxy(ProxyType.HTTP, toxiproxy.getHost(), toxiproxy.getMappedPort(8666)) |
| 53 | + .build(); |
| 54 | + } |
| 55 | + |
| 56 | + @AfterMethod(groups = { "integration" }, enabled = true) |
| 57 | + public void teardown() { |
| 58 | + if (toxiproxy != null) { |
| 59 | + toxiproxy.stop(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private void createTable(String tableQuery) throws ClickHouseException { |
| 64 | + try (ClickHouseClient client = ClickHouseClient.builder().config(new ClickHouseConfig()) |
| 65 | + .nodeSelector(ClickHouseNodeSelector.of(ClickHouseProtocol.HTTP)) |
| 66 | + .build()) { |
| 67 | + client.read(getServer(ClickHouseProtocol.HTTP)).query(tableQuery).executeAndWait().close(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + @Test(groups = { "integration" }, enabled = false) |
| 73 | + public void simpleProxyTest() throws Exception { |
| 74 | + String tableName = "simple_pojo_proxy_table"; |
| 75 | + String createSQL = SamplePOJO.generateTableCreateSQL(tableName); |
| 76 | + System.out.println(createSQL); |
| 77 | + createTable(createSQL); |
| 78 | + |
| 79 | + client.register(SamplePOJO.class, SamplePOJO.generateTableSchema(tableName)); |
| 80 | + List<Object> simplePOJOs = new ArrayList<>(); |
| 81 | + |
| 82 | + for (int i = 0; i < 1000; i++) { |
| 83 | + simplePOJOs.add(new SamplePOJO()); |
| 84 | + } |
| 85 | + proxy.enable(); |
| 86 | + InsertResponse response = client.insert(tableName, simplePOJOs).get(120, TimeUnit.SECONDS); |
| 87 | + |
| 88 | + OperationMetrics metrics = response.getMetrics(); |
| 89 | + assertEquals(simplePOJOs.size(), metrics.getMetric(ServerMetrics.NUM_ROWS_WRITTEN).getLong()); |
| 90 | + assertEquals(simplePOJOs.size(), response.getWrittenRows()); |
| 91 | + assertTrue(metrics.getMetric(ClientMetrics.OP_DURATION).getLong() > 0); |
| 92 | + assertTrue(metrics.getMetric(ClientMetrics.OP_SERIALIZATION).getLong() > 0); |
| 93 | + } |
| 94 | + |
| 95 | + @Test(groups = { "integration" }, enabled = true) |
| 96 | + public void simpleDisabledProxyTest() throws Exception { |
| 97 | + String tableName = "simple_pojo_proxy_table"; |
| 98 | + String createSQL = SamplePOJO.generateTableCreateSQL(tableName); |
| 99 | + System.out.println(createSQL); |
| 100 | + createTable(createSQL); |
| 101 | + |
| 102 | + client.register(SamplePOJO.class, SamplePOJO.generateTableSchema(tableName)); |
| 103 | + List<Object> simplePOJOs = new ArrayList<>(); |
| 104 | + |
| 105 | + for (int i = 0; i < 1000; i++) { |
| 106 | + simplePOJOs.add(new SamplePOJO()); |
| 107 | + } |
| 108 | + //proxy.disable(); |
| 109 | + try { |
| 110 | + InsertResponse response = client.insert(tableName, simplePOJOs).get(120, TimeUnit.SECONDS); |
| 111 | + } catch (Exception e) { |
| 112 | + //FOR NOW THIS WILL FAIL |
| 113 | + //IF IT SUCCEEDS (meaning the test fails), YAY PROXY WORKS WE SHOULD REIMPLEMENT THE TESTS! |
| 114 | + assertTrue(e.getMessage().contains("Operation has likely timed out.")); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments