use of java.util.concurrent.LinkedBlockingDeque in project mapdb by jankotek.
the class LinkedBlockingDequeTest method testBlockingPut.
/**
* put blocks interruptibly if full
*/
public void testBlockingPut() throws InterruptedException {
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
for (int i = 0; i < SIZE; ++i) q.put(i);
assertEquals(SIZE, q.size());
assertEquals(0, q.remainingCapacity());
Thread.currentThread().interrupt();
try {
q.put(99);
shouldThrow();
} catch (InterruptedException success) {
}
assertFalse(Thread.interrupted());
pleaseInterrupt.countDown();
try {
q.put(99);
shouldThrow();
} catch (InterruptedException success) {
}
assertFalse(Thread.interrupted());
}
});
await(pleaseInterrupt);
assertThreadStaysAlive(t);
t.interrupt();
awaitTermination(t);
assertEquals(SIZE, q.size());
assertEquals(0, q.remainingCapacity());
}
use of java.util.concurrent.LinkedBlockingDeque in project mapdb by jankotek.
the class LinkedBlockingDequeTest method testTimedPollWithOfferLast.
/**
* timed poll before a delayed offerLast fails; after offerLast succeeds;
* on interruption throws
*/
public void testTimedPollWithOfferLast() 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.poll(timeoutMillis(), MILLISECONDS));
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
barrier.await();
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
Thread.currentThread().interrupt();
try {
q.poll(LONG_DELAY_MS, MILLISECONDS);
shouldThrow();
} catch (InterruptedException success) {
}
assertFalse(Thread.interrupted());
barrier.await();
try {
q.poll(LONG_DELAY_MS, MILLISECONDS);
shouldThrow();
} catch (InterruptedException success) {
}
assertFalse(Thread.interrupted());
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
});
barrier.await();
long startTime = System.nanoTime();
assertTrue(q.offerLast(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 testTimedPollLast.
/**
* timed pollLast with nonzero timeout succeeds when non-empty, else times out
*/
public void testTimedPollLast() throws InterruptedException {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
long startTime = System.nanoTime();
assertEquals(SIZE - i - 1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
long startTime = System.nanoTime();
assertNull(q.pollLast(timeoutMillis(), MILLISECONDS));
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
checkEmpty(q);
}
use of java.util.concurrent.LinkedBlockingDeque in project mapdb by jankotek.
the class LinkedBlockingDequeTest method testAddAll4.
/**
* addAll throws IllegalStateException if not enough room
*/
public void testAddAll4() {
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE - 1);
Integer[] ints = new Integer[SIZE];
for (int i = 0; i < SIZE; ++i) ints[i] = new Integer(i);
Collection<Integer> elements = Arrays.asList(ints);
try {
q.addAll(elements);
shouldThrow();
} catch (IllegalStateException success) {
}
}
use of java.util.concurrent.LinkedBlockingDeque in project mapdb by jankotek.
the class LinkedBlockingDequeTest method testTimedOfferFirst.
/**
* timed offerFirst times out if full and elements not taken
*/
public void testTimedOfferFirst() 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.putFirst(new Object());
q.putFirst(new Object());
long startTime = System.nanoTime();
assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS));
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
pleaseInterrupt.countDown();
try {
q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
shouldThrow();
} catch (InterruptedException success) {
}
}
});
await(pleaseInterrupt);
assertThreadStaysAlive(t);
t.interrupt();
awaitTermination(t);
}
Aggregations