From 529ea5eb8fd1fd982394e74687b77e9e38a93e7b Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Fri, 29 Nov 2019 11:10:05 +1100 Subject: [PATCH] Issue #4331 Async Write Complete Test harness to reproduce unready when closing/completing. Signed-off-by: Greg Wilkins --- .../jetty/server/AsyncCompletionTest.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/AsyncCompletionTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/AsyncCompletionTest.java index bdcefa7625bc..e6d85768d1c8 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/AsyncCompletionTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/AsyncCompletionTest.java @@ -172,6 +172,8 @@ public static Stream 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); } @@ -266,4 +268,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(); + } + }); + } + } }