forked from dropwizard/dropwizard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable support for project loom for Jetty's thread pool (dropwizard#7457
) * Enable support for project loom for Jetty's thread pool * Add support for virtual threads on the admin connectors Co-authored-by: Peter Stackle <pstackle@users.noreply.github.com> --------- Co-authored-by: Peter Stackle <pstackle@users.noreply.github.com>
- Loading branch information
Showing
4 changed files
with
197 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
dropwizard-core/src/test/java/io/dropwizard/core/VirtualThreadsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package io.dropwizard.core; | ||
|
||
import com.codahale.metrics.MetricRegistry; | ||
import com.codahale.metrics.health.HealthCheckRegistry; | ||
import io.dropwizard.core.server.DefaultServerFactory; | ||
import io.dropwizard.core.setup.Environment; | ||
import io.dropwizard.jackson.Jackson; | ||
import io.dropwizard.jersey.validation.Validators; | ||
import org.eclipse.jetty.server.AbstractConnector; | ||
import org.eclipse.jetty.server.Server; | ||
import org.eclipse.jetty.server.ServerConnector; | ||
import org.eclipse.jetty.util.VirtualThreads; | ||
import org.eclipse.jetty.util.thread.ThreadPool; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.condition.EnabledForJreRange; | ||
import org.junit.jupiter.api.condition.JRE; | ||
|
||
import java.util.Arrays; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@EnabledForJreRange(min = JRE.JAVA_21) | ||
class VirtualThreadsTest { | ||
private static class VirtualThreadsConfiguration extends Configuration { | ||
} | ||
|
||
@Test | ||
void virtualThreadsEnabledWhenRequested() throws Exception { | ||
boolean isVirtualThread = probeVirtualThread( | ||
defaultServerFactory -> defaultServerFactory.setEnableVirtualThreads(true), | ||
this::selectServerThreadPool | ||
); | ||
|
||
assertThat(isVirtualThread).isTrue(); | ||
} | ||
|
||
@Test | ||
void virtualThreadsDisabledWhenNotRequested() throws Exception { | ||
boolean isVirtualThread = probeVirtualThread( | ||
defaultServerFactory -> defaultServerFactory.setEnableVirtualThreads(false), | ||
this::selectServerThreadPool | ||
); | ||
|
||
assertThat(isVirtualThread).isFalse(); | ||
} | ||
|
||
@Test | ||
void virtualAdminThreadsEnabledWhenRequested() throws Exception { | ||
boolean isVirtualThread = probeVirtualThread( | ||
defaultServerFactory -> defaultServerFactory.setEnableAdminVirtualThreads(true), | ||
this::selectAdminThreadPool | ||
); | ||
|
||
assertThat(isVirtualThread).isTrue(); | ||
} | ||
|
||
@Test | ||
void virtualAdminThreadsDisabledWhenNotRequested() throws Exception { | ||
boolean isVirtualThread = probeVirtualThread( | ||
defaultServerFactory -> defaultServerFactory.setEnableAdminVirtualThreads(false), | ||
this::selectAdminThreadPool | ||
); | ||
|
||
assertThat(isVirtualThread).isFalse(); | ||
} | ||
|
||
private boolean probeVirtualThread(Consumer<DefaultServerFactory> defaultServerFactoryConsumer, | ||
Function<Server, ThreadPool> threadPoolSelector) throws Exception { | ||
final AtomicBoolean isVirtualThread = new AtomicBoolean(false); | ||
|
||
Environment environment = new Environment("VirtualThreadsTest", Jackson.newMinimalObjectMapper(), | ||
Validators.newValidatorFactory(), new MetricRegistry(), this.getClass().getClassLoader(), | ||
new HealthCheckRegistry(), new VirtualThreadsConfiguration()); | ||
DefaultServerFactory defaultServerFactory = new DefaultServerFactory(); | ||
defaultServerFactoryConsumer.accept(defaultServerFactory); | ||
Server server = defaultServerFactory.build(environment); | ||
server.start(); | ||
try { | ||
ThreadPool threadPool = threadPoolSelector.apply(server); | ||
threadPool.execute( | ||
() -> isVirtualThread.set(VirtualThreads.isVirtualThread()) | ||
); | ||
} finally { | ||
server.stop(); | ||
} | ||
|
||
return isVirtualThread.get(); | ||
} | ||
|
||
private ThreadPool selectServerThreadPool(Server server) { | ||
return server.getThreadPool(); | ||
} | ||
|
||
private ThreadPool selectAdminThreadPool(Server server) { | ||
final int adminPort = 8081; | ||
return Arrays.stream(server.getConnectors()) | ||
.filter(ServerConnector.class::isInstance) | ||
.map(ServerConnector.class::cast) | ||
.filter(serverConnector -> serverConnector.getLocalPort() == adminPort) | ||
.map(AbstractConnector::getExecutor) | ||
.filter(ThreadPool.class::isInstance) | ||
.map(ThreadPool.class::cast) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalStateException("Couldn't find thread pool of admin connector")); | ||
} | ||
} |