Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimone Bordet2015-08-12 19:33:28 -0400
committerSimone Bordet2015-08-12 19:33:28 -0400
commit79086f3fe3c52eb85eb94dc3fa796f69011967fd (patch)
treee3420aae65c54716eedb16ca1c01957ea02721ce /jetty-servlet
parent4c7d5f661ed50c19934dbd4a0cb6c3772b4262c2 (diff)
downloadorg.eclipse.jetty.project-79086f3fe3c52eb85eb94dc3fa796f69011967fd.tar.gz
org.eclipse.jetty.project-79086f3fe3c52eb85eb94dc3fa796f69011967fd.tar.xz
org.eclipse.jetty.project-79086f3fe3c52eb85eb94dc3fa796f69011967fd.zip
474634 - AsyncListener.onError() handling.
Interim work on getting the right behavior for onError().
Diffstat (limited to 'jetty-servlet')
-rw-r--r--jetty-servlet/src/test/java/org/eclipse/jetty/servlet/AsyncServletTest.java419
1 files changed, 268 insertions, 151 deletions
diff --git a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/AsyncServletTest.java b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/AsyncServletTest.java
index 37130be6a4..26f694360c 100644
--- a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/AsyncServletTest.java
+++ b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/AsyncServletTest.java
@@ -18,8 +18,6 @@
package org.eclipse.jetty.servlet;
-import static org.junit.Assert.assertEquals;
-
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
@@ -43,6 +41,7 @@ import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import org.eclipse.jetty.server.Connector;
+import org.eclipse.jetty.server.QuietServletException;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.RequestLog;
import org.eclipse.jetty.server.Response;
@@ -60,6 +59,10 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import static org.hamcrest.Matchers.startsWith;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+
@RunWith(AdvancedRunner.class)
public class AsyncServletTest
@@ -79,7 +82,7 @@ public class AsyncServletTest
{
_connector = new ServerConnector(_server);
_server.setConnectors(new Connector[]{ _connector });
-
+
_log=new ArrayList<>();
RequestLog log=new Log();
RequestLogHandler logHandler = new RequestLogHandler();
@@ -91,7 +94,7 @@ public class AsyncServletTest
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
context.setContextPath("/ctx");
logHandler.setHandler(context);
-
+
_servletHandler=context.getServletHandler();
ServletHolder holder=new ServletHolder(_servlet);
holder.setAsyncSupported(true);
@@ -116,7 +119,7 @@ public class AsyncServletTest
public void testNormal() throws Exception
{
String response=process(null,null);
- assertEquals("HTTP/1.1 200 OK",response.substring(0,15));
+ assertThat(response,startsWith("HTTP/1.1 200 OK\r\n"));
assertContains(
"history: REQUEST /ctx/path/info\r\n"+
"history: initial\r\n",response);
@@ -129,7 +132,7 @@ public class AsyncServletTest
public void testSleep() throws Exception
{
String response=process("sleep=200",null);
- assertEquals("HTTP/1.1 200 OK",response.substring(0,15));
+ assertThat(response,startsWith("HTTP/1.1 200 OK\r\n"));
assertContains(
"history: REQUEST /ctx/path/info\r\n"+
"history: initial\r\n",response);
@@ -139,33 +142,90 @@ public class AsyncServletTest
}
@Test
- public void testSuspend() throws Exception
+ public void testStart() throws Exception
{
_expectedCode="500 ";
- String response=process("suspend=200",null);
+ String response=process("start=200",null);
Assert.assertThat(response,Matchers.startsWith("HTTP/1.1 500 Async Timeout"));
assertContains(
"history: REQUEST /ctx/path/info\r\n"+
"history: initial\r\n"+
- "history: suspend\r\n"+
+ "history: start\r\n"+
+ "history: onTimeout\r\n"+
+ "history: ERROR /ctx/path/info\r\n"+
+ "history: !initial\r\n"+
+ "history: onComplete\r\n",response);
+
+ assertContains("ERROR DISPATCH: /ctx/path/info",response);
+ }
+
+ @Test
+ public void testStartOnTimeoutDispatch() throws Exception
+ {
+ String response=process("start=200&timeout=dispatch",null);
+ assertThat(response,startsWith("HTTP/1.1 200 OK\r\n"));
+ assertContains(
+ "history: REQUEST /ctx/path/info\r\n"+
+ "history: initial\r\n"+
+ "history: start\r\n"+
+ "history: onTimeout\r\n"+
+ "history: dispatch\r\n"+
+ "history: ASYNC /ctx/path/info\r\n"+
+ "history: !initial\r\n"+
+ "history: onComplete\r\n",response);
+
+ assertContains("DISPATCHED",response);
+ }
+
+ @Test
+ public void testStartOnTimeoutError() throws Exception
+ {
+ _expectedCode="500 ";
+ String response=process("start=200&timeout=error",null);
+ assertThat(response,startsWith("HTTP/1.1 500 Async Exception\r\n"));
+ assertContains(
+ "history: REQUEST /ctx/path/info\r\n"+
+ "history: initial\r\n"+
+ "history: start\r\n"+
"history: onTimeout\r\n"+
+ "history: error\r\n"+
+ "history: onError\r\n"+
"history: ERROR /ctx/path/info\r\n"+
"history: !initial\r\n"+
"history: onComplete\r\n",response);
- assertContains("ERROR: /ctx/path/info",response);
+ assertContains("ERROR DISPATCH",response);
}
@Test
- public void testSuspendOnTimeoutDispatch() throws Exception
+ public void testStartOnTimeoutErrorComplete() throws Exception
{
- String response=process("suspend=200&timeout=dispatch",null);
- assertEquals("HTTP/1.1 200 OK",response.substring(0,15));
+ String response=process("start=200&timeout=error&error=complete",null);
+ assertThat(response,startsWith("HTTP/1.1 200 OK\r\n"));
assertContains(
"history: REQUEST /ctx/path/info\r\n"+
"history: initial\r\n"+
- "history: suspend\r\n"+
+ "history: start\r\n"+
"history: onTimeout\r\n"+
+ "history: error\r\n"+
+ "history: onError\r\n"+
+ "history: complete\r\n",response);
+
+ assertContains("COMPLETED",response);
+ }
+
+ @Test
+ public void testStartOnTimeoutErrorDispatch() throws Exception
+ {
+ String response=process("start=200&timeout=error&error=dispatch",null);
+ assertThat(response,startsWith("HTTP/1.1 200 OK\r\n"));
+ assertContains(
+ "history: REQUEST /ctx/path/info\r\n"+
+ "history: initial\r\n"+
+ "history: start\r\n"+
+ "history: onTimeout\r\n"+
+ "history: error\r\n"+
+ "history: onError\r\n"+
"history: dispatch\r\n"+
"history: ASYNC /ctx/path/info\r\n"+
"history: !initial\r\n"+
@@ -175,14 +235,14 @@ public class AsyncServletTest
}
@Test
- public void testSuspendOnTimeoutComplete() throws Exception
+ public void testStartOnTimeoutComplete() throws Exception
{
- String response=process("suspend=200&timeout=complete",null);
- assertEquals("HTTP/1.1 200 OK",response.substring(0,15));
+ String response=process("start=200&timeout=complete",null);
+ assertThat(response,startsWith("HTTP/1.1 200 OK\r\n"));
assertContains(
"history: REQUEST /ctx/path/info\r\n"+
"history: initial\r\n"+
- "history: suspend\r\n"+
+ "history: start\r\n"+
"history: onTimeout\r\n"+
"history: complete\r\n"+
"history: onComplete\r\n",response);
@@ -191,15 +251,15 @@ public class AsyncServletTest
}
@Test
- public void testSuspendWaitResume() throws Exception
+ public void testStartWaitDispatch() throws Exception
{
- String response=process("suspend=200&resume=10",null);
- assertEquals("HTTP/1.1 200 OK",response.substring(0,15));
+ String response=process("start=200&dispatch=10",null);
+ assertThat(response,startsWith("HTTP/1.1 200 OK\r\n"));
assertContains(
"history: REQUEST /ctx/path/info\r\n"+
"history: initial\r\n"+
- "history: suspend\r\n"+
- "history: resume\r\n"+
+ "history: start\r\n"+
+ "history: dispatch\r\n"+
"history: ASYNC /ctx/path/info\r\n"+
"history: !initial\r\n"+
"history: onComplete\r\n",response);
@@ -207,15 +267,15 @@ public class AsyncServletTest
}
@Test
- public void testSuspendResume() throws Exception
+ public void testStartDispatch() throws Exception
{
- String response=process("suspend=200&resume=0",null);
- assertEquals("HTTP/1.1 200 OK",response.substring(0,15));
+ String response=process("start=200&dispatch=0",null);
+ assertThat(response,startsWith("HTTP/1.1 200 OK\r\n"));
assertContains(
"history: REQUEST /ctx/path/info\r\n"+
"history: initial\r\n"+
- "history: suspend\r\n"+
- "history: resume\r\n"+
+ "history: start\r\n"+
+ "history: dispatch\r\n"+
"history: ASYNC /ctx/path/info\r\n"+
"history: !initial\r\n"+
"history: onComplete\r\n",response);
@@ -223,14 +283,32 @@ public class AsyncServletTest
}
@Test
- public void testSuspendWaitComplete() throws Exception
+ public void testStartError() throws Exception
{
- String response=process("suspend=200&complete=50",null);
- assertEquals("HTTP/1.1 200 OK",response.substring(0,15));
+ _expectedCode="500 ";
+ String response=process("start=200&throw=1",null);
+ assertThat(response,startsWith("HTTP/1.1 500 Server Error\r\n"));
assertContains(
"history: REQUEST /ctx/path/info\r\n"+
"history: initial\r\n"+
- "history: suspend\r\n"+
+ "history: start\r\n"+
+ /* TODO should there be an onError call?
+ "history: onError\r\n"+
+ "history: onComplete\r\n"
+ */"",
+ response);
+ assertContains("HTTP ERROR: 500",response);
+ }
+
+ @Test
+ public void testStartWaitComplete() throws Exception
+ {
+ String response=process("start=200&complete=50",null);
+ assertThat(response,startsWith("HTTP/1.1 200 OK\r\n"));
+ assertContains(
+ "history: REQUEST /ctx/path/info\r\n"+
+ "history: initial\r\n"+
+ "history: start\r\n"+
"history: complete\r\n"+
"history: onComplete\r\n",response);
assertContains("COMPLETED",response);
@@ -239,14 +317,14 @@ public class AsyncServletTest
}
@Test
- public void testSuspendComplete() throws Exception
+ public void testStartComplete() throws Exception
{
- String response=process("suspend=200&complete=0",null);
- assertEquals("HTTP/1.1 200 OK",response.substring(0,15));
+ String response=process("start=200&complete=0",null);
+ assertThat(response,startsWith("HTTP/1.1 200 OK\r\n"));
assertContains(
"history: REQUEST /ctx/path/info\r\n"+
"history: initial\r\n"+
- "history: suspend\r\n"+
+ "history: start\r\n"+
"history: complete\r\n"+
"history: onComplete\r\n",response);
assertContains("COMPLETED",response);
@@ -255,19 +333,20 @@ public class AsyncServletTest
}
@Test
- public void testSuspendWaitResumeSuspendWaitResume() throws Exception
+ public void testStartWaitDispatchStartWaitDispatch() throws Exception
{
- String response=process("suspend=1000&resume=10&suspend2=1000&resume2=10",null);
- assertEquals("HTTP/1.1 200 OK",response.substring(0,15));
+ String response=process("start=1000&dispatch=10&start2=1000&dispatch2=10",null);
+ assertThat(response,startsWith("HTTP/1.1 200 OK\r\n"));
assertContains(
"history: REQUEST /ctx/path/info\r\n"+
"history: initial\r\n"+
- "history: suspend\r\n"+
- "history: resume\r\n"+
+ "history: start\r\n"+
+ "history: dispatch\r\n"+
"history: ASYNC /ctx/path/info\r\n"+
"history: !initial\r\n"+
- "history: suspend\r\n"+
- "history: resume\r\n"+
+ "history: onStartAsync\r\n"+
+ "history: start\r\n"+
+ "history: dispatch\r\n"+
"history: ASYNC /ctx/path/info\r\n"+
"history: !initial\r\n"+
"history: onComplete\r\n",response);
@@ -275,58 +354,61 @@ public class AsyncServletTest
}
@Test
- public void testSuspendWaitResumeSuspendComplete() throws Exception
+ public void testStartWaitDispatchStartComplete() throws Exception
{
- String response=process("suspend=1000&resume=10&suspend2=1000&complete2=10",null);
- assertEquals("HTTP/1.1 200 OK",response.substring(0,15));
+ String response=process("start=1000&dispatch=10&start2=1000&complete2=10",null);
+ assertThat(response,startsWith("HTTP/1.1 200 OK\r\n"));
assertContains(
"history: REQUEST /ctx/path/info\r\n"+
"history: initial\r\n"+
- "history: suspend\r\n"+
- "history: resume\r\n"+
+ "history: start\r\n"+
+ "history: dispatch\r\n"+
"history: ASYNC /ctx/path/info\r\n"+
"history: !initial\r\n"+
- "history: suspend\r\n"+
+ "history: onStartAsync\r\n"+
+ "history: start\r\n"+
"history: complete\r\n"+
"history: onComplete\r\n",response);
assertContains("COMPLETED",response);
}
@Test
- public void testSuspendWaitResumeSuspend() throws Exception
+ public void testStartWaitDispatchStart() throws Exception
{
_expectedCode="500 ";
- String response=process("suspend=1000&resume=10&suspend2=10",null);
+ String response=process("start=1000&dispatch=10&start2=10",null);
assertEquals("HTTP/1.1 500 Async Timeout",response.substring(0,26));
assertContains(
"history: REQUEST /ctx/path/info\r\n"+
"history: initial\r\n"+
- "history: suspend\r\n"+
- "history: resume\r\n"+
+ "history: start\r\n"+
+ "history: dispatch\r\n"+
"history: ASYNC /ctx/path/info\r\n"+
"history: !initial\r\n"+
- "history: suspend\r\n"+
+ "history: onStartAsync\r\n"+
+ "history: start\r\n"+
"history: onTimeout\r\n"+
"history: ERROR /ctx/path/info\r\n"+
"history: !initial\r\n"+
"history: onComplete\r\n",response);
- assertContains("ERROR: /ctx/path/info",response);
+ assertContains("ERROR DISPATCH: /ctx/path/info",response);
}
@Test
- public void testSuspendTimeoutSuspendResume() throws Exception
+ public void testStartTimeoutStartDispatch() throws Exception
{
- String response=process("suspend=10&suspend2=1000&resume2=10",null);
- assertEquals("HTTP/1.1 200 OK",response.substring(0,15));
+ String response=process("start=10&start2=1000&dispatch2=10",null);
+ assertThat(response,startsWith("HTTP/1.1 200 OK\r\n"));
assertContains(
"history: REQUEST /ctx/path/info\r\n"+
"history: initial\r\n"+
- "history: suspend\r\n"+
+ "history: start\r\n"+
"history: onTimeout\r\n"+
"history: ERROR /ctx/path/info\r\n"+
"history: !initial\r\n"+
- "history: suspend\r\n"+
- "history: resume\r\n"+
+ "history: onStartAsync\r\n"+
+ "history: start\r\n"+
+ "history: dispatch\r\n"+
"history: ASYNC /ctx/path/info\r\n"+
"history: !initial\r\n"+
"history: onComplete\r\n",response);
@@ -334,53 +416,55 @@ public class AsyncServletTest
}
@Test
- public void testSuspendTimeoutSuspendComplete() throws Exception
+ public void testStartTimeoutStartComplete() throws Exception
{
- String response=process("suspend=10&suspend2=1000&complete2=10",null);
- assertEquals("HTTP/1.1 200 OK",response.substring(0,15));
+ String response=process("start=10&start2=1000&complete2=10",null);
+ assertThat(response,startsWith("HTTP/1.1 200 OK\r\n"));
assertContains(
"history: REQUEST /ctx/path/info\r\n"+
"history: initial\r\n"+
- "history: suspend\r\n"+
+ "history: start\r\n"+
"history: onTimeout\r\n"+
"history: ERROR /ctx/path/info\r\n"+
"history: !initial\r\n"+
- "history: suspend\r\n"+
+ "history: onStartAsync\r\n"+
+ "history: start\r\n"+
"history: complete\r\n"+
"history: onComplete\r\n",response);
assertContains("COMPLETED",response);
}
@Test
- public void testSuspendTimeoutSuspend() throws Exception
+ public void testStartTimeoutStart() throws Exception
{
_expectedCode="500 ";
- String response=process("suspend=10&suspend2=10",null);
+ String response=process("start=10&start2=10",null);
assertContains(
"history: REQUEST /ctx/path/info\r\n"+
"history: initial\r\n"+
- "history: suspend\r\n"+
+ "history: start\r\n"+
"history: onTimeout\r\n"+
"history: ERROR /ctx/path/info\r\n"+
"history: !initial\r\n"+
- "history: suspend\r\n"+
+ "history: onStartAsync\r\n"+
+ "history: start\r\n"+
"history: onTimeout\r\n"+
"history: ERROR /ctx/path/info\r\n"+
"history: !initial\r\n"+
"history: onComplete\r\n",response);
- assertContains("ERROR: /ctx/path/info",response);
+ assertContains("ERROR DISPATCH: /ctx/path/info",response);
}
@Test
public void testWrapStartDispatch() throws Exception
{
- String response=process("wrap=true&suspend=200&resume=20",null);
- assertEquals("HTTP/1.1 200 OK",response.substring(0,15));
+ String response=process("wrap=true&start=200&dispatch=20",null);
+ assertThat(response,startsWith("HTTP/1.1 200 OK\r\n"));
assertContains(
"history: REQUEST /ctx/path/info\r\n"+
"history: initial\r\n"+
- "history: suspend\r\n"+
- "history: resume\r\n"+
+ "history: start\r\n"+
+ "history: dispatch\r\n"+
"history: ASYNC /ctx/path/info\r\n"+
"history: wrapped REQ RSP\r\n"+
"history: !initial\r\n"+
@@ -392,49 +476,49 @@ public class AsyncServletTest
@Test
public void testStartDispatchEncodedPath() throws Exception
{
- String response=process("suspend=200&resume=20&path=/p%20th3",null);
- assertEquals("HTTP/1.1 200 OK",response.substring(0,15));
+ String response=process("start=200&dispatch=20&path=/p%20th3",null);
+ assertThat(response,startsWith("HTTP/1.1 200 OK\r\n"));
assertContains(
"history: REQUEST /ctx/path/info\r\n"+
"history: initial\r\n"+
- "history: suspend\r\n"+
- "history: resume\r\n"+
+ "history: start\r\n"+
+ "history: dispatch\r\n"+
"history: ASYNC /ctx/p%20th3\r\n"+
"history: !initial\r\n"+
"history: onComplete\r\n",response);
assertContains("DISPATCHED",response);
}
-
-
+
+
@Test
public void testFwdStartDispatch() throws Exception
{
- String response=process("fwd","suspend=200&resume=20",null);
- assertEquals("HTTP/1.1 200 OK",response.substring(0,15));
+ String response=process("fwd","start=200&dispatch=20",null);
+ assertThat(response,startsWith("HTTP/1.1 200 OK\r\n"));
assertContains(
"history: FWD REQUEST /ctx/fwd/info\r\n"+
"history: FORWARD /ctx/path1\r\n"+
"history: initial\r\n"+
- "history: suspend\r\n"+
- "history: resume\r\n"+
+ "history: start\r\n"+
+ "history: dispatch\r\n"+
"history: FWD ASYNC /ctx/fwd/info\r\n"+
"history: FORWARD /ctx/path1\r\n"+
"history: !initial\r\n"+
"history: onComplete\r\n",response);
assertContains("DISPATCHED",response);
}
-
+
@Test
public void testFwdStartDispatchPath() throws Exception
{
- String response=process("fwd","suspend=200&resume=20&path=/path2",null);
- assertEquals("HTTP/1.1 200 OK",response.substring(0,15));
+ String response=process("fwd","start=200&dispatch=20&path=/path2",null);
+ assertThat(response,startsWith("HTTP/1.1 200 OK\r\n"));
assertContains(
"history: FWD REQUEST /ctx/fwd/info\r\n"+
"history: FORWARD /ctx/path1\r\n"+
"history: initial\r\n"+
- "history: suspend\r\n"+
- "history: resume\r\n"+
+ "history: start\r\n"+
+ "history: dispatch\r\n"+
"history: ASYNC /ctx/path2\r\n"+
"history: !initial\r\n"+
"history: onComplete\r\n",response);
@@ -444,45 +528,45 @@ public class AsyncServletTest
@Test
public void testFwdWrapStartDispatch() throws Exception
{
- String response=process("fwd","wrap=true&suspend=200&resume=20",null);
- assertEquals("HTTP/1.1 200 OK",response.substring(0,15));
+ String response=process("fwd","wrap=true&start=200&dispatch=20",null);
+ assertThat(response,startsWith("HTTP/1.1 200 OK\r\n"));
assertContains(
"history: FWD REQUEST /ctx/fwd/info\r\n"+
"history: FORWARD /ctx/path1\r\n"+
"history: initial\r\n"+
- "history: suspend\r\n"+
- "history: resume\r\n"+
+ "history: start\r\n"+
+ "history: dispatch\r\n"+
"history: ASYNC /ctx/path1\r\n"+
"history: wrapped REQ RSP\r\n"+
"history: !initial\r\n"+
"history: onComplete\r\n",response);
assertContains("DISPATCHED",response);
}
-
+
@Test
public void testFwdWrapStartDispatchPath() throws Exception
{
- String response=process("fwd","wrap=true&suspend=200&resume=20&path=/path2",null);
- assertEquals("HTTP/1.1 200 OK",response.substring(0,15));
+ String response=process("fwd","wrap=true&start=200&dispatch=20&path=/path2",null);
+ assertThat(response,startsWith("HTTP/1.1 200 OK\r\n"));
assertContains(
"history: FWD REQUEST /ctx/fwd/info\r\n"+
"history: FORWARD /ctx/path1\r\n"+
"history: initial\r\n"+
- "history: suspend\r\n"+
- "history: resume\r\n"+
+ "history: start\r\n"+
+ "history: dispatch\r\n"+
"history: ASYNC /ctx/path2\r\n"+
"history: wrapped REQ RSP\r\n"+
"history: !initial\r\n"+
"history: onComplete\r\n",response);
assertContains("DISPATCHED",response);
}
-
-
+
+
@Test
public void testAsyncRead() throws Exception
{
_expectedLogs=2;
- String header="GET /ctx/path/info?suspend=2000&resume=1500 HTTP/1.1\r\n"+
+ String header="GET /ctx/path/info?start=2000&dispatch=1500 HTTP/1.1\r\n"+
"Host: localhost\r\n"+
"Content-Length: 10\r\n"+
"\r\n";
@@ -503,13 +587,13 @@ public class AsyncServletTest
socket.getOutputStream().write(close.getBytes(StandardCharsets.ISO_8859_1));
String response = IO.toString(socket.getInputStream());
- assertEquals("HTTP/1.1 200 OK",response.substring(0,15));
+ assertThat(response,startsWith("HTTP/1.1 200 OK\r\n"));
assertContains(
"history: REQUEST /ctx/path/info\r\n"+
"history: initial\r\n"+
- "history: suspend\r\n"+
+ "history: start\r\n"+
"history: async-read=10\r\n"+
- "history: resume\r\n"+
+ "history: dispatch\r\n"+
"history: ASYNC /ctx/path/info\r\n"+
"history: !initial\r\n"+
"history: onComplete\r\n",response);
@@ -520,7 +604,7 @@ public class AsyncServletTest
{
return process("path",query,content);
}
-
+
public synchronized String process(String path,String query,String content) throws Exception
{
String request = "GET /ctx/"+path+"/info";
@@ -543,6 +627,7 @@ public class AsyncServletTest
{
socket.setSoTimeout(1000000);
socket.getOutputStream().write(request.getBytes(StandardCharsets.UTF_8));
+ socket.getOutputStream().flush();
return IO.toString(socket.getInputStream());
}
catch(Exception e)
@@ -574,7 +659,7 @@ public class AsyncServletTest
request.getServletContext().getRequestDispatcher("/path1").forward(request,response);
}
}
-
+
private static class AsyncServlet extends HttpServlet
{
private static final long serialVersionUID = -8161977157098646562L;
@@ -593,36 +678,36 @@ public class AsyncServletTest
{
// ignored
}
-
+
// System.err.println(request.getDispatcherType()+" "+request.getRequestURI());
response.addHeader("history",request.getDispatcherType()+" "+request.getRequestURI());
if (request instanceof ServletRequestWrapper || response instanceof ServletResponseWrapper)
response.addHeader("history","wrapped"+((request instanceof ServletRequestWrapper)?" REQ":"")+((response instanceof ServletResponseWrapper)?" RSP":""));
-
+
boolean wrap="true".equals(request.getParameter("wrap"));
int read_before=0;
long sleep_for=-1;
- long suspend_for=-1;
- long suspend2_for=-1;
- long resume_after=-1;
- long resume2_after=-1;
+ long start_for=-1;
+ long start2_for=-1;
+ long dispatch_after=-1;
+ long dispatch2_after=-1;
long complete_after=-1;
long complete2_after=-1;
-
+
if (request.getParameter("read")!=null)
read_before=Integer.parseInt(request.getParameter("read"));
if (request.getParameter("sleep")!=null)
sleep_for=Integer.parseInt(request.getParameter("sleep"));
- if (request.getParameter("suspend")!=null)
- suspend_for=Integer.parseInt(request.getParameter("suspend"));
- if (request.getParameter("suspend2")!=null)
- suspend2_for=Integer.parseInt(request.getParameter("suspend2"));
- if (request.getParameter("resume")!=null)
- resume_after=Integer.parseInt(request.getParameter("resume"));
+ if (request.getParameter("start")!=null)
+ start_for=Integer.parseInt(request.getParameter("start"));
+ if (request.getParameter("start2")!=null)
+ start2_for=Integer.parseInt(request.getParameter("start2"));
+ if (request.getParameter("dispatch")!=null)
+ dispatch_after=Integer.parseInt(request.getParameter("dispatch"));
final String path=request.getParameter("path");
- if (request.getParameter("resume2")!=null)
- resume2_after=Integer.parseInt(request.getParameter("resume2"));
+ if (request.getParameter("dispatch2")!=null)
+ dispatch2_after=Integer.parseInt(request.getParameter("dispatch2"));
if (request.getParameter("complete")!=null)
complete_after=Integer.parseInt(request.getParameter("complete"));
if (request.getParameter("complete2")!=null)
@@ -669,13 +754,16 @@ public class AsyncServletTest
}.start();
}
- if (suspend_for>=0)
+ if (start_for>=0)
{
final AsyncContext async=wrap?request.startAsync(new HttpServletRequestWrapper(request),new HttpServletResponseWrapper(response)):request.startAsync();
- if (suspend_for>0)
- async.setTimeout(suspend_for);
+ if (start_for>0)
+ async.setTimeout(start_for);
async.addListener(__listener);
- response.addHeader("history","suspend");
+ response.addHeader("history","start");
+
+ if ("1".equals(request.getParameter("throw")))
+ throw new QuietServletException(new Exception("test throw in async 1"));
if (complete_after>0)
{
@@ -709,15 +797,15 @@ public class AsyncServletTest
response.addHeader("history","complete");
async.complete();
}
- else if (resume_after>0)
+ else if (dispatch_after>0)
{
- TimerTask resume = new TimerTask()
+ TimerTask dispatch = new TimerTask()
{
@Override
public void run()
{
- ((HttpServletResponse)async.getResponse()).addHeader("history","resume");
- if (path!=null)
+ ((HttpServletResponse)async.getResponse()).addHeader("history","dispatch");
+ if (path!=null)
{
int q=path.indexOf('?');
String uriInContext=(q>=0)
@@ -731,13 +819,13 @@ public class AsyncServletTest
};
synchronized (_timer)
{
- _timer.schedule(resume,resume_after);
+ _timer.schedule(dispatch,dispatch_after);
}
}
- else if (resume_after==0)
+ else if (dispatch_after==0)
{
- ((HttpServletResponse)async.getResponse()).addHeader("history","resume");
- if (path!=null)
+ ((HttpServletResponse)async.getResponse()).addHeader("history","dispatch");
+ if (path!=null)
async.dispatch(path);
else
async.dispatch();
@@ -767,18 +855,20 @@ public class AsyncServletTest
{
response.addHeader("history","!initial");
- if (suspend2_for>=0 && request.getAttribute("2nd")==null)
+ if (start2_for>=0 && request.getAttribute("2nd")==null)
{
final AsyncContext async=wrap?request.startAsync(new HttpServletRequestWrapper(request),new HttpServletResponseWrapper(response)):request.startAsync();
async.addListener(__listener);
request.setAttribute("2nd","cycle");
- if (suspend2_for>0)
+ if (start2_for>0)
{
- async.setTimeout(suspend2_for);
+ async.setTimeout(start2_for);
}
- // continuation.addContinuationListener(__listener);
- response.addHeader("history","suspend");
+ response.addHeader("history","start");
+
+ if ("2".equals(request.getParameter("throw")))
+ throw new QuietServletException(new Exception("test throw in async 2"));
if (complete2_after>0)
{
@@ -812,23 +902,23 @@ public class AsyncServletTest
response.addHeader("history","complete");
async.complete();
}
- else if (resume2_after>0)
+ else if (dispatch2_after>0)
{
- TimerTask resume = new TimerTask()
+ TimerTask dispatch = new TimerTask()
{
@Override
public void run()
{
- response.addHeader("history","resume");
+ response.addHeader("history","dispatch");
async.dispatch();
}
};
synchronized (_timer)
{
- _timer.schedule(resume,resume2_after);
+ _timer.schedule(dispatch,dispatch2_after);
}
}
- else if (resume2_after==0)
+ else if (dispatch2_after==0)
{
response.addHeader("history","dispatch");
async.dispatch();
@@ -836,7 +926,7 @@ public class AsyncServletTest
}
else if(request.getDispatcherType()==DispatcherType.ERROR)
{
- response.getOutputStream().println("ERROR: "+request.getContextPath()+request.getServletPath()+request.getPathInfo());
+ response.getOutputStream().println("ERROR DISPATCH: "+request.getContextPath()+request.getServletPath()+request.getPathInfo());
}
else
{
@@ -858,12 +948,20 @@ public class AsyncServletTest
if (action!=null)
{
((HttpServletResponse)event.getSuppliedResponse()).addHeader("history",action);
- if ("dispatch".equals(action))
- event.getAsyncContext().dispatch();
- if ("complete".equals(action))
+
+ switch(action)
{
- event.getSuppliedResponse().getOutputStream().println("COMPLETED\n");
- event.getAsyncContext().complete();
+ case "dispatch":
+ event.getAsyncContext().dispatch();
+ break;
+
+ case "complete":
+ event.getSuppliedResponse().getOutputStream().println("COMPLETED\n");
+ event.getAsyncContext().complete();
+ break;
+
+ case "error":
+ throw new RuntimeException("error in onTimeout");
}
}
}
@@ -871,11 +969,30 @@ public class AsyncServletTest
@Override
public void onStartAsync(AsyncEvent event) throws IOException
{
+ ((HttpServletResponse)event.getSuppliedResponse()).addHeader("history","onStartAsync");
}
@Override
public void onError(AsyncEvent event) throws IOException
{
+ ((HttpServletResponse)event.getSuppliedResponse()).addHeader("history","onError");
+ String action=event.getSuppliedRequest().getParameter("error");
+ if (action!=null)
+ {
+ ((HttpServletResponse)event.getSuppliedResponse()).addHeader("history",action);
+
+ switch(action)
+ {
+ case "dispatch":
+ event.getAsyncContext().dispatch();
+ break;
+
+ case "complete":
+ event.getSuppliedResponse().getOutputStream().println("COMPLETED\n");
+ event.getAsyncContext().complete();
+ break;
+ }
+ }
}
@Override
@@ -889,7 +1006,7 @@ public class AsyncServletTest
{
@Override
public void log(Request request, Response response)
- {
+ {
int status = response.getCommittedMetaData().getStatus();
long written = response.getHttpChannel().getBytesWritten();
_log.add(status+" "+written+" "+request.getRequestURI());

Back to the top