Search in sources :

Example 6 with FutureCallback

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());
}
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 7 with FutureCallback

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

Example 8 with FutureCallback

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

Example 9 with FutureCallback

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

Example 10 with FutureCallback

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());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FutureCallback(org.eclipse.jetty.util.FutureCallback) TimeoutException(java.util.concurrent.TimeoutException) 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