Search in sources :

Example 31 with BlockingQueue

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

the class LocalJSR166TestCase method testClear.

/**
     * clear removes all elements
     */
@Test
public void testClear() {
    BlockingQueue q = populatedQueue(SIZE);
    q.clear();
    assertTrue(q.isEmpty());
    assertEquals(0, q.size());
    assertEquals(SIZE, q.remainingCapacity());
    q.add(one);
    assertFalse(q.isEmpty());
    assertTrue(q.contains(one));
    q.clear();
    assertTrue(q.isEmpty());
}
Also used : BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test)

Example 32 with BlockingQueue

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

the class LocalJSR166TestCase method testTimedOffer.

/**
     * timed offer times out if full and elements not taken
     */
@Ignore
@Test
public void testTimedOffer() throws InterruptedException {
    final BlockingQueue q = new LocalConcurrentBlockingObjectQueue(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 : BlockingQueue(java.util.concurrent.BlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) LocalConcurrentBlockingObjectQueue(net.openhft.chronicle.sandbox.queue.LocalConcurrentBlockingObjectQueue) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 33 with BlockingQueue

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

the class LocalJSR166TestCase method testRemove.

/**
     * remove removes next element, or throws NSEE if empty
     */
@Test
public void testRemove() {
    BlockingQueue q = populatedQueue(SIZE);
    for (int i = 0; i < SIZE; ++i) {
        assertEquals(i, q.remove());
    }
    try {
        q.remove();
        shouldThrow();
    } catch (NoSuchElementException success) {
    }
}
Also used : BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test)

Example 34 with BlockingQueue

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

the class SharedJSR166TestCase method testTimedOffer.

/**
     * timed offer times out if full and elements not taken
     */
@Ignore
@Test
public void testTimedOffer() throws Exception, IOException {
    final BlockingQueue q = new SharedConcurrentBlockingObjectQueue(2, Integer.class);
    final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
    Thread t = newStartedThread(new CheckedRunnable() {

        public void realRun() throws Exception {
            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 : BlockingQueue(java.util.concurrent.BlockingQueue) SharedConcurrentBlockingObjectQueue(net.openhft.chronicle.sandbox.queue.SharedConcurrentBlockingObjectQueue) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) Ignore(org.junit.Ignore) Test(org.junit.Test) BlockingQueueTest(net.openhft.chronicle.sandbox.queue.common.BlockingQueueTest)

Example 35 with BlockingQueue

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

the class SharedJSR166TestCase method testBlockingTake.

/**
     * Take removes existing elements until empty, then blocks interruptibly
     */
@Ignore
@Test
public void testBlockingTake() throws Exception, IOException {
    final BlockingQueue q = populatedQueue(SIZE);
    final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
    Thread t = newStartedThread(new CheckedRunnable() {

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

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