This is an automated email from the ASF dual-hosted git repository.

lhotari pushed a commit to branch branch-3.1
in repository https://gitbox.apache.org/repos/asf/pulsar.git

commit 7ad7dedc0f723b677a197e03c6580dd7ded619ed
Author: Lari Hotari <[email protected]>
AuthorDate: Tue Apr 9 07:48:57 2024 -0700

    [improve][test] Replace usage of curl in Java test and fix stream leaks 
(#22463)
    
    (cherry picked from commit f3d14a6b0b15f6d3c17509b21b28a586a22e5d89)
---
 .../apache/pulsar/broker/web/WebServiceTest.java   | 69 +++++++++++-----------
 1 file changed, 33 insertions(+), 36 deletions(-)

diff --git 
a/pulsar-broker/src/test/java/org/apache/pulsar/broker/web/WebServiceTest.java 
b/pulsar-broker/src/test/java/org/apache/pulsar/broker/web/WebServiceTest.java
index 733964bee11..00c6a209c13 100644
--- 
a/pulsar-broker/src/test/java/org/apache/pulsar/broker/web/WebServiceTest.java
+++ 
b/pulsar-broker/src/test/java/org/apache/pulsar/broker/web/WebServiceTest.java
@@ -21,18 +21,17 @@ package org.apache.pulsar.broker.web;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertTrue;
 import static org.testng.Assert.fail;
-
 import com.google.common.collect.Multimap;
 import com.google.common.collect.Sets;
 import com.google.common.io.CharStreams;
 import com.google.common.io.Closeables;
 import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
-
 import java.io.BufferedReader;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
 import java.net.URL;
 import java.security.KeyStore;
 import java.security.PrivateKey;
@@ -360,68 +359,66 @@ public class WebServiceTest {
 
     @Test
     public void testCompressOutputMetricsInPrometheus() throws Exception {
-
         setupEnv(true, false, false, false, -1, false);
 
         String metricsUrl = pulsar.getWebServiceAddress() + "/metrics/";
 
-        String[] command = {"curl", "-H", "Accept-Encoding: gzip", metricsUrl};
+        URL url = new URL(metricsUrl);
+        HttpURLConnection connection = (HttpURLConnection) 
url.openConnection();
+        connection.setRequestMethod("GET");
+        connection.setRequestProperty("Accept-Encoding", "gzip");
 
-        ProcessBuilder processBuilder = new ProcessBuilder(command);
-        Process process = processBuilder.start();
-
-        InputStream inputStream = process.getInputStream();
-
-        try {
-            GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream);
+        StringBuilder content = new StringBuilder();
 
-            // Process the decompressed content
-            StringBuilder content = new StringBuilder();
-            int data;
-            while ((data = gzipInputStream.read()) != -1) {
-                content.append((char) data);
+        try (InputStream inputStream = connection.getInputStream()) {
+            try (GZIPInputStream gzipInputStream = new 
GZIPInputStream(inputStream)) {
+                // Process the decompressed content
+                int data;
+                while ((data = gzipInputStream.read()) != -1) {
+                    content.append((char) data);
+                }
             }
-            log.info("Response Content: {}", content);
 
-            process.waitFor();
+            log.info("Response Content: {}", content);
             
assertTrue(content.toString().contains("process_cpu_seconds_total"));
         } catch (IOException e) {
             log.error("Failed to decompress the content, likely the content is 
not compressed ", e);
             fail();
+        } finally {
+            connection.disconnect();
         }
     }
 
     @Test
     public void testUnCompressOutputMetricsInPrometheus() throws Exception {
-
         setupEnv(true, false, false, false, -1, false);
 
         String metricsUrl = pulsar.getWebServiceAddress() + "/metrics/";
 
-        String[] command = {"curl", metricsUrl};
+        URL url = new URL(metricsUrl);
+        HttpURLConnection connection = (HttpURLConnection) 
url.openConnection();
+        connection.setRequestMethod("GET");
 
-        ProcessBuilder processBuilder = new ProcessBuilder(command);
-        Process process = processBuilder.start();
+        StringBuilder content = new StringBuilder();
 
-        InputStream inputStream = process.getInputStream();
-        try {
-            GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream);
-            fail();
-        } catch (IOException e) {
-            log.error("Failed to decompress the content, likely the content is 
not compressed ", e);
-            assertTrue(e instanceof ZipException);
-        }
+        try (InputStream inputStream = connection.getInputStream()) {
+            try (GZIPInputStream gzipInputStream = new 
GZIPInputStream(inputStream)) {
+                fail();
+            } catch (IOException e) {
+                assertTrue(e instanceof ZipException);
+            }
 
-        BufferedReader reader = new BufferedReader(new 
InputStreamReader(inputStream));
-        StringBuilder content = new StringBuilder();
-        String line;
-        while ((line = reader.readLine()) != null) {
-            content.append(line + "\n");
+            BufferedReader reader = new BufferedReader(new 
InputStreamReader(inputStream));
+            String line;
+            while ((line = reader.readLine()) != null) {
+                content.append(line + "\n");
+            }
+        } finally {
+            connection.disconnect();
         }
 
         log.info("Response Content: {}", content);
 
-        process.waitFor();
         assertTrue(content.toString().contains("process_cpu_seconds_total"));
     }
 

Reply via email to