use of org.apache.catalina.Wrapper in project tomcat by apache.
the class TestAsyncContextImpl method testAsyncStartWithComplete.
@Test
public void testAsyncStartWithComplete() throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
AsyncStartWithCompleteServlet servlet = new AsyncStartWithCompleteServlet();
Wrapper wrapper = Tomcat.addServlet(ctx, "servlet", servlet);
wrapper.setAsyncSupported(true);
ctx.addServletMappingDecoded("/", "servlet");
TesterAccessLogValve alv = new TesterAccessLogValve();
ctx.getPipeline().addValve(alv);
tomcat.start();
// Call the servlet once
ByteChunk bc = new ByteChunk();
Map<String, List<String>> headers = new HashMap<>();
getUrl("http://localhost:" + getPort() + "/", bc, headers);
assertEquals("OK", bc.toString());
List<String> contentLength = headers.get("Content-Length");
Assert.assertNotNull(contentLength);
Assert.assertEquals(1, contentLength.size());
Assert.assertEquals("2", contentLength.get(0));
// Check the access log
alv.validateAccessLog(1, 200, 0, REQUEST_TIME);
}
use of org.apache.catalina.Wrapper in project tomcat by apache.
the class TestAsyncContextImpl method testAsyncStartNoComplete.
@Test
public void testAsyncStartNoComplete() throws Exception {
resetTracker();
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// Minimise pauses during test
tomcat.getConnector().setAttribute("connectionTimeout", Integer.valueOf(3000));
// No file system docBase required
Context ctx = tomcat.addContext("", null);
AsyncStartNoCompleteServlet servlet = new AsyncStartNoCompleteServlet();
Wrapper wrapper = Tomcat.addServlet(ctx, "servlet", servlet);
wrapper.setAsyncSupported(true);
ctx.addServletMappingDecoded("/", "servlet");
TesterAccessLogValve alv = new TesterAccessLogValve();
ctx.getPipeline().addValve(alv);
tomcat.start();
// Call the servlet the first time
getUrl("http://localhost:" + getPort() + "/?echo=run1");
Assert.assertEquals("OK-run1", getTrack());
resetTracker();
// Call the servlet the second time with a request parameter
getUrl("http://localhost:" + getPort() + "/?echo=run2");
Assert.assertEquals("OK-run2", getTrack());
// Request may complete before listener has finished processing so wait
// up to 5 seconds for the right response
// Check the access log
alv.validateAccessLog(2, 500, AsyncStartNoCompleteServlet.ASYNC_TIMEOUT, AsyncStartNoCompleteServlet.ASYNC_TIMEOUT + TIMEOUT_MARGIN + REQUEST_TIME);
}
use of org.apache.catalina.Wrapper in project tomcat by apache.
the class TestAsyncContextImpl method doTestBug59219.
private void doTestBug59219(String queryString, String expectedTrack) throws Exception {
resetTracker();
Tomcat tomcat = getTomcatInstance();
Context ctx = tomcat.addContext("", null);
Wrapper w = tomcat.addServlet("", "async", new Bug59219Servlet());
w.setAsyncSupported(true);
ctx.addServletMappingDecoded("/async", "async");
tomcat.start();
getUrl("http://localhost:" + getPort() + "/async" + queryString);
// Wait up to 5s for the response
int count = 0;
while (!expectedTrack.equals(getTrack()) && count < 100) {
Thread.sleep(50);
count++;
}
Assert.assertEquals(expectedTrack, getTrack());
}
use of org.apache.catalina.Wrapper in project tomcat by apache.
the class TestAsyncContextImpl method testDispatchFromOtherContainerThread.
@Test
public void testDispatchFromOtherContainerThread() throws Exception {
resetTracker();
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
NonAsyncServlet nonAsyncServlet = new NonAsyncServlet();
Tomcat.addServlet(ctx, "nonAsyncServlet", nonAsyncServlet);
ctx.addServletMappingDecoded("/target", "nonAsyncServlet");
AsyncStashServlet asyncStashServlet = new AsyncStashServlet();
Wrapper w1 = Tomcat.addServlet(ctx, "asyncStashServlet", asyncStashServlet);
w1.setAsyncSupported(true);
ctx.addServletMappingDecoded("/asyncStashServlet", "asyncStashServlet");
AsyncRetrieveServlet asyncRetrieveServlet = new AsyncRetrieveServlet();
Wrapper w2 = Tomcat.addServlet(ctx, "asyncRetrieveServlet", asyncRetrieveServlet);
w2.setAsyncSupported(true);
ctx.addServletMappingDecoded("/asyncRetrieveServlet", "asyncRetrieveServlet");
tomcat.start();
// First request in separate thread because the response won't be
// written until after the second request has been made.
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
getUrl("http://localhost:" + getPort() + "/asyncStashServlet");
} catch (IOException e) {
e.printStackTrace();
}
}
});
t.start();
// Wait for first request to get as far as it can
int count = 0;
while (count < 100 && getTrack() != null && !getTrack().startsWith("AsyncStashServletGet-")) {
count++;
Thread.sleep(100);
}
getUrl("http://localhost:" + getPort() + "/asyncRetrieveServlet");
// Wait for second request to release first and allow it to complete
String expectedTrack = "AsyncStashServletGet-AsyncRetrieveServletGet-NonAsyncServletGet-";
count = 0;
while (count < 100 && !getTrack().equals(expectedTrack)) {
count++;
Thread.sleep(100);
}
Assert.assertEquals(expectedTrack, getTrack());
}
use of org.apache.catalina.Wrapper in project tomcat by apache.
the class TestAsyncContextImpl method testListeners.
@Test
public void testListeners() throws Exception {
resetTracker();
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
TrackingServlet tracking = new TrackingServlet();
Wrapper wrapper = Tomcat.addServlet(ctx, "tracking", tracking);
wrapper.setAsyncSupported(true);
ctx.addServletMappingDecoded("/stage1", "tracking");
TimeoutServlet timeout = new TimeoutServlet(Boolean.TRUE, null);
Wrapper wrapper2 = Tomcat.addServlet(ctx, "timeout", timeout);
wrapper2.setAsyncSupported(true);
ctx.addServletMappingDecoded("/stage2", "timeout");
TesterAccessLogValve alv = new TesterAccessLogValve();
ctx.getPipeline().addValve(alv);
tomcat.start();
StringBuilder url = new StringBuilder(48);
url.append("http://localhost:");
url.append(getPort());
url.append("/stage1");
getUrl(url.toString());
// Request may complete before listener has finished processing so wait
// up to 5 seconds for the right response
String expectedTrack = "DispatchingServletGet-DispatchingServletGet-" + "onStartAsync-TimeoutServletGet-onStartAsync-onTimeout-" + "onComplete-";
int count = 0;
while (!expectedTrack.equals(getTrack()) && count < 100) {
Thread.sleep(50);
count++;
}
Assert.assertEquals(expectedTrack, getTrack());
// Check the access log
alv.validateAccessLog(1, 200, TimeoutServlet.ASYNC_TIMEOUT, TimeoutServlet.ASYNC_TIMEOUT + TIMEOUT_MARGIN + REQUEST_TIME);
}
Aggregations