use of java.util.concurrent.BlockingQueue in project HugeCollections-OLD by peter-lawrey.
the class LocalJSR166TestCase method testRemainingCapacity.
/**
* remainingCapacity decreases on add, increases on remove
*/
@Test
public void testRemainingCapacity() {
BlockingQueue q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, q.remainingCapacity());
assertEquals(SIZE - i, q.size());
q.remove();
}
for (int i = 0; i < SIZE; ++i) {
assertEquals(SIZE - i, q.remainingCapacity());
assertEquals(i, q.size());
q.add(new Integer(i));
}
}
use of java.util.concurrent.BlockingQueue in project HugeCollections-OLD by peter-lawrey.
the class LocalJSR166TestCase method testAdd.
/**
* add succeeds if not full; throws ISE if full
*/
@Test
public void testAdd() {
try {
BlockingQueue q = new LocalConcurrentBlockingObjectQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertTrue(q.add(new Integer(i)));
}
assertEquals(0, q.remainingCapacity());
q.add(new Integer(SIZE));
shouldThrow();
} catch (IllegalStateException success) {
}
}
use of java.util.concurrent.BlockingQueue in project HugeCollections-OLD by peter-lawrey.
the class LocalJSR166TestCase method testElement.
/**
* element returns next element, or throws NSEE if empty
*/
@Ignore
@Test
public void testElement() {
BlockingQueue q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, q.element());
assertEquals(i, q.poll());
}
try {
q.element();
shouldThrow();
} catch (NoSuchElementException success) {
}
}
use of java.util.concurrent.BlockingQueue in project HugeCollections-OLD by peter-lawrey.
the class LocalJSR166TestCase method testTimedPoll.
/**
* timed poll with nonzero timeout succeeds when non-empty, else times out
*/
@Test
public void testTimedPoll() throws InterruptedException {
BlockingQueue q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
long startTime = System.nanoTime();
assertEquals(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
long startTime = System.nanoTime();
assertNull(q.poll(timeoutMillis(), MILLISECONDS));
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
checkEmpty(q);
}
use of java.util.concurrent.BlockingQueue in project HugeCollections-OLD by peter-lawrey.
the class LocalJSR166TestCase method testPutWithTake.
/**
* put blocks interruptibly waiting for take when full
*/
@Test
public void testPutWithTake() throws InterruptedException {
final int capacity = 2;
final BlockingQueue q = new LocalConcurrentBlockingObjectQueue(capacity);
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