use of jakarta.servlet.AsyncContext in project tomcat by apache.
the class TestAsyncContextStateChanges method testAsync.
@Test
public void testAsync() throws Exception {
dispatch = false;
servletRequest = null;
asyncContext = null;
// Initialise tracking fields
failed.set(true);
servletStartLatch = new CountDownLatch(1);
threadCompleteLatch = new CountDownLatch(1);
clientDisconnectLatch = new CountDownLatch(1);
endLatch = new CountDownLatch(1);
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
AsyncServlet bug63816Servlet = new AsyncServlet();
Wrapper wrapper = Tomcat.addServlet(ctx, "bug63816Servlet", bug63816Servlet);
wrapper.setAsyncSupported(true);
ctx.addServletMappingDecoded("/*", "bug63816Servlet");
tomcat.start();
Client client = new Client();
client.setPort(getPort());
client.setRequest(new String[] { "GET / HTTP/1.1" + SimpleHttpClient.CRLF + "Host: localhost:" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF });
client.connect();
client.sendRequest();
// Wait for Servlet to start processing request
servletStartLatch.await();
if (asyncEnd.isError()) {
client.disconnect();
clientDisconnectLatch.countDown();
try {
endLatch.await();
} catch (InterruptedException e) {
// Ignore
}
} else {
client.setUseContentLength(true);
client.readResponse(true);
}
Assert.assertFalse(failed.get());
}
use of jakarta.servlet.AsyncContext in project tomcat by apache.
the class Async2 method service.
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
final AsyncContext actx = req.startAsync();
actx.setTimeout(30 * 1000);
Runnable run = new Runnable() {
@Override
public void run() {
try {
Thread.currentThread().setName("Async2-Thread");
log.info("Putting AsyncThread to sleep");
Thread.sleep(2 * 1000);
log.info("Writing data.");
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
actx.getResponse().getWriter().write("Output from background thread. Time: " + sdf.format(date) + "\n");
actx.complete();
} catch (InterruptedException x) {
log.error("Async2", x);
} catch (IllegalStateException x) {
log.error("Async2", x);
} catch (IOException x) {
log.error("Async2", x);
}
}
};
Thread t = new Thread(run);
t.start();
}
use of jakarta.servlet.AsyncContext in project tomcat by apache.
the class Async1 method service.
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
final AsyncContext actx = req.startAsync();
actx.setTimeout(30 * 1000);
Runnable run = new Runnable() {
@Override
public void run() {
try {
String path = "/jsp/async/async1.jsp";
Thread.currentThread().setName("Async1-Thread");
log.info("Putting AsyncThread to sleep");
Thread.sleep(2 * 1000);
log.info("Dispatching to " + path);
actx.dispatch(path);
} catch (InterruptedException x) {
log.error("Async1", x);
} catch (IllegalStateException x) {
log.error("Async1", x);
}
}
};
Thread t = new Thread(run);
t.start();
}
use of jakarta.servlet.AsyncContext in project tomcat by apache.
the class Async3 method service.
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
final AsyncContext actx = req.startAsync();
actx.setTimeout(30 * 1000);
actx.dispatch("/jsp/async/async3.jsp");
}
use of jakarta.servlet.AsyncContext in project spring-security by spring-projects.
the class SecurityContextHolderAwareRequestFilterTests method startAsyncStart.
@Test
public void startAsyncStart() throws Exception {
ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class);
SecurityContext context = SecurityContextHolder.createEmptyContext();
TestingAuthenticationToken expectedAuth = new TestingAuthenticationToken("user", "password", "ROLE_USER");
context.setAuthentication(expectedAuth);
SecurityContextHolder.setContext(context);
AsyncContext asyncContext = mock(AsyncContext.class);
given(this.request.startAsync()).willReturn(asyncContext);
Runnable runnable = () -> {
};
wrappedRequest().startAsync().start(runnable);
verifyZeroInteractions(this.authenticationManager, this.logoutHandler);
verify(asyncContext).start(runnableCaptor.capture());
DelegatingSecurityContextRunnable wrappedRunnable = (DelegatingSecurityContextRunnable) runnableCaptor.getValue();
assertThat(ReflectionTestUtils.getField(wrappedRunnable, "delegateSecurityContext")).isEqualTo(context);
assertThat(ReflectionTestUtils.getField(wrappedRunnable, "delegate"));
}
Aggregations