369988: Add DoSFilter.RateTracker.isRateExceeded unit test
Signed-off-by: Greg Wilkins <gregw@webtide.com>
This commit is contained in:
parent
f02244fbf6
commit
0f95f6d5e3
|
@ -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)
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue