use of org.apache.catalina.startup.Tomcat in project tomcat by apache.
the class TestAsyncContextImpl method testBug53843.
@Test
public void testBug53843() throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Bug53843ServletA servletA = new Bug53843ServletA();
Wrapper a = Tomcat.addServlet(ctx, "ServletA", servletA);
a.setAsyncSupported(true);
Tomcat.addServlet(ctx, "ServletB", new Bug53843ServletB());
ctx.addServletMappingDecoded("/ServletA", "ServletA");
ctx.addServletMappingDecoded("/ServletB", "ServletB");
tomcat.start();
StringBuilder url = new StringBuilder(48);
url.append("http://localhost:");
url.append(getPort());
url.append("/ServletA");
ByteChunk body = new ByteChunk();
int rc = getUrl(url.toString(), body, null);
assertEquals(HttpServletResponse.SC_OK, rc);
assertEquals("OK", body.toString());
assertTrue(servletA.isAsyncWhenExpected());
}
use of org.apache.catalina.startup.Tomcat in project tomcat by apache.
the class TestAsyncContextImpl method doTestTimeout.
private void doTestTimeout(Boolean completeOnTimeout, Boolean asyncDispatch) throws Exception {
resetTracker();
String dispatchUrl = null;
if (asyncDispatch != null) {
if (asyncDispatch.booleanValue()) {
dispatchUrl = "/async";
} else {
dispatchUrl = "/nonasync";
}
}
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
TimeoutServlet timeout = new TimeoutServlet(completeOnTimeout, dispatchUrl);
Wrapper wrapper = Tomcat.addServlet(ctx, "time", timeout);
wrapper.setAsyncSupported(true);
ctx.addServletMappingDecoded("/start", "time");
if (asyncDispatch != null) {
if (asyncDispatch.booleanValue()) {
AsyncStartRunnable asyncStartRunnable = new AsyncStartRunnable();
Wrapper async = Tomcat.addServlet(ctx, "async", asyncStartRunnable);
async.setAsyncSupported(true);
ctx.addServletMappingDecoded(dispatchUrl, "async");
} else {
NonAsyncServlet nonAsync = new NonAsyncServlet();
Tomcat.addServlet(ctx, "nonasync", nonAsync);
ctx.addServletMappingDecoded(dispatchUrl, "nonasync");
}
}
ctx.addApplicationListener(TrackingRequestListener.class.getName());
TesterAccessLogValve alv = new TesterAccessLogValve();
ctx.getPipeline().addValve(alv);
TesterAccessLogValve alvGlobal = new TesterAccessLogValve();
tomcat.getHost().getPipeline().addValve(alvGlobal);
tomcat.start();
try {
getUrl("http://localhost:" + getPort() + "/start");
} catch (IOException ioe) {
// Ignore - expected for some error conditions
}
StringBuilder expected = new StringBuilder("requestInitialized-");
expected.append("TimeoutServletGet-");
if (completeOnTimeout == null) {
expected.append("requestDestroyed");
} else if (completeOnTimeout.booleanValue()) {
expected.append("onTimeout-");
expected.append("onComplete-");
expected.append("requestDestroyed");
} else {
expected.append("onTimeout-");
if (asyncDispatch != null) {
if (asyncDispatch.booleanValue()) {
expected.append("onStartAsync-Runnable-");
} else {
expected.append("NonAsyncServletGet-");
}
}
expected.append("onComplete-");
expected.append("requestDestroyed");
}
// Request may complete before listener has finished processing so wait
// up to 5 seconds for the right response
String expectedTrack = expected.toString();
int count = 0;
while (!expectedTrack.equals(getTrack()) && count < 100) {
Thread.sleep(50);
count++;
}
assertEquals(expectedTrack, getTrack());
// Check the access log
if (completeOnTimeout == null || (!completeOnTimeout.booleanValue() && asyncDispatch == null)) {
alvGlobal.validateAccessLog(1, 500, TimeoutServlet.ASYNC_TIMEOUT, TimeoutServlet.ASYNC_TIMEOUT + TIMEOUT_MARGIN + REQUEST_TIME);
alv.validateAccessLog(1, 500, TimeoutServlet.ASYNC_TIMEOUT, TimeoutServlet.ASYNC_TIMEOUT + TIMEOUT_MARGIN + REQUEST_TIME);
} else {
long timeoutDelay = TimeoutServlet.ASYNC_TIMEOUT;
if (asyncDispatch != null && asyncDispatch.booleanValue() && !completeOnTimeout.booleanValue()) {
// The async dispatch includes a sleep
timeoutDelay += AsyncStartRunnable.THREAD_SLEEP_TIME;
}
alvGlobal.validateAccessLog(1, 200, timeoutDelay, timeoutDelay + TIMEOUT_MARGIN + REQUEST_TIME);
alv.validateAccessLog(1, 200, timeoutDelay, timeoutDelay + TIMEOUT_MARGIN + REQUEST_TIME);
}
}
use of org.apache.catalina.startup.Tomcat in project tomcat by apache.
the class TestAsyncContextImpl method testBug49528.
@Test
public void testBug49528() throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Bug49528Servlet servlet = new Bug49528Servlet();
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 = getUrl("http://localhost:" + getPort() + "/");
assertEquals("OK", bc.toString());
// Give the async thread a chance to finish (but not too long)
int counter = 0;
while (!servlet.isDone() && counter < 10) {
Thread.sleep(1000);
counter++;
}
assertEquals("1false2true3true4true5false", servlet.getResult());
// Check the access log
alv.validateAccessLog(1, 200, Bug49528Servlet.THREAD_SLEEP_TIME, Bug49528Servlet.THREAD_SLEEP_TIME + REQUEST_TIME);
}
use of org.apache.catalina.startup.Tomcat in project tomcat by apache.
the class TestAsyncContextImpl method testErrorHandling.
@Test
public void testErrorHandling() throws Exception {
resetTracker();
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
ErrorServlet error = new ErrorServlet();
Tomcat.addServlet(ctx, "error", error);
ctx.addServletMappingDecoded("/error", "error");
TesterAccessLogValve alv = new TesterAccessLogValve();
ctx.getPipeline().addValve(alv);
tomcat.start();
StringBuilder url = new StringBuilder(48);
url.append("http://localhost:");
url.append(getPort());
url.append("/error");
int rc = getUrl(url.toString(), new ByteChunk(), null);
assertEquals(500, rc);
// Without this test may complete before access log has a chance to log
// the request
Thread.sleep(REQUEST_TIME);
// Check the access log
alv.validateAccessLog(1, 500, 0, REQUEST_TIME);
}
use of org.apache.catalina.startup.Tomcat in project tomcat by apache.
the class TestDefaultInstanceManager method doClassUnloadingPrep.
private DefaultInstanceManager doClassUnloadingPrep() throws Exception {
Tomcat tomcat = getTomcatInstance();
// Create the context (don't use addWebapp as we want to modify the
// JSP Servlet settings).
File appDir = new File("test/webapp");
StandardContext ctxt = (StandardContext) tomcat.addContext(null, "/test", appDir.getAbsolutePath());
ctxt.addServletContainerInitializer(new JasperInitializer(), null);
// Configure the defaults and then tweak the JSP servlet settings
// Note: Min value for maxLoadedJsps is 2
Tomcat.initWebappDefaults(ctxt);
Wrapper w = (Wrapper) ctxt.findChild("jsp");
w.addInitParameter("maxLoadedJsps", "2");
tomcat.start();
return (DefaultInstanceManager) ctxt.getInstanceManager();
}
Aggregations