arrayblockingqueuetest.java
来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 1,061 行 · 第 1/3 页
JAVA
1,061 行
public void run() {
int added = 0;
try {
q.put(new Object());
++added;
q.put(new Object());
++added;
q.put(new Object());
++added;
q.put(new Object());
++added;
threadShouldThrow();
} catch (InterruptedException e){
threadAssertTrue(added >= 2);
}
}
});
try {
t.start();
Thread.sleep(SHORT_DELAY_MS);
q.take();
t.interrupt();
t.join();
} catch (Exception e){
unexpectedException();
}
}
/**
* timed offer times out if full and elements not taken
*/
public void testTimedOffer() {
final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
Thread t = new Thread(new Runnable() {
public void run() {
try {
q.put(new Object());
q.put(new Object());
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
threadShouldThrow();
} catch (InterruptedException success){}
}
});
try {
t.start();
Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
t.join();
} catch (Exception e){
unexpectedException();
}
}
/**
* take retrieves elements in FIFO order
*/
public void testTake() {
try {
ArrayBlockingQueue q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, ((Integer)q.take()).intValue());
}
} catch (InterruptedException e){
unexpectedException();
}
}
/**
* take blocks interruptibly when empty
*/
public void testTakeFromEmpty() {
final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
Thread t = new Thread(new Runnable() {
public void run() {
try {
q.take();
threadShouldThrow();
} catch (InterruptedException success){ }
}
});
try {
t.start();
Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
t.join();
} catch (Exception e){
unexpectedException();
}
}
/**
* Take removes existing elements until empty, then blocks interruptibly
*/
public void testBlockingTake() {
Thread t = new Thread(new Runnable() {
public void run() {
try {
ArrayBlockingQueue q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
threadAssertEquals(i, ((Integer)q.take()).intValue());
}
q.take();
threadShouldThrow();
} catch (InterruptedException success){
}
}});
try {
t.start();
Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
t.join();
}
catch (InterruptedException ie) {
unexpectedException();
}
}
/**
* poll succeeds unless empty
*/
public void testPoll() {
ArrayBlockingQueue q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, ((Integer)q.poll()).intValue());
}
assertNull(q.poll());
}
/**
* timed pool with zero timeout succeeds when non-empty, else times out
*/
public void testTimedPoll0() {
try {
ArrayBlockingQueue q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
}
assertNull(q.poll(0, TimeUnit.MILLISECONDS));
} catch (InterruptedException e){
unexpectedException();
}
}
/**
* timed pool with nonzero timeout succeeds when non-empty, else times out
*/
public void testTimedPoll() {
try {
ArrayBlockingQueue q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
}
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
} catch (InterruptedException e){
unexpectedException();
}
}
/**
* Interrupted timed poll throws InterruptedException instead of
* returning timeout status
*/
public void testInterruptedTimedPoll() {
Thread t = new Thread(new Runnable() {
public void run() {
try {
ArrayBlockingQueue q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
}
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
} catch (InterruptedException success){
}
}});
try {
t.start();
Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
t.join();
}
catch (InterruptedException ie) {
unexpectedException();
}
}
/**
* timed poll before a delayed offer fails; after offer succeeds;
* on interruption throws
*/
public void testTimedPollWithOffer() {
final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
Thread t = new Thread(new Runnable() {
public void run() {
try {
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
threadShouldThrow();
} catch (InterruptedException success) { }
}
});
try {
t.start();
Thread.sleep(SMALL_DELAY_MS);
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
t.interrupt();
t.join();
} catch (Exception e){
unexpectedException();
}
}
/**
* peek returns next element, or null if empty
*/
public void testPeek() {
ArrayBlockingQueue q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, ((Integer)q.peek()).intValue());
q.poll();
assertTrue(q.peek() == null ||
i != ((Integer)q.peek()).intValue());
}
assertNull(q.peek());
}
/**
* element returns next element, or throws NSEE if empty
*/
public void testElement() {
ArrayBlockingQueue q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, ((Integer)q.element()).intValue());
q.poll();
}
try {
q.element();
shouldThrow();
}
catch (NoSuchElementException success) {}
}
/**
* remove removes next element, or throws NSEE if empty
*/
public void testRemove() {
ArrayBlockingQueue q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, ((Integer)q.remove()).intValue());
}
try {
q.remove();
shouldThrow();
} catch (NoSuchElementException success){
}
}
/**
* remove(x) removes x and returns true if present
*/
public void testRemoveElement() {
ArrayBlockingQueue q = populatedQueue(SIZE);
for (int i = 1; i < SIZE; i+=2) {
assertTrue(q.remove(new Integer(i)));
}
for (int i = 0; i < SIZE; i+=2) {
assertTrue(q.remove(new Integer(i)));
assertFalse(q.remove(new Integer(i+1)));
}
assertTrue(q.isEmpty());
}
/**
* contains(x) reports true when elements added but not yet removed
*/
public void testContains() {
ArrayBlockingQueue q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertTrue(q.contains(new Integer(i)));
q.poll();
assertFalse(q.contains(new Integer(i)));
}
}
/**
* clear removes all elements
*/
public void testClear() {
ArrayBlockingQueue 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());
}
/**
* containsAll(c) is true when c contains a subset of elements
*/
public void testContainsAll() {
ArrayBlockingQueue q = populatedQueue(SIZE);
ArrayBlockingQueue p = new ArrayBlockingQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertTrue(q.containsAll(p));
assertFalse(p.containsAll(q));
p.add(new Integer(i));
}
assertTrue(p.containsAll(q));
}
/**
* retainAll(c) retains only those elements of c and reports true if changed
*/
public void testRetainAll() {
ArrayBlockingQueue q = populatedQueue(SIZE);
ArrayBlockingQueue p = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
boolean changed = q.retainAll(p);
if (i == 0)
assertFalse(changed);
else
assertTrue(changed);
assertTrue(q.containsAll(p));
assertEquals(SIZE-i, q.size());
p.remove();
}
}
/**
* removeAll(c) removes only those elements of c and reports true if changed
*/
public void testRemoveAll() {
for (int i = 1; i < SIZE; ++i) {
ArrayBlockingQueue q = populatedQueue(SIZE);
ArrayBlockingQueue p = populatedQueue(i);
assertTrue(q.removeAll(p));
assertEquals(SIZE-i, q.size());
for (int j = 0; j < i; ++j) {
Integer I = (Integer)(p.remove());
assertFalse(q.contains(I));
}
}
}
/**
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?