Search in sources :

Example 71 with Wrapper

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);
}
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 72 with Wrapper

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);
}
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 73 with Wrapper

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());
}
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)

Example 74 with Wrapper

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());
}
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) IOException(java.io.IOException) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 75 with Wrapper

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);
}
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)

Aggregations

Wrapper (org.apache.catalina.Wrapper)109 Context (org.apache.catalina.Context)57 Tomcat (org.apache.catalina.startup.Tomcat)48 AsyncContext (javax.servlet.AsyncContext)33 Test (org.junit.Test)31 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)28 ServletRequestWrapper (javax.servlet.ServletRequestWrapper)24 ServletResponseWrapper (javax.servlet.ServletResponseWrapper)24 TesterContext (org.apache.tomcat.unittest.TesterContext)24 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)22 IOException (java.io.IOException)18 StandardWrapper (org.apache.catalina.core.StandardWrapper)16 TesterAccessLogValve (org.apache.catalina.valves.TesterAccessLogValve)14 File (java.io.File)13 Container (org.apache.catalina.Container)13 ServletException (javax.servlet.ServletException)9 HashMap (java.util.HashMap)7 StandardContext (org.apache.catalina.core.StandardContext)7 Servlet (javax.servlet.Servlet)6 ArrayList (java.util.ArrayList)5