Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[improve][test] Replace usage of curl in Java test and fix stream leaks #22463

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,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 @@ -361,68 +360,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
Loading