Skip to content

Commit

Permalink
Fix Java 21 build
Browse files Browse the repository at this point in the history
Signed-off-by: Fabian Stäber <fabian@fstab.de>
  • Loading branch information
fstab committed Oct 5, 2023
1 parent 1bce0eb commit 28338f7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ public void testGolangTests() throws NoSuchFieldException, IllegalAccessExceptio
"sample_count: 2 " +
"sample_sum: -7.0 " +
"schema: 2 " +
"zero_threshold: 2.9387358770557188E-39 " +
"zero_threshold: " + Math.pow(2.0, -128.0) + " " +
"zero_count: 0 " +
"negative_span { offset: 7 length: 2 } " +
"negative_delta: 1 " +
Expand Down Expand Up @@ -564,7 +564,7 @@ public void testGolangTests() throws NoSuchFieldException, IllegalAccessExceptio
"sample_count: 2 " +
"sample_sum: 7.0 " +
"schema: 2 " +
"zero_threshold: 2.9387358770557188E-39 " +
"zero_threshold: " + Math.pow(2.0, -128.0) + " " +
"zero_count: 0 " +
"positive_span { offset: 7 length: 2 } " +
"positive_delta: 1 " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,29 @@ public void testIgnoredMetricNotScraped() {

@Test
public void testInvalidThreadIds() {
try {
int javaVersion = Integer.parseInt(System.getProperty("java.version"));
if (javaVersion >= 21) {
// With Java 21 and newer you can no longer have invalid thread ids.
return;
}
} catch (NumberFormatException ignored) {
}
PrometheusRegistry registry = new PrometheusRegistry();
JvmThreadsMetrics.builder().register(registry);
MetricSnapshots snapshots = registry.scrape();

// Number of threads to create with invalid thread ids
int numberOfInvalidThreadIds = 2;

Map<String, Double> expected = getCountByState(snapshots);
Map<String, Double> expected = getCountByState(registry.scrape());
expected.compute("UNKNOWN", (key, oldValue) -> oldValue == null ? numberOfInvalidThreadIds : oldValue + numberOfInvalidThreadIds);

final CountDownLatch countDownLatch = new CountDownLatch(numberOfInvalidThreadIds);

try {
// Create and start threads with invalid thread ids (id=0, id=-1, etc.)
for (int i = 0; i < numberOfInvalidThreadIds; i++) {
new TestThread(-i, new TestRunnable(countDownLatch)).start();
new ThreadWithInvalidId(-i, new TestRunnable(countDownLatch)).start();
}

Map<String, Double> actual = getCountByState(registry.scrape());
Expand Down Expand Up @@ -152,16 +159,21 @@ private Map<String, Double> getCountByState(MetricSnapshots snapshots) {
return result;
}

private static class TestThread extends Thread {
private static class ThreadWithInvalidId extends Thread {

private final long id;

public TestThread(long id, Runnable runnable) {
public ThreadWithInvalidId(long id, Runnable runnable) {
super(runnable);
setDaemon(true);
this.id = id;
}

/**
* Note that only Java versions < 21 call this to get the thread id.
* With Java 21 and newer it's no longer possible to make an invalid thread id.
*/
@Override
public long getId() {
return this.id;
}
Expand Down

0 comments on commit 28338f7

Please sign in to comment.