aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Becker2012-01-27 13:22:08 (EST)
committerGreg Wilkins2012-01-30 19:03:55 (EST)
commit0f95f6d5e393dcd13bbf4adad6c25b8761250c85 (patch)
treea48538bc6c9d161be59bf6b9f6fb5664da4a1da1
parentf02244fbf66e4778a316fa0c3d0e06555bf650e2 (diff)
downloadorg.eclipse.jetty.project-0f95f6d5e393dcd13bbf4adad6c25b8761250c85.zip
org.eclipse.jetty.project-0f95f6d5e393dcd13bbf4adad6c25b8761250c85.tar.gz
org.eclipse.jetty.project-0f95f6d5e393dcd13bbf4adad6c25b8761250c85.tar.bz2
369988: Add DoSFilter.RateTracker.isRateExceeded unit test
Signed-off-by: Greg Wilkins <gregw@webtide.com>
-rw-r--r--jetty-servlets/src/test/java/org/eclipse/jetty/servlets/AbstractDoSFilterTest.java5
-rw-r--r--jetty-servlets/src/test/java/org/eclipse/jetty/servlets/DoSFilterTest.java33
2 files changed, 37 insertions, 1 deletions
diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/AbstractDoSFilterTest.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/AbstractDoSFilterTest.java
index 3774a28..c2f4bcf 100644
--- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/AbstractDoSFilterTest.java
+++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/AbstractDoSFilterTest.java
@@ -169,6 +169,7 @@ public abstract class AbstractDoSFilterTest
{
Thread other = new Thread()
{
+ @Override
public void run()
{
try
@@ -204,6 +205,7 @@ public abstract class AbstractDoSFilterTest
{
Thread other = new Thread()
{
+ @Override
public void run()
{
try
@@ -284,7 +286,7 @@ public abstract class AbstractDoSFilterTest
assertEquals(11,count(responses,"HTTP/1.1 200 OK"));
int delayedRequests = count(responses,"DoSFilter: delayed");
- assertTrue(delayedRequests >= 2 && delayedRequests <= 3);
+ assertTrue("delayedRequests: " + delayedRequests + " is not between 2 and 3",delayedRequests >= 2 && delayedRequests <= 3);
}
@Test
@@ -302,6 +304,7 @@ public abstract class AbstractDoSFilterTest
public static class TestServlet extends HttpServlet implements Servlet
{
+ @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
if (request.getParameter("session")!=null)
diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/DoSFilterTest.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/DoSFilterTest.java
index a68c11f..a214ce3 100644
--- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/DoSFilterTest.java
+++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/DoSFilterTest.java
@@ -14,12 +14,17 @@
package org.eclipse.jetty.servlets;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import org.eclipse.jetty.servlets.DoSFilter.RateTracker;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.junit.BeforeClass;
+import org.junit.Test;
public class DoSFilterTest extends AbstractDoSFilterTest
{
@@ -33,6 +38,7 @@ public class DoSFilterTest extends AbstractDoSFilterTest
public static class DoSFilter2 extends DoSFilter
{
+ @Override
public void closeConnection(HttpServletRequest request, HttpServletResponse response, Thread thread)
{
try {
@@ -45,4 +51,31 @@ public class DoSFilterTest extends AbstractDoSFilterTest
}
}
}
+
+ @Test
+ public void isRateExceededTest() throws InterruptedException
+ {
+ DoSFilter doSFilter = new DoSFilter();
+
+ boolean exceeded = hitRateTracker(doSFilter,0);
+ assertTrue("Last hit should have exceeded",exceeded);
+
+ int sleep = 250;
+ exceeded = hitRateTracker(doSFilter,sleep);
+ assertFalse("Should not exceed as we sleep 300s for each hit and thus do less than 4 hits/s",exceeded);
+ }
+
+ private boolean hitRateTracker(DoSFilter doSFilter, int sleep) throws InterruptedException
+ {
+ boolean exceeded = false;
+ RateTracker rateTracker = doSFilter.new RateTracker("test2",0,4);
+
+ for (int i = 0; i < 5; i++)
+ {
+ Thread.sleep(sleep);
+ if (rateTracker.isRateExceeded(System.currentTimeMillis()))
+ exceeded = true;
+ }
+ return exceeded;
+ }
}