From a8725224a82f5053057a89f4123ccfa1f622f61b Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Fri, 29 Nov 2019 11:18:58 +1100 Subject: [PATCH] Issue #4331 Async Write Complete test cleanups Signed-off-by: Greg Wilkins --- .../jetty/server/AsyncCompletionTest.java | 60 +++++++++---------- 1 file changed, 29 insertions(+), 31 deletions(-) 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 cb21ac04adae..8ceac625e271 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 @@ -65,14 +65,16 @@ */ public class AsyncCompletionTest extends HttpServerTestFixture { - private static final Exchanger X = new Exchanger<>(); - private static final AtomicBoolean COMPLETE = new AtomicBoolean(); + private static final Exchanger X = new Exchanger<>(); + private static final AtomicBoolean __complete = new AtomicBoolean(); + private static String __data = "Now is the time for all good men to come to the aid of the party"; - private static class DelayedCallback extends Callback.Nested + + private static class PendingCallback extends Callback.Nested { - private CompletableFuture _delay = new CompletableFuture<>(); + private CompletableFuture _pending = new CompletableFuture<>(); - public DelayedCallback(Callback callback) + public PendingCallback(Callback callback) { super(callback); } @@ -80,20 +82,20 @@ public DelayedCallback(Callback callback) @Override public void succeeded() { - _delay.complete(null); + _pending.complete(null); } @Override public void failed(Throwable x) { - _delay.completeExceptionally(x); + _pending.completeExceptionally(x); } public void proceed() { try { - _delay.get(10, TimeUnit.SECONDS); + _pending.get(10, TimeUnit.SECONDS); getCallback().succeeded(); } catch(Throwable th) @@ -108,7 +110,7 @@ public void proceed() @BeforeEach public void init() throws Exception { - COMPLETE.set(false); + __complete.set(false); startServer(new ServerConnector(_server, new HttpConnectionFactory() { @@ -137,7 +139,7 @@ public ExtendedEndPoint(SocketChannel channel, ManagedSelector selector, Selecti @Override public void write(Callback callback, ByteBuffer... buffers) throws IllegalStateException { - DelayedCallback delay = new DelayedCallback(callback); + PendingCallback delay = new PendingCallback(callback); super.write(delay, buffers); try { @@ -160,7 +162,7 @@ public ExtendedHttpConnection(HttpConfiguration config, Connector connector, End @Override public void onCompleted() { - COMPLETE.compareAndSet(false,true); + __complete.compareAndSet(false,true); super.onCompleted(); } } @@ -171,11 +173,11 @@ public static Stream tests() List tests = new ArrayList<>(); 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, false), 200, AsyncWriteCompleteHandler.data}); - tests.add(new Object[]{new AsyncWriteCompleteHandler(false, true), 200, AsyncWriteCompleteHandler.data}); - tests.add(new Object[]{new AsyncWriteCompleteHandler(true, false), 200, AsyncWriteCompleteHandler.data}); - tests.add(new Object[]{new AsyncWriteCompleteHandler(true, true), 200, AsyncWriteCompleteHandler.data}); + tests.add(new Object[]{new AsyncReadyCompleteHandler(), 200, __data}); + tests.add(new Object[]{new AsyncWriteCompleteHandler(false, false), 200, __data}); + tests.add(new Object[]{new AsyncWriteCompleteHandler(false, true), 200, __data}); + tests.add(new Object[]{new AsyncWriteCompleteHandler(true, false), 200, __data}); + tests.add(new Object[]{new AsyncWriteCompleteHandler(true, true), 200, __data}); return tests.stream().map(Arguments::of); } @@ -205,7 +207,7 @@ public void testAsyncCompletion(Handler handler, int status, String message) thr assertThat(_threadPool.getBusyThreads(), Matchers.greaterThan(base)); // Getting the Delayed callback will free the thread - DelayedCallback delay = X.exchange(null, 10, TimeUnit.SECONDS); + PendingCallback delay = X.exchange(null, 10, TimeUnit.SECONDS); // wait for threads to return to base level long end = System.nanoTime() + TimeUnit.SECONDS.toNanos(10); @@ -217,12 +219,12 @@ public void testAsyncCompletion(Handler handler, int status, String message) thr } // We are now asynchronously waiting! - assertThat(COMPLETE.get(), is(false)); + assertThat(__complete.get(), is(false)); // proceed with the completion delay.proceed(); - while(!COMPLETE.get()) + while(!__complete.get()) { if (System.nanoTime() > end) throw new TimeoutException(); @@ -233,15 +235,13 @@ public void testAsyncCompletion(Handler handler, int status, String message) thr private static class AsyncReadyCompleteHandler extends AbstractHandler { - static String data = "Now is the time for all good men to come to the aid of the party"; - @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); + byte[] bytes = __data.getBytes(StandardCharsets.ISO_8859_1); @Override public void onWritePossible() throws IOException { @@ -273,15 +273,13 @@ 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; - final boolean unReady; + final boolean _unReady; + final boolean _close; AsyncWriteCompleteHandler(boolean unReady, boolean close) { - this.unReady = unReady; - this.close = close; + _unReady = unReady; + _close = close; } @Override @@ -290,7 +288,7 @@ public void handle(String target, Request baseRequest, HttpServletRequest reques AsyncContext context = request.startAsync(); ServletOutputStream out = response.getOutputStream(); out.setWriteListener(new WriteListener() { - byte[] bytes = data.getBytes(StandardCharsets.ISO_8859_1); + byte[] bytes = __data.getBytes(StandardCharsets.ISO_8859_1); @Override public void onWritePossible() throws IOException { @@ -299,9 +297,9 @@ public void onWritePossible() throws IOException response.setContentType("text/plain"); response.setContentLength(bytes.length); out.write(bytes); - if (unReady) + if (_unReady) assertThat(out.isReady(),Matchers.is(false)); - if (close) + if (_close) out.close(); context.complete(); }