Search in sources :

Example 81 with LinkedBlockingDeque

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

the class LinkedBlockingDequeTest method testDrainToWithActivePut.

/**
     * drainTo empties full deque, unblocking a waiting put.
     */
public void testDrainToWithActivePut() throws InterruptedException {
    final LinkedBlockingDeque q = populatedDeque(SIZE);
    Thread t = new Thread(new CheckedRunnable() {

        public void realRun() throws InterruptedException {
            q.put(new Integer(SIZE + 1));
        }
    });
    t.start();
    ArrayList l = new ArrayList();
    q.drainTo(l);
    assertTrue(l.size() >= SIZE);
    for (int i = 0; i < SIZE; ++i) assertEquals(l.get(i), new Integer(i));
    t.join();
    assertTrue(q.size() + l.size() >= SIZE);
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) ArrayList(java.util.ArrayList)

Example 82 with LinkedBlockingDeque

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

the class LinkedBlockingDequeTest method testTimedOffer.

/**
     * timed offer times out if full and elements not taken
     */
public void testTimedOffer() 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.put(new Object());
            q.put(new Object());
            long startTime = System.nanoTime();
            assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
            pleaseInterrupt.countDown();
            try {
                q.offer(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 83 with LinkedBlockingDeque

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

the class LinkedBlockingDequeTest method testAddAll3.

/**
     * addAll of a collection with any null elements throws NPE after
     * possibly adding some elements
     */
public void testAddAll3() {
    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
    Integer[] ints = new Integer[SIZE];
    for (int i = 0; i < SIZE - 1; ++i) ints[i] = new Integer(i);
    Collection<Integer> elements = Arrays.asList(ints);
    try {
        q.addAll(elements);
        shouldThrow();
    } catch (NullPointerException success) {
    }
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque)

Example 84 with LinkedBlockingDeque

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

the class LinkedBlockingDequeTest method testEmpty.

/**
     * isEmpty is true before add, false after
     */
public void testEmpty() {
    LinkedBlockingDeque q = new LinkedBlockingDeque();
    assertTrue(q.isEmpty());
    q.add(new Integer(1));
    assertFalse(q.isEmpty());
    q.add(new Integer(2));
    q.removeFirst();
    q.removeFirst();
    assertTrue(q.isEmpty());
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque)

Example 85 with LinkedBlockingDeque

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

the class LinkedBlockingDequeTest method testRemoveLast.

/**
     * removeLast() removes last element, or throws NSEE if empty
     */
public void testRemoveLast() {
    LinkedBlockingDeque q = populatedDeque(SIZE);
    for (int i = SIZE - 1; i >= 0; --i) {
        assertEquals(i, q.removeLast());
    }
    try {
        q.removeLast();
        shouldThrow();
    } catch (NoSuchElementException success) {
    }
    assertNull(q.peekLast());
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) NoSuchElementException(java.util.NoSuchElementException)

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