Skip to content

Commit

Permalink
[improve][test] Replace usage of curl in Java test and fix stream lea…
Browse files Browse the repository at this point in the history
…ks (apache#22463)

(cherry picked from commit f3d14a6)
(cherry picked from commit 976399c)
  • Loading branch information
lhotari authored and srinath-ctds committed Apr 23, 2024
1 parent ba98afe commit 7f05b6e
Showing 1 changed file with 33 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@
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;
Expand Down Expand Up @@ -360,68 +359,66 @@ public void testBrokerReady() throws Exception {

@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"));
}

Expand Down

0 comments on commit 7f05b6e

Please sign in to comment.