synchronousqueuetest.java
来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 895 行 · 第 1/2 页
JAVA
895 行
/*
* Written by Doug Lea with assistance from members of JCP JSR-166
* Expert Group and released to the public domain, as explained at
* http://creativecommons.org/licenses/publicdomain
* Other contributors include Andrew Wright, Jeffrey Hayes,
* Pat Fisher, Mike Judd.
*/
import junit.framework.*;
import edu.emory.mathcs.backport.java.util.*;
import edu.emory.mathcs.backport.java.util.concurrent.*;
import java.io.*;
import java.util.*;
public class SynchronousQueueTest extends JSR166TestCase {
public static void main(String[] args) {
junit.textui.TestRunner.run (suite());
}
public static Test suite() {
return new TestSuite(SynchronousQueueTest.class);
}
/**
* A SynchronousQueue is both empty and full
*/
public void testEmptyFull() {
SynchronousQueue q = new SynchronousQueue();
assertTrue(q.isEmpty());
assertEquals(0, q.size());
assertEquals(0, q.remainingCapacity());
assertFalse(q.offer(zero));
}
/**
* A fair SynchronousQueue is both empty and full
*/
public void testFairEmptyFull() {
SynchronousQueue q = new SynchronousQueue(true);
assertTrue(q.isEmpty());
assertEquals(0, q.size());
assertEquals(0, q.remainingCapacity());
assertFalse(q.offer(zero));
}
/**
* offer(null) throws NPE
*/
public void testOfferNull() {
try {
SynchronousQueue q = new SynchronousQueue();
q.offer(null);
shouldThrow();
} catch (NullPointerException success) { }
}
/**
* add(null) throws NPE
*/
public void testAddNull() {
try {
SynchronousQueue q = new SynchronousQueue();
q.add(null);
shouldThrow();
} catch (NullPointerException success) { }
}
/**
* offer fails if no active taker
*/
public void testOffer() {
SynchronousQueue q = new SynchronousQueue();
assertFalse(q.offer(one));
}
/**
* add throws ISE if no active taker
*/
public void testAdd() {
try {
SynchronousQueue q = new SynchronousQueue();
assertEquals(0, q.remainingCapacity());
q.add(one);
shouldThrow();
} catch (IllegalStateException success){
}
}
/**
* addAll(null) throws NPE
*/
public void testAddAll1() {
try {
SynchronousQueue q = new SynchronousQueue();
q.addAll(null);
shouldThrow();
}
catch (NullPointerException success) {}
}
/**
* addAll(this) throws IAE
*/
public void testAddAllSelf() {
try {
SynchronousQueue q = new SynchronousQueue();
q.addAll(q);
shouldThrow();
}
catch (IllegalArgumentException success) {}
}
/**
* addAll of a collection with null elements throws NPE
*/
public void testAddAll2() {
try {
SynchronousQueue q = new SynchronousQueue();
Integer[] ints = new Integer[1];
q.addAll(Arrays.asList(ints));
shouldThrow();
}
catch (NullPointerException success) {}
}
/**
* addAll throws ISE if no active taker
*/
public void testAddAll4() {
try {
SynchronousQueue q = new SynchronousQueue();
Integer[] ints = new Integer[1];
for (int i = 0; i < 1; ++i)
ints[i] = new Integer(i);
q.addAll(Arrays.asList(ints));
shouldThrow();
}
catch (IllegalStateException success) {}
}
/**
* put(null) throws NPE
*/
public void testPutNull() {
try {
SynchronousQueue q = new SynchronousQueue();
q.put(null);
shouldThrow();
}
catch (NullPointerException success){
}
catch (InterruptedException ie) {
unexpectedException();
}
}
/**
* put blocks interruptibly if no active taker
*/
public void testBlockingPut() {
Thread t = new Thread(new Runnable() {
public void run() {
try {
SynchronousQueue q = new SynchronousQueue();
q.put(zero);
threadShouldThrow();
} catch (InterruptedException ie){
}
}});
t.start();
try {
Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
t.join();
}
catch (InterruptedException ie) {
unexpectedException();
}
}
/**
* put blocks waiting for take
*/
public void testPutWithTake() {
final SynchronousQueue q = new SynchronousQueue();
Thread t = new Thread(new Runnable() {
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){
assertTrue(added >= 1);
}
}
});
try {
t.start();
Thread.sleep(SHORT_DELAY_MS);
q.take();
Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
t.join();
} catch (Exception e){
unexpectedException();
}
}
/**
* timed offer times out if elements not taken
*/
public void testTimedOffer() {
final SynchronousQueue q = new SynchronousQueue();
Thread t = new Thread(new Runnable() {
public void run() {
try {
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
threadShouldThrow();
} catch (InterruptedException success){}
}
});
try {
t.start();
Thread.sleep(SMALL_DELAY_MS);
t.interrupt();
t.join();
} catch (Exception e){
unexpectedException();
}
}
/**
* take blocks interruptibly when empty
*/
public void testTakeFromEmpty() {
final SynchronousQueue q = new SynchronousQueue();
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();
}
}
/**
* put blocks interruptibly if no active taker
*/
public void testFairBlockingPut() {
Thread t = new Thread(new Runnable() {
public void run() {
try {
SynchronousQueue q = new SynchronousQueue(true);
q.put(zero);
threadShouldThrow();
} catch (InterruptedException ie){
}
}});
t.start();
try {
Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
t.join();
}
catch (InterruptedException ie) {
unexpectedException();
}
}
/**
* put blocks waiting for take
*/
public void testFairPutWithTake() {
final SynchronousQueue q = new SynchronousQueue(true);
Thread t = new Thread(new Runnable() {
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){
assertTrue(added >= 1);
}
}
});
try {
t.start();
Thread.sleep(SHORT_DELAY_MS);
q.take();
Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
t.join();
} catch (Exception e){
unexpectedException();
}
}
/**
* timed offer times out if elements not taken
*/
public void testFairTimedOffer() {
final SynchronousQueue q = new SynchronousQueue(true);
Thread t = new Thread(new Runnable() {
public void run() {
try {
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
threadShouldThrow();
} catch (InterruptedException success){}
}
});
try {
t.start();
Thread.sleep(SMALL_DELAY_MS);
t.interrupt();
t.join();
} catch (Exception e){
unexpectedException();
}
}
/**
* take blocks interruptibly when empty
*/
public void testFairTakeFromEmpty() {
final SynchronousQueue q = new SynchronousQueue(true);
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();
}
}
/**
* poll fails unless active taker
*/
public void testPoll() {
SynchronousQueue q = new SynchronousQueue();
assertNull(q.poll());
}
/**
* timed pool with zero timeout times out if no active taker
*/
public void testTimedPoll0() {
try {
SynchronousQueue q = new SynchronousQueue();
assertNull(q.poll(0, TimeUnit.MILLISECONDS));
} catch (InterruptedException e){
unexpectedException();
}
}
/**
* timed pool with nonzero timeout times out if no active taker
*/
public void testTimedPoll() {
try {
SynchronousQueue q = new SynchronousQueue();
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 {
SynchronousQueue q = new SynchronousQueue();
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
} catch (InterruptedException success){
}
}});
t.start();
try {
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 SynchronousQueue q = new SynchronousQueue();
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 {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?