arrayblockingqueuetest.java
来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 1,061 行 · 第 1/3 页
JAVA
1,061 行
* toArray contains all elements
*/
public void testToArray() {
ArrayBlockingQueue q = populatedQueue(SIZE);
Object[] o = q.toArray();
try {
for(int i = 0; i < o.length; i++)
assertEquals(o[i], q.take());
} catch (InterruptedException e){
unexpectedException();
}
}
/**
* toArray(a) contains all elements
*/
public void testToArray2() {
ArrayBlockingQueue q = populatedQueue(SIZE);
Integer[] ints = new Integer[SIZE];
ints = (Integer[])q.toArray(ints);
try {
for(int i = 0; i < ints.length; i++)
assertEquals(ints[i], q.take());
} catch (InterruptedException e){
unexpectedException();
}
}
/**
* toArray(null) throws NPE
*/
public void testToArray_BadArg() {
try {
ArrayBlockingQueue q = populatedQueue(SIZE);
Object o[] = q.toArray(null);
shouldThrow();
} catch(NullPointerException success){}
}
/**
* toArray with incompatible array type throws CCE
*/
public void testToArray1_BadArg() {
try {
ArrayBlockingQueue q = populatedQueue(SIZE);
Object o[] = q.toArray(new String[10] );
shouldThrow();
} catch(ArrayStoreException success){}
}
/**
* iterator iterates through all elements
*/
public void testIterator() {
ArrayBlockingQueue q = populatedQueue(SIZE);
Iterator it = q.iterator();
try {
while(it.hasNext()){
assertEquals(it.next(), q.take());
}
} catch (InterruptedException e){
unexpectedException();
}
}
/**
* iterator.remove removes current element
*/
public void testIteratorRemove () {
final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
q.add(two);
q.add(one);
q.add(three);
Iterator it = q.iterator();
it.next();
it.remove();
it = q.iterator();
assertEquals(it.next(), one);
assertEquals(it.next(), three);
assertFalse(it.hasNext());
}
/**
* iterator ordering is FIFO
*/
public void testIteratorOrdering() {
final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
q.add(one);
q.add(two);
q.add(three);
assertEquals("queue should be full", 0, q.remainingCapacity());
int k = 0;
for (Iterator it = q.iterator(); it.hasNext();) {
int i = ((Integer)(it.next())).intValue();
assertEquals(++k, i);
}
assertEquals(3, k);
}
/**
* Modifications do not cause iterators to fail
*/
public void testWeaklyConsistentIteration () {
final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
q.add(one);
q.add(two);
q.add(three);
try {
for (Iterator it = q.iterator(); it.hasNext();) {
q.remove();
it.next();
}
}
catch (ConcurrentModificationException e) {
unexpectedException();
}
assertEquals(0, q.size());
}
/**
* toString contains toStrings of elements
*/
public void testToString() {
ArrayBlockingQueue q = populatedQueue(SIZE);
String s = q.toString();
for (int i = 0; i < SIZE; ++i) {
assertTrue(s.indexOf(String.valueOf(i)) >= 0);
}
}
/**
* offer transfers elements across Executor tasks
*/
public void testOfferInExecutor() {
final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
q.add(one);
q.add(two);
ExecutorService executor = Executors.newFixedThreadPool(2);
executor.execute(new Runnable() {
public void run() {
threadAssertFalse(q.offer(three));
try {
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
threadAssertEquals(0, q.remainingCapacity());
}
catch (InterruptedException e) {
threadUnexpectedException();
}
}
});
executor.execute(new Runnable() {
public void run() {
try {
Thread.sleep(SMALL_DELAY_MS);
threadAssertEquals(one, q.take());
}
catch (InterruptedException e) {
threadUnexpectedException();
}
}
});
joinPool(executor);
}
/**
* poll retrieves elements across Executor threads
*/
public void testPollInExecutor() {
final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
ExecutorService executor = Executors.newFixedThreadPool(2);
executor.execute(new Runnable() {
public void run() {
threadAssertNull(q.poll());
try {
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
threadAssertTrue(q.isEmpty());
}
catch (InterruptedException e) {
threadUnexpectedException();
}
}
});
executor.execute(new Runnable() {
public void run() {
try {
Thread.sleep(SMALL_DELAY_MS);
q.put(one);
}
catch (InterruptedException e) {
threadUnexpectedException();
}
}
});
joinPool(executor);
}
/**
* A deserialized serialized queue has same elements in same order
*/
public void testSerialization() {
ArrayBlockingQueue q = populatedQueue(SIZE);
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
out.writeObject(q);
out.close();
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
assertEquals(q.size(), r.size());
while (!q.isEmpty())
assertEquals(q.remove(), r.remove());
} catch(Exception e){
e.printStackTrace();
unexpectedException();
}
}
/**
* drainTo(null) throws NPE
*/
public void testDrainToNull() {
ArrayBlockingQueue q = populatedQueue(SIZE);
try {
q.drainTo(null);
shouldThrow();
} catch(NullPointerException success) {
}
}
/**
* drainTo(this) throws IAE
*/
public void testDrainToSelf() {
ArrayBlockingQueue q = populatedQueue(SIZE);
try {
q.drainTo(q);
shouldThrow();
} catch(IllegalArgumentException success) {
}
}
/**
* drainTo(c) empties queue into another collection c
*/
public void testDrainTo() {
ArrayBlockingQueue q = populatedQueue(SIZE);
ArrayList l = new ArrayList();
q.drainTo(l);
assertEquals(q.size(), 0);
assertEquals(l.size(), SIZE);
for (int i = 0; i < SIZE; ++i)
assertEquals(l.get(i), new Integer(i));
q.add(zero);
q.add(one);
assertFalse(q.isEmpty());
assertTrue(q.contains(zero));
assertTrue(q.contains(one));
l.clear();
q.drainTo(l);
assertEquals(q.size(), 0);
assertEquals(l.size(), 2);
for (int i = 0; i < 2; ++i)
assertEquals(l.get(i), new Integer(i));
}
/**
* drainTo empties full queue, unblocking a waiting put.
*/
public void testDrainToWithActivePut() {
final ArrayBlockingQueue q = populatedQueue(SIZE);
Thread t = new Thread(new Runnable() {
public void run() {
try {
q.put(new Integer(SIZE+1));
} catch (InterruptedException ie){
threadUnexpectedException();
}
}
});
try {
t.start();
ArrayList l = new ArrayList();
q.drainTo(l);
assertTrue(l.size() >= SIZE);
for (int i = 0; i < SIZE; ++i)
assertEquals(l.get(i), new Integer(i));
t.join();
assertTrue(q.size() + l.size() >= SIZE);
} catch(Exception e){
unexpectedException();
}
}
/**
* drainTo(null, n) throws NPE
*/
public void testDrainToNullN() {
ArrayBlockingQueue q = populatedQueue(SIZE);
try {
q.drainTo(null, 0);
shouldThrow();
} catch(NullPointerException success) {
}
}
/**
* drainTo(this, n) throws IAE
*/
public void testDrainToSelfN() {
ArrayBlockingQueue q = populatedQueue(SIZE);
try {
q.drainTo(q, 0);
shouldThrow();
} catch(IllegalArgumentException success) {
}
}
/**
* drainTo(c, n) empties first max {n, size} elements of queue into c
*/
public void testDrainToN() {
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2);
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(l.size(), k);
assertEquals(q.size(), SIZE-k);
for (int j = 0; j < k; ++j)
assertEquals(l.get(j), new Integer(j));
while (q.poll() != null) ;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?