use of java.util.concurrent.LinkedBlockingQueue in project mapdb by jankotek.
the class LinkedBlockingQueueTest method testBlockingPut.
/**
* put blocks interruptibly if full
*/
public void testBlockingPut() throws InterruptedException {
final LinkedBlockingQueue q = new LinkedBlockingQueue(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.LinkedBlockingQueue in project mapdb by jankotek.
the class LinkedBlockingQueueTest method testClear.
/**
* clear removes all elements
*/
public void testClear() {
LinkedBlockingQueue q = populatedQueue(SIZE);
q.clear();
assertTrue(q.isEmpty());
assertEquals(0, q.size());
assertEquals(SIZE, q.remainingCapacity());
q.add(one);
assertFalse(q.isEmpty());
assertTrue(q.contains(one));
q.clear();
assertTrue(q.isEmpty());
}
use of java.util.concurrent.LinkedBlockingQueue in project mapdb by jankotek.
the class LinkedBlockingQueueTest method testDrainToN.
/**
* drainTo(c, n) empties first min(n, size) elements of queue into c
*/
public void testDrainToN() {
LinkedBlockingQueue q = new LinkedBlockingQueue();
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.LinkedBlockingQueue in project mapdb by jankotek.
the class LinkedBlockingQueueTest method testRemoveElementAndAdd.
/**
* An add following remove(x) succeeds
*/
public void testRemoveElementAndAdd() throws InterruptedException {
LinkedBlockingQueue q = new LinkedBlockingQueue();
assertTrue(q.add(new Integer(1)));
assertTrue(q.add(new Integer(2)));
assertTrue(q.remove(new Integer(1)));
assertTrue(q.remove(new Integer(2)));
assertTrue(q.add(new Integer(3)));
assertNotNull(q.take());
}
use of java.util.concurrent.LinkedBlockingQueue in project mapdb by jankotek.
the class LinkedBlockingQueueTest method testPutWithTake.
/**
* put blocks interruptibly waiting for take when full
*/
public void testPutWithTake() throws InterruptedException {
final int capacity = 2;
final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
final CountDownLatch pleaseTake = new CountDownLatch(1);
final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
for (int i = 0; i < capacity; i++) q.put(i);
pleaseTake.countDown();
q.put(86);
pleaseInterrupt.countDown();
try {
q.put(99);
shouldThrow();
} catch (InterruptedException success) {
}
assertFalse(Thread.interrupted());
}
});
await(pleaseTake);
assertEquals(0, q.remainingCapacity());
assertEquals(0, q.take());
await(pleaseInterrupt);
assertThreadStaysAlive(t);
t.interrupt();
awaitTermination(t);
assertEquals(0, q.remainingCapacity());
}
Aggregations