Java Code Examples for java.util.concurrent.LinkedBlockingDeque#add()

The following examples show how to use java.util.concurrent.LinkedBlockingDeque#add() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: j2objc   File: LinkedBlockingDequeTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Descending iterator ordering is reverse FIFO
 */
public void testDescendingIteratorOrdering() {
    final LinkedBlockingDeque q = new LinkedBlockingDeque();
    for (int iters = 0; iters < 100; ++iters) {
        q.add(new Integer(3));
        q.add(new Integer(2));
        q.add(new Integer(1));
        int k = 0;
        for (Iterator it = q.descendingIterator(); it.hasNext();) {
            assertEquals(++k, it.next());
        }

        assertEquals(3, k);
        q.remove();
        q.remove();
        q.remove();
    }
}
 
Example 2
Source Project: j2objc   File: LinkedBlockingDequeTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * offer transfers elements across Executor tasks
 */
public void testOfferInExecutor() {
    final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
    q.add(one);
    q.add(two);
    final CheckedBarrier threadsStarted = new CheckedBarrier(2);
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    try (PoolCleaner cleaner = cleaner(executor)) {
        executor.execute(new CheckedRunnable() {
            public void realRun() throws InterruptedException {
                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 InterruptedException {
                threadsStarted.await();
                assertSame(one, q.take());
            }});
    }
}
 
Example 3
/**
 * descendingIterator.remove removes current element
 */
public void testDescendingIteratorRemove() {
    final LinkedBlockingDeque q = new LinkedBlockingDeque();
    for (int iters = 0; iters < 100; ++iters) {
        q.add(new Integer(3));
        q.add(new Integer(2));
        q.add(new Integer(1));
        Iterator it = q.descendingIterator();
        assertEquals(it.next(), new Integer(1));
        it.remove();
        assertEquals(it.next(), new Integer(2));
        it = q.descendingIterator();
        assertEquals(it.next(), new Integer(2));
        assertEquals(it.next(), new Integer(3));
        it.remove();
        assertFalse(it.hasNext());
        q.remove();
    }
}
 
Example 4
@Test
@SuppressWarnings("CallToThreadRun")
public void testCleanup() throws IOException {
	TestWriteRequest request = new TestWriteRequest();
	LinkedBlockingDeque<ChannelStateWriteRequest> deque = new LinkedBlockingDeque<>();
	deque.add(request);
	TestRequestDispatcher requestProcessor = new TestRequestDispatcher();
	ChannelStateWriteRequestExecutorImpl worker = new ChannelStateWriteRequestExecutorImpl(TASK_NAME, requestProcessor, deque);

	worker.close();
	worker.run();

	assertTrue(requestProcessor.isStopped());
	assertTrue(deque.isEmpty());
	assertTrue(request.isCancelled());
}
 
Example 5
/**
 * drainTo(c) empties deque into another collection c
 */
public void testDrainTo() {
    LinkedBlockingDeque q = populatedDeque(SIZE);
    ArrayList l = new ArrayList();
    q.drainTo(l);
    assertEquals(0, q.size());
    assertEquals(SIZE, l.size());
    for (int i = 0; i < SIZE; ++i)
        assertEquals(l.get(i), new Integer(i));
    q.add(zero);
    q.add(one);
    assertFalse(q.isEmpty());
    assertTrue(q.contains(zero));
    assertTrue(q.contains(one));
    l.clear();
    q.drainTo(l);
    assertEquals(0, q.size());
    assertEquals(2, l.size());
    for (int i = 0; i < 2; ++i)
        assertEquals(l.get(i), new Integer(i));
}
 
Example 6
Source Project: j2objc   File: LinkedBlockingDequeTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * drainTo(c) empties deque into another collection c
 */
public void testDrainTo() {
    LinkedBlockingDeque q = populatedDeque(SIZE);
    ArrayList l = new ArrayList();
    q.drainTo(l);
    assertEquals(0, q.size());
    assertEquals(SIZE, l.size());
    for (int i = 0; i < SIZE; ++i)
        assertEquals(l.get(i), new Integer(i));
    q.add(zero);
    q.add(one);
    assertFalse(q.isEmpty());
    assertTrue(q.contains(zero));
    assertTrue(q.contains(one));
    l.clear();
    q.drainTo(l);
    assertEquals(0, q.size());
    assertEquals(2, l.size());
    for (int i = 0; i < 2; ++i)
        assertEquals(l.get(i), new Integer(i));
}
 
Example 7
Source Project: j2objc   File: LinkedBlockingDequeTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * descendingIterator.remove removes current element
 */
public void testDescendingIteratorRemove() {
    final LinkedBlockingDeque q = new LinkedBlockingDeque();
    for (int iters = 0; iters < 100; ++iters) {
        q.add(new Integer(3));
        q.add(new Integer(2));
        q.add(new Integer(1));
        Iterator it = q.descendingIterator();
        assertEquals(it.next(), new Integer(1));
        it.remove();
        assertEquals(it.next(), new Integer(2));
        it = q.descendingIterator();
        assertEquals(it.next(), new Integer(2));
        assertEquals(it.next(), new Integer(3));
        it.remove();
        assertFalse(it.hasNext());
        q.remove();
    }
}
 
Example 8
Source Project: j2objc   File: LinkedBlockingDequeTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * iterator.remove removes current element
 */
public void testIteratorRemove() {
    final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
    q.add(two);
    q.add(one);
    q.add(three);

    Iterator it = q.iterator();
    it.next();
    it.remove();

    it = q.iterator();
    assertSame(it.next(), one);
    assertSame(it.next(), three);
    assertFalse(it.hasNext());
}
 
Example 9
/**
 * isEmpty is true before add, false after
 */
public void testEmpty() {
    LinkedBlockingDeque q = new LinkedBlockingDeque();
    assertTrue(q.isEmpty());
    q.add(new Integer(1));
    assertFalse(q.isEmpty());
    q.add(new Integer(2));
    q.removeFirst();
    q.removeFirst();
    assertTrue(q.isEmpty());
}
 
Example 10
/**
 * size changes when elements added and removed
 */
public void testSize() {
    LinkedBlockingDeque q = populatedDeque(SIZE);
    for (int i = 0; i < SIZE; ++i) {
        assertEquals(SIZE - i, q.size());
        q.removeFirst();
    }
    for (int i = 0; i < SIZE; ++i) {
        assertEquals(i, q.size());
        q.add(new Integer(i));
    }
}
 
Example 11
/**
 * Deque transitions from empty to full when elements added
 */
public void testEmptyFull() {
    LinkedBlockingDeque q = new LinkedBlockingDeque(2);
    assertTrue(q.isEmpty());
    assertEquals("should have room for 2", 2, q.remainingCapacity());
    q.add(one);
    assertFalse(q.isEmpty());
    q.add(two);
    assertFalse(q.isEmpty());
    assertEquals(0, q.remainingCapacity());
    assertFalse(q.offer(three));
}
 
Example 12
/**
 * add succeeds if not full; throws ISE if full
 */
public void testAdd() {
    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
    for (int i = 0; i < SIZE; ++i)
        assertTrue(q.add(new Integer(i)));
    assertEquals(0, q.remainingCapacity());
    try {
        q.add(new Integer(SIZE));
        shouldThrow();
    } catch (IllegalStateException success) {}
}
 
Example 13
/**
 * clear removes all elements
 */
public void testClear() {
    LinkedBlockingDeque q = populatedDeque(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());
}
 
Example 14
/**
 * containsAll(c) is true when c contains a subset of elements
 */
public void testContainsAll() {
    LinkedBlockingDeque q = populatedDeque(SIZE);
    LinkedBlockingDeque p = new LinkedBlockingDeque(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));
}
 
Example 15
Source Project: j2objc   File: LinkedBlockingDequeTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * iterator ordering is FIFO
 */
public void testIteratorOrdering() {
    final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
    q.add(one);
    q.add(two);
    q.add(three);
    assertEquals(0, q.remainingCapacity());
    int k = 0;
    for (Iterator it = q.iterator(); it.hasNext();) {
        assertEquals(++k, it.next());
    }
    assertEquals(3, k);
}
 
Example 16
/**
 * iterator ordering is FIFO
 */
public void testIteratorOrdering() {
    final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
    q.add(one);
    q.add(two);
    q.add(three);
    assertEquals(0, q.remainingCapacity());
    int k = 0;
    for (Iterator it = q.iterator(); it.hasNext();) {
        assertEquals(++k, it.next());
    }
    assertEquals(3, k);
}
 
Example 17
Source Project: j2objc   File: LinkedBlockingDequeTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * size changes when elements added and removed
 */
public void testSize() {
    LinkedBlockingDeque q = populatedDeque(SIZE);
    for (int i = 0; i < SIZE; ++i) {
        assertEquals(SIZE - i, q.size());
        q.removeFirst();
    }
    for (int i = 0; i < SIZE; ++i) {
        assertEquals(i, q.size());
        q.add(new Integer(i));
    }
}
 
Example 18
Source Project: j2objc   File: LinkedBlockingDequeTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Deque transitions from empty to full when elements added
 */
public void testEmptyFull() {
    LinkedBlockingDeque q = new LinkedBlockingDeque(2);
    assertTrue(q.isEmpty());
    assertEquals("should have room for 2", 2, q.remainingCapacity());
    q.add(one);
    assertFalse(q.isEmpty());
    q.add(two);
    assertFalse(q.isEmpty());
    assertEquals(0, q.remainingCapacity());
    assertFalse(q.offer(three));
}
 
Example 19
Source Project: j2objc   File: LinkedBlockingDequeTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * add succeeds if not full; throws ISE if full
 */
public void testAdd() {
    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
    for (int i = 0; i < SIZE; ++i)
        assertTrue(q.add(new Integer(i)));
    assertEquals(0, q.remainingCapacity());
    try {
        q.add(new Integer(SIZE));
        shouldThrow();
    } catch (IllegalStateException success) {}
}
 
Example 20
Source Project: j2objc   File: LinkedBlockingDequeTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Modifications do not cause iterators to fail
 */
public void testWeaklyConsistentIteration() {
    final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
    q.add(one);
    q.add(two);
    q.add(three);
    for (Iterator it = q.iterator(); it.hasNext();) {
        q.remove();
        it.next();
    }
    assertEquals(0, q.size());
}