📄 linkedblockingdequetest.java
字号:
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 offerFirst times out if full and elements not taken
*/
public void testTimedOfferFirst() {
final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
Thread t = new Thread(new Runnable() {
public void run() {
try {
q.putFirst(new Object());
q.putFirst(new Object());
threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
q.offerFirst(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 retrieves elements in FIFO order
*/
public void testTakeFirst() {
try {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, ((Integer)q.takeFirst()).intValue());
}
} catch (InterruptedException e){
unexpectedException();
}
}
/**
* takeFirst blocks interruptibly when empty
*/
public void testTakeFirstFromEmpty() {
final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
Thread t = new Thread(new Runnable() {
public void run() {
try {
q.takeFirst();
threadShouldThrow();
} catch (InterruptedException success){ }
}
});
try {
t.start();
Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
t.join();
} catch (Exception e){
unexpectedException();
}
}
/**
* TakeFirst removes existing elements until empty, then blocks interruptibly
*/
public void testBlockingTakeFirst() {
Thread t = new Thread(new Runnable() {
public void run() {
try {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, ((Integer)q.takeFirst()).intValue());
}
q.takeFirst();
threadShouldThrow();
} catch (InterruptedException success){
}
}});
t.start();
try {
Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
t.join();
}
catch (InterruptedException ie) {
unexpectedException();
}
}
/**
* timed pollFirst with zero timeout succeeds when non-empty, else times out
*/
public void testTimedPollFirst0() {
try {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, ((Integer)q.pollFirst(0, TimeUnit.MILLISECONDS)).intValue());
}
assertNull(q.pollFirst(0, TimeUnit.MILLISECONDS));
} catch (InterruptedException e){
unexpectedException();
}
}
/**
* timed pollFirst with nonzero timeout succeeds when non-empty, else times out
*/
public void testTimedPollFirst() {
try {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
}
assertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
} catch (InterruptedException e){
unexpectedException();
}
}
/**
* Interrupted timed pollFirst throws InterruptedException instead of
* returning timeout status
*/
public void testInterruptedTimedPollFirst() {
Thread t = new Thread(new Runnable() {
public void run() {
try {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
}
threadAssertNull(q.pollFirst(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 pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
* on interruption throws
*/
public void testTimedPollFirstWithOfferFirst() {
final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
Thread t = new Thread(new Runnable() {
public void run() {
try {
threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
threadShouldThrow();
} catch (InterruptedException success) { }
}
});
try {
t.start();
Thread.sleep(SMALL_DELAY_MS);
assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
t.interrupt();
t.join();
} catch (Exception e){
unexpectedException();
}
}
/**
* putLast(null) throws NPE
*/
public void testPutLastNull() {
try {
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
q.putLast(null);
shouldThrow();
}
catch (NullPointerException success){
}
catch (InterruptedException ie) {
unexpectedException();
}
}
/**
* all elements successfully putLast are contained
*/
public void testPutLast() {
try {
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
Integer I = new Integer(i);
q.putLast(I);
assertTrue(q.contains(I));
}
assertEquals(0, q.remainingCapacity());
}
catch (InterruptedException ie) {
unexpectedException();
}
}
/**
* putLast blocks interruptibly if full
*/
public void testBlockingPutLast() {
Thread t = new Thread(new Runnable() {
public void run() {
int added = 0;
try {
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
q.putLast(new Integer(i));
++added;
}
q.putLast(new Integer(SIZE));
threadShouldThrow();
} catch (InterruptedException ie){
threadAssertEquals(added, SIZE);
}
}});
t.start();
try {
Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
t.join();
}
catch (InterruptedException ie) {
unexpectedException();
}
}
/**
* putLast blocks waiting for take when full
*/
public void testPutLastWithTake() {
final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
Thread t = new Thread(new Runnable() {
public void run() {
int added = 0;
try {
q.putLast(new Object());
++added;
q.putLast(new Object());
++added;
q.putLast(new Object());
++added;
q.putLast(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 offerLast times out if full and elements not taken
*/
public void testTimedOfferLast() {
final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
Thread t = new Thread(new Runnable() {
public void run() {
try {
q.putLast(new Object());
q.putLast(new Object());
threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
q.offerLast(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();
}
}
/**
* takeLast retrieves elements in FIFO order
*/
public void testTakeLast() {
try {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
}
} catch (InterruptedException e){
unexpectedException();
}
}
/**
* takeLast blocks interruptibly when empty
*/
public void testTakeLastFromEmpty() {
final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
Thread t = new Thread(new Runnable() {
public void run() {
try {
q.takeLast();
threadShouldThrow();
} catch (InterruptedException success){ }
}
});
try {
t.start();
Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
t.join();
} catch (Exception e){
unexpectedException();
}
}
/**
* TakeLast removes existing elements until empty, then blocks interruptibly
*/
public void testBlockingTakeLast() {
Thread t = new Thread(new Runnable() {
public void run() {
try {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
}
q.takeLast();
threadShouldThrow();
} catch (InterruptedException success){
}
}});
t.start();
try {
Thread.sleep(SHORT_DELAY_MS);
t.interrupt();
t.join();
}
catch (InterruptedException ie) {
unexpectedException();
}
}
/**
* timed pollLast with zero timeout succeeds when non-empty, else times out
*/
public void testTimedPollLast0() {
try {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, TimeUnit.MILLISECONDS)).intValue());
}
assertNull(q.pollLast(0, TimeUnit.MILLISECONDS));
} catch (InterruptedException e){
unexpectedException();
}
}
/**
* timed pollLast with nonzero timeout succeeds when non-empty, else times out
*/
public void testTimedPollLast() {
try {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
}
assertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
} catch (InterruptedException e){
unexpectedException();
}
}
/**
* Interrupted timed pollLast throws InterruptedException instead of
* returning timeout status
*/
public void testInterruptedTimedPollLast() {
Thread t = new Thread(new Runnable() {
public void run() {
try {
LinkedBlockingDeque q = populatedDeque(SIZE);
for (int i = 0; i < SIZE; ++i) {
threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
}
threadAssertNull(q.pollLast(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 offerLast fails; after offerLast succeeds;
* on interruption throws
*/
public void testTimedPollWithOfferLast() {
final LinkedBlockingDeque q = new LinkedBlockingDeque(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.offerLast(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
t.interrupt();
t.join();
} catch (Exception e){
unexpectedException();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -