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,36 +114,38 @@ 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);
|
||||
try (Socket socket = new Socket(_host,_port))
|
||||
{
|
||||
socket.setSoTimeout(30000);
|
||||
|
||||
OutputStream out = socket.getOutputStream();
|
||||
|
||||
for (int i = loops; i-- > 0;)
|
||||
{
|
||||
socket.getOutputStream().write(loopRequests.getBytes("UTF-8"));
|
||||
socket.getOutputStream().flush();
|
||||
out.write(loopRequests.getBytes("UTF-8"));
|
||||
out.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();
|
||||
}
|
||||
out.write(lastRequest.getBytes("UTF-8"));
|
||||
out.flush();
|
||||
|
||||
|
||||
String response;
|
||||
InputStream in = socket.getInputStream();
|
||||
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");
|
||||
}
|
||||
else
|
||||
{
|
||||
response = IO.toString(socket.getInputStream(),"UTF-8");
|
||||
}
|
||||
socket.close();
|
||||
String response = IO.toString(in,"UTF-8");
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
private int count(String responses,String substring)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue