Skip to content

[UNDERTOW-1870] Backport of RFE with property instead of API change #1731

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

Open
wants to merge 1 commit into
base: 2.3.x
Choose a base branch
from
Open
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 @@ -87,7 +87,7 @@ public class AsyncContextImpl implements AsyncContext {

//todo: make default configurable
private volatile long timeout = 30000;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be prefixed DEFAULT_ ?

public static final String ASYNC_CONTEXT_TIMEOUT = "io.undertow.servlet.ASYNC_CONTEXT_TIMEOUT";
private volatile XnioExecutor.Key timeoutKey;

private boolean dispatched;
Expand All @@ -106,6 +106,7 @@ public AsyncContextImpl(final HttpServerExchange exchange, final ServletRequest
this.servletRequestContext = servletRequestContext;
this.requestSupplied = requestSupplied;
this.previousAsyncContext = previousAsyncContext;
this.timeout = Integer.parseInt(System.getProperty(ASYNC_CONTEXT_TIMEOUT, timeout+""));
initiatingThread = Thread.currentThread();
exchange.dispatch(SameThreadExecutor.INSTANCE, new Runnable() {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2025 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.undertow.servlet.test.listener.request.async.onTimeout.property;

import java.io.IOException;

import io.undertow.servlet.spec.AsyncContextImpl;
import jakarta.servlet.AsyncContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

/**
* @author baranowb
*/
public class AsyncServlet extends HttpServlet {

public static final String TEST_TIMEOUT="timeout";
public static final String TIMEOUT_START_TSTAMP = "tstamp";
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
long timeout = Long.parseLong((String)req.getParameter(TEST_TIMEOUT));
System.setProperty(AsyncContextImpl.ASYNC_CONTEXT_TIMEOUT, timeout+"");
req.setAttribute(TIMEOUT_START_TSTAMP, System.currentTimeMillis());
AsyncContext ctx = req.startAsync();
ctx.addListener(new SimpleAsyncListener());
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(timeout+3000L);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.setProperty(AsyncContextImpl.ASYNC_CONTEXT_TIMEOUT, "30000");
}
});
t.start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2025 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.undertow.servlet.test.listener.request.async.onTimeout.property;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import io.undertow.server.handlers.PathHandler;
import io.undertow.servlet.api.DeploymentInfo;
import io.undertow.servlet.api.DeploymentManager;
import io.undertow.servlet.api.ListenerInfo;
import io.undertow.servlet.api.ServletContainer;
import io.undertow.servlet.api.ServletInfo;
import io.undertow.servlet.test.listener.request.async.onTimeout.SimpleRequestListener;
import io.undertow.servlet.test.util.TestClassIntrospector;
import io.undertow.testutils.DefaultServer;
import io.undertow.testutils.TestHttpClient;
import io.undertow.util.StatusCodes;
import jakarta.servlet.ServletException;

@RunWith(DefaultServer.class)
public class DefaultAsyncTimeoutTestCase {
@BeforeClass
public static void setup() throws ServletException {

final PathHandler root = new PathHandler();
final ServletContainer container = ServletContainer.Factory.newInstance();

ServletInfo a = new ServletInfo("asyncServlet", AsyncServlet.class)
.setAsyncSupported(true)
.addMapping("/async");

DeploymentInfo builder = new DeploymentInfo()
.setClassLoader(DefaultAsyncTimeoutTestCase.class.getClassLoader())
.setContextPath("/servletContext")
.setClassIntrospecter(TestClassIntrospector.INSTANCE)
.setDeploymentName("servletContext.war")
.addServlets(a)
.addListener(new ListenerInfo(SimpleRequestListener.class));

DeploymentManager manager = container.addDeployment(builder);
manager.deploy();
root.addPrefixPath(builder.getContextPath(), manager.start());

DefaultServer.setRootHandler(root);
}

@Test
public void testDefaultTimeout() throws IOException {
TestHttpClient client = new TestHttpClient();
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/async?"+AsyncServlet.TEST_TIMEOUT+"=30000");
HttpResponse response = client.execute(get);
Assert.assertEquals(StatusCodes.OK, response.getStatusLine().getStatusCode());
Assert.assertFalse(SimpleRequestListener.hasNestedInvocationOccured());
} finally {
client.getConnectionManager().shutdown();
}
}

@Test
public void testNewDefaultTimeout() throws IOException {
TestHttpClient client = new TestHttpClient();
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/async?"+AsyncServlet.TEST_TIMEOUT+"=20000");
HttpResponse response = client.execute(get);
Assert.assertEquals(StatusCodes.OK, response.getStatusLine().getStatusCode());
Assert.assertFalse(SimpleRequestListener.hasNestedInvocationOccured());
} finally {
client.getConnectionManager().shutdown();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2025 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.undertow.servlet.test.listener.request.async.onTimeout.property;

import io.undertow.util.StatusCodes;

import java.io.IOException;

import jakarta.servlet.AsyncEvent;
import jakarta.servlet.AsyncListener;
import jakarta.servlet.http.HttpServletResponse;

public class SimpleAsyncListener implements AsyncListener {

public static final String HEADER="TIMER_HEADER";
@Override
public void onComplete(AsyncEvent event) throws IOException {
}

@Override
public void onTimeout(AsyncEvent event) throws IOException {
HttpServletResponse response = (HttpServletResponse) event.getSuppliedResponse();
final long startTstamp = (long) event.getSuppliedRequest().getAttribute(AsyncServlet.TIMEOUT_START_TSTAMP);
long timeout = Long.parseLong((String) event.getSuppliedRequest().getParameter(AsyncServlet.TEST_TIMEOUT));
long elapsed = System.currentTimeMillis() - startTstamp;
response.setHeader(HEADER, (System.currentTimeMillis() - startTstamp) + "");
if (Math.abs(timeout - elapsed) < 2000) {
response.setStatus(StatusCodes.OK);
} else {
response.setStatus(StatusCodes.CONFLICT);
}
event.getAsyncContext().complete();
}

@Override
public void onError(AsyncEvent event) throws IOException {
}

@Override
public void onStartAsync(AsyncEvent event) throws IOException {
}

}
Loading