Search in sources :

Example 36 with BlockingQueue

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

the class SharedJSR166TestCase method testContainsAll.

/**
     * containsAll(c) is true when c contains a subset of elements
     */
@Test
public void testContainsAll() throws IOException {
    BlockingQueue<Integer> q = populatedQueue(SIZE);
    BlockingQueue p = new SharedConcurrentBlockingObjectQueue<Integer>(SIZE, Integer.class);
    for (int i = 0; i < SIZE; ++i) {
        assertTrue(q.containsAll(p));
        assertFalse(p.containsAll(q));
        p.add(new Integer(i));
    }
    assertTrue(p.containsAll(q));
}
Also used : BlockingQueue(java.util.concurrent.BlockingQueue) SharedConcurrentBlockingObjectQueue(net.openhft.chronicle.sandbox.queue.SharedConcurrentBlockingObjectQueue) Test(org.junit.Test) BlockingQueueTest(net.openhft.chronicle.sandbox.queue.common.BlockingQueueTest)

Example 37 with BlockingQueue

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

the class SharedJSR166TestCase method testPutWithTake.

/**
     * put blocks interruptibly waiting for take when full
     */
@Test
public void testPutWithTake() throws Exception {
    final int capacity = 2;
    final BlockingQueue q = new SharedConcurrentBlockingObjectQueue<Integer>(capacity, Integer.class);
    final CountDownLatch pleaseTake = new CountDownLatch(1);
    final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
    Thread t = newStartedThread(new CheckedRunnable() {

        public void realRun() throws Exception {
            for (int i = 0; i < capacity; i++) q.put(i);
            pleaseTake.countDown();
            q.put(86);
            pleaseInterrupt.countDown();
            try {
                q.put(99);
                shouldThrow();
            } catch (InterruptedException success) {
            }
            assertFalse(Thread.interrupted());
        }
    });
    await(pleaseTake);
    assertEquals(0, q.remainingCapacity());
    assertEquals(0, q.take());
    await(pleaseInterrupt);
    assertThreadStaysAlive(t);
    t.interrupt();
    awaitTermination(t);
    assertEquals(0, q.remainingCapacity());
}
Also used : BlockingQueue(java.util.concurrent.BlockingQueue) SharedConcurrentBlockingObjectQueue(net.openhft.chronicle.sandbox.queue.SharedConcurrentBlockingObjectQueue) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) Test(org.junit.Test) BlockingQueueTest(net.openhft.chronicle.sandbox.queue.common.BlockingQueueTest)

Example 38 with BlockingQueue

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

the class SharedJSR166TestCase method testPoll.

/**
     * poll succeeds unless empty
     */
@Test
public void testPoll() throws IOException {
    BlockingQueue q = populatedQueue(SIZE);
    for (int i = 0; i < SIZE; ++i) {
        assertEquals(i, q.poll());
    }
    assertNull(q.poll());
}
Also used : BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) BlockingQueueTest(net.openhft.chronicle.sandbox.queue.common.BlockingQueueTest)

Example 39 with BlockingQueue

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

the class LocalJSR166TestCase method testBlockingPut.

/**
     * put blocks interruptibly if full
     */
@Test
public void testBlockingPut() throws InterruptedException {
    final BlockingQueue q = new LocalConcurrentBlockingObjectQueue(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 : BlockingQueue(java.util.concurrent.BlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) LocalConcurrentBlockingObjectQueue(net.openhft.chronicle.sandbox.queue.LocalConcurrentBlockingObjectQueue) Test(org.junit.Test)

Example 40 with BlockingQueue

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

the class LocalJSR166TestCase method testRetainAll.

/**
     * retainAll(c) retains only those elements of c and reports true if changed
     */
@Test
public void testRetainAll() {
    BlockingQueue q = populatedQueue(SIZE);
    BlockingQueue p = populatedQueue(SIZE);
    for (int i = 0; i < SIZE; ++i) {
        boolean changed = q.retainAll(p);
        if (i == 0)
            assertFalse(changed);
        else
            assertTrue(changed);
        assertTrue(q.containsAll(p));
        assertEquals(SIZE - i, q.size());
        p.remove();
    }
}
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