Search in sources :

Example 1 with SharedConcurrentBlockingObjectQueue

use of net.openhft.chronicle.sandbox.queue.SharedConcurrentBlockingObjectQueue 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 2 with SharedConcurrentBlockingObjectQueue

use of net.openhft.chronicle.sandbox.queue.SharedConcurrentBlockingObjectQueue 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 3 with SharedConcurrentBlockingObjectQueue

use of net.openhft.chronicle.sandbox.queue.SharedConcurrentBlockingObjectQueue 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 4 with SharedConcurrentBlockingObjectQueue

use of net.openhft.chronicle.sandbox.queue.SharedConcurrentBlockingObjectQueue in project HugeCollections-OLD by peter-lawrey.

the class SharedJSR166TestCase method testBlockingPut.

/**
     * put blocks interruptibly if full
     */
@Test
public void testBlockingPut() throws Exception, IOException {
    final BlockingQueue<Integer> q = new SharedConcurrentBlockingObjectQueue<Integer>(SIZE, Integer.class);
    final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
    Thread t = newStartedThread(new CheckedRunnable() {

        public void realRun() throws Exception {
            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 : 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 5 with SharedConcurrentBlockingObjectQueue

use of net.openhft.chronicle.sandbox.queue.SharedConcurrentBlockingObjectQueue in project HugeCollections-OLD by peter-lawrey.

the class SharedJSR166TestCase method testOfferInExecutor.

/**
     * offer transfers elements across Executor tasks
     */
@Test
public void testOfferInExecutor() throws IOException {
    final BlockingQueue<Integer> q = new SharedConcurrentBlockingObjectQueue<Integer>(2, Integer.class);
    q.add(one);
    q.add(two);
    ExecutorService executor = Executors.newFixedThreadPool(2);
    final CheckedBarrier threadsStarted = new CheckedBarrier(2);
    executor.execute(new CheckedRunnable() {

        public void realRun() throws Exception {
            assertFalse(q.offer(three));
            threadsStarted.await();
            assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
            assertEquals(0, q.remainingCapacity());
        }
    });
    executor.execute(new CheckedRunnable() {

        public void realRun() throws Exception {
            threadsStarted.await();
            assertEquals(0, q.remainingCapacity());
            assertEquals(one, q.take());
        }
    });
    joinPool(executor);
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) SharedConcurrentBlockingObjectQueue(net.openhft.chronicle.sandbox.queue.SharedConcurrentBlockingObjectQueue) IOException(java.io.IOException) Test(org.junit.Test) BlockingQueueTest(net.openhft.chronicle.sandbox.queue.common.BlockingQueueTest)

Aggregations

SharedConcurrentBlockingObjectQueue (net.openhft.chronicle.sandbox.queue.SharedConcurrentBlockingObjectQueue)7 BlockingQueueTest (net.openhft.chronicle.sandbox.queue.common.BlockingQueueTest)7 Test (org.junit.Test)7 IOException (java.io.IOException)5 BlockingQueue (java.util.concurrent.BlockingQueue)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 ExecutorService (java.util.concurrent.ExecutorService)2 Ignore (org.junit.Ignore)2