fixed bad unit test

This commit is contained in:
Greg Wilkins 2016-05-19 17:38:56 +10:00
parent e1489340fe
commit 74110d445e
1 changed files with 20 additions and 1 deletions

View File

@ -19,7 +19,9 @@
package org.eclipse.jetty.servlet; package org.eclipse.jetty.servlet;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
@ -39,18 +41,23 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public class PostServletTest public class PostServletTest
{ {
private static final Logger LOG = Log.getLogger(PostServletTest.class); private static final Logger LOG = Log.getLogger(PostServletTest.class);
private static final AtomicBoolean posted = new AtomicBoolean(false);
private static final AtomicReference<Throwable> ex0=new AtomicReference<>(); private static final AtomicReference<Throwable> ex0=new AtomicReference<>();
private static final AtomicReference<Throwable> ex1=new AtomicReference<>(); private static final AtomicReference<Throwable> ex1=new AtomicReference<>();
private static CountDownLatch complete;
public static class BasicReadPostServlet extends HttpServlet public static class BasicReadPostServlet extends HttpServlet
{ {
protected void doPost(HttpServletRequest request, HttpServletResponse response) protected void doPost(HttpServletRequest request, HttpServletResponse response)
{ {
posted.set(true);
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
try try
{ {
@ -76,6 +83,10 @@ public class PostServletTest
LOG.warn(e1.toString()); LOG.warn(e1.toString());
} }
} }
finally
{
complete.countDown();
}
} }
} }
@ -85,8 +96,10 @@ public class PostServletTest
@Before @Before
public void startServer() throws Exception public void startServer() throws Exception
{ {
complete=new CountDownLatch(1);
ex0.set(null); ex0.set(null);
ex1.set(null); ex1.set(null);
posted.set(false);
server = new Server(); server = new Server();
connector = new LocalConnector(server); connector = new LocalConnector(server);
server.addConnector(connector); server.addConnector(connector);
@ -154,6 +167,7 @@ public class PostServletTest
String resp = connector.getResponses(req.toString()); String resp = connector.getResponses(req.toString());
assertThat(resp,is("")); // Aborted before response committed assertThat(resp,is("")); // Aborted before response committed
} }
assertTrue(complete.await(5,TimeUnit.SECONDS));
assertThat(ex0.get(),not(nullValue())); assertThat(ex0.get(),not(nullValue()));
assertThat(ex1.get(),not(nullValue())); assertThat(ex1.get(),not(nullValue()));
} }
@ -171,6 +185,8 @@ public class PostServletTest
{ {
LocalConnector.LocalEndPoint endp=connector.executeRequest(req.toString()); LocalConnector.LocalEndPoint endp=connector.executeRequest(req.toString());
Thread.sleep(1000); Thread.sleep(1000);
assertFalse(posted.get());
req.setLength(0); req.setLength(0);
// intentionally bad (not a valid chunked char here) // intentionally bad (not a valid chunked char here)
for (int i=1024;i-->0;) for (int i=1024;i-->0;)
@ -211,7 +227,9 @@ public class PostServletTest
LocalConnector.LocalEndPoint endp=connector.executeRequest(req.toString()); LocalConnector.LocalEndPoint endp=connector.executeRequest(req.toString());
req.setLength(0); req.setLength(0);
Thread.sleep(1000); while(!posted.get())
Thread.sleep(100);
Thread.sleep(100);
req.append("x\r\n"); req.append("x\r\n");
req.append("World\n"); req.append("World\n");
req.append("\r\n"); req.append("\r\n");
@ -224,6 +242,7 @@ public class PostServletTest
assertThat("resp", resp, containsString("HTTP/1.1 200 ")); assertThat("resp", resp, containsString("HTTP/1.1 200 "));
assertThat("resp", resp, not(containsString("\r\n0\r\n"))); // aborted assertThat("resp", resp, not(containsString("\r\n0\r\n"))); // aborted
} }
assertTrue(complete.await(5,TimeUnit.SECONDS));
assertThat(ex0.get(),not(nullValue())); assertThat(ex0.get(),not(nullValue()));
assertThat(ex1.get(),not(nullValue())); assertThat(ex1.get(),not(nullValue()));
} }