Search in sources :

Example 11 with TesterAccessLogValve

use of org.apache.catalina.valves.TesterAccessLogValve in project tomcat by apache.

the class TestAsyncContextImpl method testBug50352.

@Test
public void testBug50352() throws Exception {
    resetTracker();
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    AsyncStartRunnable servlet = new AsyncStartRunnable();
    Wrapper wrapper = Tomcat.addServlet(ctx, "servlet", servlet);
    wrapper.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/", "servlet");
    TesterAccessLogValve alv = new TesterAccessLogValve();
    ctx.getPipeline().addValve(alv);
    tomcat.start();
    getUrl("http://localhost:" + getPort() + "/");
    // Request may complete before listener has finished processing so wait
    // up to 5 seconds for the right response
    String expectedTrack = "Runnable-onComplete-";
    int count = 0;
    while (!expectedTrack.equals(getTrack()) && count < 100) {
        Thread.sleep(50);
        count++;
    }
    assertEquals(expectedTrack, getTrack());
    // Check the access log
    alv.validateAccessLog(1, 200, AsyncStartRunnable.THREAD_SLEEP_TIME, AsyncStartRunnable.THREAD_SLEEP_TIME + REQUEST_TIME);
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) Wrapper(org.apache.catalina.Wrapper) ServletResponseWrapper(javax.servlet.ServletResponseWrapper) ServletRequestWrapper(javax.servlet.ServletRequestWrapper) Tomcat(org.apache.catalina.startup.Tomcat) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 12 with TesterAccessLogValve

use of org.apache.catalina.valves.TesterAccessLogValve 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);
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) Wrapper(org.apache.catalina.Wrapper) ServletResponseWrapper(javax.servlet.ServletResponseWrapper) ServletRequestWrapper(javax.servlet.ServletRequestWrapper) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 13 with TesterAccessLogValve

use of org.apache.catalina.valves.TesterAccessLogValve 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);
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) Wrapper(org.apache.catalina.Wrapper) ServletResponseWrapper(javax.servlet.ServletResponseWrapper) ServletRequestWrapper(javax.servlet.ServletRequestWrapper) Tomcat(org.apache.catalina.startup.Tomcat) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 14 with TesterAccessLogValve

use of org.apache.catalina.valves.TesterAccessLogValve 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);
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) Wrapper(org.apache.catalina.Wrapper) ServletResponseWrapper(javax.servlet.ServletResponseWrapper) ServletRequestWrapper(javax.servlet.ServletRequestWrapper) Tomcat(org.apache.catalina.startup.Tomcat) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 15 with TesterAccessLogValve

use of org.apache.catalina.valves.TesterAccessLogValve in project tomcat by apache.

the class TestAsyncContextImpl method testAsyncContextListenerClearing.

// https://bz.apache.org/bugzilla/show_bug.cgi?id=57326
@Test
public void testAsyncContextListenerClearing() throws Exception {
    resetTracker();
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Servlet stage1 = new DispatchingServletTracking("/stage2", true);
    Wrapper wrapper1 = Tomcat.addServlet(ctx, "stage1", stage1);
    wrapper1.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/stage1", "stage1");
    Servlet stage2 = new DispatchingServletTracking("/stage3", false);
    Wrapper wrapper2 = Tomcat.addServlet(ctx, "stage2", stage2);
    wrapper2.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/stage2", "stage2");
    Servlet stage3 = new NonAsyncServlet();
    Tomcat.addServlet(ctx, "stage3", stage3);
    ctx.addServletMappingDecoded("/stage3", "stage3");
    TesterAccessLogValve alv = new TesterAccessLogValve();
    ctx.getPipeline().addValve(alv);
    tomcat.start();
    getUrl("http://localhost:" + getPort() + "/stage1");
    assertEquals("doGet-startAsync-doGet-startAsync-onStartAsync-NonAsyncServletGet-onComplete-", getTrack());
    // Check the access log
    alv.validateAccessLog(1, 200, 0, REQUEST_TIME);
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) Wrapper(org.apache.catalina.Wrapper) ServletResponseWrapper(javax.servlet.ServletResponseWrapper) ServletRequestWrapper(javax.servlet.ServletRequestWrapper) Tomcat(org.apache.catalina.startup.Tomcat) HttpServlet(javax.servlet.http.HttpServlet) Servlet(javax.servlet.Servlet) GenericServlet(javax.servlet.GenericServlet) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

AsyncContext (javax.servlet.AsyncContext)16 Context (org.apache.catalina.Context)16 Tomcat (org.apache.catalina.startup.Tomcat)16 TesterAccessLogValve (org.apache.catalina.valves.TesterAccessLogValve)16 TesterContext (org.apache.tomcat.unittest.TesterContext)15 ServletRequestWrapper (javax.servlet.ServletRequestWrapper)14 ServletResponseWrapper (javax.servlet.ServletResponseWrapper)14 Wrapper (org.apache.catalina.Wrapper)14 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)11 Test (org.junit.Test)11 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)9 IOException (java.io.IOException)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 ErrorPage (org.apache.tomcat.util.descriptor.web.ErrorPage)2 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Socket (java.net.Socket)1 HashMap (java.util.HashMap)1 SocketFactory (javax.net.SocketFactory)1