use of java.util.concurrent.LinkedTransferQueue in project j2objc by google.
the class LinkedTransferQueueTest method testTransfer2.
/**
* transfer waits until a poll occurs. The transfered element
* is returned by this associated poll.
*/
public void testTransfer2() throws InterruptedException {
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
final CountDownLatch threadStarted = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadStarted.countDown();
q.transfer(five);
checkEmpty(q);
}
});
threadStarted.await();
Callable<Boolean> oneElement = new Callable<Boolean>() {
public Boolean call() {
return !q.isEmpty() && q.size() == 1;
}
};
waitForThreadToEnterWaitState(t, oneElement);
assertSame(five, q.poll());
checkEmpty(q);
awaitTermination(t);
}
use of java.util.concurrent.LinkedTransferQueue in project j2objc by google.
the class LinkedTransferQueueTest method testToArray.
/**
* toArray() contains all elements in FIFO order
*/
public void testToArray() {
LinkedTransferQueue q = populatedQueue(SIZE);
Object[] o = q.toArray();
for (int i = 0; i < o.length; i++) {
assertSame(o[i], q.poll());
}
}
use of java.util.concurrent.LinkedTransferQueue in project j2objc by google.
the class LinkedTransferQueueTest method testTryTransfer7.
/**
* tryTransfer waits for any elements previously in to be removed
* before transfering to a poll or take
*/
public void testTryTransfer7() throws InterruptedException {
final LinkedTransferQueue q = new LinkedTransferQueue();
assertTrue(q.offer(four));
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
long startTime = System.nanoTime();
assertTrue(q.tryTransfer(five, LONG_DELAY_MS, MILLISECONDS));
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
checkEmpty(q);
}
});
while (q.size() != 2) Thread.yield();
assertEquals(2, q.size());
assertSame(four, q.poll());
assertSame(five, q.poll());
checkEmpty(q);
awaitTermination(t);
}
use of java.util.concurrent.LinkedTransferQueue in project j2objc by google.
the class LinkedTransferQueueTest method testTryTransfer5.
/**
* tryTransfer blocks interruptibly if no takers
*/
public void testTryTransfer5() throws InterruptedException {
final LinkedTransferQueue q = new LinkedTransferQueue();
final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
assertTrue(q.isEmpty());
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
long startTime = System.nanoTime();
Thread.currentThread().interrupt();
try {
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
shouldThrow();
} catch (InterruptedException success) {
}
assertFalse(Thread.interrupted());
pleaseInterrupt.countDown();
try {
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
shouldThrow();
} catch (InterruptedException success) {
}
assertFalse(Thread.interrupted());
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
});
await(pleaseInterrupt);
assertThreadStaysAlive(t);
t.interrupt();
awaitTermination(t);
checkEmpty(q);
}
use of java.util.concurrent.LinkedTransferQueue in project j2objc by google.
the class LinkedTransferQueueTest method testTryTransfer2.
/**
* tryTransfer returns false and does not enqueue if there are no
* consumers waiting to poll or take.
*/
public void testTryTransfer2() throws InterruptedException {
final LinkedTransferQueue q = new LinkedTransferQueue();
assertFalse(q.tryTransfer(new Object()));
assertFalse(q.hasWaitingConsumer());
checkEmpty(q);
}
Aggregations