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 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)