Search in sources :

Example 11 with FutureCallback

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();
}
Also used : ByteBuffer(java.nio.ByteBuffer) FutureCallback(org.eclipse.jetty.util.FutureCallback) Test(org.junit.Test)

Example 12 with FutureCallback

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());
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) WriteListener(javax.servlet.WriteListener) FuturePromise(org.eclipse.jetty.util.FuturePromise) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) ServletOutputStream(javax.servlet.ServletOutputStream) IStream(org.eclipse.jetty.http2.IStream) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) FutureCallback(org.eclipse.jetty.util.FutureCallback) Session(org.eclipse.jetty.http2.api.Session) ISession(org.eclipse.jetty.http2.ISession) Test(org.junit.Test)

Example 13 with FutureCallback

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();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) FutureCallback(org.eclipse.jetty.util.FutureCallback) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket) Test(org.junit.Test)

Example 14 with FutureCallback

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());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(org.eclipse.jetty.util.FutureCallback) Test(org.junit.Test)

Example 15 with FutureCallback

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());
    }
}
Also used : ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(org.eclipse.jetty.util.FutureCallback) Test(org.junit.Test)

Aggregations

FutureCallback (org.eclipse.jetty.util.FutureCallback)17 Test (org.junit.Test)13 ExecutionException (java.util.concurrent.ExecutionException)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 IOException (java.io.IOException)4 InterruptedIOException (java.io.InterruptedIOException)4 ByteBuffer (java.nio.ByteBuffer)4 TimeoutException (java.util.concurrent.TimeoutException)2 HttpFields (org.eclipse.jetty.http.HttpFields)2 MetaData (org.eclipse.jetty.http.MetaData)2 ISession (org.eclipse.jetty.http2.ISession)2 Session (org.eclipse.jetty.http2.api.Session)2 Stream (org.eclipse.jetty.http2.api.Stream)2 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)2 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)2 FuturePromise (org.eclipse.jetty.util.FuturePromise)2 Socket (java.net.Socket)1 ServerSocketChannel (java.nio.channels.ServerSocketChannel)1 SocketChannel (java.nio.channels.SocketChannel)1 HashMap (java.util.HashMap)1