Search in sources :

Example 91 with LinkedBlockingDeque

use of java.util.concurrent.LinkedBlockingDeque in project mapdb by jankotek.

the class LinkedBlockingDequeTest method testTimedPollFirstWithOfferFirst.

/**
     * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
     * on interruption throws
     */
public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
    final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
    final CheckedBarrier barrier = new CheckedBarrier(2);
    Thread t = newStartedThread(new CheckedRunnable() {

        public void realRun() throws InterruptedException {
            long startTime = System.nanoTime();
            assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS));
            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
            barrier.await();
            assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
            Thread.currentThread().interrupt();
            try {
                q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
                shouldThrow();
            } catch (InterruptedException success) {
            }
            barrier.await();
            try {
                q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
                shouldThrow();
            } catch (InterruptedException success) {
            }
            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
        }
    });
    barrier.await();
    long startTime = System.nanoTime();
    assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS));
    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
    barrier.await();
    assertThreadStaysAlive(t);
    t.interrupt();
    awaitTermination(t);
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque)

Example 92 with LinkedBlockingDeque

use of java.util.concurrent.LinkedBlockingDeque in project mapdb by jankotek.

the class LinkedBlockingDequeTest method testBlockingTake.

/**
     * take removes existing elements until empty, then blocks interruptibly
     */
public void testBlockingTake() throws InterruptedException {
    final LinkedBlockingDeque q = populatedDeque(SIZE);
    final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
    Thread t = newStartedThread(new CheckedRunnable() {

        public void realRun() throws InterruptedException {
            for (int i = 0; i < SIZE; ++i) {
                assertEquals(i, q.take());
            }
            Thread.currentThread().interrupt();
            try {
                q.take();
                shouldThrow();
            } catch (InterruptedException success) {
            }
            assertFalse(Thread.interrupted());
            pleaseInterrupt.countDown();
            try {
                q.take();
                shouldThrow();
            } catch (InterruptedException success) {
            }
            assertFalse(Thread.interrupted());
        }
    });
    await(pleaseInterrupt);
    assertThreadStaysAlive(t);
    t.interrupt();
    awaitTermination(t);
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 93 with LinkedBlockingDeque

use of java.util.concurrent.LinkedBlockingDeque in project mapdb by jankotek.

the class LinkedBlockingDequeTest method testDrainToN.

/**
     * drainTo(c, n) empties first min(n, size) elements of queue into c
     */
public void testDrainToN() {
    LinkedBlockingDeque q = new LinkedBlockingDeque();
    for (int i = 0; i < SIZE + 2; ++i) {
        for (int j = 0; j < SIZE; j++) assertTrue(q.offer(new Integer(j)));
        ArrayList l = new ArrayList();
        q.drainTo(l, i);
        int k = (i < SIZE) ? i : SIZE;
        assertEquals(k, l.size());
        assertEquals(SIZE - k, q.size());
        for (int j = 0; j < k; ++j) assertEquals(l.get(j), new Integer(j));
        do {
        } while (q.poll() != null);
    }
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) ArrayList(java.util.ArrayList)

Example 94 with LinkedBlockingDeque

use of java.util.concurrent.LinkedBlockingDeque in project mapdb by jankotek.

the class LinkedBlockingDequeTest method testTimedOfferLast.

/**
     * timed offerLast times out if full and elements not taken
     */
public void testTimedOfferLast() throws InterruptedException {
    final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
    final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
    Thread t = newStartedThread(new CheckedRunnable() {

        public void realRun() throws InterruptedException {
            q.putLast(new Object());
            q.putLast(new Object());
            long startTime = System.nanoTime();
            assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS));
            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
            pleaseInterrupt.countDown();
            try {
                q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
                shouldThrow();
            } catch (InterruptedException success) {
            }
        }
    });
    await(pleaseInterrupt);
    assertThreadStaysAlive(t);
    t.interrupt();
    awaitTermination(t);
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 95 with LinkedBlockingDeque

use of java.util.concurrent.LinkedBlockingDeque in project mapdb by jankotek.

the class LinkedBlockingDequeTest method testPutLast.

/**
     * all elements successfully putLast are contained
     */
public void testPutLast() throws InterruptedException {
    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
    for (int i = 0; i < SIZE; ++i) {
        Integer x = new Integer(i);
        q.putLast(x);
        assertTrue(q.contains(x));
    }
    assertEquals(0, q.remainingCapacity());
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque)

Aggregations

LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)119 Test (org.junit.Test)21 CountDownLatch (java.util.concurrent.CountDownLatch)20 NoSuchElementException (java.util.NoSuchElementException)8 ArrayList (java.util.ArrayList)7 Iterator (java.util.Iterator)7 IOException (java.io.IOException)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 ExecutorService (java.util.concurrent.ExecutorService)5 BlockingDeque (java.util.concurrent.BlockingDeque)4 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)4 ByteBuffer (java.nio.ByteBuffer)3 HashMap (java.util.HashMap)3 IotHubOutboundPacket (com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket)2 AmqpsMessage (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsMessage)2 AmqpsTransport (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport)2 ByteBuf (io.netty.buffer.ByteBuf)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2