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);
}
}
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);
}
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) {
}
}
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)));
}
}
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);
}
Aggregations