11package com .clickhouse .client ;
22
33import com .clickhouse .client .api .Client ;
4+ import com .clickhouse .client .api .ClientException ;
45import com .clickhouse .client .api .ClientFaultCause ;
56import com .clickhouse .client .api .ConnectionInitiationException ;
67import com .clickhouse .client .api .ConnectionReuseStrategy ;
2122import org .apache .hc .core5 .http .ConnectionRequestTimeoutException ;
2223import org .apache .hc .core5 .http .HttpStatus ;
2324import org .apache .hc .core5 .net .URIBuilder ;
25+ import org .testcontainers .utility .ThrowingFunction ;
2426import org .testng .Assert ;
2527import org .testng .annotations .DataProvider ;
2628import org .testng .annotations .Test ;
3335import java .util .Random ;
3436import java .util .concurrent .CompletableFuture ;
3537import java .util .concurrent .ExecutionException ;
38+ import java .util .concurrent .Future ;
3639import java .util .concurrent .TimeUnit ;
3740import java .util .concurrent .atomic .AtomicInteger ;
41+ import java .util .function .Consumer ;
3842
3943import static com .github .tomakehurst .wiremock .stubbing .Scenario .STARTED ;
4044
4145public class HttpTransportTests extends BaseIntegrationTest {
4246
47+ static {
48+ System .setProperty ("org.slf4j.simpleLogger.defaultLogLevel" , "DEBUG" );
49+ }
4350
4451 @ Test (groups = {"integration" },dataProvider = "testConnectionTTLProvider" )
4552 @ SuppressWarnings ("java:S2925" )
@@ -219,26 +226,24 @@ public void testSecureConnection() {
219226 }
220227 }
221228
222- @ Test (groups = { "integration" }, enabled = true )
223- public void testNoHttpResponseFailure () {
229+ @ Test (groups = { "integration" }, dataProvider = "NoResponseFailureProvider" )
230+ public void testInsertAndNoHttpResponseFailure (String body , int maxRetries , ThrowingFunction <Client , Void > function ,
231+ boolean shouldFail ) {
224232 WireMockServer faultyServer = new WireMockServer ( WireMockConfiguration
225233 .options ().port (9090 ).notifier (new ConsoleNotifier (false )));
226234 faultyServer .start ();
227235
228- byte [] requestBody = ("INSERT INTO table01 FORMAT " +
229- ClickHouseFormat .TSV .name () + " \n 1\t 2\t 3\n " ).getBytes ();
230-
231236 // First request gets no response
232237 faultyServer .addStubMapping (WireMock .post (WireMock .anyUrl ())
233- .withRequestBody (WireMock .binaryEqualTo ( requestBody ))
238+ .withRequestBody (WireMock .equalTo ( body ))
234239 .inScenario ("Retry" )
235240 .whenScenarioStateIs (STARTED )
236241 .willSetStateTo ("Failed" )
237242 .willReturn (WireMock .aResponse ().withFault (Fault .EMPTY_RESPONSE )).build ());
238243
239244 // Second request gets a response (retry)
240245 faultyServer .addStubMapping (WireMock .post (WireMock .anyUrl ())
241- .withRequestBody (WireMock .binaryEqualTo ( requestBody ))
246+ .withRequestBody (WireMock .equalTo ( body ))
242247 .inScenario ("Retry" )
243248 .whenScenarioStateIs ("Failed" )
244249 .willSetStateTo ("Done" )
@@ -250,20 +255,53 @@ public void testNoHttpResponseFailure() {
250255 .addEndpoint (Protocol .HTTP , "localhost" , faultyServer .port (), false )
251256 .setUsername ("default" )
252257 .setPassword ("" )
253- .useNewImplementation (true )
254- // .useNewImplementation(System.getProperty("client.tests.useNewImplementation", "false").equals("true"))
258+ .useNewImplementation (true ) // because of the internal differences
255259 .compressClientRequest (false )
256- .setOption ( ClickHouseClientOption . RETRY . getKey (), "2" )
260+ .setMaxRetries ( maxRetries )
257261 .build ();
258262
259263 try {
260- InsertResponse insertResponse = mockServerClient .insert ("table01" ,
261- new ByteArrayInputStream ("1\t 2\t 3\n " .getBytes ()), ClickHouseFormat .TSV ).get (30 , TimeUnit .SECONDS );
262- insertResponse .close ();
264+ function .apply (mockServerClient );
265+ } catch (ClientException e ) {
266+ e .printStackTrace ();
267+ if (!shouldFail ) {
268+ Assert .fail ("Unexpected exception" , e );
269+ }
270+ return ;
263271 } catch (Exception e ) {
264272 Assert .fail ("Unexpected exception" , e );
265273 } finally {
266274 faultyServer .stop ();
267275 }
276+
277+ if (shouldFail ) {
278+ Assert .fail ("Expected exception" );
279+ }
280+ }
281+
282+ @ DataProvider (name = "NoResponseFailureProvider" )
283+ public static Object [][] noResponseFailureProvider () {
284+
285+ String insertBody = "INSERT INTO table01 FORMAT " + ClickHouseFormat .TSV .name () + " \n 1\t 2\t 3\n " ;
286+ ThrowingFunction <Client , Void > insertFunction = (client ) -> {
287+ InsertResponse insertResponse = client .insert ("table01" ,
288+ new ByteArrayInputStream ("1\t 2\t 3\n " .getBytes ()), ClickHouseFormat .TSV ).get (30 , TimeUnit .SECONDS );
289+ insertResponse .close ();
290+ return null ;
291+ };
292+
293+ String selectBody = "select timezone()" ;
294+ ThrowingFunction <Client , Void > queryFunction = (client ) -> {
295+ QueryResponse response = client .query ("select timezone()" ).get (30 , TimeUnit .SECONDS );
296+ response .close ();
297+ return null ;
298+ };
299+
300+ return new Object [][]{
301+ {insertBody , 1 , insertFunction , false },
302+ {selectBody , 1 , queryFunction , false },
303+ {insertBody , 0 , insertFunction , true },
304+ {selectBody , 0 , queryFunction , true }
305+ };
268306 }
269307}
0 commit comments