|
25 | 25 | import java.util.concurrent.atomic.AtomicBoolean; |
26 | 26 |
|
27 | 27 | import com.github.tomakehurst.wiremock.WireMockServer; |
| 28 | +import com.github.tomakehurst.wiremock.admin.model.ScenarioState; |
28 | 29 | import com.github.tomakehurst.wiremock.client.WireMock; |
29 | 30 | import com.github.tomakehurst.wiremock.core.WireMockConfiguration; |
30 | 31 | import com.github.tomakehurst.wiremock.http.Fault; |
31 | 32 | import com.github.tomakehurst.wiremock.stubbing.Scenario; |
| 33 | +import com.github.tomakehurst.wiremock.stubbing.StubMapping; |
| 34 | +import com.github.tomakehurst.wiremock.stubbing.Scenario; |
32 | 35 | import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory; |
33 | 36 | import org.apache.hc.core5.http.NoHttpResponseException; |
| 37 | +import org.apache.hc.core5.http.HttpStatus; |
34 | 38 | import org.testng.Assert; |
35 | 39 | import org.testng.annotations.DataProvider; |
36 | 40 | import org.testng.annotations.Test; |
@@ -144,12 +148,70 @@ public void testFailureWhileRequest() { |
144 | 148 | httpClient.executeAndWait(request); |
145 | 149 | } catch (ClickHouseException e) { |
146 | 150 | Assert.assertEquals(e.getErrorCode(), ClickHouseException.ERROR_NETWORK); |
| 151 | + return; |
147 | 152 | } |
| 153 | + |
| 154 | + Assert.fail("Should throw exception"); |
148 | 155 | } finally { |
149 | 156 | faultyServer.stop(); |
150 | 157 | } |
151 | 158 | } |
152 | 159 |
|
| 160 | + @Test(groups = {"unit"}, dataProvider = "retryOnFailureProvider") |
| 161 | + public void testRetryOnFailure(StubMapping failureStub) { |
| 162 | + faultyServer = new WireMockServer(9090); |
| 163 | + faultyServer.start(); |
| 164 | + try { |
| 165 | + faultyServer.addStubMapping(failureStub); |
| 166 | + faultyServer.addStubMapping(WireMock.post(WireMock.anyUrl()) |
| 167 | + .withRequestBody(WireMock.equalTo("SELECT 1")) |
| 168 | + .inScenario("Retry") |
| 169 | + .whenScenarioStateIs("Failed") |
| 170 | + .willReturn(WireMock.aResponse() |
| 171 | + .withHeader("X-ClickHouse-Summary", |
| 172 | + "{ \"read_bytes\": \"10\", \"read_rows\": \"1\"}")) |
| 173 | + .build()); |
| 174 | + |
| 175 | + ClickHouseHttpClient httpClient = new ClickHouseHttpClient(); |
| 176 | + Map<ClickHouseOption, Serializable> options = new HashMap<>(); |
| 177 | + options.put(ClickHouseHttpOption.AHC_RETRY_ON_FAILURE, true); |
| 178 | + ClickHouseConfig config = new ClickHouseConfig(options); |
| 179 | + httpClient.init(config); |
| 180 | + ClickHouseRequest request = httpClient.read("http://localhost:9090/").query("SELECT 1"); |
| 181 | + |
| 182 | + ClickHouseResponse response = null; |
| 183 | + try { |
| 184 | + response = httpClient.executeAndWait(request); |
| 185 | + } catch (ClickHouseException e) { |
| 186 | + Assert.fail("Should not throw exception", e); |
| 187 | + } |
| 188 | + Assert.assertEquals(response.getSummary().getReadBytes(), 10); |
| 189 | + Assert.assertEquals(response.getSummary().getReadRows(), 1); |
| 190 | + } finally { |
| 191 | + faultyServer.stop(); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + @DataProvider(name = "retryOnFailureProvider") |
| 196 | + private static StubMapping[] retryOnFailureProvider() { |
| 197 | + return new StubMapping[] { |
| 198 | + WireMock.post(WireMock.anyUrl()) |
| 199 | + .withRequestBody(WireMock.equalTo("SELECT 1")) |
| 200 | + .inScenario("Retry") |
| 201 | + .whenScenarioStateIs(Scenario.STARTED) |
| 202 | + .willReturn(WireMock.aResponse().withFault(Fault.EMPTY_RESPONSE)) |
| 203 | + .willSetStateTo("Failed") |
| 204 | + .build() |
| 205 | + ,WireMock.post(WireMock.anyUrl()) |
| 206 | + .withRequestBody(WireMock.equalTo("SELECT 1")) |
| 207 | + .inScenario("Retry") |
| 208 | + .whenScenarioStateIs(Scenario.STARTED) |
| 209 | + .willReturn(WireMock.aResponse().withStatus(HttpStatus.SC_SERVICE_UNAVAILABLE)) |
| 210 | + .willSetStateTo("Failed") |
| 211 | + .build() |
| 212 | + }; |
| 213 | + } |
| 214 | + |
153 | 215 | @Test(groups = {"unit"}, dataProvider = "validationTimeoutProvider") |
154 | 216 | public void testNoHttpResponseExceptionWithValidation(long validationTimeout) { |
155 | 217 |
|
|
0 commit comments