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