|
16 | 16 |
|
17 | 17 | import java.io.IOException; |
18 | 18 | import java.io.Serializable; |
| 19 | +import java.net.ConnectException; |
19 | 20 | import java.util.Collections; |
20 | 21 | import java.util.HashMap; |
21 | 22 | import java.util.List; |
|
25 | 26 |
|
26 | 27 | import com.github.tomakehurst.wiremock.WireMockServer; |
27 | 28 | import com.github.tomakehurst.wiremock.client.WireMock; |
| 29 | +import com.github.tomakehurst.wiremock.core.WireMockConfiguration; |
28 | 30 | import com.github.tomakehurst.wiremock.http.Fault; |
| 31 | +import com.github.tomakehurst.wiremock.stubbing.Scenario; |
29 | 32 | import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory; |
| 33 | +import org.apache.hc.core5.http.NoHttpResponseException; |
30 | 34 | import org.testng.Assert; |
| 35 | +import org.testng.annotations.DataProvider; |
31 | 36 | import org.testng.annotations.Test; |
32 | 37 |
|
33 | 38 | public class ApacheHttpConnectionImplTest extends ClickHouseHttpClientTest { |
@@ -144,4 +149,63 @@ public void testFailureWhileRequest() { |
144 | 149 | faultyServer.stop(); |
145 | 150 | } |
146 | 151 | } |
| 152 | + |
| 153 | + @Test(groups = {"unit"}, dataProvider = "validationTimeoutProvider") |
| 154 | + public void testNoHttpResponseExceptionWithValidation(long validationTimeout) { |
| 155 | + |
| 156 | + faultyServer = new WireMockServer(9090); |
| 157 | + faultyServer.start(); |
| 158 | + |
| 159 | + faultyServer.addStubMapping(WireMock.post(WireMock.anyUrl()) |
| 160 | + .inScenario("validateOnStaleConnection") |
| 161 | + .withRequestBody(WireMock.equalTo("SELECT 100")) |
| 162 | + .willReturn(WireMock.aResponse() |
| 163 | + .withHeader("X-ClickHouse-Summary", |
| 164 | + "{ \"read_bytes\": \"10\", \"read_rows\": \"1\"}")) |
| 165 | + .build()); |
| 166 | + |
| 167 | + |
| 168 | + ClickHouseHttpClient httpClient = new ClickHouseHttpClient(); |
| 169 | + Map<ClickHouseOption, Serializable> options = new HashMap<>(); |
| 170 | + options.put(ClickHouseHttpOption.AHC_VALIDATE_AFTER_INACTIVITY, validationTimeout); |
| 171 | + options.put(ClickHouseHttpOption.MAX_OPEN_CONNECTIONS, 1); |
| 172 | + ClickHouseConfig config = new ClickHouseConfig(options); |
| 173 | + httpClient.init(config); |
| 174 | + ClickHouseRequest request = httpClient.read("http://localhost:9090/").query("SELECT 100"); |
| 175 | + |
| 176 | + Runnable powerBlink = () -> { |
| 177 | + try { |
| 178 | + Thread.sleep(100); |
| 179 | + faultyServer.stop(); |
| 180 | + Thread.sleep(50); |
| 181 | + faultyServer.start(); |
| 182 | + } catch (InterruptedException e) { |
| 183 | + Assert.fail("Unexpected exception", e); |
| 184 | + } |
| 185 | + }; |
| 186 | + try { |
| 187 | + ClickHouseResponse response = httpClient.executeAndWait(request); |
| 188 | + Assert.assertEquals(response.getSummary().getReadRows(), 1); |
| 189 | + response.close(); |
| 190 | + new Thread(powerBlink).start(); |
| 191 | + Thread.sleep(200); |
| 192 | + response = httpClient.executeAndWait(request); |
| 193 | + Assert.assertEquals(response.getSummary().getReadRows(), 1); |
| 194 | + response.close(); |
| 195 | + } catch (Exception e) { |
| 196 | + if (validationTimeout < 0) { |
| 197 | + Assert.assertTrue(e instanceof ClickHouseException); |
| 198 | + Assert.assertTrue(e.getCause() instanceof ConnectException); |
| 199 | + } else { |
| 200 | + Assert.fail("Unexpected exception", e); |
| 201 | + } |
| 202 | + } finally { |
| 203 | + faultyServer.stop(); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + @DataProvider(name = "validationTimeoutProvider") |
| 208 | + public static Object[] validationTimeoutProvider() { |
| 209 | + return new Long[] {-1L , 100L }; |
| 210 | + } |
147 | 211 | } |
0 commit comments