use of org.eclipse.jetty.util.FutureCallback in project jetty.project by eclipse.
the class ByteArrayEndPointTest method testWrite.
@Test
public void testWrite() throws Exception {
ByteArrayEndPoint endp = new ByteArrayEndPoint(_scheduler, 5000, (byte[]) null, 15);
endp.setGrowOutput(false);
endp.setOutput(BufferUtil.allocate(10));
ByteBuffer data = BufferUtil.toBuffer("Data.");
ByteBuffer more = BufferUtil.toBuffer(" Some more.");
FutureCallback fcb = new FutureCallback();
endp.write(fcb, data);
assertTrue(fcb.isDone());
assertEquals(null, fcb.get());
assertEquals("Data.", endp.getOutputString());
fcb = new FutureCallback();
endp.write(fcb, more);
assertFalse(fcb.isDone());
assertEquals("Data. Some", endp.getOutputString());
assertEquals("Data. Some", endp.takeOutputString());
assertTrue(fcb.isDone());
assertEquals(null, fcb.get());
assertEquals(" more.", endp.getOutputString());
endp.close();
}
use of org.eclipse.jetty.util.FutureCallback in project jetty.project by eclipse.
the class StreamResetTest method testStreamSendingResetIsRemoved.
@Test
public void testStreamSendingResetIsRemoved() throws Exception {
start(new ServerSessionListener.Adapter());
Session client = newClient(new Session.Listener.Adapter());
MetaData.Request request = newRequest("GET", new HttpFields());
HeadersFrame requestFrame = new HeadersFrame(request, null, false);
FuturePromise<Stream> promise = new FuturePromise<>();
client.newStream(requestFrame, promise, new Stream.Listener.Adapter());
Stream stream = promise.get(5, TimeUnit.SECONDS);
ResetFrame resetFrame = new ResetFrame(stream.getId(), ErrorCode.CANCEL_STREAM_ERROR.code);
FutureCallback resetCallback = new FutureCallback();
stream.reset(resetFrame, resetCallback);
resetCallback.get(5, TimeUnit.SECONDS);
// After reset the stream should be gone.
Assert.assertEquals(0, client.getStreams().size());
}
use of org.eclipse.jetty.util.FutureCallback in project jetty.project by eclipse.
the class SslConnectionTest method testWriteOnConnect.
@Test
public void testWriteOnConnect() throws Exception {
_testFill = false;
_writeCallback = new FutureCallback();
Socket client = newClient();
client.setSoTimeout(10000);
SocketChannel server = _connector.accept();
server.configureBlocking(false);
_manager.accept(server);
byte[] buffer = new byte[1024];
int len = client.getInputStream().read(buffer);
Assert.assertEquals("Hello Client", new String(buffer, 0, len, StandardCharsets.UTF_8));
Assert.assertEquals(null, _writeCallback.get(100, TimeUnit.MILLISECONDS));
client.close();
}
use of org.eclipse.jetty.util.FutureCallback in project jetty.project by eclipse.
the class WriteFlusherTest method testFailWhileBlocking.
@Test
public void testFailWhileBlocking() 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());
String reason = "Failure";
flusher.onFail(new IOException(reason));
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.assertEquals(reason, cause.getMessage());
}
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 testConcurrentIncompleteFlushAndOnFail.
@Test
public void testConcurrentIncompleteFlushAndOnFail() throws Exception {
int capacity = 8;
ByteArrayEndPoint endPoint = new ByteArrayEndPoint(new byte[0], capacity);
String reason = "the_reason";
WriteFlusher flusher = new WriteFlusher(endPoint) {
@Override
protected void onIncompleteFlush() {
onFail(new Throwable(reason));
}
};
FutureCallback callback = new FutureCallback();
byte[] content = new byte[capacity * 2];
flusher.write(callback, BufferUtil.toBuffer(content));
try {
// Callback must be failed.
callback.get(1, TimeUnit.SECONDS);
} catch (ExecutionException x) {
Assert.assertEquals(reason, x.getCause().getMessage());
}
}
Aggregations