use of net.openhft.chronicle.sandbox.queue.SharedConcurrentBlockingObjectQueue in project HugeCollections-OLD by peter-lawrey.
the class SharedJSR166TestCase method testTimedOffer.
/**
* timed offer times out if full and elements not taken
*/
@Ignore
@Test
public void testTimedOffer() throws Exception, IOException {
final BlockingQueue q = new SharedConcurrentBlockingObjectQueue(2, Integer.class);
final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws Exception {
q.put(new Object());
q.put(new Object());
long startTime = System.nanoTime();
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
pleaseInterrupt.countDown();
try {
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
shouldThrow();
} catch (InterruptedException success) {
}
}
});
await(pleaseInterrupt);
assertThreadStaysAlive(t);
t.interrupt();
awaitTermination(t);
}
use of net.openhft.chronicle.sandbox.queue.SharedConcurrentBlockingObjectQueue in project HugeCollections-OLD by peter-lawrey.
the class SharedJSR166TestCase method testContainsAll.
/**
* containsAll(c) is true when c contains a subset of elements
*/
@Test
public void testContainsAll() throws IOException {
BlockingQueue<Integer> q = populatedQueue(SIZE);
BlockingQueue p = new SharedConcurrentBlockingObjectQueue<Integer>(SIZE, Integer.class);
for (int i = 0; i < SIZE; ++i) {
assertTrue(q.containsAll(p));
assertFalse(p.containsAll(q));
p.add(new Integer(i));
}
assertTrue(p.containsAll(q));
}
use of net.openhft.chronicle.sandbox.queue.SharedConcurrentBlockingObjectQueue in project HugeCollections-OLD by peter-lawrey.
the class SharedJSR166TestCase method testPutWithTake.
/**
* put blocks interruptibly waiting for take when full
*/
@Test
public void testPutWithTake() throws Exception {
final int capacity = 2;
final BlockingQueue q = new SharedConcurrentBlockingObjectQueue<Integer>(capacity, Integer.class);
final CountDownLatch pleaseTake = new CountDownLatch(1);
final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws Exception {
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());
}
use of net.openhft.chronicle.sandbox.queue.SharedConcurrentBlockingObjectQueue in project HugeCollections-OLD by peter-lawrey.
the class SharedJSR166TestCase method testBlockingPut.
/**
* put blocks interruptibly if full
*/
@Test
public void testBlockingPut() throws Exception, IOException {
final BlockingQueue<Integer> q = new SharedConcurrentBlockingObjectQueue<Integer>(SIZE, Integer.class);
final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws Exception {
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 net.openhft.chronicle.sandbox.queue.SharedConcurrentBlockingObjectQueue in project HugeCollections-OLD by peter-lawrey.
the class SharedJSR166TestCase method testOfferInExecutor.
/**
* offer transfers elements across Executor tasks
*/
@Test
public void testOfferInExecutor() throws IOException {
final BlockingQueue<Integer> q = new SharedConcurrentBlockingObjectQueue<Integer>(2, Integer.class);
q.add(one);
q.add(two);
ExecutorService executor = Executors.newFixedThreadPool(2);
final CheckedBarrier threadsStarted = new CheckedBarrier(2);
executor.execute(new CheckedRunnable() {
public void realRun() throws Exception {
assertFalse(q.offer(three));
threadsStarted.await();
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
assertEquals(0, q.remainingCapacity());
}
});
executor.execute(new CheckedRunnable() {
public void realRun() throws Exception {
threadsStarted.await();
assertEquals(0, q.remainingCapacity());
assertEquals(one, q.take());
}
});
joinPool(executor);
}
Aggregations