Skip to content

Commit cb9cd5c

Browse files
Lexert19Lexert19
authored andcommitted
fix test resource leaks
1 parent 76801e8 commit cb9cd5c

1 file changed

Lines changed: 84 additions & 22 deletions

File tree

integration-test/src/test/java/org/apache/iotdb/db/it/IoTDBRestServiceIT.java

Lines changed: 84 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,13 +2365,14 @@ public void queryDateAndBlobV2(CloseableHttpClient httpClient) {
23652365
}
23662366

23672367
public void queryWithValidTimeZoneHeader(CloseableHttpClient httpClient) {
2368+
CloseableHttpResponse response = null;
23682369
try {
23692370
HttpPost httpPost = getHttpPost("http://127.0.0.1:" + port + "/rest/v1/query");
23702371
httpPost.setHeader("Time-Zone", "+05:00");
23712372
String sql =
23722373
"{\"sql\":\"SELECT count(s3) FROM root.sg25 GROUP BY ([2026-03-28T00:00:00, 2026-03-29T00:00:00), 1d)\"}";
23732374
httpPost.setEntity(new StringEntity(sql, Charset.defaultCharset()));
2374-
CloseableHttpResponse response = httpClient.execute(httpPost);
2375+
response = httpClient.execute(httpPost);
23752376
assertEquals(200, response.getStatusLine().getStatusCode());
23762377
String message = EntityUtils.toString(response.getEntity(), "utf-8");
23772378
JsonObject result = JsonParser.parseString(message).getAsJsonObject();
@@ -2381,11 +2382,22 @@ public void queryWithValidTimeZoneHeader(CloseableHttpClient httpClient) {
23812382
ZonedDateTime.of(2026, 3, 28, 0, 0, 0, 0, ZoneId.of("+05:00")).toInstant().toEpochMilli();
23822383
assertEquals(expectedTimestamp, result.getAsJsonArray("timestamps").get(0).getAsLong());
23832384
} catch (IOException e) {
2385+
e.printStackTrace();
23842386
fail(e.getMessage());
2387+
} finally {
2388+
try {
2389+
if (response != null) {
2390+
response.close();
2391+
}
2392+
} catch (IOException e) {
2393+
e.printStackTrace();
2394+
fail(e.getMessage());
2395+
}
23852396
}
23862397
}
23872398

23882399
public void nonQueryWithValidTimeZoneHeader(CloseableHttpClient httpClient) {
2400+
CloseableHttpResponse response = null;
23892401
try {
23902402
HttpPost createPost = getHttpPost("http://127.0.0.1:" + port + "/rest/v1/nonQuery");
23912403
nonQuery(
@@ -2403,28 +2415,37 @@ public void nonQueryWithValidTimeZoneHeader(CloseableHttpClient httpClient) {
24032415
HttpPost queryPost = getHttpPost("http://127.0.0.1:" + port + "/rest/v1/query");
24042416
queryPost.setEntity(
24052417
new StringEntity("{\"sql\":\"SELECT s1 FROM root.sg_tz.d1\"}", StandardCharsets.UTF_8));
2406-
try (CloseableHttpResponse resp = httpClient.execute(queryPost)) {
2407-
String message = EntityUtils.toString(resp.getEntity(), StandardCharsets.UTF_8);
2408-
JsonObject result = JsonParser.parseString(message).getAsJsonObject();
2409-
long expected =
2410-
ZonedDateTime.of(2026, 3, 28, 0, 0, 0, 0, ZoneId.of("+05:00"))
2411-
.toInstant()
2412-
.toEpochMilli();
2413-
assertEquals(expected, result.getAsJsonArray("timestamps").get(0).getAsLong());
2414-
}
2418+
response = httpClient.execute(queryPost);
2419+
String message = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
2420+
JsonObject result = JsonParser.parseString(message).getAsJsonObject();
2421+
long expected =
2422+
ZonedDateTime.of(2026, 3, 28, 0, 0, 0, 0, ZoneId.of("+05:00")).toInstant().toEpochMilli();
2423+
assertEquals(expected, result.getAsJsonArray("timestamps").get(0).getAsLong());
2424+
24152425
} catch (IOException e) {
2426+
e.printStackTrace();
24162427
fail(e.getMessage());
2428+
} finally {
2429+
try {
2430+
if (response != null) {
2431+
response.close();
2432+
}
2433+
} catch (IOException e) {
2434+
e.printStackTrace();
2435+
fail(e.getMessage());
2436+
}
24172437
}
24182438
}
24192439

24202440
public void queryWithValidTimeZoneHeaderV2(CloseableHttpClient httpClient) {
2441+
CloseableHttpResponse response = null;
24212442
try {
24222443
HttpPost httpPost = getHttpPost("http://127.0.0.1:" + port + "/rest/v2/query");
24232444
httpPost.setHeader("Time-Zone", "+05:00");
24242445
String sql =
24252446
"{\"sql\":\"SELECT count(s3) FROM root.sg25 GROUP BY ([2026-03-28T00:00:00, 2026-03-29T00:00:00), 1d)\"}";
24262447
httpPost.setEntity(new StringEntity(sql, Charset.defaultCharset()));
2427-
CloseableHttpResponse response = httpClient.execute(httpPost);
2448+
response = httpClient.execute(httpPost);
24282449
assertEquals(200, response.getStatusLine().getStatusCode());
24292450
String message = EntityUtils.toString(response.getEntity(), "utf-8");
24302451
JsonObject result = JsonParser.parseString(message).getAsJsonObject();
@@ -2434,11 +2455,22 @@ public void queryWithValidTimeZoneHeaderV2(CloseableHttpClient httpClient) {
24342455
ZonedDateTime.of(2026, 3, 28, 0, 0, 0, 0, ZoneId.of("+05:00")).toInstant().toEpochMilli();
24352456
assertEquals(expectedTimestamp, result.getAsJsonArray("timestamps").get(0).getAsLong());
24362457
} catch (IOException e) {
2458+
e.printStackTrace();
24372459
fail(e.getMessage());
2460+
} finally {
2461+
try {
2462+
if (response != null) {
2463+
response.close();
2464+
}
2465+
} catch (IOException e) {
2466+
e.printStackTrace();
2467+
fail(e.getMessage());
2468+
}
24382469
}
24392470
}
24402471

24412472
public void nonQueryWithValidTimeZoneHeaderV2(CloseableHttpClient httpClient) {
2473+
CloseableHttpResponse response = null;
24422474
try {
24432475
HttpPost createPost = getHttpPost("http://127.0.0.1:" + port + "/rest/v2/nonQuery");
24442476
nonQuery(
@@ -2456,51 +2488,81 @@ public void nonQueryWithValidTimeZoneHeaderV2(CloseableHttpClient httpClient) {
24562488
HttpPost queryPost = getHttpPost("http://127.0.0.1:" + port + "/rest/v2/query");
24572489
queryPost.setEntity(
24582490
new StringEntity("{\"sql\":\"SELECT s1 FROM root.sg_tz.d2\"}", StandardCharsets.UTF_8));
2459-
try (CloseableHttpResponse resp = httpClient.execute(queryPost)) {
2460-
String message = EntityUtils.toString(resp.getEntity(), StandardCharsets.UTF_8);
2461-
JsonObject result = JsonParser.parseString(message).getAsJsonObject();
2462-
long expected =
2463-
ZonedDateTime.of(2026, 3, 28, 0, 0, 0, 0, ZoneId.of("+05:00"))
2464-
.toInstant()
2465-
.toEpochMilli();
2466-
assertEquals(expected, result.getAsJsonArray("timestamps").get(0).getAsLong());
2467-
}
2491+
response = httpClient.execute(queryPost);
2492+
String message = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
2493+
JsonObject result = JsonParser.parseString(message).getAsJsonObject();
2494+
long expected =
2495+
ZonedDateTime.of(2026, 3, 28, 0, 0, 0, 0, ZoneId.of("+05:00")).toInstant().toEpochMilli();
2496+
assertEquals(expected, result.getAsJsonArray("timestamps").get(0).getAsLong());
2497+
24682498
} catch (IOException e) {
2499+
e.printStackTrace();
24692500
fail(e.getMessage());
2501+
} finally {
2502+
try {
2503+
if (response != null) {
2504+
response.close();
2505+
}
2506+
} catch (IOException e) {
2507+
e.printStackTrace();
2508+
fail(e.getMessage());
2509+
}
24702510
}
24712511
}
24722512

24732513
public void queryWithInvalidTimeZoneHeader(CloseableHttpClient httpClient) {
2514+
CloseableHttpResponse response = null;
24742515
try {
24752516
HttpPost httpPost = getHttpPost("http://127.0.0.1:" + port + "/rest/v1/query");
24762517
httpPost.setHeader("Time-Zone", "Invalid/Zone");
24772518
String sql = "{\"sql\":\"SELECT s3 FROM root.sg25\"}";
24782519
httpPost.setEntity(new StringEntity(sql, Charset.defaultCharset()));
2479-
CloseableHttpResponse response = httpClient.execute(httpPost);
2520+
response = httpClient.execute(httpPost);
24802521
assertEquals(400, response.getStatusLine().getStatusCode());
24812522
String message = EntityUtils.toString(response.getEntity(), "utf-8");
24822523
JsonObject result = JsonParser.parseString(message).getAsJsonObject();
24832524
assertEquals(TSStatusCode.ILLEGAL_PARAMETER.getStatusCode(), result.get("code").getAsInt());
24842525
assertTrue(result.get("message").getAsString().contains("Invalid time zone"));
24852526
} catch (IOException e) {
2527+
e.printStackTrace();
24862528
fail(e.getMessage());
2529+
} finally {
2530+
try {
2531+
if (response != null) {
2532+
response.close();
2533+
}
2534+
} catch (IOException e) {
2535+
e.printStackTrace();
2536+
fail(e.getMessage());
2537+
}
24872538
}
24882539
}
24892540

24902541
public void queryWithInvalidTimeZoneHeaderV2(CloseableHttpClient httpClient) {
2542+
CloseableHttpResponse response = null;
24912543
try {
24922544
HttpPost httpPost = getHttpPost("http://127.0.0.1:" + port + "/rest/v2/query");
24932545
httpPost.setHeader("Time-Zone", "Invalid/Zone");
24942546
String sql = "{\"sql\":\"SELECT s3 FROM root.sg25\"}";
24952547
httpPost.setEntity(new StringEntity(sql, Charset.defaultCharset()));
2496-
CloseableHttpResponse response = httpClient.execute(httpPost);
2548+
response = httpClient.execute(httpPost);
24972549
assertEquals(400, response.getStatusLine().getStatusCode());
24982550
String message = EntityUtils.toString(response.getEntity(), "utf-8");
24992551
JsonObject result = JsonParser.parseString(message).getAsJsonObject();
25002552
assertEquals(TSStatusCode.ILLEGAL_PARAMETER.getStatusCode(), result.get("code").getAsInt());
25012553
assertTrue(result.get("message").getAsString().contains("Invalid time zone"));
25022554
} catch (IOException e) {
2555+
e.printStackTrace();
25032556
fail(e.getMessage());
2557+
} finally {
2558+
try {
2559+
if (response != null) {
2560+
response.close();
2561+
}
2562+
} catch (IOException e) {
2563+
e.printStackTrace();
2564+
fail(e.getMessage());
2565+
}
25042566
}
25052567
}
25062568
}

0 commit comments

Comments
 (0)