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