Polish "Add support for virtual threads in OtlpMetricRegistry configuration"

See gh-42407
This commit is contained in:
Moritz Halbritter
2024-09-26 13:39:38 +02:00
parent e615eb313a
commit 593d2cccc4
2 changed files with 11 additions and 9 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.boot.testsupport.assertj;
import java.lang.reflect.Method;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
@@ -24,6 +25,8 @@ import java.util.concurrent.TimeUnit;
import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.Assert;
import org.springframework.util.ReflectionUtils;
/**
* AssertJ {@link Assert} for {@link ScheduledThreadPoolExecutor}.
*
@@ -66,12 +69,11 @@ public final class ScheduledExecutorServiceAssert
private boolean producesVirtualThreads() {
try {
return this.actual.schedule(() -> {
// https://openjdk.org/jeps/444
// jep 444 specifies that virtual threads will belong to
// a special thread group given the name "VirtualThreads"
ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
String threadGroupName = (threadGroup != null) ? threadGroup.getName() : "";
return threadGroupName.equalsIgnoreCase("VirtualThreads");
Method isVirtual = ReflectionUtils.findMethod(Thread.class, "isVirtual");
if (isVirtual == null) {
return false;
}
return (boolean) ReflectionUtils.invokeMethod(isVirtual, Thread.currentThread());
}, 0, TimeUnit.SECONDS).get();
}
catch (InterruptedException | ExecutionException ex) {