Using try-with-resources on test
This commit is contained in:
parent
89b22d0c90
commit
feca821522
|
@ -24,6 +24,8 @@ import static org.junit.Assert.assertThat;
|
|||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
import java.util.EnumSet;
|
||||
|
||||
|
@ -112,35 +114,37 @@ public abstract class AbstractDoSFilterTest
|
|||
|
||||
private String doRequests(String loopRequests, int loops, long pauseBetweenLoops, long pauseBeforeLast, String lastRequest) throws Exception
|
||||
{
|
||||
Socket socket = new Socket(_host, _port);
|
||||
socket.setSoTimeout(30000);
|
||||
|
||||
for (int i=loops;i-->0;)
|
||||
try (Socket socket = new Socket(_host,_port))
|
||||
{
|
||||
socket.getOutputStream().write(loopRequests.getBytes("UTF-8"));
|
||||
socket.getOutputStream().flush();
|
||||
if (i>0 && pauseBetweenLoops>0)
|
||||
Thread.sleep(pauseBetweenLoops);
|
||||
}
|
||||
if (pauseBeforeLast>0)
|
||||
Thread.sleep(pauseBeforeLast);
|
||||
socket.getOutputStream().write(lastRequest.getBytes("UTF-8"));
|
||||
socket.getOutputStream().flush();
|
||||
socket.setSoTimeout(30000);
|
||||
|
||||
OutputStream out = socket.getOutputStream();
|
||||
|
||||
String response;
|
||||
if (loopRequests.contains("/unresponsive"))
|
||||
{
|
||||
// don't read in anything, forcing the request to time out
|
||||
Thread.sleep(_requestMaxTime * 2);
|
||||
response = IO.toString(socket.getInputStream(),"UTF-8");
|
||||
for (int i = loops; i-- > 0;)
|
||||
{
|
||||
out.write(loopRequests.getBytes("UTF-8"));
|
||||
out.flush();
|
||||
if (i > 0 && pauseBetweenLoops > 0)
|
||||
{
|
||||
Thread.sleep(pauseBetweenLoops);
|
||||
}
|
||||
}
|
||||
if (pauseBeforeLast > 0)
|
||||
{
|
||||
Thread.sleep(pauseBeforeLast);
|
||||
}
|
||||
out.write(lastRequest.getBytes("UTF-8"));
|
||||
out.flush();
|
||||
|
||||
InputStream in = socket.getInputStream();
|
||||
if (loopRequests.contains("/unresponsive"))
|
||||
{
|
||||
// don't read in anything, forcing the request to time out
|
||||
Thread.sleep(_requestMaxTime * 2);
|
||||
}
|
||||
String response = IO.toString(in,"UTF-8");
|
||||
return response;
|
||||
}
|
||||
else
|
||||
{
|
||||
response = IO.toString(socket.getInputStream(),"UTF-8");
|
||||
}
|
||||
socket.close();
|
||||
return response;
|
||||
}
|
||||
|
||||
private int count(String responses,String substring)
|
||||
|
|
Loading…
Reference in New Issue