Skip to content

Commit

Permalink
Issue #4331 Async Write Complete
Browse files Browse the repository at this point in the history
test cleanups

Signed-off-by: Greg Wilkins <gregw@webtide.com>
  • Loading branch information
gregw committed Dec 3, 2019
1 parent be28e87 commit d686f58
Showing 1 changed file with 29 additions and 31 deletions.
Expand Up @@ -65,35 +65,37 @@
*/
public class AsyncCompletionTest extends HttpServerTestFixture
{
private static final Exchanger<DelayedCallback> X = new Exchanger<>();
private static final AtomicBoolean COMPLETE = new AtomicBoolean();
private static final Exchanger<PendingCallback> 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<Void> _delay = new CompletableFuture<>();
private CompletableFuture<Void> _pending = new CompletableFuture<>();

public DelayedCallback(Callback callback)
public PendingCallback(Callback callback)
{
super(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)
Expand All @@ -107,7 +109,7 @@ public void proceed()
@BeforeEach
public void init() throws Exception
{
COMPLETE.set(false);
__complete.set(false);

startServer(new ServerConnector(_server, new HttpConnectionFactory()
{
Expand Down Expand Up @@ -136,7 +138,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
{
Expand All @@ -159,7 +161,7 @@ public ExtendedHttpConnection(HttpConfiguration config, Connector connector, End
@Override
public void onCompleted()
{
COMPLETE.compareAndSet(false, true);
__complete.compareAndSet(false,true);
super.onCompleted();
}
}
Expand All @@ -170,11 +172,11 @@ public static Stream<Arguments> tests()
List<Object[]> 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);
}

Expand Down Expand Up @@ -204,7 +206,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);
Expand All @@ -216,12 +218,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();
Expand All @@ -232,16 +234,14 @@ 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
Expand Down Expand Up @@ -274,15 +274,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
Expand All @@ -291,7 +289,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
{
Expand All @@ -300,9 +298,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();
}
Expand Down

0 comments on commit d686f58

Please sign in to comment.