Skip to content

Commit

Permalink
Make disabling async a valid option not just for testing (#700)
Browse files Browse the repository at this point in the history
For virtual threads you don't really need async enabled. This adds an option to disable it.
  • Loading branch information
graemerocher authored May 22, 2024
1 parent dbcb1dd commit 9135e6e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,9 @@ protected Server jettyServer(
final ServletHolder servletHolder = new ServletHolder(new DefaultMicronautServlet(applicationContext));
contextHandler.addServlet(servletHolder, configuration.getMapping());

Boolean isAsync = applicationContext.getEnvironment()
.getProperty("micronaut.server.testing.async", Boolean.class, true);
boolean isAsync = configuration.isAsyncSupported();
if (Boolean.FALSE.equals(isAsync)) {
LOG.warn("Async support disabled for testing purposes.");
LOG.debug("Servlet async mode is disabled");
}
servletHolder.setAsyncSupported(isAsync);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import spock.lang.Specification

@MicronautTest
@Property(name = "spec.name", value = SPEC_NAME)
@Property(name = "micronaut.server.testing.async", value = "false")
@Property(name = "micronaut.servlet.async-supported", value = "false")
class JettyResponseEncoderSpec extends Specification {

private static final String SPEC_NAME = "JettyResponseEncoderSpec"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ protected Tomcat tomcatServer(Connector connector, MicronautServletConfiguration
new DefaultMicronautServlet(getApplicationContext())
);

Boolean isAsync = getApplicationContext().getEnvironment().getProperty("micronaut.server.testing.async", Boolean.class, true);
boolean isAsync = configuration.isAsyncSupported();
if (Boolean.FALSE.equals(isAsync)) {
LOG.warn("Async support disabled for testing purposes.");
LOG.debug("Servlet async mode is disabled");
}
servlet.setAsyncSupported(isAsync);
servlet.addMapping(configuration.getMapping());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,9 @@ public void release() {
}
}
);
Boolean isAsync = getApplicationContext().getEnvironment().getProperty("micronaut.server.testing.async", Boolean.class, true);
boolean isAsync = servletConfiguration.isAsyncSupported();
if (Boolean.FALSE.equals(isAsync)) {
LOG.warn("Async support disabled for testing purposes.");
LOG.debug("Servlet async mode is disabled");
}
servletInfo.setAsyncSupported(isAsync);
servletInfo.addMapping(servletConfiguration.getMapping());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,13 @@ public interface ServletConfiguration {
* @return True if it is.
*/
boolean isAsyncFileServingEnabled();

/**
* Whether to do request processing asynchronously by default (defaults to {@code true}).
* @return True whether async is enabled
* @since 4.8.0
*/
default boolean isAsyncSupported() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

import io.micronaut.context.annotation.ConfigurationInject;
import io.micronaut.context.annotation.ConfigurationProperties;
import io.micronaut.context.annotation.Property;
import io.micronaut.context.env.Environment;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.bind.annotation.Bindable;
import io.micronaut.core.naming.Named;
import io.micronaut.core.util.StringUtils;
import io.micronaut.http.server.HttpServerConfiguration;
import io.micronaut.servlet.http.ServletConfiguration;

Expand All @@ -46,6 +49,8 @@ public class MicronautServletConfiguration implements Named, ServletConfiguratio
private final String name;
private boolean asyncFileServingEnabled = true;

private boolean asyncSupported = true;


/**
* Default constructor.
Expand Down Expand Up @@ -73,6 +78,33 @@ public MicronautServletConfiguration(
}
}

@Override
public boolean isAsyncSupported() {
return asyncSupported;
}

/**
* Set whether async is supported or not.
* @param asyncSupported True if async is supported.
*/
public void setAsyncSupported(boolean asyncSupported) {
this.asyncSupported = asyncSupported;
}

/**
* Legacy property to disable async for testing.
*
* @param asyncSupported Is async supported
* @deprecated Use {@link #setAsyncSupported(boolean)} instead
*/
@Deprecated(forRemoval = true, since = "4.8.0")
@Property(name = "micronaut.server.testing.async")
public void setTestAsyncSupported(@Nullable Boolean asyncSupported) {
if (asyncSupported != null) {
this.asyncSupported = asyncSupported;
}
}

/**
* @return The servlet mapping.
*/
Expand Down Expand Up @@ -103,6 +135,6 @@ public void setAsyncFileServingEnabled(boolean enabled) {

@Override
public boolean isAsyncFileServingEnabled() {
return asyncFileServingEnabled;
return asyncSupported && asyncFileServingEnabled;
}
}

0 comments on commit 9135e6e

Please sign in to comment.