Using try-with-resources on test

This commit is contained in:
Joakim Erdfelt 2013-06-20 06:25:51 -07:00
parent 89b22d0c90
commit feca821522
1 changed files with 29 additions and 25 deletions

View File

@ -24,6 +24,8 @@ import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket; import java.net.Socket;
import java.util.EnumSet; 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 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); socket.setSoTimeout(30000);
for (int i=loops;i-->0;) OutputStream out = socket.getOutputStream();
for (int i = loops; i-- > 0;)
{
out.write(loopRequests.getBytes("UTF-8"));
out.flush();
if (i > 0 && pauseBetweenLoops > 0)
{ {
socket.getOutputStream().write(loopRequests.getBytes("UTF-8"));
socket.getOutputStream().flush();
if (i>0 && pauseBetweenLoops>0)
Thread.sleep(pauseBetweenLoops); Thread.sleep(pauseBetweenLoops);
} }
if (pauseBeforeLast>0) }
if (pauseBeforeLast > 0)
{
Thread.sleep(pauseBeforeLast); Thread.sleep(pauseBeforeLast);
socket.getOutputStream().write(lastRequest.getBytes("UTF-8")); }
socket.getOutputStream().flush(); out.write(lastRequest.getBytes("UTF-8"));
out.flush();
InputStream in = socket.getInputStream();
String response;
if (loopRequests.contains("/unresponsive")) if (loopRequests.contains("/unresponsive"))
{ {
// don't read in anything, forcing the request to time out // don't read in anything, forcing the request to time out
Thread.sleep(_requestMaxTime * 2); Thread.sleep(_requestMaxTime * 2);
response = IO.toString(socket.getInputStream(),"UTF-8");
} }
else String response = IO.toString(in,"UTF-8");
{
response = IO.toString(socket.getInputStream(),"UTF-8");
}
socket.close();
return response; return response;
} }
}
private int count(String responses,String substring) private int count(String responses,String substring)
{ {