Search in sources :

Example 71 with BlockingQueue

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

the class BlockingQueueTest method testTimedPollWithOffer.

/**
     * timed poll before a delayed offer times out; after offer succeeds;
     * on interruption throws
     */
public void testTimedPollWithOffer() throws InterruptedException {
    final BlockingQueue q = emptyCollection();
    final CheckedBarrier barrier = new CheckedBarrier(2);
    final Object zero = makeElement(0);
    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.offer(zero, LONG_DELAY_MS, MILLISECONDS));
    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
    barrier.await();
    assertThreadStaysAlive(t);
    t.interrupt();
    awaitTermination(t);
}
Also used : BlockingQueue(java.util.concurrent.BlockingQueue)

Example 72 with BlockingQueue

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

the class BlockingQueueTest method testTimedOfferNull.

/**
     * timed offer(null) throws NullPointerException
     */
public void testTimedOfferNull() throws InterruptedException {
    final BlockingQueue q = emptyCollection();
    long startTime = System.nanoTime();
    try {
        q.offer(null, LONG_DELAY_MS, MILLISECONDS);
        shouldThrow();
    } catch (NullPointerException success) {
    }
    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
Also used : BlockingQueue(java.util.concurrent.BlockingQueue)

Example 73 with BlockingQueue

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

the class BlockingQueueTest method testTakeFromEmptyBlocksInterruptibly.

/**
     * take() blocks interruptibly when empty
     */
public void testTakeFromEmptyBlocksInterruptibly() {
    final BlockingQueue q = emptyCollection();
    final CountDownLatch threadStarted = new CountDownLatch(1);
    Thread t = newStartedThread(new CheckedRunnable() {

        public void realRun() {
            threadStarted.countDown();
            try {
                q.take();
                shouldThrow();
            } catch (InterruptedException success) {
            }
            assertFalse(Thread.interrupted());
        }
    });
    await(threadStarted);
    assertThreadStaysAlive(t);
    t.interrupt();
    awaitTermination(t);
}
Also used : BlockingQueue(java.util.concurrent.BlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 74 with BlockingQueue

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

the class BlockingQueueTest method testDrainToSelfN.

/**
     * drainTo(this, n) throws IllegalArgumentException
     */
public void testDrainToSelfN() {
    final BlockingQueue q = emptyCollection();
    try {
        q.drainTo(q, 0);
        shouldThrow();
    } catch (IllegalArgumentException success) {
    }
}
Also used : BlockingQueue(java.util.concurrent.BlockingQueue)

Example 75 with BlockingQueue

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

the class BlockingQueueTest method testRemoveElement.

/**
     * remove(x) removes x and returns true if present
     *
     */
public void testRemoveElement() {
    final BlockingQueue q = emptyCollection();
    final int size = Math.min(q.remainingCapacity(), SIZE);
    final Object[] elts = new Object[size];
    assertFalse(q.contains(makeElement(99)));
    assertFalse(q.remove(makeElement(99)));
    checkEmpty(q);
    for (int i = 0; i < size; i++) q.add(elts[i] = makeElement(i));
    for (int i = 1; i < size; i += 2) {
        for (int pass = 0; pass < 2; pass++) {
            assertEquals((pass == 0), q.contains(elts[i]));
            assertEquals((pass == 0), q.remove(elts[i]));
            assertFalse(q.contains(elts[i]));
            assertTrue(q.contains(elts[i - 1]));
            if (i < size - 1)
                assertTrue(q.contains(elts[i + 1]));
        }
    }
    if (size > 0)
        assertTrue(q.contains(elts[0]));
    for (int i = size - 2; i >= 0; i -= 2) {
        assertTrue(q.contains(elts[i]));
        assertFalse(q.contains(elts[i + 1]));
        assertTrue(q.remove(elts[i]));
        assertFalse(q.contains(elts[i]));
        assertFalse(q.remove(elts[i + 1]));
        assertFalse(q.contains(elts[i + 1]));
    }
    checkEmpty(q);
}
Also used : BlockingQueue(java.util.concurrent.BlockingQueue)

Aggregations

BlockingQueue (java.util.concurrent.BlockingQueue)129 Test (org.junit.Test)59 CountDownLatch (java.util.concurrent.CountDownLatch)21 LocalConcurrentBlockingObjectQueue (net.openhft.chronicle.sandbox.queue.LocalConcurrentBlockingObjectQueue)21 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)18 ArrayList (java.util.ArrayList)12 Ignore (org.junit.Ignore)12 IOException (java.io.IOException)10 BlockingQueueTest (net.openhft.chronicle.sandbox.queue.common.BlockingQueueTest)10 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)9 TimeUnit (java.util.concurrent.TimeUnit)9 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)8 ByteBuffer (java.nio.ByteBuffer)4 List (java.util.List)4 Assert (org.junit.Assert)4 SynchronousQueue (java.util.concurrent.SynchronousQueue)3 TimeoutException (java.util.concurrent.TimeoutException)3 AsyncContext (javax.servlet.AsyncContext)3 ReadListener (javax.servlet.ReadListener)3 ServletException (javax.servlet.ServletException)3