use of java.util.concurrent.BlockingQueue in project mapdb by jankotek.
the class BlockingQueueTest method testTimedPollWithOffer.
/**
* timed poll before a delayed offer times out; after offer succeeds;
* on interruption throws
*/
public void testTimedPollWithOffer() throws InterruptedException {
final BlockingQueue q = emptyCollection();
final CheckedBarrier barrier = new CheckedBarrier(2);
final Object zero = makeElement(0);
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.offer(zero, LONG_DELAY_MS, MILLISECONDS));
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
barrier.await();
assertThreadStaysAlive(t);
t.interrupt();
awaitTermination(t);
}
use of java.util.concurrent.BlockingQueue in project mapdb by jankotek.
the class BlockingQueueTest method testTimedOfferNull.
/**
* timed offer(null) throws NullPointerException
*/
public void testTimedOfferNull() throws InterruptedException {
final BlockingQueue q = emptyCollection();
long startTime = System.nanoTime();
try {
q.offer(null, LONG_DELAY_MS, MILLISECONDS);
shouldThrow();
} catch (NullPointerException success) {
}
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
use of java.util.concurrent.BlockingQueue in project mapdb by jankotek.
the class BlockingQueueTest method testTakeFromEmptyBlocksInterruptibly.
/**
* take() blocks interruptibly when empty
*/
public void testTakeFromEmptyBlocksInterruptibly() {
final BlockingQueue q = emptyCollection();
final CountDownLatch threadStarted = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() {
threadStarted.countDown();
try {
q.take();
shouldThrow();
} catch (InterruptedException success) {
}
assertFalse(Thread.interrupted());
}
});
await(threadStarted);
assertThreadStaysAlive(t);
t.interrupt();
awaitTermination(t);
}
use of java.util.concurrent.BlockingQueue in project mapdb by jankotek.
the class BlockingQueueTest method testDrainToSelfN.
/**
* drainTo(this, n) throws IllegalArgumentException
*/
public void testDrainToSelfN() {
final BlockingQueue q = emptyCollection();
try {
q.drainTo(q, 0);
shouldThrow();
} catch (IllegalArgumentException success) {
}
}
use of java.util.concurrent.BlockingQueue in project mapdb by jankotek.
the class BlockingQueueTest method testRemoveElement.
/**
* remove(x) removes x and returns true if present
*
*/
public void testRemoveElement() {
final BlockingQueue q = emptyCollection();
final int size = Math.min(q.remainingCapacity(), SIZE);
final Object[] elts = new Object[size];
assertFalse(q.contains(makeElement(99)));
assertFalse(q.remove(makeElement(99)));
checkEmpty(q);
for (int i = 0; i < size; i++) q.add(elts[i] = makeElement(i));
for (int i = 1; i < size; i += 2) {
for (int pass = 0; pass < 2; pass++) {
assertEquals((pass == 0), q.contains(elts[i]));
assertEquals((pass == 0), q.remove(elts[i]));
assertFalse(q.contains(elts[i]));
assertTrue(q.contains(elts[i - 1]));
if (i < size - 1)
assertTrue(q.contains(elts[i + 1]));
}
}
if (size > 0)
assertTrue(q.contains(elts[0]));
for (int i = size - 2; i >= 0; i -= 2) {
assertTrue(q.contains(elts[i]));
assertFalse(q.contains(elts[i + 1]));
assertTrue(q.remove(elts[i]));
assertFalse(q.contains(elts[i]));
assertFalse(q.remove(elts[i + 1]));
assertFalse(q.contains(elts[i + 1]));
}
checkEmpty(q);
}
Aggregations