Search in sources :

Example 46 with BlockingQueue

use of java.util.concurrent.BlockingQueue in project HugeCollections-OLD by peter-lawrey.

the class BlockingQueueTest method testDrainToNull.

/**
     * drainTo(null) throws NullPointerException
     */
public void testDrainToNull() {
    final BlockingQueue q = emptyCollection();
    try {
        q.drainTo(null);
        shouldThrow();
    } catch (NullPointerException success) {
    }
}
Also used : BlockingQueue(java.util.concurrent.BlockingQueue)

Example 47 with BlockingQueue

use of java.util.concurrent.BlockingQueue in project HugeCollections-OLD by peter-lawrey.

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());
        }
    });
    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 48 with BlockingQueue

use of java.util.concurrent.BlockingQueue in project HugeCollections-OLD by peter-lawrey.

the class LocalJSR166TestCase method testContains.

/**
     * contains(x) reports true when elements added but not yet removed
     */
@Test
public void testContains() {
    BlockingQueue q = populatedQueue(SIZE);
    for (int i = 0; i < SIZE; ++i) {
        assertTrue(q.contains(new Integer(i)));
        assertEquals(i, q.poll());
        assertFalse(q.contains(new Integer(i)));
    }
}
Also used : BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test)

Example 49 with BlockingQueue

use of java.util.concurrent.BlockingQueue in project HugeCollections-OLD by peter-lawrey.

the class LocalJSR166TestCase method populatedQueue.

/**
     * Returns a new queue of given size containing consecutive
     * Integers 0 ... n.
     */
private BlockingQueue populatedQueue(int n) {
    BlockingQueue q = new LocalConcurrentBlockingObjectQueue(n);
    assertTrue(q.isEmpty());
    for (int i = 0; i < n; i++) assertTrue(q.offer(new Integer(i)));
    assertFalse(q.isEmpty());
    assertEquals(0, q.remainingCapacity());
    assertEquals(n, q.size());
    return q;
}
Also used : BlockingQueue(java.util.concurrent.BlockingQueue) LocalConcurrentBlockingObjectQueue(net.openhft.chronicle.sandbox.queue.LocalConcurrentBlockingObjectQueue)

Example 50 with BlockingQueue

use of java.util.concurrent.BlockingQueue in project HugeCollections-OLD by peter-lawrey.

the class LocalJSR166TestCase method testRemoveAll.

/**
     * removeAll(c) removes only those elements of c and reports true if changed
     */
@Test
public void testRemoveAll() {
    for (int i = 1; i < SIZE; ++i) {
        BlockingQueue q = populatedQueue(SIZE);
        BlockingQueue p = populatedQueue(i);
        assertTrue(q.removeAll(p));
        assertEquals(SIZE - i, q.size());
        for (int j = 0; j < i; ++j) {
            Integer I = (Integer) (p.remove());
            assertFalse(q.contains(I));
        }
    }
}
Also used : BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test)

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