Search in sources :

Example 46 with LinkedBlockingDeque

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

the class LinkedBlockingDequeTest method testBlockingPut.

/**
     * put blocks interruptibly if full
     */
public void testBlockingPut() throws InterruptedException {
    final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
    final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
    Thread t = newStartedThread(new CheckedRunnable() {

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

Example 47 with LinkedBlockingDeque

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

the class LinkedBlockingDequeTest method testTimedPollWithOfferLast.

/**
     * timed poll before a delayed offerLast fails; after offerLast succeeds;
     * on interruption throws
     */
public void testTimedPollWithOfferLast() 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.poll(timeoutMillis(), MILLISECONDS));
            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
            barrier.await();
            assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
            Thread.currentThread().interrupt();
            try {
                q.poll(LONG_DELAY_MS, MILLISECONDS);
                shouldThrow();
            } catch (InterruptedException success) {
            }
            assertFalse(Thread.interrupted());
            barrier.await();
            try {
                q.poll(LONG_DELAY_MS, MILLISECONDS);
                shouldThrow();
            } catch (InterruptedException success) {
            }
            assertFalse(Thread.interrupted());
            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
        }
    });
    barrier.await();
    long startTime = System.nanoTime();
    assertTrue(q.offerLast(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 48 with LinkedBlockingDeque

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

the class LinkedBlockingDequeTest method testTimedPollLast.

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

Example 49 with LinkedBlockingDeque

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

the class LinkedBlockingDequeTest method testAddAll4.

/**
     * addAll throws IllegalStateException if not enough room
     */
public void testAddAll4() {
    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE - 1);
    Integer[] ints = new Integer[SIZE];
    for (int i = 0; i < SIZE; ++i) ints[i] = new Integer(i);
    Collection<Integer> elements = Arrays.asList(ints);
    try {
        q.addAll(elements);
        shouldThrow();
    } catch (IllegalStateException success) {
    }
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque)

Example 50 with LinkedBlockingDeque

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

the class LinkedBlockingDequeTest method testTimedOfferFirst.

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

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