Search in sources :

Example 16 with FutureCallback

use of org.eclipse.jetty.util.FutureCallback in project jetty.project by eclipse.

the class ByteArrayEndPointTest method testReadable.

@Test
public void testReadable() throws Exception {
    ByteArrayEndPoint endp = new ByteArrayEndPoint(_scheduler, 5000);
    endp.addInput("test input");
    ByteBuffer buffer = BufferUtil.allocate(1024);
    FutureCallback fcb = new FutureCallback();
    endp.fillInterested(fcb);
    fcb.get(100, TimeUnit.MILLISECONDS);
    assertTrue(fcb.isDone());
    assertEquals(null, fcb.get());
    assertEquals(10, endp.fill(buffer));
    assertEquals("test input", BufferUtil.toString(buffer));
    fcb = new FutureCallback();
    endp.fillInterested(fcb);
    Thread.sleep(100);
    assertFalse(fcb.isDone());
    assertEquals(0, endp.fill(buffer));
    endp.addInput(" more");
    fcb.get(1000, TimeUnit.MILLISECONDS);
    assertTrue(fcb.isDone());
    assertEquals(null, fcb.get());
    assertEquals(5, endp.fill(buffer));
    assertEquals("test input more", BufferUtil.toString(buffer));
    fcb = new FutureCallback();
    endp.fillInterested(fcb);
    Thread.sleep(100);
    assertFalse(fcb.isDone());
    assertEquals(0, endp.fill(buffer));
    endp.addInput((ByteBuffer) null);
    assertTrue(fcb.isDone());
    assertEquals(null, fcb.get());
    assertEquals(-1, endp.fill(buffer));
    fcb = new FutureCallback();
    endp.fillInterested(fcb);
    fcb.get(1000, TimeUnit.MILLISECONDS);
    assertTrue(fcb.isDone());
    assertEquals(null, fcb.get());
    assertEquals(-1, endp.fill(buffer));
    endp.close();
    fcb = new FutureCallback();
    endp.fillInterested(fcb);
    try {
        fcb.get(1000, TimeUnit.MILLISECONDS);
        fail();
    } catch (ExecutionException e) {
        assertThat(e.toString(), containsString("Closed"));
    }
}
Also used : ExecutionException(java.util.concurrent.ExecutionException) ByteBuffer(java.nio.ByteBuffer) FutureCallback(org.eclipse.jetty.util.FutureCallback) Test(org.junit.Test)

Example 17 with FutureCallback

use of org.eclipse.jetty.util.FutureCallback in project jetty.project by eclipse.

the class ByteArrayEndPointTest method testIdle.

@Slow
@Test
public void testIdle() throws Exception {
    long idleTimeout = 1500;
    long halfIdleTimeout = idleTimeout / 2;
    long oneAndHalfIdleTimeout = idleTimeout + halfIdleTimeout;
    ByteArrayEndPoint endp = new ByteArrayEndPoint(_scheduler, idleTimeout);
    endp.setGrowOutput(false);
    endp.addInput("test");
    endp.setOutput(BufferUtil.allocate(5));
    assertTrue(endp.isOpen());
    Thread.sleep(oneAndHalfIdleTimeout);
    // Still open because it has not been oshut or closed explicitly
    // and there are no callbacks, so idle timeout is ignored.
    assertTrue(endp.isOpen());
    // Normal read is immediate, since there is data to read.
    ByteBuffer buffer = BufferUtil.allocate(1024);
    FutureCallback fcb = new FutureCallback();
    endp.fillInterested(fcb);
    fcb.get(idleTimeout, TimeUnit.MILLISECONDS);
    assertTrue(fcb.isDone());
    assertEquals(4, endp.fill(buffer));
    assertEquals("test", BufferUtil.toString(buffer));
    // Wait for a read timeout.
    fcb = new FutureCallback();
    endp.fillInterested(fcb);
    long start = System.nanoTime();
    try {
        fcb.get();
        fail();
    } catch (ExecutionException t) {
        assertThat(t.getCause(), instanceOf(TimeoutException.class));
    }
    assertThat(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start), greaterThan(halfIdleTimeout));
    assertThat("Endpoint open", endp.isOpen(), is(true));
    // We need to delay the write timeout test below from the read timeout test above.
    // The reason is that the scheduler thread that fails the endPoint WriteFlusher
    // because of the read timeout above runs concurrently with the write below, and
    // if it runs just after the write below, the test fails because the write callback
    // below fails immediately rather than after the idle timeout.
    Thread.sleep(halfIdleTimeout);
    // Write more than the output capacity, then wait for idle timeout.
    fcb = new FutureCallback();
    endp.write(fcb, BufferUtil.toBuffer("This is too long"));
    start = System.nanoTime();
    try {
        fcb.get();
        fail();
    } catch (ExecutionException t) {
        assertThat(t.getCause(), instanceOf(TimeoutException.class));
    }
    assertThat(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start), greaterThan(halfIdleTimeout));
    // Still open because it has not been oshut or closed explicitly.
    assertThat("Endpoint open", endp.isOpen(), is(true));
    // Make sure the endPoint is closed when the callback fails.
    endp.fillInterested(new Closer(endp));
    Thread.sleep(halfIdleTimeout);
    // Still open because it has not been oshut or closed explicitly.
    assertThat("Endpoint open", endp.isOpen(), is(true));
    // Shutdown output.
    endp.shutdownOutput();
    Thread.sleep(idleTimeout);
    assertThat("Endpoint closed", endp.isOpen(), is(false));
}
Also used : ExecutionException(java.util.concurrent.ExecutionException) ByteBuffer(java.nio.ByteBuffer) FutureCallback(org.eclipse.jetty.util.FutureCallback) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

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