use of java.util.concurrent.LinkedBlockingDeque in project mapdb by jankotek.
the class LinkedBlockingDequeTest method testTimedPollFirstWithOfferFirst.
/**
* timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
* on interruption throws
*/
public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
final CheckedBarrier barrier = new CheckedBarrier(2);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
long startTime = System.nanoTime();
assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS));
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
barrier.await();
assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
Thread.currentThread().interrupt();
try {
q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
shouldThrow();
} catch (InterruptedException success) {
}
barrier.await();
try {
q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
shouldThrow();
} catch (InterruptedException success) {
}
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
});
barrier.await();
long startTime = System.nanoTime();
assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS));
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
barrier.await();
assertThreadStaysAlive(t);
t.interrupt();
awaitTermination(t);
}
use of java.util.concurrent.LinkedBlockingDeque in project mapdb by jankotek.
the class LinkedBlockingDequeTest method testBlockingTake.
/**
* take removes existing elements until empty, then blocks interruptibly
*/
public void testBlockingTake() throws InterruptedException {
final LinkedBlockingDeque q = populatedDeque(SIZE);
final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, q.take());
}
Thread.currentThread().interrupt();
try {
q.take();
shouldThrow();
} catch (InterruptedException success) {
}
assertFalse(Thread.interrupted());
pleaseInterrupt.countDown();
try {
q.take();
shouldThrow();
} catch (InterruptedException success) {
}
assertFalse(Thread.interrupted());
}
});
await(pleaseInterrupt);
assertThreadStaysAlive(t);
t.interrupt();
awaitTermination(t);
}
use of java.util.concurrent.LinkedBlockingDeque in project mapdb by jankotek.
the class LinkedBlockingDequeTest method testDrainToN.
/**
* drainTo(c, n) empties first min(n, size) elements of queue into c
*/
public void testDrainToN() {
LinkedBlockingDeque q = new LinkedBlockingDeque();
for (int i = 0; i < SIZE + 2; ++i) {
for (int j = 0; j < SIZE; j++) assertTrue(q.offer(new Integer(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(l.get(j), new Integer(j));
do {
} while (q.poll() != null);
}
}
use of java.util.concurrent.LinkedBlockingDeque in project mapdb by jankotek.
the class LinkedBlockingDequeTest method testTimedOfferLast.
/**
* timed offerLast times out if full and elements not taken
*/
public void testTimedOfferLast() 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.putLast(new Object());
q.putLast(new Object());
long startTime = System.nanoTime();
assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS));
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
pleaseInterrupt.countDown();
try {
q.offerLast(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 testPutLast.
/**
* all elements successfully putLast are contained
*/
public void testPutLast() throws InterruptedException {
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
Integer x = new Integer(i);
q.putLast(x);
assertTrue(q.contains(x));
}
assertEquals(0, q.remainingCapacity());
}
Aggregations