use of java.util.concurrent.LinkedTransferQueue in project mapdb by jankotek.
the class LinkedTransferQueueTest method testRemoveElementAndAdd.
/**
* An add following remove(x) succeeds
*/
public void testRemoveElementAndAdd() throws InterruptedException {
LinkedTransferQueue q = new LinkedTransferQueue();
assertTrue(q.add(one));
assertTrue(q.add(two));
assertTrue(q.remove(one));
assertTrue(q.remove(two));
assertTrue(q.add(three));
assertSame(q.take(), three);
}
use of java.util.concurrent.LinkedTransferQueue in project mapdb by jankotek.
the class LinkedTransferQueueTest method testDrainTo.
/**
* drainTo(c) empties queue into another collection c
*/
public void testDrainTo() {
LinkedTransferQueue q = populatedQueue(SIZE);
ArrayList l = new ArrayList();
q.drainTo(l);
assertEquals(0, q.size());
assertEquals(SIZE, l.size());
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, l.get(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(i, l.get(i));
}
}
use of java.util.concurrent.LinkedTransferQueue in project mapdb by jankotek.
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 mapdb by jankotek.
the class LinkedTransferQueueTest method testAddAll3.
/**
* addAll of a collection with any null elements throws
* NullPointerException after possibly adding some elements
*/
public void testAddAll3() {
LinkedTransferQueue q = new LinkedTransferQueue();
Integer[] ints = new Integer[SIZE];
for (int i = 0; i < SIZE - 1; ++i) ints[i] = i;
try {
q.addAll(Arrays.asList(ints));
shouldThrow();
} catch (NullPointerException success) {
}
}
use of java.util.concurrent.LinkedTransferQueue in project mapdb by jankotek.
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();
waitForThreadToEnterWaitState(t);
assertEquals(1, q.getWaitingConsumerCount());
assertTrue(q.hasWaitingConsumer());
assertTrue(q.offer(one));
assertEquals(0, q.getWaitingConsumerCount());
assertFalse(q.hasWaitingConsumer());
awaitTermination(t);
}
Aggregations