Test code cleanup.

Converted static fields to non-static, and removed unused code.
This commit is contained in:
Simone Bordet 2016-06-13 12:15:45 +02:00
parent b40c2ae66f
commit e803bf6c81
1 changed files with 202 additions and 280 deletions

View File

@ -18,8 +18,6 @@
package org.eclipse.jetty.server;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;
@ -45,6 +43,10 @@ import org.junit.Test;
public class LocalAsyncContextTest
{
private final Locker _completeLock = new Locker();
private Throwable __completed;
private Throwable __dispatched;
private final AtomicReference<Throwable> __completed1 = new AtomicReference<>();
protected Server _server;
protected SuspendHandler _handler;
protected Connector _connector;
@ -54,7 +56,7 @@ public class LocalAsyncContextTest
{
_server = new Server();
_connector = initConnector();
_server.setConnectors(new Connector[]{ _connector });
_server.addConnector(_connector);
SessionHandler session = new SessionHandler();
_handler = new SuspendHandler();
@ -93,13 +95,14 @@ public class LocalAsyncContextTest
response = process(null);
check(response, "TIMEOUT");
spinAssertEquals(1,()->{
spinAssertEquals(1, () ->
{
try (Locker.Lock lock = _completeLock.lock())
{
return __completed == null ? 0 : 1;
}
});
spinAssertEquals(1,()->{return __completed1.get()==null?0:1;});
spinAssertEquals(1, () -> __completed1.get() == null ? 0 : 1);
}
@Test
@ -148,7 +151,6 @@ public class LocalAsyncContextTest
_handler.setCompleteAfter(200);
response = process(null);
check(response, "STARTASYNC", "COMPLETED");
}
@Test
@ -173,7 +175,6 @@ public class LocalAsyncContextTest
_handler.setCompleteAfter(-1);
response = process("wibble");
check(response, "DISPATCHED");
}
@Test
@ -228,16 +229,16 @@ public class LocalAsyncContextTest
_handler.setResumeAfter2(200);
_handler.setCompleteAfter2(-1);
response = process(null);
check(response,"STARTASYNC","DISPATCHED","startasync","STARTASYNC","DISPATCHED");
check(response, "STARTASYNC", "DISPATCHED", "startasync", "STARTASYNC2", "DISPATCHED");
spinAssertEquals(1,()->{
spinAssertEquals(1, () ->
{
try (Locker.Lock lock = _completeLock.lock())
{
return __completed == null ? 0 : 1;
}
});
spinAssertEquals(0,()->{return __completed1.get()==null?0:1;});
spinAssertEquals(0, () -> __completed1.get() == null ? 0 : 1);
}
protected void check(String response, String... content)
@ -263,8 +264,7 @@ public class LocalAsyncContextTest
else
request += "Content-Length: " + content.length() + "\r\n" + "\r\n" + content;
String response=getResponse(request);
return response;
return getResponse(request);
}
protected String getResponse(String request) throws Exception
@ -275,7 +275,7 @@ public class LocalAsyncContextTest
return endp.takeOutputString();
}
private static class SuspendHandler extends HandlerWrapper
private class SuspendHandler extends HandlerWrapper
{
private int _read;
private long _suspendFor = -1;
@ -289,120 +289,52 @@ public class LocalAsyncContextTest
{
}
public int getRead()
{
return _read;
}
public void setRead(int read)
{
_read = read;
}
public long getSuspendFor()
{
return _suspendFor;
}
public void setSuspendFor(long suspendFor)
{
_suspendFor = suspendFor;
}
public long getResumeAfter()
{
return _resumeAfter;
}
public void setResumeAfter(long resumeAfter)
{
_resumeAfter = resumeAfter;
}
public long getCompleteAfter()
{
return _completeAfter;
}
public void setCompleteAfter(long completeAfter)
{
_completeAfter = completeAfter;
}
/* ------------------------------------------------------------ */
/** Get the suspendFor2.
* @return the suspendFor2
*/
public long getSuspendFor2()
{
return _suspendFor2;
}
/* ------------------------------------------------------------ */
/** Set the suspendFor2.
* @param suspendFor2 the suspendFor2 to set
*/
public void setSuspendFor2(long suspendFor2)
{
_suspendFor2 = suspendFor2;
}
/* ------------------------------------------------------------ */
/** Get the resumeAfter2.
* @return the resumeAfter2
*/
public long getResumeAfter2()
{
return _resumeAfter2;
}
/* ------------------------------------------------------------ */
/** Set the resumeAfter2.
* @param resumeAfter2 the resumeAfter2 to set
*/
public void setResumeAfter2(long resumeAfter2)
{
_resumeAfter2 = resumeAfter2;
}
/* ------------------------------------------------------------ */
/** Get the completeAfter2.
* @return the completeAfter2
*/
public long getCompleteAfter2()
{
return _completeAfter2;
}
/* ------------------------------------------------------------ */
/** Set the completeAfter2.
* @param completeAfter2 the completeAfter2 to set
*/
public void setCompleteAfter2(long completeAfter2)
{
_completeAfter2 = completeAfter2;
}
/* ------------------------------------------------------------ */
@Override
public void handle(String target, final Request baseRequest, final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException
{
try
{
if (DispatcherType.REQUEST.equals(baseRequest.getDispatcherType()))
{
if (_read > 0)
{
byte[] buf=new byte[_read];
request.getInputStream().read(buf);
int read = _read;
byte[] buf = new byte[read];
while (read > 0)
read -= request.getInputStream().read(buf);
}
else if (_read < 0)
{
@ -419,10 +351,10 @@ public class LocalAsyncContextTest
if (_suspendFor > 0)
asyncContext.setTimeout(_suspendFor);
if (_completeAfter > 0)
{
new Thread() {
new Thread()
{
@Override
public void run()
{
@ -451,7 +383,8 @@ public class LocalAsyncContextTest
if (_resumeAfter > 0)
{
new Thread() {
new Thread()
{
@Override
public void run()
{
@ -476,13 +409,9 @@ public class LocalAsyncContextTest
else
{
if (request.getAttribute("TIMEOUT") != null)
{
response.getOutputStream().println("TIMEOUT");
}
else
{
response.getOutputStream().println("DISPATCHED");
}
if (_suspendFor2 >= 0)
{
@ -494,7 +423,8 @@ public class LocalAsyncContextTest
if (_completeAfter2 > 0)
{
new Thread() {
new Thread()
{
@Override
public void run()
{
@ -523,7 +453,8 @@ public class LocalAsyncContextTest
if (_resumeAfter2 > 0)
{
new Thread() {
new Thread()
{
@Override
public void run()
{
@ -551,18 +482,9 @@ public class LocalAsyncContextTest
}
}
}
finally
{
}
}
}
static Locker _completeLock = new Locker();
static Throwable __completed;
static Throwable __dispatched;
static AtomicReference<Throwable> __completed1 = new AtomicReference<>();
private static AsyncListener __asyncListener = new AsyncListener()
private AsyncListener __asyncListener = new AsyncListener()
{
@Override
public void onComplete(AsyncEvent event) throws IOException
@ -635,7 +557,7 @@ public class LocalAsyncContextTest
}
};
private static AsyncListener __asyncListener1 = new AsyncListener()
private AsyncListener __asyncListener1 = new AsyncListener()
{
@Override
public void onComplete(AsyncEvent event) throws IOException
@ -672,7 +594,6 @@ public class LocalAsyncContextTest
public void onTimeout(AsyncEvent event) throws IOException
{
}
};
static <T> void spinAssertEquals(T expected, Supplier<T> actualSupplier)
@ -697,10 +618,11 @@ public class LocalAsyncContextTest
}
catch (InterruptedException e)
{
// Ignored
}
now = System.nanoTime();
}
assertEquals(expected,actual);
Assert.assertEquals(expected, actual);
}
}