Search in sources :

Example 1 with LocalConcurrentBlockingObjectQueue

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

the class LocalJSR166TestCase method testConstructor7.

/**
     * Queue contains all elements of collection used to initialize
     */
@Ignore
@Test
public void testConstructor7() {
    Integer[] ints = new Integer[SIZE];
    for (int i = 0; i < SIZE; ++i) ints[i] = i;
    Collection<Integer> elements = Arrays.asList(ints);
    BlockingQueue q = new LocalConcurrentBlockingObjectQueue(SIZE, true, elements);
    for (int i = 0; i < SIZE; ++i) assertEquals(ints[i], q.poll());
}
Also used : BlockingQueue(java.util.concurrent.BlockingQueue) LocalConcurrentBlockingObjectQueue(net.openhft.chronicle.sandbox.queue.LocalConcurrentBlockingObjectQueue) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with LocalConcurrentBlockingObjectQueue

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

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

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

the class LocalJSR166TestCase method testAdd.

/**
     * add succeeds if not full; throws ISE if full
     */
@Test
public void testAdd() {
    try {
        BlockingQueue q = new LocalConcurrentBlockingObjectQueue(SIZE);
        for (int i = 0; i < SIZE; ++i) {
            assertTrue(q.add(new Integer(i)));
        }
        assertEquals(0, q.remainingCapacity());
        q.add(new Integer(SIZE));
        shouldThrow();
    } catch (IllegalStateException success) {
    }
}
Also used : BlockingQueue(java.util.concurrent.BlockingQueue) LocalConcurrentBlockingObjectQueue(net.openhft.chronicle.sandbox.queue.LocalConcurrentBlockingObjectQueue) Test(org.junit.Test)

Example 5 with LocalConcurrentBlockingObjectQueue

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

the class LocalJSR166TestCase method testPutWithTake.

/**
     * put blocks interruptibly waiting for take when full
     */
@Test
public void testPutWithTake() throws InterruptedException {
    final int capacity = 2;
    final BlockingQueue q = new LocalConcurrentBlockingObjectQueue(capacity);
    final CountDownLatch pleaseTake = new CountDownLatch(1);
    final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
    Thread t = newStartedThread(new CheckedRunnable() {

        public void realRun() throws InterruptedException {
            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) CountDownLatch(java.util.concurrent.CountDownLatch) LocalConcurrentBlockingObjectQueue(net.openhft.chronicle.sandbox.queue.LocalConcurrentBlockingObjectQueue) Test(org.junit.Test)

Aggregations

BlockingQueue (java.util.concurrent.BlockingQueue)21 LocalConcurrentBlockingObjectQueue (net.openhft.chronicle.sandbox.queue.LocalConcurrentBlockingObjectQueue)21 Test (org.junit.Test)20 Ignore (org.junit.Ignore)5 CountDownLatch (java.util.concurrent.CountDownLatch)3 ExecutorService (java.util.concurrent.ExecutorService)2