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

Test fix: SolrExporterTestBase, port bind #3269

Merged
merged 1 commit into from
Mar 22, 2025
Merged
Show file tree
Hide file tree
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 @@ -102,7 +102,9 @@ void start() throws IOException {
}

void stop() {
httpServer.stop();
if (httpServer != null) {
httpServer.stop();
}

metricsCollector.removeObserver(prometheusCollector);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.invoke.MethodHandles;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.URI;
import java.net.URISyntaxException;
Expand All @@ -36,11 +38,15 @@
import org.apache.solr.prometheus.PrometheusExporterTestBase;
import org.apache.solr.prometheus.utils.Helpers;
import org.junit.After;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Test base class. */
@ThreadLeakScope(ThreadLeakScope.Scope.NONE)
public class SolrExporterTestBase extends PrometheusExporterTestBase {

private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

private SolrExporter solrExporter;
private CloseableHttpClient httpClient;
private int promtheusExporterPort;
Expand All @@ -57,20 +63,34 @@ public void tearDown() throws Exception {

protected void startMetricsExporterWithConfiguration(String scrapeConfiguration)
throws Exception {
try (ServerSocket socket = new ServerSocket(0)) {
promtheusExporterPort = socket.getLocalPort();
}

solrExporter =
new SolrExporter(
promtheusExporterPort,
25,
10,
SolrScrapeConfiguration.solrCloud(cluster.getZkServer().getZkAddress()),
Helpers.loadConfiguration(scrapeConfiguration),
"test");
final int maxAttempts = 3;
for (int attempt = 1; attempt <= maxAttempts; attempt++) {
try (ServerSocket socket = new ServerSocket(0)) {
promtheusExporterPort = socket.getLocalPort();
}

try {
solrExporter =
new SolrExporter(
promtheusExporterPort,
25,
10,
SolrScrapeConfiguration.solrCloud(cluster.getZkServer().getZkAddress()),
Helpers.loadConfiguration(scrapeConfiguration),
"test");

solrExporter.start();
break;
} catch (BindException e) {
solrExporter.stop();
if (attempt == maxAttempts) {
throw e;
}
log.warn("Failed to start exporter with port bind exception, retrying on a new port");
}
}

solrExporter.start();
httpClient = HttpClients.createDefault();

for (int i = 0; i < 50; ++i) {
Expand Down
Loading