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 <[email protected]>
  • Loading branch information
gregw committed Nov 29, 2019
1 parent 41b3d70 commit a872522
Showing 1 changed file with 29 additions and 31 deletions.
Original file line number Diff line number Diff line change
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 @@ -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()
{
Expand Down Expand Up @@ -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
{
Expand All @@ -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();
}
}
Expand All @@ -171,11 +173,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 @@ -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);
Expand All @@ -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();
Expand All @@ -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
{
Expand Down Expand Up @@ -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
Expand All @@ -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
{
Expand All @@ -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();
}
Expand Down

0 comments on commit a872522

Please sign in to comment.