Search in sources :

Example 11 with LinkedBlockingDeque

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

the class LinkedBlockingDequeTest method testPop.

/**
     * pop removes next element, or throws NSEE if empty
     */
public void testPop() {
    LinkedBlockingDeque q = populatedDeque(SIZE);
    for (int i = 0; i < SIZE; ++i) {
        assertEquals(i, q.pop());
    }
    try {
        q.pop();
        shouldThrow();
    } catch (NoSuchElementException success) {
    }
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) NoSuchElementException(java.util.NoSuchElementException)

Example 12 with LinkedBlockingDeque

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

the class LinkedBlockingDequeTest method testInterruptedTimedPollLast.

/**
     * Interrupted timed pollLast throws InterruptedException instead of
     * returning timeout status
     */
public void testInterruptedTimedPollLast() throws InterruptedException {
    final LinkedBlockingDeque q = populatedDeque(SIZE);
    final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
    Thread t = newStartedThread(new CheckedRunnable() {

        public void realRun() throws InterruptedException {
            long startTime = System.nanoTime();
            for (int i = 0; i < SIZE; ++i) {
                assertEquals(SIZE - i - 1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
            }
            Thread.currentThread().interrupt();
            try {
                q.pollLast(LONG_DELAY_MS, MILLISECONDS);
                shouldThrow();
            } catch (InterruptedException success) {
            }
            assertFalse(Thread.interrupted());
            pleaseInterrupt.countDown();
            try {
                q.pollLast(LONG_DELAY_MS, MILLISECONDS);
                shouldThrow();
            } catch (InterruptedException success) {
            }
            assertFalse(Thread.interrupted());
            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
        }
    });
    await(pleaseInterrupt);
    assertThreadStaysAlive(t);
    t.interrupt();
    awaitTermination(t);
    checkEmpty(q);
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 13 with LinkedBlockingDeque

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

the class LinkedBlockingDequeTest method testTimedPollFirst.

/**
     * timed pollFirst with nonzero timeout succeeds when non-empty, else times out
     */
public void testTimedPollFirst() throws InterruptedException {
    LinkedBlockingDeque q = populatedDeque(SIZE);
    for (int i = 0; i < SIZE; ++i) {
        long startTime = System.nanoTime();
        assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
    }
    long startTime = System.nanoTime();
    assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS));
    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
    checkEmpty(q);
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque)

Example 14 with LinkedBlockingDeque

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

the class LinkedBlockingDequeTest method testIteratorRemove.

/**
     * iterator.remove removes current element
     */
public void testIteratorRemove() {
    final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
    q.add(two);
    q.add(one);
    q.add(three);
    Iterator it = q.iterator();
    it.next();
    it.remove();
    it = q.iterator();
    assertSame(it.next(), one);
    assertSame(it.next(), three);
    assertFalse(it.hasNext());
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) Iterator(java.util.Iterator)

Example 15 with LinkedBlockingDeque

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

the class LinkedBlockingDequeTest method testDescendingIteratorRemove.

/**
     * descendingIterator.remove removes current element
     */
public void testDescendingIteratorRemove() {
    final LinkedBlockingDeque q = new LinkedBlockingDeque();
    for (int iters = 0; iters < 100; ++iters) {
        q.add(new Integer(3));
        q.add(new Integer(2));
        q.add(new Integer(1));
        Iterator it = q.descendingIterator();
        assertEquals(it.next(), new Integer(1));
        it.remove();
        assertEquals(it.next(), new Integer(2));
        it = q.descendingIterator();
        assertEquals(it.next(), new Integer(2));
        assertEquals(it.next(), new Integer(3));
        it.remove();
        assertFalse(it.hasNext());
        q.remove();
    }
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) Iterator(java.util.Iterator)

Aggregations

LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)116 CountDownLatch (java.util.concurrent.CountDownLatch)20 Test (org.junit.Test)19 NoSuchElementException (java.util.NoSuchElementException)8 ArrayList (java.util.ArrayList)7 Iterator (java.util.Iterator)7 IOException (java.io.IOException)5 ExecutorService (java.util.concurrent.ExecutorService)5 BlockingDeque (java.util.concurrent.BlockingDeque)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)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