Code cleanups.

This commit is contained in:
Simone Bordet 2016-07-15 18:09:44 +02:00
parent a3b5e7ebb7
commit a38e819907
1 changed files with 51 additions and 54 deletions

View File

@ -102,7 +102,6 @@ public class AsyncIOServletTest
throw new IllegalStateException();
}
scope.set(new Throwable());
}
@Override
public void exitScope(Context context, Request request)
@ -110,20 +109,18 @@ public class AsyncIOServletTest
if (scope.get()==null)
throw new IllegalStateException();
scope.set(null);
}
}
});
server.start();
}
private static void assertScope()
{
if (scope.get()==null)
Assert.fail("Not in scope");
}
@After
public void stopServer() throws Exception
{
@ -186,7 +183,7 @@ public class AsyncIOServletTest
Assert.assertThat("onError message",t.getMessage(),is(throwable.getMessage()));
latch.countDown();
response.setStatus(500);
asyncContext.complete();
}
});
@ -215,8 +212,8 @@ public class AsyncIOServletTest
line=in.readLine();
}
line=in.readLine();
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
assertTrue(latch.await(5, TimeUnit.SECONDS));
}
}
@ -303,7 +300,7 @@ public class AsyncIOServletTest
response.flushBuffer();
return;
}
final AsyncContext asyncContext = request.startAsync(request, response);
request.getInputStream().setReadListener(new ReadListener()
{
@ -414,11 +411,11 @@ public class AsyncIOServletTest
SimpleHttpParser parser = new SimpleHttpParser();
SimpleHttpResponse response = parser.readResponse(new BufferedReader(new InputStreamReader(client.getInputStream(), "UTF-8")));
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
assertTrue(latch.await(5, TimeUnit.SECONDS));
Assert.assertEquals("500", response.getCode());
}
}
@Test
public void testAsyncWriteClosed() throws Exception
@ -428,7 +425,7 @@ public class AsyncIOServletTest
for (int i=0;i<10;i++)
text=text+text;
final byte[] data = text.getBytes(StandardCharsets.ISO_8859_1);
startServer(new HttpServlet()
{
@Override
@ -436,7 +433,7 @@ public class AsyncIOServletTest
{
assertScope();
response.flushBuffer();
final AsyncContext async = request.startAsync();
final ServletOutputStream out = response.getOutputStream();
out.setWriteListener(new WriteListener()
@ -494,18 +491,18 @@ public class AsyncIOServletTest
line=in.readLine();
assertThat(line, containsString("discontent. How Now Brown Cow. The "));
}
if (!latch.await(5, TimeUnit.SECONDS))
Assert.fail();
}
@Test
public void testIsReadyAtEOF() throws Exception
{
String text = "TEST\n";
final byte[] data = text.getBytes(StandardCharsets.ISO_8859_1);
startServer(new HttpServlet()
{
@Override
@ -513,17 +510,17 @@ public class AsyncIOServletTest
{
assertScope();
response.flushBuffer();
final AsyncContext async = request.startAsync();
final ServletInputStream in = request.getInputStream();
final ServletOutputStream out = response.getOutputStream();
in.setReadListener(new ReadListener()
{
transient int _i=0;
transient boolean _minusOne=false;;
transient boolean _finished=false;;
@Override
public void onError(Throwable t)
{
@ -531,7 +528,7 @@ public class AsyncIOServletTest
t.printStackTrace();
async.complete();
}
@Override
public void onDataAvailable() throws IOException
{
@ -544,17 +541,17 @@ public class AsyncIOServletTest
else if (data[_i++]!=b)
throw new IllegalStateException();
}
if (in.isFinished())
_finished=true;
}
@Override
public void onAllDataRead() throws IOException
{
assertScope();
out.write(String.format("i=%d eof=%b finished=%b",_i,_minusOne,_finished).getBytes(StandardCharsets.ISO_8859_1));
async.complete();
async.complete();
}
});
}
@ -584,14 +581,14 @@ public class AsyncIOServletTest
assertThat(line, containsString("i="+data.length+" eof=true finished=true"));
}
}
@Test
public void testOnAllDataRead() throws Exception
{
String text = "X";
final byte[] data = text.getBytes(StandardCharsets.ISO_8859_1);
startServer(new HttpServlet()
{
@Override
@ -599,12 +596,12 @@ public class AsyncIOServletTest
{
assertScope();
response.flushBuffer();
final AsyncContext async = request.startAsync();
async.setTimeout(5000);
final ServletInputStream in = request.getInputStream();
final ServletOutputStream out = response.getOutputStream();
in.setReadListener(new ReadListener()
{
@Override
@ -614,7 +611,7 @@ public class AsyncIOServletTest
t.printStackTrace();
async.complete();
}
@Override
public void onDataAvailable() throws IOException
{
@ -636,13 +633,13 @@ public class AsyncIOServletTest
e.printStackTrace();
}
}
@Override
public void onAllDataRead() throws IOException
{
assertScope();
out.write("OK\n".getBytes(StandardCharsets.ISO_8859_1));
async.complete();
async.complete();
}
});
}
@ -674,13 +671,13 @@ public class AsyncIOServletTest
assertThat(line, containsString("OK"));
}
}
@Test
public void testOtherThreadOnAllDataRead() throws Exception
{
String text = "X";
final byte[] data = text.getBytes(StandardCharsets.ISO_8859_1);
startServer(new HttpServlet()
{
@Override
@ -688,15 +685,15 @@ public class AsyncIOServletTest
{
assertScope();
response.flushBuffer();
final AsyncContext async = request.startAsync();
async.setTimeout(500000);
final ServletInputStream in = request.getInputStream();
final ServletOutputStream out = response.getOutputStream();
if (request.getDispatcherType()==DispatcherType.ERROR)
throw new IllegalStateException();
in.setReadListener(new ReadListener()
{
@Override
@ -706,7 +703,7 @@ public class AsyncIOServletTest
t.printStackTrace();
async.complete();
}
@Override
public void onDataAvailable() throws IOException
{
@ -736,12 +733,12 @@ public class AsyncIOServletTest
}
});
}
@Override
public void onAllDataRead() throws IOException
{
out.write("OK\n".getBytes(StandardCharsets.ISO_8859_1));
async.complete();
async.complete();
}
});
}
@ -773,7 +770,7 @@ public class AsyncIOServletTest
assertThat(line, containsString("OK"));
}
}
@Test
public void testCompleteBeforeOnAllDataRead() throws Exception
@ -781,7 +778,7 @@ public class AsyncIOServletTest
String text = "XYZ";
final byte[] data = text.getBytes(StandardCharsets.ISO_8859_1);
final AtomicBoolean allDataRead = new AtomicBoolean(false);
startServer(new HttpServlet()
{
@Override
@ -789,11 +786,11 @@ public class AsyncIOServletTest
{
assertScope();
response.flushBuffer();
final AsyncContext async = request.startAsync();
final ServletInputStream in = request.getInputStream();
final ServletOutputStream out = response.getOutputStream();
in.setReadListener(new ReadListener()
{
@Override
@ -802,7 +799,7 @@ public class AsyncIOServletTest
assertScope();
t.printStackTrace();
}
@Override
public void onDataAvailable() throws IOException
{
@ -815,17 +812,17 @@ public class AsyncIOServletTest
out.write("OK\n".getBytes(StandardCharsets.ISO_8859_1));
async.complete();
return;
}
}
}
}
@Override
public void onAllDataRead() throws IOException
{
assertScope();
out.write("BAD!!!\n".getBytes(StandardCharsets.ISO_8859_1));
allDataRead.set(true);
throw new IllegalStateException();
throw new IllegalStateException();
}
});
}
@ -859,14 +856,14 @@ public class AsyncIOServletTest
Assert.assertFalse(allDataRead.get());
}
}
@Test
public void testEmptyAsyncRead() throws Exception
{
final AtomicBoolean oda = new AtomicBoolean();
final CountDownLatch latch = new CountDownLatch(1);
startServer(new HttpServlet()
{
@Override
@ -879,14 +876,14 @@ public class AsyncIOServletTest
request.getInputStream().setReadListener(new ReadListener()
{
@Override
public void onDataAvailable() throws IOException
public void onDataAvailable() throws IOException
{
assertScope();
oda.set(true);
}
@Override
public void onAllDataRead() throws IOException
public void onAllDataRead() throws IOException
{
assertScope();
asyncContext.complete();
@ -894,12 +891,12 @@ public class AsyncIOServletTest
}
@Override
public void onError(Throwable t)
public void onError(Throwable t)
{
assertScope();
t.printStackTrace();
asyncContext.complete();
}
}
});
}
});
@ -920,7 +917,7 @@ public class AsyncIOServletTest
// wait for onAllDataRead BEFORE closing client
latch.await();
}
// ODA not called at all!
Assert.assertFalse(oda.get());
}