Search in sources :

Example 21 with LinkedTransferQueue

use of java.util.concurrent.LinkedTransferQueue in project j2objc by google.

the class LinkedTransferQueueTest method testDrainToN.

/**
 * drainTo(c, n) empties first min(n, size) elements of queue into c
 */
public void testDrainToN() {
    LinkedTransferQueue q = new LinkedTransferQueue();
    for (int i = 0; i < SIZE + 2; ++i) {
        for (int j = 0; j < SIZE; j++) {
            assertTrue(q.offer(j));
        }
        ArrayList l = new ArrayList();
        q.drainTo(l, i);
        int k = (i < SIZE) ? i : SIZE;
        assertEquals(k, l.size());
        assertEquals(SIZE - k, q.size());
        for (int j = 0; j < k; ++j) assertEquals(j, l.get(j));
        do {
        } while (q.poll() != null);
    }
}
Also used : ArrayList(java.util.ArrayList) LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue)

Example 22 with LinkedTransferQueue

use of java.util.concurrent.LinkedTransferQueue in project j2objc by google.

the class LinkedTransferQueueTest method testWaitingConsumer.

/**
 * timed poll() or take() increments the waiting consumer count;
 * offer(e) decrements the waiting consumer count
 */
public void testWaitingConsumer() throws InterruptedException {
    final LinkedTransferQueue q = new LinkedTransferQueue();
    assertEquals(0, q.getWaitingConsumerCount());
    assertFalse(q.hasWaitingConsumer());
    final CountDownLatch threadStarted = new CountDownLatch(1);
    Thread t = newStartedThread(new CheckedRunnable() {

        public void realRun() throws InterruptedException {
            threadStarted.countDown();
            long startTime = System.nanoTime();
            assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
            assertEquals(0, q.getWaitingConsumerCount());
            assertFalse(q.hasWaitingConsumer());
            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
        }
    });
    threadStarted.await();
    Callable<Boolean> oneConsumer = new Callable<Boolean>() {

        public Boolean call() {
            return q.hasWaitingConsumer() && q.getWaitingConsumerCount() == 1;
        }
    };
    waitForThreadToEnterWaitState(t, oneConsumer);
    assertTrue(q.offer(one));
    assertEquals(0, q.getWaitingConsumerCount());
    assertFalse(q.hasWaitingConsumer());
    awaitTermination(t);
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue) Callable(java.util.concurrent.Callable)

Example 23 with LinkedTransferQueue

use of java.util.concurrent.LinkedTransferQueue in project j2objc by google.

the class LinkedTransferQueueTest method testToArray1_BadArg.

/**
 * toArray(incompatible array type) throws ArrayStoreException
 */
public void testToArray1_BadArg() {
    LinkedTransferQueue q = populatedQueue(SIZE);
    try {
        q.toArray(new String[10]);
        shouldThrow();
    } catch (ArrayStoreException success) {
    }
}
Also used : LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue)

Example 24 with LinkedTransferQueue

use of java.util.concurrent.LinkedTransferQueue in project j2objc by google.

the class LinkedTransferQueueTest method testToString.

/**
 * toString contains toStrings of elements
 */
public void testToString() {
    LinkedTransferQueue q = populatedQueue(SIZE);
    String s = q.toString();
    for (int i = 0; i < SIZE; ++i) {
        assertTrue(s.contains(String.valueOf(i)));
    }
}
Also used : LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue)

Example 25 with LinkedTransferQueue

use of java.util.concurrent.LinkedTransferQueue in project j2objc by google.

the class LinkedTransferQueueTest method testIterator.

/**
 * iterator iterates through all elements
 */
public void testIterator() throws InterruptedException {
    LinkedTransferQueue q = populatedQueue(SIZE);
    Iterator it = q.iterator();
    int i;
    for (i = 0; it.hasNext(); i++) assertTrue(q.contains(it.next()));
    assertEquals(i, SIZE);
    assertIteratorExhausted(it);
    it = q.iterator();
    for (i = 0; it.hasNext(); i++) assertEquals(it.next(), q.take());
    assertEquals(i, SIZE);
    assertIteratorExhausted(it);
}
Also used : Iterator(java.util.Iterator) LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue)

Aggregations

LinkedTransferQueue (java.util.concurrent.LinkedTransferQueue)66 ArrayList (java.util.ArrayList)10 Iterator (java.util.Iterator)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 List (java.util.List)4 ExecutorService (java.util.concurrent.ExecutorService)4 Callable (java.util.concurrent.Callable)2 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 MappedByteBuffer (java.nio.MappedByteBuffer)1 FileChannel (java.nio.channels.FileChannel)1 AbstractList (java.util.AbstractList)1 Arrays.asList (java.util.Arrays.asList)1 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 Queue (java.util.Queue)1 RejectedExecutionHandler (java.util.concurrent.RejectedExecutionHandler)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 TransferQueue (java.util.concurrent.TransferQueue)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1