use of org.eclipse.jetty.util.FutureCallback in project jetty.project by eclipse.
the class WriteFlusherTest method testCloseWhileBlocking.
@Test
public void testCloseWhileBlocking() throws Exception {
ByteArrayEndPoint endPoint = new ByteArrayEndPoint(new byte[0], 10);
AtomicBoolean incompleteFlush = new AtomicBoolean();
WriteFlusher flusher = new WriteFlusher(endPoint) {
@Override
protected void onIncompleteFlush() {
incompleteFlush.set(true);
}
};
FutureCallback callback = new FutureCallback();
flusher.write(callback, BufferUtil.toBuffer("How now brown cow!"));
Assert.assertFalse(callback.isDone());
Assert.assertFalse(callback.isCancelled());
Assert.assertTrue(incompleteFlush.get());
incompleteFlush.set(false);
Assert.assertEquals("How now br", endPoint.takeOutputString());
endPoint.close();
flusher.completeWrite();
Assert.assertTrue(callback.isDone());
Assert.assertFalse(incompleteFlush.get());
try {
callback.get();
Assert.fail();
} catch (ExecutionException e) {
Throwable cause = e.getCause();
Assert.assertTrue(cause instanceof IOException);
Assert.assertThat(cause.getMessage(), Matchers.containsString("CLOSED"));
}
Assert.assertEquals("", endPoint.takeOutputString());
Assert.assertTrue(flusher.isIdle());
}
use of org.eclipse.jetty.util.FutureCallback in project jetty.project by eclipse.
the class WriteFlusherTest method testConcurrentWriteAndOnFail.
@Test
public void testConcurrentWriteAndOnFail() throws Exception {
ByteArrayEndPoint endPoint = new ByteArrayEndPoint(new byte[0], 16);
WriteFlusher flusher = new WriteFlusher(endPoint) {
@Override
protected ByteBuffer[] flush(ByteBuffer[] buffers) throws IOException {
ByteBuffer[] result = super.flush(buffers);
boolean notified = onFail(new Throwable());
Assert.assertFalse(notified);
return result;
}
@Override
protected void onIncompleteFlush() {
}
};
FutureCallback callback = new FutureCallback();
flusher.write(callback, BufferUtil.toBuffer("foo"));
// Callback must be successfully completed.
callback.get(1, TimeUnit.SECONDS);
// Flusher must be idle - not failed - since the write succeeded.
Assert.assertTrue(flusher.isIdle());
}
use of org.eclipse.jetty.util.FutureCallback in project jetty.project by eclipse.
the class WriteFlusherTest method testConcurrent.
@Test
public void testConcurrent() throws Exception {
Random random = new Random();
ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(100);
try {
String reason = "THE_CAUSE";
ConcurrentWriteFlusher[] flushers = new ConcurrentWriteFlusher[50000];
FutureCallback[] futures = new FutureCallback[flushers.length];
for (int i = 0; i < flushers.length; ++i) {
int size = 5 + random.nextInt(15);
ByteArrayEndPoint endPoint = new ByteArrayEndPoint(new byte[0], size);
ConcurrentWriteFlusher flusher = new ConcurrentWriteFlusher(endPoint, scheduler, random);
flushers[i] = flusher;
FutureCallback callback = new FutureCallback();
futures[i] = callback;
scheduler.schedule(() -> flusher.onFail(new Throwable(reason)), random.nextInt(75) + 1, TimeUnit.MILLISECONDS);
flusher.write(callback, BufferUtil.toBuffer("How Now Brown Cow."), BufferUtil.toBuffer(" The quick brown fox jumped over the lazy dog!"));
}
int completed = 0;
int failed = 0;
for (int i = 0; i < flushers.length; ++i) {
try {
futures[i].get(15, TimeUnit.SECONDS);
Assert.assertEquals("How Now Brown Cow. The quick brown fox jumped over the lazy dog!", flushers[i].getContent());
completed++;
} catch (ExecutionException x) {
Assert.assertEquals(reason, x.getCause().getMessage());
failed++;
}
}
Assert.assertThat(completed, Matchers.greaterThan(0));
Assert.assertThat(failed, Matchers.greaterThan(0));
Assert.assertEquals(flushers.length, completed + failed);
} finally {
scheduler.shutdown();
}
}
use of org.eclipse.jetty.util.FutureCallback in project jetty.project by eclipse.
the class WriteFlusherTest method testCompleteWrite.
private void testCompleteWrite(boolean failBefore) throws Exception {
ByteArrayEndPoint endPoint = new ByteArrayEndPoint(new byte[0], 16);
endPoint.setGrowOutput(true);
AtomicBoolean incompleteFlush = new AtomicBoolean();
WriteFlusher flusher = new WriteFlusher(endPoint) {
@Override
protected void onIncompleteFlush() {
incompleteFlush.set(true);
}
};
if (failBefore)
flusher.onFail(new IOException("Ignored because no operation in progress"));
FutureCallback callback = new FutureCallback();
flusher.write(callback, BufferUtil.toBuffer("How "), BufferUtil.toBuffer("now "), BufferUtil.toBuffer("brown "), BufferUtil.toBuffer("cow!"));
Assert.assertTrue(callback.isDone());
Assert.assertFalse(incompleteFlush.get());
Assert.assertEquals("How now brown cow!", endPoint.takeOutputString());
Assert.assertTrue(flusher.isIdle());
}
use of org.eclipse.jetty.util.FutureCallback in project jetty.project by eclipse.
the class WriteFlusherTest method testCompleteBlocking.
@Test
public void testCompleteBlocking() throws Exception {
ByteArrayEndPoint endPoint = new ByteArrayEndPoint(new byte[0], 10);
AtomicBoolean incompleteFlush = new AtomicBoolean();
WriteFlusher flusher = new WriteFlusher(endPoint) {
@Override
protected void onIncompleteFlush() {
incompleteFlush.set(true);
}
};
FutureCallback callback = new FutureCallback();
flusher.write(callback, BufferUtil.toBuffer("How now brown cow!"));
Assert.assertFalse(callback.isDone());
Assert.assertFalse(callback.isCancelled());
Assert.assertTrue(incompleteFlush.get());
try {
callback.get(100, TimeUnit.MILLISECONDS);
Assert.fail();
} catch (TimeoutException x) {
incompleteFlush.set(false);
}
Assert.assertEquals("How now br", endPoint.takeOutputString());
flusher.completeWrite();
Assert.assertTrue(callback.isDone());
Assert.assertEquals("own cow!", endPoint.takeOutputString());
Assert.assertFalse(incompleteFlush.get());
Assert.assertTrue(flusher.isIdle());
}
Aggregations