37 |
|
protected abstract BlockingQueue emptyCollection(); |
38 |
|
|
39 |
|
/** |
40 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
40 |
> |
* timed poll before a delayed offer times out; after offer succeeds; |
41 |
|
* on interruption throws |
42 |
|
*/ |
43 |
|
public void testTimedPollWithOffer() throws InterruptedException { |
45 |
|
final CheckedBarrier barrier = new CheckedBarrier(2); |
46 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
47 |
|
public void realRun() throws InterruptedException { |
48 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
48 |
> |
long startTime = System.nanoTime(); |
49 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
50 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
51 |
|
|
52 |
|
barrier.await(); |
53 |
< |
assertSame(zero, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
53 |
> |
|
54 |
> |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
55 |
|
|
56 |
|
Thread.currentThread().interrupt(); |
57 |
|
try { |
58 |
< |
q.poll(SHORT_DELAY_MS, MILLISECONDS); |
58 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
59 |
|
shouldThrow(); |
60 |
|
} catch (InterruptedException success) {} |
61 |
+ |
assertFalse(Thread.interrupted()); |
62 |
|
|
63 |
|
barrier.await(); |
64 |
|
try { |
65 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
65 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
66 |
|
shouldThrow(); |
67 |
|
} catch (InterruptedException success) {} |
68 |
+ |
assertFalse(Thread.interrupted()); |
69 |
|
}}); |
70 |
|
|
71 |
|
barrier.await(); |
72 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
72 |
> |
long startTime = System.nanoTime(); |
73 |
> |
assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS)); |
74 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
75 |
> |
|
76 |
|
barrier.await(); |
77 |
< |
sleep(SHORT_DELAY_MS); |
77 |
> |
assertThreadStaysAlive(t); |
78 |
|
t.interrupt(); |
79 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
79 |
> |
awaitTermination(t); |
80 |
|
} |
81 |
|
|
82 |
|
/** |
83 |
|
* take() blocks interruptibly when empty |
84 |
|
*/ |
85 |
< |
public void testTakeFromEmptyBlocksInterruptibly() |
78 |
< |
throws InterruptedException { |
85 |
> |
public void testTakeFromEmptyBlocksInterruptibly() { |
86 |
|
final BlockingQueue q = emptyCollection(); |
87 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
88 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
89 |
|
public void realRun() { |
83 |
– |
long t0 = System.nanoTime(); |
90 |
|
threadStarted.countDown(); |
91 |
|
try { |
92 |
|
q.take(); |
93 |
|
shouldThrow(); |
94 |
|
} catch (InterruptedException success) {} |
95 |
< |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
95 |
> |
assertFalse(Thread.interrupted()); |
96 |
|
}}); |
97 |
|
|
98 |
< |
threadStarted.await(); |
99 |
< |
delay(SHORT_DELAY_MS); |
94 |
< |
assertTrue(t.isAlive()); |
98 |
> |
await(threadStarted); |
99 |
> |
assertThreadStaysAlive(t); |
100 |
|
t.interrupt(); |
101 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
101 |
> |
awaitTermination(t); |
102 |
|
} |
103 |
|
|
104 |
|
/** |
105 |
|
* take() throws InterruptedException immediately if interrupted |
106 |
|
* before waiting |
107 |
|
*/ |
108 |
< |
public void testTakeFromEmptyAfterInterrupt() |
104 |
< |
throws InterruptedException { |
108 |
> |
public void testTakeFromEmptyAfterInterrupt() { |
109 |
|
final BlockingQueue q = emptyCollection(); |
110 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
111 |
|
public void realRun() { |
108 |
– |
long t0 = System.nanoTime(); |
112 |
|
Thread.currentThread().interrupt(); |
113 |
|
try { |
114 |
|
q.take(); |
115 |
|
shouldThrow(); |
116 |
|
} catch (InterruptedException success) {} |
117 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
117 |
> |
assertFalse(Thread.interrupted()); |
118 |
> |
}}); |
119 |
> |
|
120 |
> |
awaitTermination(t); |
121 |
> |
} |
122 |
> |
|
123 |
> |
/** |
124 |
> |
* timed poll() blocks interruptibly when empty |
125 |
> |
*/ |
126 |
> |
public void testTimedPollFromEmptyBlocksInterruptibly() { |
127 |
> |
final BlockingQueue q = emptyCollection(); |
128 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
129 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
130 |
> |
public void realRun() { |
131 |
> |
threadStarted.countDown(); |
132 |
> |
try { |
133 |
> |
q.poll(2 * LONG_DELAY_MS, MILLISECONDS); |
134 |
> |
shouldThrow(); |
135 |
> |
} catch (InterruptedException success) {} |
136 |
> |
assertFalse(Thread.interrupted()); |
137 |
> |
}}); |
138 |
> |
|
139 |
> |
await(threadStarted); |
140 |
> |
assertThreadStaysAlive(t); |
141 |
> |
t.interrupt(); |
142 |
> |
awaitTermination(t); |
143 |
> |
} |
144 |
> |
|
145 |
> |
/** |
146 |
> |
* timed poll() throws InterruptedException immediately if |
147 |
> |
* interrupted before waiting |
148 |
> |
*/ |
149 |
> |
public void testTimedPollFromEmptyAfterInterrupt() { |
150 |
> |
final BlockingQueue q = emptyCollection(); |
151 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
152 |
> |
public void realRun() { |
153 |
> |
Thread.currentThread().interrupt(); |
154 |
> |
try { |
155 |
> |
q.poll(2 * LONG_DELAY_MS, MILLISECONDS); |
156 |
> |
shouldThrow(); |
157 |
> |
} catch (InterruptedException success) {} |
158 |
> |
assertFalse(Thread.interrupted()); |
159 |
|
}}); |
160 |
|
|
161 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
161 |
> |
awaitTermination(t); |
162 |
|
} |
163 |
|
|
164 |
|
/** For debugging. */ |