Search in sources :

Example 6 with TesterAccessLogValve

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

the class TestAsyncContextImpl method testBug49567.

@Test
public void testBug49567() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Bug49567Servlet servlet = new Bug49567Servlet();
    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, Bug49567Servlet.THREAD_SLEEP_TIME, Bug49567Servlet.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) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 7 with TesterAccessLogValve

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

the class TestAsyncContextImpl method doTestBug51197.

private void doTestBug51197(boolean threaded, boolean customError) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    AsyncErrorServlet asyncErrorServlet = new AsyncErrorServlet(HttpServletResponse.SC_BAD_REQUEST, threaded);
    Wrapper wrapper = Tomcat.addServlet(ctx, "asyncErrorServlet", asyncErrorServlet);
    wrapper.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/asyncErrorServlet", "asyncErrorServlet");
    if (customError) {
        CustomErrorServlet customErrorServlet = new CustomErrorServlet();
        Tomcat.addServlet(ctx, "customErrorServlet", customErrorServlet);
        ctx.addServletMappingDecoded("/customErrorServlet", "customErrorServlet");
        ErrorPage ep = new ErrorPage();
        ep.setLocation("/customErrorServlet");
        ctx.addErrorPage(ep);
    }
    TesterAccessLogValve alv = new TesterAccessLogValve();
    ctx.getPipeline().addValve(alv);
    tomcat.start();
    StringBuilder url = new StringBuilder(48);
    url.append("http://localhost:");
    url.append(getPort());
    url.append("/asyncErrorServlet");
    ByteChunk res = new ByteChunk();
    int rc = getUrl(url.toString(), res, null);
    assertEquals(HttpServletResponse.SC_BAD_REQUEST, rc);
    // SRV 10.9.2 - Handling an error is entirely the application's
    // responsibility when an error occurs on an application thread.
    // Calling sendError() followed by complete() and expecting the standard
    // error page mechanism to kick in could be viewed as handling the error
    String responseBody = res.toString();
    Assert.assertNotNull(responseBody);
    assertTrue(responseBody.length() > 0);
    if (customError) {
        assertTrue(responseBody, responseBody.contains(CustomErrorServlet.ERROR_MESSAGE));
    } else {
        assertTrue(responseBody, responseBody.contains(AsyncErrorServlet.ERROR_MESSAGE));
    }
    // 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, HttpServletResponse.SC_BAD_REQUEST, 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) ErrorPage(org.apache.tomcat.util.descriptor.web.ErrorPage) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve)

Example 8 with TesterAccessLogValve

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

the class TestNonBlockingAPI method testNonBlockingWriteError.

@Test
public void testNonBlockingWriteError() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    TesterAccessLogValve alv = new TesterAccessLogValve();
    ctx.getPipeline().addValve(alv);
    NBWriteServlet servlet = new NBWriteServlet();
    String servletName = NBWriteServlet.class.getName();
    Tomcat.addServlet(ctx, servletName, servlet);
    ctx.addServletMappingDecoded("/", servletName);
    tomcat.getConnector().setProperty("socket.txBufSize", "1024");
    tomcat.start();
    SocketFactory factory = SocketFactory.getDefault();
    Socket s = factory.createSocket("localhost", getPort());
    ByteChunk result = new ByteChunk();
    OutputStream os = s.getOutputStream();
    os.write(("GET / HTTP/1.1\r\n" + "Host: localhost:" + getPort() + "\r\n" + "Connection: close\r\n" + "\r\n").getBytes(StandardCharsets.ISO_8859_1));
    os.flush();
    InputStream is = s.getInputStream();
    byte[] buffer = new byte[8192];
    int read = 0;
    int readSinceLastPause = 0;
    int readTotal = 0;
    while (read != -1 && readTotal < WRITE_SIZE / 32) {
        long start = System.currentTimeMillis();
        read = is.read(buffer);
        long end = System.currentTimeMillis();
        log.info("Client read [" + read + "] bytes in [" + (end - start) + "] ms");
        if (read > 0) {
            result.append(buffer, 0, read);
        }
        readSinceLastPause += read;
        readTotal += read;
        if (readSinceLastPause > WRITE_SIZE / 64) {
            readSinceLastPause = 0;
            Thread.sleep(WRITE_PAUSE_MS);
        }
    }
    os.close();
    is.close();
    s.close();
    String resultString = result.toString();
    log.info("Client read " + resultString.length() + " bytes");
    int lineStart = 0;
    int lineEnd = resultString.indexOf('\n', 0);
    String line = resultString.substring(lineStart, lineEnd + 1);
    Assert.assertEquals("HTTP/1.1 200 \r\n", line);
    // Listeners are invoked and access valve entries created on a different
    // thread so give that thread a chance to complete its work.
    int count = 0;
    while (count < 100 && !(servlet.wlistener.onErrorInvoked || servlet.rlistener.onErrorInvoked)) {
        Thread.sleep(100);
        count++;
    }
    while (count < 100 && alv.getEntryCount() < 1) {
        Thread.sleep(100);
        count++;
    }
    Assert.assertTrue("Error listener should have been invoked.", servlet.wlistener.onErrorInvoked || servlet.rlistener.onErrorInvoked);
    // TODO Figure out why non-blocking writes with the NIO connector appear
    // to be slower on Linux
    alv.validateAccessLog(1, 500, WRITE_PAUSE_MS, WRITE_PAUSE_MS + 30 * 1000);
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) SocketFactory(javax.net.SocketFactory) ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve) Socket(java.net.Socket) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 9 with TesterAccessLogValve

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

the class TestAsyncContextImpl method doTestDispatchError.

private void doTestDispatchError(int iter, boolean useThread, boolean completeOnError) throws Exception {
    resetTracker();
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    DispatchingServlet dispatch = new DispatchingServlet(true, completeOnError);
    Wrapper wrapper = Tomcat.addServlet(ctx, "dispatch", dispatch);
    wrapper.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/stage1", "dispatch");
    ErrorServlet error = new ErrorServlet();
    Tomcat.addServlet(ctx, "error", error);
    ctx.addServletMappingDecoded("/stage2", "error");
    ctx.addApplicationListener(TrackingRequestListener.class.getName());
    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?iter=");
    url.append(iter);
    if (useThread) {
        url.append("&useThread=y");
    }
    getUrl(url.toString());
    StringBuilder expected = new StringBuilder("requestInitialized-");
    int loop = iter;
    while (loop > 0) {
        expected.append("DispatchingServletGet-");
        if (loop != iter) {
            expected.append("onStartAsync-");
        }
        loop--;
    }
    expected.append("ErrorServletGet-onError-onComplete-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
    alv.validateAccessLog(1, 500, 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) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve)

Example 10 with TesterAccessLogValve

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

the class TestAsyncContextImpl method doTestDispatch.

private void doTestDispatch(int iter, boolean useThread) throws Exception {
    resetTracker();
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    DispatchingServlet dispatch = new DispatchingServlet(false, false);
    Wrapper wrapper = Tomcat.addServlet(ctx, "dispatch", dispatch);
    wrapper.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/stage1", "dispatch");
    NonAsyncServlet nonasync = new NonAsyncServlet();
    Wrapper wrapper2 = Tomcat.addServlet(ctx, "nonasync", nonasync);
    wrapper2.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/stage2", "nonasync");
    ctx.addApplicationListener(TrackingRequestListener.class.getName());
    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?iter=");
    url.append(iter);
    if (useThread) {
        url.append("&useThread=y");
    }
    getUrl(url.toString());
    StringBuilder expected = new StringBuilder("requestInitialized-");
    int loop = iter;
    while (loop > 0) {
        expected.append("DispatchingServletGet-");
        loop--;
    }
    expected.append("NonAsyncServletGet-");
    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
    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) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve)

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