use of java.util.concurrent.LinkedTransferQueue in project mapdb by jankotek.
the class LinkedTransferQueueTest method testOfferInExecutor.
/**
* offer transfers elements across Executor tasks
*/
public void testOfferInExecutor() {
final LinkedTransferQueue q = new LinkedTransferQueue();
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 {
threadsStarted.await();
long startTime = System.nanoTime();
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
});
executor.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadsStarted.await();
assertSame(one, q.take());
checkEmpty(q);
}
});
}
}
use of java.util.concurrent.LinkedTransferQueue in project mapdb by jankotek.
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();
waitForThreadToEnterWaitState(t);
assertEquals(1, q.size());
assertSame(five, q.poll());
checkEmpty(q);
awaitTermination(t);
}
use of java.util.concurrent.LinkedTransferQueue in project mapdb by jankotek.
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);
}
use of java.util.concurrent.LinkedTransferQueue in project mapdb by jankotek.
the class LinkedTransferQueueTest method testConstructor5.
/**
* Queue contains all elements of the collection it is initialized by
*/
public void testConstructor5() {
Integer[] ints = new Integer[SIZE];
for (int i = 0; i < SIZE; ++i) {
ints[i] = i;
}
List intList = Arrays.asList(ints);
LinkedTransferQueue q = new LinkedTransferQueue(intList);
assertEquals(q.size(), intList.size());
assertEquals(q.toString(), intList.toString());
assertTrue(Arrays.equals(q.toArray(), intList.toArray()));
assertTrue(Arrays.equals(q.toArray(new Object[0]), intList.toArray(new Object[0])));
assertTrue(Arrays.equals(q.toArray(new Object[SIZE]), intList.toArray(new Object[SIZE])));
for (int i = 0; i < SIZE; ++i) {
assertEquals(ints[i], q.poll());
}
}
use of java.util.concurrent.LinkedTransferQueue in project mapdb by jankotek.
the class LinkedTransferQueueTest method testTryTransfer6.
/**
* tryTransfer gives up after the timeout and returns false
*/
public void testTryTransfer6() throws InterruptedException {
final LinkedTransferQueue q = new LinkedTransferQueue();
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
long startTime = System.nanoTime();
assertFalse(q.tryTransfer(new Object(), timeoutMillis(), MILLISECONDS));
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
checkEmpty(q);
}
});
awaitTermination(t);
checkEmpty(q);
}
Aggregations