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"));
}
}
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));
}
Aggregations