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; package org.eclipse.jetty.server;
import static org.junit.Assert.assertEquals;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -45,6 +43,10 @@ import org.junit.Test;
public class LocalAsyncContextTest 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 Server _server;
protected SuspendHandler _handler; protected SuspendHandler _handler;
protected Connector _connector; protected Connector _connector;
@ -54,7 +56,7 @@ public class LocalAsyncContextTest
{ {
_server = new Server(); _server = new Server();
_connector = initConnector(); _connector = initConnector();
_server.setConnectors(new Connector[]{ _connector }); _server.addConnector(_connector);
SessionHandler session = new SessionHandler(); SessionHandler session = new SessionHandler();
_handler = new SuspendHandler(); _handler = new SuspendHandler();
@ -63,9 +65,9 @@ public class LocalAsyncContextTest
_server.setHandler(session); _server.setHandler(session);
_server.start(); _server.start();
try(Locker.Lock lock = _completeLock.lock()) try (Locker.Lock lock = _completeLock.lock())
{ {
__completed=null; __completed = null;
} }
__completed1.set(null); __completed1.set(null);
} }
@ -90,16 +92,17 @@ public class LocalAsyncContextTest
_handler.setSuspendFor(1000); _handler.setSuspendFor(1000);
_handler.setResumeAfter(-1); _handler.setResumeAfter(-1);
_handler.setCompleteAfter(-1); _handler.setCompleteAfter(-1);
response=process(null); response = process(null);
check(response,"TIMEOUT"); check(response, "TIMEOUT");
spinAssertEquals(1,()->{ spinAssertEquals(1, () ->
try(Locker.Lock lock = _completeLock.lock())
{ {
return __completed==null?0: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 @Test
@ -110,8 +113,8 @@ public class LocalAsyncContextTest
_handler.setSuspendFor(10000); _handler.setSuspendFor(10000);
_handler.setResumeAfter(0); _handler.setResumeAfter(0);
_handler.setCompleteAfter(-1); _handler.setCompleteAfter(-1);
response=process(null); response = process(null);
check(response,"STARTASYNC","DISPATCHED"); check(response, "STARTASYNC", "DISPATCHED");
} }
@Test @Test
@ -122,8 +125,8 @@ public class LocalAsyncContextTest
_handler.setSuspendFor(10000); _handler.setSuspendFor(10000);
_handler.setResumeAfter(100); _handler.setResumeAfter(100);
_handler.setCompleteAfter(-1); _handler.setCompleteAfter(-1);
response=process(null); response = process(null);
check(response,"STARTASYNC","DISPATCHED"); check(response, "STARTASYNC", "DISPATCHED");
} }
@Test @Test
@ -134,8 +137,8 @@ public class LocalAsyncContextTest
_handler.setSuspendFor(10000); _handler.setSuspendFor(10000);
_handler.setResumeAfter(-1); _handler.setResumeAfter(-1);
_handler.setCompleteAfter(0); _handler.setCompleteAfter(0);
response=process(null); response = process(null);
check(response,"STARTASYNC","COMPLETED"); check(response, "STARTASYNC", "COMPLETED");
} }
@Test @Test
@ -146,9 +149,8 @@ public class LocalAsyncContextTest
_handler.setSuspendFor(10000); _handler.setSuspendFor(10000);
_handler.setResumeAfter(-1); _handler.setResumeAfter(-1);
_handler.setCompleteAfter(200); _handler.setCompleteAfter(200);
response=process(null); response = process(null);
check(response,"STARTASYNC","COMPLETED"); check(response, "STARTASYNC", "COMPLETED");
} }
@Test @Test
@ -159,8 +161,8 @@ public class LocalAsyncContextTest
_handler.setRead(-1); _handler.setRead(-1);
_handler.setResumeAfter(0); _handler.setResumeAfter(0);
_handler.setCompleteAfter(-1); _handler.setCompleteAfter(-1);
response=process("wibble"); response = process("wibble");
check(response,"STARTASYNC","DISPATCHED"); check(response, "STARTASYNC", "DISPATCHED");
} }
@Test @Test
@ -171,9 +173,8 @@ public class LocalAsyncContextTest
_handler.setRead(-1); _handler.setRead(-1);
_handler.setResumeAfter(100); _handler.setResumeAfter(100);
_handler.setCompleteAfter(-1); _handler.setCompleteAfter(-1);
response=process("wibble"); response = process("wibble");
check(response,"DISPATCHED"); check(response, "DISPATCHED");
} }
@Test @Test
@ -184,35 +185,35 @@ public class LocalAsyncContextTest
_handler.setRead(-1); _handler.setRead(-1);
_handler.setResumeAfter(-1); _handler.setResumeAfter(-1);
_handler.setCompleteAfter(0); _handler.setCompleteAfter(0);
response=process("wibble"); response = process("wibble");
check(response,"COMPLETED"); check(response, "COMPLETED");
_handler.setResumeAfter(-1); _handler.setResumeAfter(-1);
_handler.setCompleteAfter(100); _handler.setCompleteAfter(100);
response=process("wibble"); response = process("wibble");
check(response,"COMPLETED"); check(response, "COMPLETED");
_handler.setRead(6); _handler.setRead(6);
_handler.setResumeAfter(0); _handler.setResumeAfter(0);
_handler.setCompleteAfter(-1); _handler.setCompleteAfter(-1);
response=process("wibble"); response = process("wibble");
check(response,"DISPATCHED"); check(response, "DISPATCHED");
_handler.setResumeAfter(100); _handler.setResumeAfter(100);
_handler.setCompleteAfter(-1); _handler.setCompleteAfter(-1);
response=process("wibble"); response = process("wibble");
check(response,"DISPATCHED"); check(response, "DISPATCHED");
_handler.setResumeAfter(-1); _handler.setResumeAfter(-1);
_handler.setCompleteAfter(0); _handler.setCompleteAfter(0);
response=process("wibble"); response = process("wibble");
check(response,"COMPLETED"); check(response, "COMPLETED");
_handler.setResumeAfter(-1); _handler.setResumeAfter(-1);
_handler.setCompleteAfter(100); _handler.setCompleteAfter(100);
response=process("wibble"); response = process("wibble");
check(response,"COMPLETED"); check(response, "COMPLETED");
} }
@Test @Test
@ -227,202 +228,133 @@ public class LocalAsyncContextTest
_handler.setSuspendFor2(1000); _handler.setSuspendFor2(1000);
_handler.setResumeAfter2(200); _handler.setResumeAfter2(200);
_handler.setCompleteAfter2(-1); _handler.setCompleteAfter2(-1);
response=process(null); 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; 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) protected void check(String response, String... content)
{ {
Assert.assertThat(response,Matchers.startsWith("HTTP/1.1 200 OK")); Assert.assertThat(response, Matchers.startsWith("HTTP/1.1 200 OK"));
int i=0; int i = 0;
for (String m:content) for (String m : content)
{ {
Assert.assertThat(response,Matchers.containsString(m)); Assert.assertThat(response, Matchers.containsString(m));
i=response.indexOf(m,i); i = response.indexOf(m, i);
i+=m.length(); i += m.length();
} }
} }
private synchronized String process(String content) throws Exception private synchronized String process(String content) throws Exception
{ {
String request = "GET / HTTP/1.1\r\n" + String request = "GET / HTTP/1.1\r\n" +
"Host: localhost\r\n"+ "Host: localhost\r\n" +
"Connection: close\r\n"; "Connection: close\r\n";
if (content==null) if (content == null)
request+="\r\n"; request += "\r\n";
else else
request+="Content-Length: "+content.length()+"\r\n" +"\r\n" + content; request += "Content-Length: " + content.length() + "\r\n" + "\r\n" + content;
String response=getResponse(request); return getResponse(request);
return response;
} }
protected String getResponse(String request) throws Exception protected String getResponse(String request) throws Exception
{ {
LocalConnector connector=(LocalConnector)_connector; LocalConnector connector = (LocalConnector)_connector;
LocalConnector.LocalEndPoint endp = connector.executeRequest(request); LocalConnector.LocalEndPoint endp = connector.executeRequest(request);
endp.waitUntilClosed(); endp.waitUntilClosed();
return endp.takeOutputString(); return endp.takeOutputString();
} }
private static class SuspendHandler extends HandlerWrapper private class SuspendHandler extends HandlerWrapper
{ {
private int _read; private int _read;
private long _suspendFor=-1; private long _suspendFor = -1;
private long _resumeAfter=-1; private long _resumeAfter = -1;
private long _completeAfter=-1; private long _completeAfter = -1;
private long _suspendFor2=-1; private long _suspendFor2 = -1;
private long _resumeAfter2=-1; private long _resumeAfter2 = -1;
private long _completeAfter2=-1; private long _completeAfter2 = -1;
public SuspendHandler() public SuspendHandler()
{ {
} }
public int getRead()
{
return _read;
}
public void setRead(int read) public void setRead(int read)
{ {
_read = read; _read = read;
} }
public long getSuspendFor()
{
return _suspendFor;
}
public void setSuspendFor(long suspendFor) public void setSuspendFor(long suspendFor)
{ {
_suspendFor = suspendFor; _suspendFor = suspendFor;
} }
public long getResumeAfter()
{
return _resumeAfter;
}
public void setResumeAfter(long resumeAfter) public void setResumeAfter(long resumeAfter)
{ {
_resumeAfter = resumeAfter; _resumeAfter = resumeAfter;
} }
public long getCompleteAfter()
{
return _completeAfter;
}
public void setCompleteAfter(long completeAfter) public void setCompleteAfter(long completeAfter)
{ {
_completeAfter = 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) public void setSuspendFor2(long suspendFor2)
{ {
_suspendFor2 = 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) public void setResumeAfter2(long resumeAfter2)
{ {
_resumeAfter2 = 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) public void setCompleteAfter2(long completeAfter2)
{ {
_completeAfter2 = completeAfter2; _completeAfter2 = completeAfter2;
} }
/* ------------------------------------------------------------ */
@Override @Override
public void handle(String target, final Request baseRequest, final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException 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 (DispatcherType.REQUEST.equals(baseRequest.getDispatcherType()))
{ {
if (_read>0) if (_read > 0)
{ {
byte[] buf=new byte[_read]; int read = _read;
request.getInputStream().read(buf); byte[] buf = new byte[read];
while (read > 0)
read -= request.getInputStream().read(buf);
} }
else if (_read<0) else if (_read < 0)
{ {
InputStream in = request.getInputStream(); InputStream in = request.getInputStream();
int b=in.read(); int b = in.read();
while(b!=-1) while (b != -1)
b=in.read(); b = in.read();
} }
final AsyncContext asyncContext = baseRequest.startAsync(); final AsyncContext asyncContext = baseRequest.startAsync();
response.getOutputStream().println("STARTASYNC"); response.getOutputStream().println("STARTASYNC");
asyncContext.addListener(__asyncListener); asyncContext.addListener(__asyncListener);
asyncContext.addListener(__asyncListener1); asyncContext.addListener(__asyncListener1);
if (_suspendFor>0) if (_suspendFor > 0)
asyncContext.setTimeout(_suspendFor); asyncContext.setTimeout(_suspendFor);
if (_completeAfter > 0)
if (_completeAfter>0) {
new Thread()
{ {
new Thread() {
@Override @Override
public void run() public void run()
{ {
@ -434,14 +366,14 @@ public class LocalAsyncContextTest
baseRequest.setHandled(true); baseRequest.setHandled(true);
asyncContext.complete(); asyncContext.complete();
} }
catch(Exception e) catch (Exception e)
{ {
e.printStackTrace(); e.printStackTrace();
} }
} }
}.start(); }.start();
} }
else if (_completeAfter==0) else if (_completeAfter == 0)
{ {
response.getOutputStream().println("COMPLETED"); response.getOutputStream().println("COMPLETED");
response.setStatus(200); response.setStatus(200);
@ -449,52 +381,50 @@ public class LocalAsyncContextTest
asyncContext.complete(); asyncContext.complete();
} }
if (_resumeAfter>0) if (_resumeAfter > 0)
{
new Thread()
{ {
new Thread() {
@Override @Override
public void run() public void run()
{ {
try try
{ {
Thread.sleep(_resumeAfter); Thread.sleep(_resumeAfter);
if(((HttpServletRequest)asyncContext.getRequest()).getSession(true).getId()!=null) if (((HttpServletRequest)asyncContext.getRequest()).getSession(true).getId() != null)
asyncContext.dispatch(); asyncContext.dispatch();
} }
catch(Exception e) catch (Exception e)
{ {
e.printStackTrace(); e.printStackTrace();
} }
} }
}.start(); }.start();
} }
else if (_resumeAfter==0) else if (_resumeAfter == 0)
{ {
asyncContext.dispatch(); asyncContext.dispatch();
} }
} }
else else
{ {
if (request.getAttribute("TIMEOUT")!=null) if (request.getAttribute("TIMEOUT") != null)
{
response.getOutputStream().println("TIMEOUT"); response.getOutputStream().println("TIMEOUT");
}
else else
{
response.getOutputStream().println("DISPATCHED"); response.getOutputStream().println("DISPATCHED");
}
if (_suspendFor2>=0) if (_suspendFor2 >= 0)
{ {
final AsyncContext asyncContext = baseRequest.startAsync(); final AsyncContext asyncContext = baseRequest.startAsync();
response.getOutputStream().println("STARTASYNC2"); response.getOutputStream().println("STARTASYNC2");
if (_suspendFor2>0) if (_suspendFor2 > 0)
asyncContext.setTimeout(_suspendFor2); asyncContext.setTimeout(_suspendFor2);
_suspendFor2=-1; _suspendFor2 = -1;
if (_completeAfter2>0) if (_completeAfter2 > 0)
{
new Thread()
{ {
new Thread() {
@Override @Override
public void run() public void run()
{ {
@ -506,14 +436,14 @@ public class LocalAsyncContextTest
baseRequest.setHandled(true); baseRequest.setHandled(true);
asyncContext.complete(); asyncContext.complete();
} }
catch(Exception e) catch (Exception e)
{ {
e.printStackTrace(); e.printStackTrace();
} }
} }
}.start(); }.start();
} }
else if (_completeAfter2==0) else if (_completeAfter2 == 0)
{ {
response.getOutputStream().println("COMPLETED2"); response.getOutputStream().println("COMPLETED2");
response.setStatus(200); response.setStatus(200);
@ -521,9 +451,10 @@ public class LocalAsyncContextTest
asyncContext.complete(); asyncContext.complete();
} }
if (_resumeAfter2>0) if (_resumeAfter2 > 0)
{
new Thread()
{ {
new Thread() {
@Override @Override
public void run() public void run()
{ {
@ -532,14 +463,14 @@ public class LocalAsyncContextTest
Thread.sleep(_resumeAfter2); Thread.sleep(_resumeAfter2);
asyncContext.dispatch(); asyncContext.dispatch();
} }
catch(Exception e) catch (Exception e)
{ {
e.printStackTrace(); e.printStackTrace();
} }
} }
}.start(); }.start();
} }
else if (_resumeAfter2==0) else if (_resumeAfter2 == 0)
{ {
asyncContext.dispatch(); asyncContext.dispatch();
} }
@ -551,40 +482,31 @@ public class LocalAsyncContextTest
} }
} }
} }
finally
{
}
}
} }
static Locker _completeLock = new Locker(); private AsyncListener __asyncListener = new AsyncListener()
static Throwable __completed;
static Throwable __dispatched;
static AtomicReference<Throwable> __completed1 = new AtomicReference<>();
private static AsyncListener __asyncListener = new AsyncListener()
{ {
@Override @Override
public void onComplete(AsyncEvent event) throws IOException public void onComplete(AsyncEvent event) throws IOException
{ {
Throwable complete = new Throwable(); Throwable complete = new Throwable();
Throwable dispatched = HttpChannel.getDispatchedFrom(); Throwable dispatched = HttpChannel.getDispatchedFrom();
try(Locker.Lock lock = _completeLock.lock()) try (Locker.Lock lock = _completeLock.lock())
{ {
if (__completed==null) if (__completed == null)
{ {
__completed=complete; __completed = complete;
__dispatched=dispatched; __dispatched = dispatched;
} }
else else
{ {
System.err.println("First onCompleted dispatched from:"); System.err.println("First onCompleted dispatched from:");
if (__dispatched!=null) if (__dispatched != null)
__dispatched.printStackTrace(); __dispatched.printStackTrace();
System.err.println("First onCompleted:"); System.err.println("First onCompleted:");
__completed.printStackTrace(); __completed.printStackTrace();
System.err.println("Second onCompleted dispatched from:"); System.err.println("Second onCompleted dispatched from:");
if (dispatched!=null) if (dispatched != null)
dispatched.printStackTrace(); dispatched.printStackTrace();
complete.printStackTrace(); complete.printStackTrace();
throw new IllegalStateException(); throw new IllegalStateException();
@ -597,22 +519,22 @@ public class LocalAsyncContextTest
{ {
Throwable complete = new Throwable(); Throwable complete = new Throwable();
Throwable dispatched = HttpChannel.getDispatchedFrom(); Throwable dispatched = HttpChannel.getDispatchedFrom();
try(Locker.Lock lock = _completeLock.lock()) try (Locker.Lock lock = _completeLock.lock())
{ {
if (__completed==null) if (__completed == null)
{ {
__completed=complete; __completed = complete;
__dispatched=dispatched; __dispatched = dispatched;
} }
else else
{ {
System.err.println("First onCompleted dispatched from:"); System.err.println("First onCompleted dispatched from:");
if (__dispatched!=null) if (__dispatched != null)
__dispatched.printStackTrace(); __dispatched.printStackTrace();
System.err.println("First onCompleted:"); System.err.println("First onCompleted:");
__completed.printStackTrace(); __completed.printStackTrace();
System.err.println("Second onCompleted dispatched from:"); System.err.println("Second onCompleted dispatched from:");
if (dispatched!=null) if (dispatched != null)
dispatched.printStackTrace(); dispatched.printStackTrace();
complete.printStackTrace(); complete.printStackTrace();
throw new IllegalStateException(); throw new IllegalStateException();
@ -630,18 +552,18 @@ public class LocalAsyncContextTest
@Override @Override
public void onTimeout(AsyncEvent event) throws IOException public void onTimeout(AsyncEvent event) throws IOException
{ {
event.getSuppliedRequest().setAttribute("TIMEOUT",Boolean.TRUE); event.getSuppliedRequest().setAttribute("TIMEOUT", Boolean.TRUE);
event.getAsyncContext().dispatch(); event.getAsyncContext().dispatch();
} }
}; };
private static AsyncListener __asyncListener1 = new AsyncListener() private AsyncListener __asyncListener1 = new AsyncListener()
{ {
@Override @Override
public void onComplete(AsyncEvent event) throws IOException public void onComplete(AsyncEvent event) throws IOException
{ {
Throwable complete = new Throwable(); Throwable complete = new Throwable();
if (!__completed1.compareAndSet(null,complete)) if (!__completed1.compareAndSet(null, complete))
{ {
__completed1.get().printStackTrace(); __completed1.get().printStackTrace();
complete.printStackTrace(); complete.printStackTrace();
@ -654,7 +576,7 @@ public class LocalAsyncContextTest
public void onError(AsyncEvent event) throws IOException public void onError(AsyncEvent event) throws IOException
{ {
Throwable complete = new Throwable(); Throwable complete = new Throwable();
if (!__completed1.compareAndSet(null,complete)) if (!__completed1.compareAndSet(null, complete))
{ {
__completed1.get().printStackTrace(); __completed1.get().printStackTrace();
complete.printStackTrace(); complete.printStackTrace();
@ -672,24 +594,23 @@ public class LocalAsyncContextTest
public void onTimeout(AsyncEvent event) throws IOException public void onTimeout(AsyncEvent event) throws IOException
{ {
} }
}; };
static <T> void spinAssertEquals(T expected, Supplier<T> actualSupplier) static <T> void spinAssertEquals(T expected, Supplier<T> actualSupplier)
{ {
spinAssertEquals(expected,actualSupplier,10,TimeUnit.SECONDS); spinAssertEquals(expected, actualSupplier, 10, TimeUnit.SECONDS);
} }
static <T> void spinAssertEquals(T expected, Supplier<T> actualSupplier, long waitFor, TimeUnit units) static <T> void spinAssertEquals(T expected, Supplier<T> actualSupplier, long waitFor, TimeUnit units)
{ {
long now = System.nanoTime(); long now = System.nanoTime();
long end = now+units.toNanos(waitFor); long end = now + units.toNanos(waitFor);
T actual=null; T actual = null;
while(now < end) while (now < end)
{ {
actual=actualSupplier.get(); actual = actualSupplier.get();
if (actual==null && expected==null || if (actual == null && expected == null ||
actual!=null && actual.equals(expected)) actual != null && actual.equals(expected))
break; break;
try try
{ {
@ -697,10 +618,11 @@ public class LocalAsyncContextTest
} }
catch (InterruptedException e) catch (InterruptedException e)
{ {
// Ignored
} }
now = System.nanoTime(); now = System.nanoTime();
} }
assertEquals(expected,actual); Assert.assertEquals(expected, actual);
} }
} }