Skip to content

Commit

Permalink
Make jvm.thread.count assertion independent of the description (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobat authored Nov 6, 2024
1 parent 88735b7 commit e53a41f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ public static WebArchive createTestArchive() {

@Test
void testThreadCountMetric() throws IOException {
MetricsReader.assertLogMessage("jvm.thread.count", "Number of executing platform threads.", "{thread}",
MetricDataType.LONG_SUM.toString());
MetricsReader.assertLogMessagePattern(
"name=jvm\\.thread\\.count, description=Number of executing(.*) threads(.*), unit=\\{thread\\}, type=" +
MetricDataType.LONG_SUM.toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.input.Tailer;
import org.apache.commons.io.input.TailerListenerAdapter;
Expand Down Expand Up @@ -79,6 +81,40 @@ public static void assertLogMessage(String metricName, String metricDescription,
}
}

/**
* This method asserts that a log line matching the following format
*
* "searchPattern=<line pattern that must be matched>"
*
* Can be found in the log file pointed to by the system property mptelemetry.tck.log.file.path. It will wait for up
* to fifteen seconds for the log to appear.
*
* @param searchPattern
* The pattern to search for in the log file
*/
public static void assertLogMessagePattern(String searchPattern) {

ExecutorService es = Executors.newFixedThreadPool(1);

LogFileTailerMatcherAdaptor logFileTailerAdaptor =
new LogFileTailerMatcherAdaptor(searchPattern);

Tailer tailer = Tailer.builder()
.setStartThread(true)
.setPath(logFilePath)
.setExecutorService(es)
.setReOpen(false)
.setTailerListener(logFileTailerAdaptor)
.setTailFromEnd(false)
.get();

try (tailer) {
Awaitility.await().atMost(15, SECONDS)
.untilAsserted(() -> Assert.assertTrue(logFileTailerAdaptor.foundMetric(),
"Did not find " + searchPattern + " in logfile: " + logFilePath));
}
}

private static class LogFileTailerAdaptor extends TailerListenerAdapter {

private final String searchString;
Expand All @@ -105,4 +141,31 @@ public boolean foundMetric() {
}
}

private static class LogFileTailerMatcherAdaptor extends TailerListenerAdapter {

private final Pattern pattern;
private boolean foundMetric = false;
private Tailer tailer = null;

public LogFileTailerMatcherAdaptor(String searchString) {
this.pattern = Pattern.compile(searchString);
}

public void init(Tailer tailer) {
this.tailer = tailer;
}

public void handle(String line) {
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
foundMetric = true;
tailer.close();
}
}

public boolean foundMetric() {
return foundMetric;
}
}

}

0 comments on commit e53a41f

Please sign in to comment.