Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make disabling async a valid option not just for testing #700

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading