|
1 | 1 | package com.clickhouse.client; |
2 | 2 |
|
3 | 3 | import com.clickhouse.client.api.Client; |
| 4 | +import com.clickhouse.client.api.ConnectionInitiationException; |
4 | 5 | import com.clickhouse.client.api.enums.ProxyType; |
5 | 6 | import com.clickhouse.client.api.query.GenericRecord; |
| 7 | +import com.clickhouse.client.api.query.QueryResponse; |
| 8 | +import com.clickhouse.client.config.ClickHouseClientOption; |
6 | 9 | import com.github.tomakehurst.wiremock.WireMockServer; |
7 | 10 | import com.github.tomakehurst.wiremock.client.WireMock; |
8 | 11 | import com.github.tomakehurst.wiremock.common.Slf4jNotifier; |
9 | 12 | import com.github.tomakehurst.wiremock.core.WireMockConfiguration; |
10 | 13 | import com.github.tomakehurst.wiremock.http.trafficlistener.WiremockNetworkTrafficListener; |
| 14 | +import org.apache.hc.core5.http.ConnectionRequestTimeoutException; |
| 15 | +import org.apache.hc.core5.http.HttpStatus; |
11 | 16 | import org.apache.hc.core5.net.URIBuilder; |
12 | 17 | import org.testng.Assert; |
13 | 18 | import org.testng.annotations.DataProvider; |
|
18 | 23 | import java.time.temporal.ChronoUnit; |
19 | 24 | import java.util.List; |
20 | 25 | import java.util.Random; |
| 26 | +import java.util.concurrent.CompletableFuture; |
| 27 | +import java.util.concurrent.ExecutionException; |
21 | 28 | import java.util.concurrent.atomic.AtomicInteger; |
22 | 29 |
|
23 | 30 | public class ConnectionManagementTests extends BaseIntegrationTest{ |
@@ -116,4 +123,44 @@ public void closed(Socket socket) { |
116 | 123 | } |
117 | 124 | } |
118 | 125 |
|
| 126 | + @Test(groups = {"integration"}) |
| 127 | + public void testConnectionRequestTimeout() { |
| 128 | + |
| 129 | + int serverPort = new Random().nextInt(1000) + 10000; |
| 130 | + System.out.println("proxyPort: " + serverPort); |
| 131 | + ConnectionCounterListener connectionCounter = new ConnectionCounterListener(); |
| 132 | + WireMockServer proxy = new WireMockServer(WireMockConfiguration |
| 133 | + .options().port(serverPort) |
| 134 | + .networkTrafficListener(connectionCounter) |
| 135 | + .notifier(new Slf4jNotifier(true))); |
| 136 | + proxy.start(); |
| 137 | + proxy.addStubMapping(WireMock.post(WireMock.anyUrl()) |
| 138 | + .willReturn(WireMock.aResponse().withFixedDelay(5000) |
| 139 | + .withStatus(HttpStatus.SC_NOT_FOUND)).build()); |
| 140 | + |
| 141 | + Client.Builder clientBuilder = new Client.Builder() |
| 142 | + .addEndpoint("http://localhost:" + serverPort) |
| 143 | + .setUsername("default") |
| 144 | + .setPassword(getPassword()) |
| 145 | + .useNewImplementation(true) |
| 146 | + .setMaxConnections(1) |
| 147 | + .setOption(ClickHouseClientOption.ASYNC.getKey(), "true") |
| 148 | + .setSocketTimeout(10000, ChronoUnit.MILLIS) |
| 149 | + .setConnectionRequestTimeout(5, ChronoUnit.MILLIS); |
| 150 | + |
| 151 | + try (Client client = clientBuilder.build()) { |
| 152 | + CompletableFuture<QueryResponse> f1 = client.query("select 1"); |
| 153 | + CompletableFuture<QueryResponse> f2 = client.query("select 1"); |
| 154 | + f2.get(); |
| 155 | + } catch (ExecutionException e) { |
| 156 | + e.printStackTrace(); |
| 157 | + Assert.assertTrue(e.getCause() instanceof ConnectionInitiationException); |
| 158 | + Assert.assertTrue(e.getCause().getCause() instanceof ConnectionRequestTimeoutException); |
| 159 | + } catch (Exception e) { |
| 160 | + e.printStackTrace(); |
| 161 | + Assert.fail("Unexpected exception", e); |
| 162 | + } finally { |
| 163 | + proxy.stop(); |
| 164 | + } |
| 165 | + } |
119 | 166 | } |
0 commit comments