Skip to content

Commit 4cafdcb

Browse files
authored
Merge pull request #1596 from ClickHouse/fix-handling-no-response
wrappend req execution. added test
2 parents 3659b25 + 8f7bc67 commit 4cafdcb

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

clickhouse-http-client/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@
103103
<artifactId>testng</artifactId>
104104
<scope>test</scope>
105105
</dependency>
106+
<!-- Replace with newer version as soon java 11 is minimal version -->
107+
<dependency>
108+
<groupId>com.github.tomakehurst</groupId>
109+
<artifactId>wiremock-jre8</artifactId>
110+
<version>2.35.2</version>
111+
<scope>test</scope>
112+
</dependency>
113+
106114
</dependencies>
107115

108116
<build>

clickhouse-http-client/src/main/java/com/clickhouse/client/http/ApacheHttpConnectionImpl.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.clickhouse.client.AbstractSocketClient;
44
import com.clickhouse.client.ClickHouseClient;
55
import com.clickhouse.client.ClickHouseConfig;
6+
import com.clickhouse.client.ClickHouseException;
67
import com.clickhouse.client.ClickHouseNode;
78
import com.clickhouse.client.ClickHouseRequest;
89
import com.clickhouse.client.ClickHouseSocketFactory;
@@ -249,7 +250,12 @@ protected ClickHouseHttpResponse post(ClickHouseConfig config, String sql, Click
249250
ClickHouseHttpEntity postBody = new ClickHouseHttpEntity(config, contentType, contentEncoding, boundary,
250251
sql, data, tables);
251252
post.setEntity(postBody);
252-
CloseableHttpResponse response = client.execute(post);
253+
CloseableHttpResponse response;
254+
try {
255+
response = client.execute(post);
256+
} catch (IOException e) {
257+
throw new ConnectException(ClickHouseUtils.format("HTTP request failed: %s", e.getMessage()));
258+
}
253259

254260
checkResponse(config, response);
255261
// buildResponse should use the config of current request in case of reusable

clickhouse-http-client/src/test/java/com/clickhouse/client/http/ApacheHttpConnectionImplTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.clickhouse.client.ClickHouseClient;
44
import com.clickhouse.client.ClickHouseConfig;
5+
import com.clickhouse.client.ClickHouseException;
56
import com.clickhouse.client.ClickHouseNode;
67
import com.clickhouse.client.ClickHouseProtocol;
78
import com.clickhouse.client.ClickHouseRequest;
@@ -22,6 +23,9 @@
2223
import java.util.TreeMap;
2324
import java.util.concurrent.atomic.AtomicBoolean;
2425

26+
import com.github.tomakehurst.wiremock.WireMockServer;
27+
import com.github.tomakehurst.wiremock.client.WireMock;
28+
import com.github.tomakehurst.wiremock.http.Fault;
2529
import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory;
2630
import org.testng.Assert;
2731
import org.testng.annotations.Test;
@@ -115,4 +119,29 @@ public void testCustomOptions() throws Exception {
115119
CustomSocketFactory.created.set(false);
116120
}
117121
}
122+
123+
private WireMockServer faultyServer;
124+
125+
@Test(groups = {"unit"})
126+
public void testFailureWhileRequest() {
127+
faultyServer = new WireMockServer(9090);
128+
faultyServer.start();
129+
try {
130+
faultyServer.addStubMapping(WireMock.post(WireMock.anyUrl())
131+
.willReturn(WireMock.aResponse().withFault(Fault.EMPTY_RESPONSE)).build());
132+
133+
ClickHouseHttpClient httpClient = new ClickHouseHttpClient();
134+
ClickHouseConfig config = new ClickHouseConfig();
135+
httpClient.init(config);
136+
ClickHouseRequest request = httpClient.read("http://localhost:9090/").query("SELECT 1");
137+
138+
try {
139+
httpClient.executeAndWait(request);
140+
} catch (ClickHouseException e) {
141+
Assert.assertEquals(e.getErrorCode(), ClickHouseException.ERROR_NETWORK);
142+
}
143+
} finally {
144+
faultyServer.stop();
145+
}
146+
}
118147
}

0 commit comments

Comments
 (0)