Skip to content

Commit

Permalink
Issue #4331 Async Write Complete
Browse files Browse the repository at this point in the history
Test harness to reproduce unready when closing/completing.

Signed-off-by: Greg Wilkins <gregw@webtide.com>
  • Loading branch information
gregw committed Dec 2, 2019
1 parent ec969f4 commit 4592441
Showing 1 changed file with 43 additions and 0 deletions.
Expand Up @@ -171,6 +171,8 @@ public static Stream<Arguments> tests()
tests.add(new Object[]{new HelloWorldHandler(), 200, "Hello world"});
tests.add(new Object[]{new SendErrorHandler(499, "Test async sendError"), 499, "Test async sendError"});
tests.add(new Object[]{new AsyncReadyCompleteHandler(), 200, AsyncReadyCompleteHandler.data});
tests.add(new Object[]{new AsyncWriteCompleteHandler(false), 200, AsyncWriteCompleteHandler.data});
tests.add(new Object[]{new AsyncWriteCompleteHandler(true), 200, AsyncWriteCompleteHandler.data});
return tests.stream().map(Arguments::of);
}

Expand Down Expand Up @@ -267,4 +269,45 @@ public void onError(Throwable t)
});
}
}

private static class AsyncWriteCompleteHandler extends AbstractHandler
{
static String data = "Now is the time for all good men to come to the aid of the party";

final boolean close;

AsyncWriteCompleteHandler(boolean close)
{
this.close = close;
}

@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
AsyncContext context = request.startAsync();
ServletOutputStream out = response.getOutputStream();
out.setWriteListener(new WriteListener() {
byte[] bytes = data.getBytes(StandardCharsets.ISO_8859_1);
@Override
public void onWritePossible() throws IOException
{
if (out.isReady())
{
response.setContentType("text/plain");
response.setContentLength(bytes.length);
out.write(bytes);
if (close)
out.close();
context.complete();
}
}

@Override
public void onError(Throwable t)
{
t.printStackTrace();
}
});
}
}
}

0 comments on commit 4592441

Please sign in to comment.