8 |
|
*/ |
9 |
|
|
10 |
|
import junit.framework.*; |
11 |
< |
import java.util.*; |
12 |
< |
import java.util.concurrent.*; |
11 |
> |
import java.util.Arrays; |
12 |
> |
import java.util.Collection; |
13 |
> |
import java.util.Queue; |
14 |
> |
import java.util.concurrent.BlockingQueue; |
15 |
> |
import java.util.concurrent.CountDownLatch; |
16 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
17 |
|
|
18 |
|
/** |
19 |
< |
* Contains tests generally applicable to BlockingQueue implementations. |
19 |
> |
* Contains "contract" tests applicable to all BlockingQueue implementations. |
20 |
|
*/ |
21 |
|
public abstract class BlockingQueueTest extends JSR166TestCase { |
22 |
|
/* |
36 |
|
return new TestSuite(this.getClass()); |
37 |
|
} |
38 |
|
|
39 |
+ |
//---------------------------------------------------------------- |
40 |
+ |
// Configuration methods |
41 |
+ |
//---------------------------------------------------------------- |
42 |
+ |
|
43 |
|
/** Returns an empty instance of the implementation class. */ |
44 |
|
protected abstract BlockingQueue emptyCollection(); |
45 |
|
|
46 |
|
/** |
47 |
+ |
* Returns an element suitable for insertion in the collection. |
48 |
+ |
* Override for collections with unusual element types. |
49 |
+ |
*/ |
50 |
+ |
protected Object makeElement(int i) { |
51 |
+ |
return Integer.valueOf(i); |
52 |
+ |
} |
53 |
+ |
|
54 |
+ |
//---------------------------------------------------------------- |
55 |
+ |
// Tests |
56 |
+ |
//---------------------------------------------------------------- |
57 |
+ |
|
58 |
+ |
/** |
59 |
+ |
* offer(null) throws NullPointerException |
60 |
+ |
*/ |
61 |
+ |
public void testOfferNull() { |
62 |
+ |
final Queue q = emptyCollection(); |
63 |
+ |
try { |
64 |
+ |
q.offer(null); |
65 |
+ |
shouldThrow(); |
66 |
+ |
} catch (NullPointerException success) {} |
67 |
+ |
} |
68 |
+ |
|
69 |
+ |
/** |
70 |
+ |
* add(null) throws NullPointerException |
71 |
+ |
*/ |
72 |
+ |
public void testAddNull() { |
73 |
+ |
final Collection q = emptyCollection(); |
74 |
+ |
try { |
75 |
+ |
q.add(null); |
76 |
+ |
shouldThrow(); |
77 |
+ |
} catch (NullPointerException success) {} |
78 |
+ |
} |
79 |
+ |
|
80 |
+ |
/** |
81 |
+ |
* timed offer(null) throws NullPointerException |
82 |
+ |
*/ |
83 |
+ |
public void testTimedOfferNull() throws InterruptedException { |
84 |
+ |
final BlockingQueue q = emptyCollection(); |
85 |
+ |
long startTime = System.nanoTime(); |
86 |
+ |
try { |
87 |
+ |
q.offer(null, LONG_DELAY_MS, MILLISECONDS); |
88 |
+ |
shouldThrow(); |
89 |
+ |
} catch (NullPointerException success) {} |
90 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
91 |
+ |
} |
92 |
+ |
|
93 |
+ |
/** |
94 |
+ |
* put(null) throws NullPointerException |
95 |
+ |
*/ |
96 |
+ |
public void testPutNull() throws InterruptedException { |
97 |
+ |
final BlockingQueue q = emptyCollection(); |
98 |
+ |
try { |
99 |
+ |
q.put(null); |
100 |
+ |
shouldThrow(); |
101 |
+ |
} catch (NullPointerException success) {} |
102 |
+ |
} |
103 |
+ |
|
104 |
+ |
/** |
105 |
+ |
* put(null) throws NullPointerException |
106 |
+ |
*/ |
107 |
+ |
public void testAddAllNull() throws InterruptedException { |
108 |
+ |
final Collection q = emptyCollection(); |
109 |
+ |
try { |
110 |
+ |
q.addAll(null); |
111 |
+ |
shouldThrow(); |
112 |
+ |
} catch (NullPointerException success) {} |
113 |
+ |
} |
114 |
+ |
|
115 |
+ |
/** |
116 |
+ |
* addAll of a collection with null elements throws NullPointerException |
117 |
+ |
*/ |
118 |
+ |
public void testAddAllNullElements() { |
119 |
+ |
final Collection q = emptyCollection(); |
120 |
+ |
final Collection<Integer> elements = Arrays.asList(new Integer[SIZE]); |
121 |
+ |
try { |
122 |
+ |
q.addAll(elements); |
123 |
+ |
shouldThrow(); |
124 |
+ |
} catch (NullPointerException success) {} |
125 |
+ |
} |
126 |
+ |
|
127 |
+ |
/** |
128 |
+ |
* toArray(null) throws NullPointerException |
129 |
+ |
*/ |
130 |
+ |
public void testToArray_NullArray() { |
131 |
+ |
final Collection q = emptyCollection(); |
132 |
+ |
try { |
133 |
+ |
q.toArray(null); |
134 |
+ |
shouldThrow(); |
135 |
+ |
} catch (NullPointerException success) {} |
136 |
+ |
} |
137 |
+ |
|
138 |
+ |
/** |
139 |
+ |
* drainTo(null) throws NullPointerException |
140 |
+ |
*/ |
141 |
+ |
public void testDrainToNull() { |
142 |
+ |
final BlockingQueue q = emptyCollection(); |
143 |
+ |
try { |
144 |
+ |
q.drainTo(null); |
145 |
+ |
shouldThrow(); |
146 |
+ |
} catch (NullPointerException success) {} |
147 |
+ |
} |
148 |
+ |
|
149 |
+ |
/** |
150 |
+ |
* drainTo(this) throws IllegalArgumentException |
151 |
+ |
*/ |
152 |
+ |
public void testDrainToSelf() { |
153 |
+ |
final BlockingQueue q = emptyCollection(); |
154 |
+ |
try { |
155 |
+ |
q.drainTo(q); |
156 |
+ |
shouldThrow(); |
157 |
+ |
} catch (IllegalArgumentException success) {} |
158 |
+ |
} |
159 |
+ |
|
160 |
+ |
/** |
161 |
+ |
* drainTo(null, n) throws NullPointerException |
162 |
+ |
*/ |
163 |
+ |
public void testDrainToNullN() { |
164 |
+ |
final BlockingQueue q = emptyCollection(); |
165 |
+ |
try { |
166 |
+ |
q.drainTo(null, 0); |
167 |
+ |
shouldThrow(); |
168 |
+ |
} catch (NullPointerException success) {} |
169 |
+ |
} |
170 |
+ |
|
171 |
+ |
/** |
172 |
+ |
* drainTo(this, n) throws IllegalArgumentException |
173 |
+ |
*/ |
174 |
+ |
public void testDrainToSelfN() { |
175 |
+ |
final BlockingQueue q = emptyCollection(); |
176 |
+ |
try { |
177 |
+ |
q.drainTo(q, 0); |
178 |
+ |
shouldThrow(); |
179 |
+ |
} catch (IllegalArgumentException success) {} |
180 |
+ |
} |
181 |
+ |
|
182 |
+ |
/** |
183 |
|
* timed poll before a delayed offer times out; after offer succeeds; |
184 |
|
* on interruption throws |
185 |
|
*/ |
186 |
|
public void testTimedPollWithOffer() throws InterruptedException { |
187 |
|
final BlockingQueue q = emptyCollection(); |
188 |
|
final CheckedBarrier barrier = new CheckedBarrier(2); |
189 |
+ |
final Object zero = makeElement(0); |
190 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
191 |
|
public void realRun() throws InterruptedException { |
192 |
|
long startTime = System.nanoTime(); |