Package net.openhft.chronicle.sandbox.queue

Examples of net.openhft.chronicle.sandbox.queue.LocalConcurrentBlockingObjectQueue


     * 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) {
        }
    }
View Full Code Here


     * possibly adding some elements
     */
    @Test
    public void testAddAll3() {
        try {
            BlockingQueue q = new LocalConcurrentBlockingObjectQueue(SIZE);
            Integer[] ints = new Integer[SIZE];
            for (int i = 0; i < SIZE - 1; ++i)
                ints[i] = new Integer(i);
            q.addAll(Arrays.asList(ints));
            shouldThrow();
        } catch (NullPointerException success) {
        }
    }
View Full Code Here

     * addAll throws ISE if not enough room
     */
    @Test
    public void testAddAll4() {
        try {
            BlockingQueue q = new LocalConcurrentBlockingObjectQueue(1);
            Integer[] ints = new Integer[SIZE];
            for (int i = 0; i < SIZE; ++i)
                ints[i] = new Integer(i);
            q.addAll(Arrays.asList(ints));
            shouldThrow();
        } catch (IllegalStateException success) {
        }
    }
View Full Code Here

    public void testAddAll5() {
        Integer[] empty = new Integer[0];
        Integer[] ints = new Integer[SIZE];
        for (int i = 0; i < SIZE; ++i)
            ints[i] = new Integer(i);
        BlockingQueue q = new LocalConcurrentBlockingObjectQueue(SIZE);
        assertFalse(q.addAll(Arrays.asList(empty)));
        assertTrue(q.addAll(Arrays.asList(ints)));
        for (int i = 0; i < SIZE; ++i)
            assertEquals(ints[i], q.poll());
    }
View Full Code Here

    /**
     * all elements successfully put are contained
     */
    @Test
    public void testPut() throws InterruptedException {
        BlockingQueue q = new LocalConcurrentBlockingObjectQueue(SIZE);
        for (int i = 0; i < SIZE; ++i) {
            Integer I = new Integer(i);
            q.put(I);
            assertTrue(q.contains(I));
        }
        assertEquals(0, q.remainingCapacity());
    }
View Full Code Here

    /**
     * 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());
    }
View Full Code Here

     * 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());
    }
View Full Code Here

     * 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) {
                }
            }
        });
View Full Code Here

     * containsAll(c) is true when c contains a subset of elements
     */
    @Test
    public void testContainsAll() {
        BlockingQueue q = populatedQueue(SIZE);
        LocalConcurrentBlockingObjectQueue p = new LocalConcurrentBlockingObjectQueue(SIZE);
        for (int i = 0; i < SIZE; ++i) {
            assertTrue(q.containsAll(p));
            assertFalse(p.containsAll(q));
            p.add(new Integer(i));
        }
        assertTrue(p.containsAll(q));
    }
View Full Code Here

    /**
     * toArray() contains all elements in FIFO order
     */
    @Test
    public void testToArray() {
        BlockingQueue q = new LocalConcurrentBlockingObjectQueue(SIZE);
        for (int i = 0; i < SIZE; i++) {
            checkToArray(q);
            q.add(i);
        }
        // Provoke wraparound
        for (int i = 0; i < SIZE; i++) {
            checkToArray(q);
            assertEquals(i, q.poll());
            checkToArray(q);
            q.add(SIZE + i);
        }
        for (int i = 0; i < SIZE; i++) {
            checkToArray(q);
            assertEquals(SIZE + i, q.poll());
        }
    }
View Full Code Here

TOP

Related Classes of net.openhft.chronicle.sandbox.queue.LocalConcurrentBlockingObjectQueue

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.