use of java.util.concurrent.LinkedBlockingDeque in project mapdb by jankotek.
the class LinkedBlockingDequeTest method testDrainToWithActivePut.
/**
* drainTo empties full deque, unblocking a waiting put.
*/
public void testDrainToWithActivePut() throws InterruptedException {
final LinkedBlockingDeque q = populatedDeque(SIZE);
Thread t = new Thread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
q.put(new Integer(SIZE + 1));
}
});
t.start();
ArrayList l = new ArrayList();
q.drainTo(l);
assertTrue(l.size() >= SIZE);
for (int i = 0; i < SIZE; ++i) assertEquals(l.get(i), new Integer(i));
t.join();
assertTrue(q.size() + l.size() >= SIZE);
}
use of java.util.concurrent.LinkedBlockingDeque in project mapdb by jankotek.
the class LinkedBlockingDequeTest method testTimedOffer.
/**
* timed offer times out if full and elements not taken
*/
public void testTimedOffer() throws InterruptedException {
final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
q.put(new Object());
q.put(new Object());
long startTime = System.nanoTime();
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
pleaseInterrupt.countDown();
try {
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
shouldThrow();
} catch (InterruptedException success) {
}
}
});
await(pleaseInterrupt);
assertThreadStaysAlive(t);
t.interrupt();
awaitTermination(t);
}
use of java.util.concurrent.LinkedBlockingDeque in project mapdb by jankotek.
the class LinkedBlockingDequeTest method testAddAll3.
/**
* addAll of a collection with any null elements throws NPE after
* possibly adding some elements
*/
public void testAddAll3() {
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
Integer[] ints = new Integer[SIZE];
for (int i = 0; i < SIZE - 1; ++i) ints[i] = new Integer(i);
Collection<Integer> elements = Arrays.asList(ints);
try {
q.addAll(elements);
shouldThrow();
} catch (NullPointerException success) {
}
}
use of java.util.concurrent.LinkedBlockingDeque in project mapdb by jankotek.
the class LinkedBlockingDequeTest method testEmpty.
/**
* isEmpty is true before add, false after
*/
public void testEmpty() {
LinkedBlockingDeque q = new LinkedBlockingDeque();
assertTrue(q.isEmpty());
q.add(new Integer(1));
assertFalse(q.isEmpty());
q.add(new Integer(2));
q.removeFirst();
q.removeFirst();
assertTrue(q.isEmpty());
}
use of java.util.concurrent.LinkedBlockingDeque in project mapdb by jankotek.
the class LinkedBlockingDequeTest method testRemoveLast.
/**
* removeLast() removes last element, or throws NSEE if empty
*/
public void testRemoveLast() {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = SIZE - 1; i >= 0; --i) {
assertEquals(i, q.removeLast());
}
try {
q.removeLast();
shouldThrow();
} catch (NoSuchElementException success) {
}
assertNull(q.peekLast());
}
Aggregations