1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
2 |
> |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
12 |
+ |
import java.util.concurrent.atomic.*; |
13 |
|
|
14 |
< |
public class ScheduledExecutorTest extends TestCase{ |
13 |
< |
|
14 |
< |
boolean flag = false; |
15 |
< |
|
14 |
> |
public class ScheduledExecutorTest extends JSR166TestCase { |
15 |
|
public static void main(String[] args) { |
16 |
|
junit.textui.TestRunner.run (suite()); |
17 |
|
} |
19 |
– |
|
20 |
– |
|
18 |
|
public static Test suite() { |
19 |
|
return new TestSuite(ScheduledExecutorTest.class); |
20 |
|
} |
21 |
|
|
25 |
– |
private static long SHORT_DELAY_MS = 100; |
26 |
– |
private static long MEDIUM_DELAY_MS = 1000; |
27 |
– |
private static long LONG_DELAY_MS = 10000; |
28 |
– |
|
29 |
– |
static class MyRunnable implements Runnable { |
30 |
– |
volatile boolean waiting = true; |
31 |
– |
volatile boolean done = false; |
32 |
– |
public void run(){ |
33 |
– |
try{ |
34 |
– |
Thread.sleep(SHORT_DELAY_MS); |
35 |
– |
waiting = false; |
36 |
– |
done = true; |
37 |
– |
} catch(Exception e){ |
38 |
– |
} |
39 |
– |
} |
40 |
– |
} |
41 |
– |
|
42 |
– |
static class MyCallable implements Callable { |
43 |
– |
volatile boolean waiting = true; |
44 |
– |
volatile boolean done = false; |
45 |
– |
public Object call(){ |
46 |
– |
try{ |
47 |
– |
Thread.sleep(SHORT_DELAY_MS); |
48 |
– |
waiting = false; |
49 |
– |
done = true; |
50 |
– |
}catch(Exception e){} |
51 |
– |
return Boolean.TRUE; |
52 |
– |
} |
53 |
– |
} |
54 |
– |
|
55 |
– |
public Runnable newRunnable(){ |
56 |
– |
return new Runnable(){ |
57 |
– |
public void run(){ |
58 |
– |
try{Thread.sleep(SHORT_DELAY_MS); |
59 |
– |
} catch(Exception e){ |
60 |
– |
} |
61 |
– |
} |
62 |
– |
}; |
63 |
– |
} |
64 |
– |
|
65 |
– |
public Runnable newNoopRunnable() { |
66 |
– |
return new Runnable(){ |
67 |
– |
public void run(){ |
68 |
– |
} |
69 |
– |
}; |
70 |
– |
} |
22 |
|
|
23 |
|
/** |
24 |
< |
* Test to verify execute successfully runs the given Runnable |
25 |
< |
*/ |
26 |
< |
public void testExecute(){ |
27 |
< |
try{ |
28 |
< |
MyRunnable runnable =new MyRunnable(); |
29 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
30 |
< |
one.execute(runnable); |
31 |
< |
Thread.sleep(SHORT_DELAY_MS/2); |
32 |
< |
assertTrue(runnable.waiting); |
33 |
< |
one.shutdown(); |
34 |
< |
try{ |
24 |
> |
* execute successfully executes a runnable |
25 |
> |
*/ |
26 |
> |
public void testExecute() { |
27 |
> |
try { |
28 |
> |
TrackedShortRunnable runnable =new TrackedShortRunnable(); |
29 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
30 |
> |
p1.execute(runnable); |
31 |
> |
assertFalse(runnable.done); |
32 |
> |
Thread.sleep(SHORT_DELAY_MS); |
33 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
34 |
> |
try { |
35 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
36 |
|
} catch(InterruptedException e){ |
37 |
< |
fail("unexpected exception"); |
37 |
> |
unexpectedException(); |
38 |
|
} |
88 |
– |
assertFalse(runnable.waiting); |
39 |
|
assertTrue(runnable.done); |
40 |
< |
one.shutdown(); |
40 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
41 |
> |
joinPool(p1); |
42 |
|
} |
43 |
|
catch(Exception e){ |
44 |
< |
fail("unexpected exception"); |
44 |
> |
unexpectedException(); |
45 |
|
} |
46 |
+ |
|
47 |
|
} |
48 |
|
|
49 |
+ |
|
50 |
|
/** |
51 |
< |
* Test to verify schedule successfully runs the given Callable. |
99 |
< |
* The waiting flag shows that the Callable is not started until |
100 |
< |
* immediately. |
51 |
> |
* delayed schedule of callable successfully executes after delay |
52 |
|
*/ |
53 |
< |
public void testSchedule1(){ |
54 |
< |
try{ |
55 |
< |
MyCallable callable = new MyCallable(); |
56 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
57 |
< |
Future f = one.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
58 |
< |
assertTrue(callable.waiting); |
53 |
> |
public void testSchedule1() { |
54 |
> |
try { |
55 |
> |
TrackedCallable callable = new TrackedCallable(); |
56 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
57 |
> |
Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
58 |
> |
assertFalse(callable.done); |
59 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
60 |
|
assertTrue(callable.done); |
61 |
|
assertEquals(Boolean.TRUE, f.get()); |
62 |
< |
one.shutdown(); |
63 |
< |
}catch(RejectedExecutionException e){} |
62 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
63 |
> |
joinPool(p1); |
64 |
> |
} catch(RejectedExecutionException e){} |
65 |
|
catch(Exception e){ |
66 |
< |
fail("unexpected exception"); |
66 |
> |
e.printStackTrace(); |
67 |
> |
unexpectedException(); |
68 |
|
} |
69 |
|
} |
70 |
|
|
71 |
|
/** |
72 |
< |
* Another version of schedule, only using Runnable instead of Callable |
72 |
> |
* delayed schedule of runnable successfully executes after delay |
73 |
|
*/ |
74 |
< |
public void testSchedule3(){ |
75 |
< |
try{ |
76 |
< |
MyRunnable runnable = new MyRunnable(); |
77 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
78 |
< |
one.schedule(runnable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
79 |
< |
Thread.sleep(SHORT_DELAY_MS/2); |
80 |
< |
assertTrue(runnable.waiting); |
74 |
> |
public void testSchedule3() { |
75 |
> |
try { |
76 |
> |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
77 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
78 |
> |
p1.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS); |
79 |
> |
Thread.sleep(SHORT_DELAY_MS); |
80 |
> |
assertFalse(runnable.done); |
81 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
82 |
|
assertTrue(runnable.done); |
83 |
< |
one.shutdown(); |
83 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
84 |
> |
joinPool(p1); |
85 |
|
} catch(Exception e){ |
86 |
< |
fail("unexpected exception"); |
86 |
> |
unexpectedException(); |
87 |
|
} |
88 |
|
} |
89 |
|
|
90 |
|
/** |
91 |
< |
* The final version of schedule, using both long, TimeUnit and Runnable |
91 |
> |
* scheduleAtFixedRate executes runnable after given initial delay |
92 |
|
*/ |
93 |
< |
public void testSchedule4(){ |
94 |
< |
try{ |
95 |
< |
MyRunnable runnable = new MyRunnable(); |
96 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
97 |
< |
one.schedule(runnable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
98 |
< |
// Thread.sleep(505); |
145 |
< |
assertTrue(runnable.waiting); |
93 |
> |
public void testSchedule4() { |
94 |
> |
try { |
95 |
> |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
96 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
97 |
> |
ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
98 |
> |
assertFalse(runnable.done); |
99 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
100 |
|
assertTrue(runnable.done); |
101 |
< |
one.shutdown(); |
101 |
> |
h.cancel(true); |
102 |
> |
joinPool(p1); |
103 |
|
} catch(Exception e){ |
104 |
< |
fail("unexpected exception"); |
104 |
> |
unexpectedException(); |
105 |
|
} |
106 |
|
} |
107 |
< |
|
108 |
< |
|
109 |
< |
// exception tests |
107 |
> |
|
108 |
> |
static class RunnableCounter implements Runnable { |
109 |
> |
AtomicInteger count = new AtomicInteger(0); |
110 |
> |
public void run() { count.getAndIncrement(); } |
111 |
> |
} |
112 |
|
|
113 |
|
/** |
114 |
< |
* Test to verify schedule(Runnable, long) throws RejectedExecutionException |
159 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
114 |
> |
* scheduleWithFixedDelay executes runnable after given initial delay |
115 |
|
*/ |
116 |
< |
public void testSchedule1_RejectedExecutionException(){ |
117 |
< |
try{ |
118 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
119 |
< |
se.shutdown(); |
120 |
< |
se.schedule(new Runnable(){ |
121 |
< |
public void run(){} |
122 |
< |
}, 10000, TimeUnit.MILLISECONDS); |
123 |
< |
fail("shoud throw"); |
124 |
< |
}catch(RejectedExecutionException e){} |
116 |
> |
public void testSchedule5() { |
117 |
> |
try { |
118 |
> |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
119 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
120 |
> |
ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
121 |
> |
assertFalse(runnable.done); |
122 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
123 |
> |
assertTrue(runnable.done); |
124 |
> |
h.cancel(true); |
125 |
> |
joinPool(p1); |
126 |
> |
} catch(Exception e){ |
127 |
> |
unexpectedException(); |
128 |
> |
} |
129 |
> |
} |
130 |
> |
|
131 |
> |
/** |
132 |
> |
* scheduleAtFixedRate executes series of tasks at given rate |
133 |
> |
*/ |
134 |
> |
public void testFixedRateSequence() { |
135 |
> |
try { |
136 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
137 |
> |
RunnableCounter counter = new RunnableCounter(); |
138 |
> |
ScheduledFuture h = |
139 |
> |
p1.scheduleAtFixedRate(counter, 0, 1, TimeUnit.MILLISECONDS); |
140 |
> |
Thread.sleep(SMALL_DELAY_MS); |
141 |
> |
h.cancel(true); |
142 |
> |
int c = counter.count.get(); |
143 |
> |
// By time scaling conventions, we must have at least |
144 |
> |
// an execution per SHORT delay, but no more than one SHORT more |
145 |
> |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
146 |
> |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
147 |
> |
assertTrue(h.isDone()); |
148 |
> |
joinPool(p1); |
149 |
> |
} catch(Exception e){ |
150 |
> |
unexpectedException(); |
151 |
> |
} |
152 |
|
} |
153 |
|
|
154 |
|
/** |
155 |
< |
* Test to verify schedule(Callable, long, TimeUnit) throws RejectedExecutionException |
174 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
155 |
> |
* scheduleWithFixedDelay executes series of tasks with given period |
156 |
|
*/ |
157 |
< |
public void testSchedule2_RejectedExecutionException(){ |
158 |
< |
try{ |
159 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
160 |
< |
se.shutdown(); |
161 |
< |
se.schedule(new Callable(){ |
162 |
< |
public Object call(){ |
163 |
< |
return Boolean.TRUE; |
164 |
< |
} |
165 |
< |
}, (long)100, TimeUnit.SECONDS); |
166 |
< |
fail("should throw"); |
167 |
< |
}catch(RejectedExecutionException e){} |
157 |
> |
public void testFixedDelaySequence() { |
158 |
> |
try { |
159 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
160 |
> |
RunnableCounter counter = new RunnableCounter(); |
161 |
> |
ScheduledFuture h = |
162 |
> |
p1.scheduleWithFixedDelay(counter, 0, 1, TimeUnit.MILLISECONDS); |
163 |
> |
Thread.sleep(SMALL_DELAY_MS); |
164 |
> |
h.cancel(true); |
165 |
> |
int c = counter.count.get(); |
166 |
> |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
167 |
> |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
168 |
> |
assertTrue(h.isDone()); |
169 |
> |
joinPool(p1); |
170 |
> |
} catch(Exception e){ |
171 |
> |
unexpectedException(); |
172 |
> |
} |
173 |
|
} |
174 |
|
|
175 |
+ |
|
176 |
|
/** |
177 |
< |
* Test to verify schedule(Callable, long) throws RejectedExecutionException |
178 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
179 |
< |
*/ |
180 |
< |
public void testSchedule3_RejectedExecutionException(){ |
181 |
< |
try{ |
182 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
183 |
< |
se.shutdown(); |
184 |
< |
se.schedule(new Callable(){ |
185 |
< |
public Object call(){ |
186 |
< |
return Boolean.TRUE; |
187 |
< |
} |
188 |
< |
}, 10000, TimeUnit.MILLISECONDS); |
189 |
< |
fail("should throw"); |
190 |
< |
}catch(RejectedExecutionException e){} |
177 |
> |
* execute (null) throws NPE |
178 |
> |
*/ |
179 |
> |
public void testExecuteNull() { |
180 |
> |
ScheduledThreadPoolExecutor se = null; |
181 |
> |
try { |
182 |
> |
se = new ScheduledThreadPoolExecutor(1); |
183 |
> |
se.execute(null); |
184 |
> |
shouldThrow(); |
185 |
> |
} catch(NullPointerException success){} |
186 |
> |
catch(Exception e){ |
187 |
> |
unexpectedException(); |
188 |
> |
} |
189 |
> |
|
190 |
> |
joinPool(se); |
191 |
|
} |
192 |
|
|
193 |
|
/** |
194 |
< |
* Test to verify scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws |
195 |
< |
* RejectedExecutionException. |
196 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
197 |
< |
*/ |
198 |
< |
public void testScheduleAtFixedRate1_RejectedExecutionException(){ |
199 |
< |
try{ |
200 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
201 |
< |
se.shutdown(); |
202 |
< |
se.scheduleAtFixedRate(new Runnable(){ |
203 |
< |
public void run(){} |
204 |
< |
}, 100, 100, TimeUnit.SECONDS); |
205 |
< |
fail("should throw"); |
206 |
< |
}catch(RejectedExecutionException e){} |
194 |
> |
* schedule (null) throws NPE |
195 |
> |
*/ |
196 |
> |
public void testScheduleNull() { |
197 |
> |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
198 |
> |
try { |
199 |
> |
TrackedCallable callable = null; |
200 |
> |
Future f = se.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
201 |
> |
shouldThrow(); |
202 |
> |
} catch(NullPointerException success){} |
203 |
> |
catch(Exception e){ |
204 |
> |
unexpectedException(); |
205 |
> |
} |
206 |
> |
joinPool(se); |
207 |
|
} |
208 |
< |
|
208 |
> |
|
209 |
|
/** |
210 |
< |
* Test to verify scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws |
211 |
< |
* RejectedExecutionException. |
212 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
213 |
< |
*/ |
214 |
< |
public void testScheduleAtFixedRate2_RejectedExecutionException(){ |
228 |
< |
try{ |
229 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
210 |
> |
* execute throws RejectedExecutionException if shutdown |
211 |
> |
*/ |
212 |
> |
public void testSchedule1_RejectedExecutionException() { |
213 |
> |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
214 |
> |
try { |
215 |
|
se.shutdown(); |
216 |
< |
se.scheduleAtFixedRate(new Runnable(){ |
217 |
< |
public void run(){} |
218 |
< |
}, 1, 100, TimeUnit.SECONDS); |
219 |
< |
fail("should throw"); |
220 |
< |
}catch(RejectedExecutionException e){} |
216 |
> |
se.schedule(new NoOpRunnable(), |
217 |
> |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
218 |
> |
shouldThrow(); |
219 |
> |
} catch(RejectedExecutionException success){ |
220 |
> |
} catch (SecurityException ok) { |
221 |
> |
} |
222 |
> |
|
223 |
> |
joinPool(se); |
224 |
> |
|
225 |
|
} |
226 |
|
|
227 |
|
/** |
228 |
< |
* Test to verify scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws |
240 |
< |
* RejectedExecutionException. |
241 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
228 |
> |
* schedule throws RejectedExecutionException if shutdown |
229 |
|
*/ |
230 |
< |
public void testScheduleWithFixedDelay1_RejectedExecutionException(){ |
231 |
< |
try{ |
232 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
230 |
> |
public void testSchedule2_RejectedExecutionException() { |
231 |
> |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
232 |
> |
try { |
233 |
|
se.shutdown(); |
234 |
< |
se.scheduleWithFixedDelay(new Runnable(){ |
235 |
< |
public void run(){} |
236 |
< |
}, 100, 100, TimeUnit.SECONDS); |
237 |
< |
fail("should throw"); |
238 |
< |
}catch(RejectedExecutionException e){} |
234 |
> |
se.schedule(new NoOpCallable(), |
235 |
> |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
236 |
> |
shouldThrow(); |
237 |
> |
} catch(RejectedExecutionException success){ |
238 |
> |
} catch (SecurityException ok) { |
239 |
> |
} |
240 |
> |
joinPool(se); |
241 |
|
} |
242 |
|
|
243 |
|
/** |
244 |
< |
* Test to verify scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws |
256 |
< |
* RejectedExecutionException. |
257 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
244 |
> |
* schedule callable throws RejectedExecutionException if shutdown |
245 |
|
*/ |
246 |
< |
public void testScheduleWithFixedDelay2_RejectedExecutionException(){ |
247 |
< |
try{ |
248 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
246 |
> |
public void testSchedule3_RejectedExecutionException() { |
247 |
> |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
248 |
> |
try { |
249 |
|
se.shutdown(); |
250 |
< |
se.scheduleWithFixedDelay(new Runnable(){ |
251 |
< |
public void run(){} |
252 |
< |
}, 1, 100, TimeUnit.SECONDS); |
253 |
< |
fail("should throw"); |
254 |
< |
}catch(RejectedExecutionException e){} |
250 |
> |
se.schedule(new NoOpCallable(), |
251 |
> |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
252 |
> |
shouldThrow(); |
253 |
> |
} catch(RejectedExecutionException success){ |
254 |
> |
} catch (SecurityException ok) { |
255 |
> |
} |
256 |
> |
joinPool(se); |
257 |
|
} |
258 |
|
|
259 |
|
/** |
260 |
< |
* Test to verify execute throws RejectedExecutionException |
272 |
< |
* This occurs on an attempt to schedule a task on a shutdown executor |
260 |
> |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
261 |
|
*/ |
262 |
< |
public void testExecute_RejectedExecutionException(){ |
263 |
< |
try{ |
264 |
< |
ScheduledExecutor se = new ScheduledExecutor(1); |
262 |
> |
public void testScheduleAtFixedRate1_RejectedExecutionException() { |
263 |
> |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
264 |
> |
try { |
265 |
|
se.shutdown(); |
266 |
< |
se.execute(new Runnable(){ |
267 |
< |
public void run(){} |
268 |
< |
}); |
269 |
< |
fail("should throw"); |
270 |
< |
}catch(RejectedExecutionException e){} |
266 |
> |
se.scheduleAtFixedRate(new NoOpRunnable(), |
267 |
> |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
268 |
> |
shouldThrow(); |
269 |
> |
} catch(RejectedExecutionException success){ |
270 |
> |
} catch (SecurityException ok) { |
271 |
> |
} |
272 |
> |
joinPool(se); |
273 |
|
} |
274 |
< |
|
285 |
< |
|
286 |
< |
|
274 |
> |
|
275 |
|
/** |
276 |
< |
* Test to verify getActiveCount gives correct values |
276 |
> |
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown |
277 |
|
*/ |
278 |
< |
public void testGetActiveCount(){ |
279 |
< |
ScheduledExecutor two = new ScheduledExecutor(2); |
278 |
> |
public void testScheduleWithFixedDelay1_RejectedExecutionException() { |
279 |
> |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
280 |
|
try { |
281 |
< |
assertEquals(0, two.getActiveCount()); |
282 |
< |
two.execute(newRunnable()); |
283 |
< |
try{ |
284 |
< |
Thread.sleep(SHORT_DELAY_MS/2); |
285 |
< |
} catch(Exception e){ |
286 |
< |
fail("unexpected exception"); |
287 |
< |
} |
288 |
< |
assertEquals(1, two.getActiveCount()); |
289 |
< |
} finally { |
290 |
< |
two.shutdown(); |
281 |
> |
se.shutdown(); |
282 |
> |
se.scheduleWithFixedDelay(new NoOpRunnable(), |
283 |
> |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
284 |
> |
shouldThrow(); |
285 |
> |
} catch(RejectedExecutionException success){ |
286 |
> |
} catch (SecurityException ok) { |
287 |
> |
} |
288 |
> |
joinPool(se); |
289 |
> |
} |
290 |
> |
|
291 |
> |
/** |
292 |
> |
* getActiveCount increases but doesn't overestimate, when a |
293 |
> |
* thread becomes active |
294 |
> |
*/ |
295 |
> |
public void testGetActiveCount() { |
296 |
> |
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); |
297 |
> |
assertEquals(0, p2.getActiveCount()); |
298 |
> |
p2.execute(new SmallRunnable()); |
299 |
> |
try { |
300 |
> |
Thread.sleep(SHORT_DELAY_MS); |
301 |
> |
} catch(Exception e){ |
302 |
> |
unexpectedException(); |
303 |
|
} |
304 |
+ |
assertEquals(1, p2.getActiveCount()); |
305 |
+ |
joinPool(p2); |
306 |
|
} |
307 |
|
|
308 |
|
/** |
309 |
< |
* Test to verify getCompleteTaskCount gives correct values |
309 |
> |
* getCompletedTaskCount increases, but doesn't overestimate, |
310 |
> |
* when tasks complete |
311 |
|
*/ |
312 |
< |
public void testGetCompletedTaskCount(){ |
313 |
< |
ScheduledExecutor two = new ScheduledExecutor(2); |
312 |
> |
public void testGetCompletedTaskCount() { |
313 |
> |
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); |
314 |
> |
assertEquals(0, p2.getCompletedTaskCount()); |
315 |
> |
p2.execute(new SmallRunnable()); |
316 |
|
try { |
317 |
< |
assertEquals(0, two.getCompletedTaskCount()); |
318 |
< |
two.execute(newRunnable()); |
319 |
< |
try{ |
315 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
316 |
< |
} catch(Exception e){ |
317 |
< |
fail("unexpected exception"); |
318 |
< |
} |
319 |
< |
assertEquals(1, two.getCompletedTaskCount()); |
320 |
< |
} finally { |
321 |
< |
two.shutdown(); |
317 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
318 |
> |
} catch(Exception e){ |
319 |
> |
unexpectedException(); |
320 |
|
} |
321 |
+ |
assertEquals(1, p2.getCompletedTaskCount()); |
322 |
+ |
joinPool(p2); |
323 |
|
} |
324 |
|
|
325 |
|
/** |
326 |
< |
* Test to verify getCorePoolSize gives correct values |
326 |
> |
* getCorePoolSize returns size given in constructor if not otherwise set |
327 |
|
*/ |
328 |
< |
public void testGetCorePoolSize(){ |
329 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
330 |
< |
try { |
331 |
< |
assertEquals(1, one.getCorePoolSize()); |
332 |
< |
} finally { |
333 |
< |
one.shutdown(); |
334 |
< |
} |
328 |
> |
public void testGetCorePoolSize() { |
329 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
330 |
> |
assertEquals(1, p1.getCorePoolSize()); |
331 |
> |
joinPool(p1); |
332 |
|
} |
333 |
|
|
334 |
|
/** |
335 |
< |
* Test to verify getLargestPoolSize gives correct values |
335 |
> |
* getLargestPoolSize increases, but doesn't overestimate, when |
336 |
> |
* multiple threads active |
337 |
|
*/ |
338 |
< |
public void testGetLargestPoolSize(){ |
339 |
< |
ScheduledExecutor two = new ScheduledExecutor(2); |
338 |
> |
public void testGetLargestPoolSize() { |
339 |
> |
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); |
340 |
> |
assertEquals(0, p2.getLargestPoolSize()); |
341 |
> |
p2.execute(new SmallRunnable()); |
342 |
> |
p2.execute(new SmallRunnable()); |
343 |
|
try { |
344 |
< |
assertEquals(0, two.getLargestPoolSize()); |
345 |
< |
two.execute(newRunnable()); |
346 |
< |
two.execute(newRunnable()); |
346 |
< |
try{ |
347 |
< |
Thread.sleep(SHORT_DELAY_MS); |
348 |
< |
} catch(Exception e){ |
349 |
< |
fail("unexpected exception"); |
350 |
< |
} |
351 |
< |
assertEquals(2, two.getLargestPoolSize()); |
352 |
< |
} finally { |
353 |
< |
two.shutdown(); |
344 |
> |
Thread.sleep(SHORT_DELAY_MS); |
345 |
> |
} catch(Exception e){ |
346 |
> |
unexpectedException(); |
347 |
|
} |
348 |
+ |
assertEquals(2, p2.getLargestPoolSize()); |
349 |
+ |
joinPool(p2); |
350 |
|
} |
351 |
|
|
352 |
|
/** |
353 |
< |
* Test to verify getPoolSize gives correct values |
353 |
> |
* getPoolSize increases, but doesn't overestimate, when threads |
354 |
> |
* become active |
355 |
|
*/ |
356 |
< |
public void testGetPoolSize(){ |
357 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
358 |
< |
try { |
359 |
< |
assertEquals(0, one.getPoolSize()); |
360 |
< |
one.execute(newRunnable()); |
361 |
< |
assertEquals(1, one.getPoolSize()); |
366 |
< |
} finally { |
367 |
< |
one.shutdown(); |
368 |
< |
} |
356 |
> |
public void testGetPoolSize() { |
357 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
358 |
> |
assertEquals(0, p1.getPoolSize()); |
359 |
> |
p1.execute(new SmallRunnable()); |
360 |
> |
assertEquals(1, p1.getPoolSize()); |
361 |
> |
joinPool(p1); |
362 |
|
} |
363 |
|
|
364 |
|
/** |
365 |
< |
* Test to verify getTaskCount gives correct values |
365 |
> |
* getTaskCount increases, but doesn't overestimate, when tasks |
366 |
> |
* submitted |
367 |
|
*/ |
368 |
< |
public void testGetTaskCount(){ |
369 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
368 |
> |
public void testGetTaskCount() { |
369 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
370 |
> |
assertEquals(0, p1.getTaskCount()); |
371 |
> |
for(int i = 0; i < 5; i++) |
372 |
> |
p1.execute(new SmallRunnable()); |
373 |
|
try { |
374 |
< |
assertEquals(0, one.getTaskCount()); |
375 |
< |
for(int i = 0; i < 5; i++) |
376 |
< |
one.execute(newRunnable()); |
377 |
< |
try{ |
378 |
< |
Thread.sleep(SHORT_DELAY_MS); |
379 |
< |
} catch(Exception e){ |
380 |
< |
fail("unexpected exception"); |
381 |
< |
} |
382 |
< |
assertEquals(5, one.getTaskCount()); |
374 |
> |
Thread.sleep(SHORT_DELAY_MS); |
375 |
> |
} catch(Exception e){ |
376 |
> |
unexpectedException(); |
377 |
> |
} |
378 |
> |
assertEquals(5, p1.getTaskCount()); |
379 |
> |
joinPool(p1); |
380 |
> |
} |
381 |
> |
|
382 |
> |
/** |
383 |
> |
* getThreadFactory returns factory in constructor if not set |
384 |
> |
*/ |
385 |
> |
public void testGetThreadFactory() { |
386 |
> |
ThreadFactory tf = new SimpleThreadFactory(); |
387 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf); |
388 |
> |
assertSame(tf, p.getThreadFactory()); |
389 |
> |
joinPool(p); |
390 |
> |
} |
391 |
> |
|
392 |
> |
/** |
393 |
> |
* setThreadFactory sets the thread factory returned by getThreadFactory |
394 |
> |
*/ |
395 |
> |
public void testSetThreadFactory() { |
396 |
> |
ThreadFactory tf = new SimpleThreadFactory(); |
397 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
398 |
> |
p.setThreadFactory(tf); |
399 |
> |
assertSame(tf, p.getThreadFactory()); |
400 |
> |
joinPool(p); |
401 |
> |
} |
402 |
> |
|
403 |
> |
/** |
404 |
> |
* setThreadFactory(null) throws NPE |
405 |
> |
*/ |
406 |
> |
public void testSetThreadFactoryNull() { |
407 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
408 |
> |
try { |
409 |
> |
p.setThreadFactory(null); |
410 |
> |
shouldThrow(); |
411 |
> |
} catch (NullPointerException success) { |
412 |
|
} finally { |
413 |
< |
one.shutdown(); |
413 |
> |
joinPool(p); |
414 |
|
} |
415 |
|
} |
416 |
|
|
417 |
|
/** |
418 |
< |
* Test to verify isShutDown gives correct values |
418 |
> |
* is isShutDown is false before shutdown, true after |
419 |
|
*/ |
420 |
< |
public void testIsShutdown(){ |
420 |
> |
public void testIsShutdown() { |
421 |
|
|
422 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
422 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
423 |
|
try { |
424 |
< |
assertFalse(one.isShutdown()); |
424 |
> |
assertFalse(p1.isShutdown()); |
425 |
|
} |
426 |
|
finally { |
427 |
< |
one.shutdown(); |
427 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
428 |
|
} |
429 |
< |
assertTrue(one.isShutdown()); |
429 |
> |
assertTrue(p1.isShutdown()); |
430 |
|
} |
431 |
|
|
432 |
|
|
433 |
|
/** |
434 |
< |
* Test to verify isTerminated gives correct values |
409 |
< |
* Makes sure termination does not take an innapropriate |
410 |
< |
* amount of time |
434 |
> |
* isTerminated is false before termination, true after |
435 |
|
*/ |
436 |
< |
public void testIsTerminated(){ |
437 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
436 |
> |
public void testIsTerminated() { |
437 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
438 |
|
try { |
439 |
< |
one.execute(newRunnable()); |
439 |
> |
p1.execute(new SmallRunnable()); |
440 |
|
} finally { |
441 |
< |
one.shutdown(); |
441 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
442 |
|
} |
443 |
< |
boolean flag = false; |
444 |
< |
try{ |
445 |
< |
flag = one.awaitTermination(10, TimeUnit.SECONDS); |
443 |
> |
try { |
444 |
> |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
445 |
> |
assertTrue(p1.isTerminated()); |
446 |
|
} catch(Exception e){ |
447 |
< |
fail("unexpected exception"); |
447 |
> |
unexpectedException(); |
448 |
|
} |
425 |
– |
assertTrue(one.isTerminated()); |
426 |
– |
if(!flag) |
427 |
– |
fail("ThreadPoolExecutor - thread pool did not terminate within suitable timeframe"); |
449 |
|
} |
450 |
|
|
451 |
|
/** |
452 |
< |
* Test to verify that purge correctly removes cancelled tasks |
432 |
< |
* from the queue |
452 |
> |
* isTerminating is not true when running or when terminated |
453 |
|
*/ |
454 |
< |
public void testPurge(){ |
455 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
454 |
> |
public void testIsTerminating() { |
455 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
456 |
> |
assertFalse(p1.isTerminating()); |
457 |
> |
try { |
458 |
> |
p1.execute(new SmallRunnable()); |
459 |
> |
assertFalse(p1.isTerminating()); |
460 |
> |
} finally { |
461 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
462 |
> |
} |
463 |
> |
try { |
464 |
> |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
465 |
> |
assertTrue(p1.isTerminated()); |
466 |
> |
assertFalse(p1.isTerminating()); |
467 |
> |
} catch(Exception e){ |
468 |
> |
unexpectedException(); |
469 |
> |
} |
470 |
> |
} |
471 |
> |
|
472 |
> |
/** |
473 |
> |
* getQueue returns the work queue, which contains queued tasks |
474 |
> |
*/ |
475 |
> |
public void testGetQueue() { |
476 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
477 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
478 |
> |
for(int i = 0; i < 5; i++){ |
479 |
> |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); |
480 |
> |
} |
481 |
> |
try { |
482 |
> |
Thread.sleep(SHORT_DELAY_MS); |
483 |
> |
BlockingQueue<Runnable> q = p1.getQueue(); |
484 |
> |
assertTrue(q.contains(tasks[4])); |
485 |
> |
assertFalse(q.contains(tasks[0])); |
486 |
> |
} catch(Exception e) { |
487 |
> |
unexpectedException(); |
488 |
> |
} finally { |
489 |
> |
joinPool(p1); |
490 |
> |
} |
491 |
> |
} |
492 |
> |
|
493 |
> |
/** |
494 |
> |
* remove(task) removes queued task, and fails to remove active task |
495 |
> |
*/ |
496 |
> |
public void testRemove() { |
497 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
498 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
499 |
> |
for(int i = 0; i < 5; i++){ |
500 |
> |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
501 |
> |
} |
502 |
> |
try { |
503 |
> |
Thread.sleep(SHORT_DELAY_MS); |
504 |
> |
BlockingQueue<Runnable> q = p1.getQueue(); |
505 |
> |
assertFalse(p1.remove((Runnable)tasks[0])); |
506 |
> |
assertTrue(q.contains((Runnable)tasks[4])); |
507 |
> |
assertTrue(q.contains((Runnable)tasks[3])); |
508 |
> |
assertTrue(p1.remove((Runnable)tasks[4])); |
509 |
> |
assertFalse(p1.remove((Runnable)tasks[4])); |
510 |
> |
assertFalse(q.contains((Runnable)tasks[4])); |
511 |
> |
assertTrue(q.contains((Runnable)tasks[3])); |
512 |
> |
assertTrue(p1.remove((Runnable)tasks[3])); |
513 |
> |
assertFalse(q.contains((Runnable)tasks[3])); |
514 |
> |
} catch(Exception e) { |
515 |
> |
unexpectedException(); |
516 |
> |
} finally { |
517 |
> |
joinPool(p1); |
518 |
> |
} |
519 |
> |
} |
520 |
> |
|
521 |
> |
/** |
522 |
> |
* purge removes cancelled tasks from the queue |
523 |
> |
*/ |
524 |
> |
public void testPurge() { |
525 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
526 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
527 |
> |
for(int i = 0; i < 5; i++){ |
528 |
> |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
529 |
> |
} |
530 |
|
try { |
437 |
– |
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
438 |
– |
for(int i = 0; i < 5; i++){ |
439 |
– |
tasks[i] = one.schedule(newRunnable(), 1, TimeUnit.MILLISECONDS); |
440 |
– |
} |
531 |
|
int max = 5; |
532 |
|
if (tasks[4].cancel(true)) --max; |
533 |
|
if (tasks[3].cancel(true)) --max; |
534 |
< |
one.purge(); |
535 |
< |
long count = one.getTaskCount(); |
536 |
< |
assertTrue(count > 0 && count <= max); |
534 |
> |
// There must eventually be an interference-free point at |
535 |
> |
// which purge will not fail. (At worst, when queue is empty.) |
536 |
> |
int k; |
537 |
> |
for (k = 0; k < SMALL_DELAY_MS; ++k) { |
538 |
> |
p1.purge(); |
539 |
> |
long count = p1.getTaskCount(); |
540 |
> |
if (count >= 0 && count <= max) |
541 |
> |
break; |
542 |
> |
Thread.sleep(1); |
543 |
> |
} |
544 |
> |
assertTrue(k < SMALL_DELAY_MS); |
545 |
> |
} catch(Exception e) { |
546 |
> |
unexpectedException(); |
547 |
|
} finally { |
548 |
< |
one.shutdown(); |
548 |
> |
joinPool(p1); |
549 |
|
} |
550 |
|
} |
551 |
|
|
552 |
|
/** |
553 |
< |
* Test to verify shutDownNow returns a list |
454 |
< |
* containing the correct number of elements |
553 |
> |
* shutDownNow returns a list containing tasks that were not run |
554 |
|
*/ |
555 |
< |
public void testShutDownNow(){ |
556 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
555 |
> |
public void testShutDownNow() { |
556 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
557 |
|
for(int i = 0; i < 5; i++) |
558 |
< |
one.schedule(newRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
559 |
< |
List l = one.shutdownNow(); |
560 |
< |
assertTrue(one.isShutdown()); |
558 |
> |
p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
559 |
> |
List l; |
560 |
> |
try { |
561 |
> |
l = p1.shutdownNow(); |
562 |
> |
} catch (SecurityException ok) { |
563 |
> |
return; |
564 |
> |
} |
565 |
> |
assertTrue(p1.isShutdown()); |
566 |
|
assertTrue(l.size() > 0 && l.size() <= 5); |
567 |
+ |
joinPool(p1); |
568 |
|
} |
569 |
|
|
570 |
< |
public void testShutDown1(){ |
570 |
> |
/** |
571 |
> |
* In default setting, shutdown cancels periodic but not delayed |
572 |
> |
* tasks at shutdown |
573 |
> |
*/ |
574 |
> |
public void testShutDown1() { |
575 |
|
try { |
576 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
577 |
< |
assertTrue(one.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
578 |
< |
assertFalse(one.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
576 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
577 |
> |
assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
578 |
> |
assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
579 |
|
|
580 |
< |
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
580 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
581 |
|
for(int i = 0; i < 5; i++) |
582 |
< |
tasks[i] = one.schedule(newNoopRunnable(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS); |
583 |
< |
one.shutdown(); |
584 |
< |
BlockingQueue q = one.getQueue(); |
582 |
> |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
583 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
584 |
> |
BlockingQueue q = p1.getQueue(); |
585 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
586 |
< |
ScheduledCancellable t = (ScheduledCancellable)it.next(); |
586 |
> |
ScheduledFuture t = (ScheduledFuture)it.next(); |
587 |
|
assertFalse(t.isCancelled()); |
588 |
|
} |
589 |
< |
assertTrue(one.isShutdown()); |
590 |
< |
Thread.sleep(SHORT_DELAY_MS); |
589 |
> |
assertTrue(p1.isShutdown()); |
590 |
> |
Thread.sleep(SMALL_DELAY_MS); |
591 |
|
for (int i = 0; i < 5; ++i) { |
592 |
|
assertTrue(tasks[i].isDone()); |
593 |
|
assertFalse(tasks[i].isCancelled()); |
595 |
|
|
596 |
|
} |
597 |
|
catch(Exception ex) { |
598 |
< |
fail("unexpected exception"); |
598 |
> |
unexpectedException(); |
599 |
|
} |
600 |
|
} |
601 |
|
|
602 |
|
|
603 |
< |
public void testShutDown2(){ |
603 |
> |
/** |
604 |
> |
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
605 |
> |
* delayed tasks are cancelled at shutdown |
606 |
> |
*/ |
607 |
> |
public void testShutDown2() { |
608 |
|
try { |
609 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
610 |
< |
one.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
611 |
< |
ScheduledCancellable[] tasks = new ScheduledCancellable[5]; |
609 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
610 |
> |
p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
611 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
612 |
|
for(int i = 0; i < 5; i++) |
613 |
< |
tasks[i] = one.schedule(newNoopRunnable(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS); |
614 |
< |
one.shutdown(); |
615 |
< |
assertTrue(one.isShutdown()); |
616 |
< |
BlockingQueue q = one.getQueue(); |
613 |
> |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
614 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
615 |
> |
assertTrue(p1.isShutdown()); |
616 |
> |
BlockingQueue q = p1.getQueue(); |
617 |
|
assertTrue(q.isEmpty()); |
618 |
< |
Thread.sleep(SHORT_DELAY_MS); |
619 |
< |
assertTrue(one.isTerminated()); |
618 |
> |
Thread.sleep(SMALL_DELAY_MS); |
619 |
> |
assertTrue(p1.isTerminated()); |
620 |
|
} |
621 |
|
catch(Exception ex) { |
622 |
< |
fail("unexpected exception"); |
622 |
> |
unexpectedException(); |
623 |
|
} |
624 |
|
} |
625 |
|
|
626 |
|
|
627 |
< |
public void testShutDown3(){ |
627 |
> |
/** |
628 |
> |
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
629 |
> |
* periodic tasks are not cancelled at shutdown |
630 |
> |
*/ |
631 |
> |
public void testShutDown3() { |
632 |
|
try { |
633 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
634 |
< |
one.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
635 |
< |
ScheduledCancellable task = |
636 |
< |
one.scheduleAtFixedRate(newNoopRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
637 |
< |
one.shutdown(); |
638 |
< |
assertTrue(one.isShutdown()); |
639 |
< |
BlockingQueue q = one.getQueue(); |
633 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
634 |
> |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
635 |
> |
ScheduledFuture task = |
636 |
> |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
637 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
638 |
> |
assertTrue(p1.isShutdown()); |
639 |
> |
BlockingQueue q = p1.getQueue(); |
640 |
|
assertTrue(q.isEmpty()); |
641 |
|
Thread.sleep(SHORT_DELAY_MS); |
642 |
< |
assertTrue(one.isTerminated()); |
642 |
> |
assertTrue(p1.isTerminated()); |
643 |
|
} |
644 |
|
catch(Exception ex) { |
645 |
< |
fail("unexpected exception"); |
645 |
> |
unexpectedException(); |
646 |
|
} |
647 |
|
} |
648 |
|
|
649 |
< |
public void testShutDown4(){ |
650 |
< |
ScheduledExecutor one = new ScheduledExecutor(1); |
649 |
> |
/** |
650 |
> |
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
651 |
> |
* periodic tasks are cancelled at shutdown |
652 |
> |
*/ |
653 |
> |
public void testShutDown4() { |
654 |
> |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
655 |
|
try { |
656 |
< |
one.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
657 |
< |
ScheduledCancellable task = |
658 |
< |
one.scheduleAtFixedRate(newNoopRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
656 |
> |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
657 |
> |
ScheduledFuture task = |
658 |
> |
p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, TimeUnit.MILLISECONDS); |
659 |
|
assertFalse(task.isCancelled()); |
660 |
< |
one.shutdown(); |
660 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
661 |
|
assertFalse(task.isCancelled()); |
662 |
< |
assertFalse(one.isTerminated()); |
663 |
< |
assertTrue(one.isShutdown()); |
662 |
> |
assertFalse(p1.isTerminated()); |
663 |
> |
assertTrue(p1.isShutdown()); |
664 |
|
Thread.sleep(SHORT_DELAY_MS); |
665 |
|
assertFalse(task.isCancelled()); |
666 |
< |
task.cancel(true); |
667 |
< |
assertTrue(task.isCancelled()); |
666 |
> |
assertTrue(task.cancel(true)); |
667 |
> |
assertTrue(task.isDone()); |
668 |
|
Thread.sleep(SHORT_DELAY_MS); |
669 |
< |
assertTrue(one.isTerminated()); |
669 |
> |
assertTrue(p1.isTerminated()); |
670 |
|
} |
671 |
|
catch(Exception ex) { |
672 |
< |
fail("unexpected exception"); |
672 |
> |
unexpectedException(); |
673 |
|
} |
674 |
< |
finally { |
675 |
< |
one.shutdownNow(); |
674 |
> |
finally { |
675 |
> |
joinPool(p1); |
676 |
> |
} |
677 |
> |
} |
678 |
> |
|
679 |
> |
/** |
680 |
> |
* completed submit of callable returns result |
681 |
> |
*/ |
682 |
> |
public void testSubmitCallable() { |
683 |
> |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
684 |
> |
try { |
685 |
> |
Future<String> future = e.submit(new StringTask()); |
686 |
> |
String result = future.get(); |
687 |
> |
assertSame(TEST_STRING, result); |
688 |
> |
} |
689 |
> |
catch (ExecutionException ex) { |
690 |
> |
unexpectedException(); |
691 |
> |
} |
692 |
> |
catch (InterruptedException ex) { |
693 |
> |
unexpectedException(); |
694 |
> |
} finally { |
695 |
> |
joinPool(e); |
696 |
> |
} |
697 |
> |
} |
698 |
> |
|
699 |
> |
/** |
700 |
> |
* completed submit of runnable returns successfully |
701 |
> |
*/ |
702 |
> |
public void testSubmitRunnable() { |
703 |
> |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
704 |
> |
try { |
705 |
> |
Future<?> future = e.submit(new NoOpRunnable()); |
706 |
> |
future.get(); |
707 |
> |
assertTrue(future.isDone()); |
708 |
> |
} |
709 |
> |
catch (ExecutionException ex) { |
710 |
> |
unexpectedException(); |
711 |
> |
} |
712 |
> |
catch (InterruptedException ex) { |
713 |
> |
unexpectedException(); |
714 |
> |
} finally { |
715 |
> |
joinPool(e); |
716 |
> |
} |
717 |
> |
} |
718 |
> |
|
719 |
> |
/** |
720 |
> |
* completed submit of (runnable, result) returns result |
721 |
> |
*/ |
722 |
> |
public void testSubmitRunnable2() { |
723 |
> |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
724 |
> |
try { |
725 |
> |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
726 |
> |
String result = future.get(); |
727 |
> |
assertSame(TEST_STRING, result); |
728 |
> |
} |
729 |
> |
catch (ExecutionException ex) { |
730 |
> |
unexpectedException(); |
731 |
> |
} |
732 |
> |
catch (InterruptedException ex) { |
733 |
> |
unexpectedException(); |
734 |
> |
} finally { |
735 |
> |
joinPool(e); |
736 |
> |
} |
737 |
> |
} |
738 |
> |
|
739 |
> |
/** |
740 |
> |
* invokeAny(null) throws NPE |
741 |
> |
*/ |
742 |
> |
public void testInvokeAny1() { |
743 |
> |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
744 |
> |
try { |
745 |
> |
e.invokeAny(null); |
746 |
> |
} catch (NullPointerException success) { |
747 |
> |
} catch(Exception ex) { |
748 |
> |
unexpectedException(); |
749 |
> |
} finally { |
750 |
> |
joinPool(e); |
751 |
> |
} |
752 |
> |
} |
753 |
> |
|
754 |
> |
/** |
755 |
> |
* invokeAny(empty collection) throws IAE |
756 |
> |
*/ |
757 |
> |
public void testInvokeAny2() { |
758 |
> |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
759 |
> |
try { |
760 |
> |
e.invokeAny(new ArrayList<Callable<String>>()); |
761 |
> |
} catch (IllegalArgumentException success) { |
762 |
> |
} catch(Exception ex) { |
763 |
> |
unexpectedException(); |
764 |
> |
} finally { |
765 |
> |
joinPool(e); |
766 |
> |
} |
767 |
> |
} |
768 |
> |
|
769 |
> |
/** |
770 |
> |
* invokeAny(c) throws NPE if c has null elements |
771 |
> |
*/ |
772 |
> |
public void testInvokeAny3() { |
773 |
> |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
774 |
> |
try { |
775 |
> |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
776 |
> |
l.add(new StringTask()); |
777 |
> |
l.add(null); |
778 |
> |
e.invokeAny(l); |
779 |
> |
} catch (NullPointerException success) { |
780 |
> |
} catch(Exception ex) { |
781 |
> |
unexpectedException(); |
782 |
> |
} finally { |
783 |
> |
joinPool(e); |
784 |
|
} |
785 |
|
} |
786 |
|
|
787 |
+ |
/** |
788 |
+ |
* invokeAny(c) throws ExecutionException if no task completes |
789 |
+ |
*/ |
790 |
+ |
public void testInvokeAny4() { |
791 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
792 |
+ |
try { |
793 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
794 |
+ |
l.add(new NPETask()); |
795 |
+ |
e.invokeAny(l); |
796 |
+ |
} catch (ExecutionException success) { |
797 |
+ |
} catch(Exception ex) { |
798 |
+ |
unexpectedException(); |
799 |
+ |
} finally { |
800 |
+ |
joinPool(e); |
801 |
+ |
} |
802 |
+ |
} |
803 |
+ |
|
804 |
+ |
/** |
805 |
+ |
* invokeAny(c) returns result of some task |
806 |
+ |
*/ |
807 |
+ |
public void testInvokeAny5() { |
808 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
809 |
+ |
try { |
810 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
811 |
+ |
l.add(new StringTask()); |
812 |
+ |
l.add(new StringTask()); |
813 |
+ |
String result = e.invokeAny(l); |
814 |
+ |
assertSame(TEST_STRING, result); |
815 |
+ |
} catch (ExecutionException success) { |
816 |
+ |
} catch(Exception ex) { |
817 |
+ |
unexpectedException(); |
818 |
+ |
} finally { |
819 |
+ |
joinPool(e); |
820 |
+ |
} |
821 |
+ |
} |
822 |
+ |
|
823 |
+ |
/** |
824 |
+ |
* invokeAll(null) throws NPE |
825 |
+ |
*/ |
826 |
+ |
public void testInvokeAll1() { |
827 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
828 |
+ |
try { |
829 |
+ |
e.invokeAll(null); |
830 |
+ |
} catch (NullPointerException success) { |
831 |
+ |
} catch(Exception ex) { |
832 |
+ |
unexpectedException(); |
833 |
+ |
} finally { |
834 |
+ |
joinPool(e); |
835 |
+ |
} |
836 |
+ |
} |
837 |
+ |
|
838 |
+ |
/** |
839 |
+ |
* invokeAll(empty collection) returns empty collection |
840 |
+ |
*/ |
841 |
+ |
public void testInvokeAll2() { |
842 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
843 |
+ |
try { |
844 |
+ |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
845 |
+ |
assertTrue(r.isEmpty()); |
846 |
+ |
} catch(Exception ex) { |
847 |
+ |
unexpectedException(); |
848 |
+ |
} finally { |
849 |
+ |
joinPool(e); |
850 |
+ |
} |
851 |
+ |
} |
852 |
+ |
|
853 |
+ |
/** |
854 |
+ |
* invokeAll(c) throws NPE if c has null elements |
855 |
+ |
*/ |
856 |
+ |
public void testInvokeAll3() { |
857 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
858 |
+ |
try { |
859 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
860 |
+ |
l.add(new StringTask()); |
861 |
+ |
l.add(null); |
862 |
+ |
e.invokeAll(l); |
863 |
+ |
} catch (NullPointerException success) { |
864 |
+ |
} catch(Exception ex) { |
865 |
+ |
unexpectedException(); |
866 |
+ |
} finally { |
867 |
+ |
joinPool(e); |
868 |
+ |
} |
869 |
+ |
} |
870 |
+ |
|
871 |
+ |
/** |
872 |
+ |
* get of invokeAll(c) throws exception on failed task |
873 |
+ |
*/ |
874 |
+ |
public void testInvokeAll4() { |
875 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
876 |
+ |
try { |
877 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
878 |
+ |
l.add(new NPETask()); |
879 |
+ |
List<Future<String>> result = e.invokeAll(l); |
880 |
+ |
assertEquals(1, result.size()); |
881 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
882 |
+ |
it.next().get(); |
883 |
+ |
} catch(ExecutionException success) { |
884 |
+ |
} catch(Exception ex) { |
885 |
+ |
unexpectedException(); |
886 |
+ |
} finally { |
887 |
+ |
joinPool(e); |
888 |
+ |
} |
889 |
+ |
} |
890 |
+ |
|
891 |
+ |
/** |
892 |
+ |
* invokeAll(c) returns results of all completed tasks |
893 |
+ |
*/ |
894 |
+ |
public void testInvokeAll5() { |
895 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
896 |
+ |
try { |
897 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
898 |
+ |
l.add(new StringTask()); |
899 |
+ |
l.add(new StringTask()); |
900 |
+ |
List<Future<String>> result = e.invokeAll(l); |
901 |
+ |
assertEquals(2, result.size()); |
902 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
903 |
+ |
assertSame(TEST_STRING, it.next().get()); |
904 |
+ |
} catch (ExecutionException success) { |
905 |
+ |
} catch(Exception ex) { |
906 |
+ |
unexpectedException(); |
907 |
+ |
} finally { |
908 |
+ |
joinPool(e); |
909 |
+ |
} |
910 |
+ |
} |
911 |
+ |
|
912 |
+ |
/** |
913 |
+ |
* timed invokeAny(null) throws NPE |
914 |
+ |
*/ |
915 |
+ |
public void testTimedInvokeAny1() { |
916 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
917 |
+ |
try { |
918 |
+ |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
919 |
+ |
} catch (NullPointerException success) { |
920 |
+ |
} catch(Exception ex) { |
921 |
+ |
unexpectedException(); |
922 |
+ |
} finally { |
923 |
+ |
joinPool(e); |
924 |
+ |
} |
925 |
+ |
} |
926 |
+ |
|
927 |
+ |
/** |
928 |
+ |
* timed invokeAny(,,null) throws NPE |
929 |
+ |
*/ |
930 |
+ |
public void testTimedInvokeAnyNullTimeUnit() { |
931 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
932 |
+ |
try { |
933 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
934 |
+ |
l.add(new StringTask()); |
935 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
936 |
+ |
} catch (NullPointerException success) { |
937 |
+ |
} catch(Exception ex) { |
938 |
+ |
unexpectedException(); |
939 |
+ |
} finally { |
940 |
+ |
joinPool(e); |
941 |
+ |
} |
942 |
+ |
} |
943 |
+ |
|
944 |
+ |
/** |
945 |
+ |
* timed invokeAny(empty collection) throws IAE |
946 |
+ |
*/ |
947 |
+ |
public void testTimedInvokeAny2() { |
948 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
949 |
+ |
try { |
950 |
+ |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
951 |
+ |
} catch (IllegalArgumentException success) { |
952 |
+ |
} catch(Exception ex) { |
953 |
+ |
unexpectedException(); |
954 |
+ |
} finally { |
955 |
+ |
joinPool(e); |
956 |
+ |
} |
957 |
+ |
} |
958 |
+ |
|
959 |
+ |
/** |
960 |
+ |
* timed invokeAny(c) throws NPE if c has null elements |
961 |
+ |
*/ |
962 |
+ |
public void testTimedInvokeAny3() { |
963 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
964 |
+ |
try { |
965 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
966 |
+ |
l.add(new StringTask()); |
967 |
+ |
l.add(null); |
968 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
969 |
+ |
} catch (NullPointerException success) { |
970 |
+ |
} catch(Exception ex) { |
971 |
+ |
ex.printStackTrace(); |
972 |
+ |
unexpectedException(); |
973 |
+ |
} finally { |
974 |
+ |
joinPool(e); |
975 |
+ |
} |
976 |
+ |
} |
977 |
+ |
|
978 |
+ |
/** |
979 |
+ |
* timed invokeAny(c) throws ExecutionException if no task completes |
980 |
+ |
*/ |
981 |
+ |
public void testTimedInvokeAny4() { |
982 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
983 |
+ |
try { |
984 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
985 |
+ |
l.add(new NPETask()); |
986 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
987 |
+ |
} catch(ExecutionException success) { |
988 |
+ |
} catch(Exception ex) { |
989 |
+ |
unexpectedException(); |
990 |
+ |
} finally { |
991 |
+ |
joinPool(e); |
992 |
+ |
} |
993 |
+ |
} |
994 |
+ |
|
995 |
+ |
/** |
996 |
+ |
* timed invokeAny(c) returns result of some task |
997 |
+ |
*/ |
998 |
+ |
public void testTimedInvokeAny5() { |
999 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1000 |
+ |
try { |
1001 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1002 |
+ |
l.add(new StringTask()); |
1003 |
+ |
l.add(new StringTask()); |
1004 |
+ |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1005 |
+ |
assertSame(TEST_STRING, result); |
1006 |
+ |
} catch (ExecutionException success) { |
1007 |
+ |
} catch(Exception ex) { |
1008 |
+ |
unexpectedException(); |
1009 |
+ |
} finally { |
1010 |
+ |
joinPool(e); |
1011 |
+ |
} |
1012 |
+ |
} |
1013 |
+ |
|
1014 |
+ |
/** |
1015 |
+ |
* timed invokeAll(null) throws NPE |
1016 |
+ |
*/ |
1017 |
+ |
public void testTimedInvokeAll1() { |
1018 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1019 |
+ |
try { |
1020 |
+ |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1021 |
+ |
} catch (NullPointerException success) { |
1022 |
+ |
} catch(Exception ex) { |
1023 |
+ |
unexpectedException(); |
1024 |
+ |
} finally { |
1025 |
+ |
joinPool(e); |
1026 |
+ |
} |
1027 |
+ |
} |
1028 |
+ |
|
1029 |
+ |
/** |
1030 |
+ |
* timed invokeAll(,,null) throws NPE |
1031 |
+ |
*/ |
1032 |
+ |
public void testTimedInvokeAllNullTimeUnit() { |
1033 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1034 |
+ |
try { |
1035 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1036 |
+ |
l.add(new StringTask()); |
1037 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1038 |
+ |
} catch (NullPointerException success) { |
1039 |
+ |
} catch(Exception ex) { |
1040 |
+ |
unexpectedException(); |
1041 |
+ |
} finally { |
1042 |
+ |
joinPool(e); |
1043 |
+ |
} |
1044 |
+ |
} |
1045 |
+ |
|
1046 |
+ |
/** |
1047 |
+ |
* timed invokeAll(empty collection) returns empty collection |
1048 |
+ |
*/ |
1049 |
+ |
public void testTimedInvokeAll2() { |
1050 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1051 |
+ |
try { |
1052 |
+ |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1053 |
+ |
assertTrue(r.isEmpty()); |
1054 |
+ |
} catch(Exception ex) { |
1055 |
+ |
unexpectedException(); |
1056 |
+ |
} finally { |
1057 |
+ |
joinPool(e); |
1058 |
+ |
} |
1059 |
+ |
} |
1060 |
+ |
|
1061 |
+ |
/** |
1062 |
+ |
* timed invokeAll(c) throws NPE if c has null elements |
1063 |
+ |
*/ |
1064 |
+ |
public void testTimedInvokeAll3() { |
1065 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1066 |
+ |
try { |
1067 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1068 |
+ |
l.add(new StringTask()); |
1069 |
+ |
l.add(null); |
1070 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1071 |
+ |
} catch (NullPointerException success) { |
1072 |
+ |
} catch(Exception ex) { |
1073 |
+ |
unexpectedException(); |
1074 |
+ |
} finally { |
1075 |
+ |
joinPool(e); |
1076 |
+ |
} |
1077 |
+ |
} |
1078 |
+ |
|
1079 |
+ |
/** |
1080 |
+ |
* get of element of invokeAll(c) throws exception on failed task |
1081 |
+ |
*/ |
1082 |
+ |
public void testTimedInvokeAll4() { |
1083 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1084 |
+ |
try { |
1085 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1086 |
+ |
l.add(new NPETask()); |
1087 |
+ |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1088 |
+ |
assertEquals(1, result.size()); |
1089 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1090 |
+ |
it.next().get(); |
1091 |
+ |
} catch(ExecutionException success) { |
1092 |
+ |
} catch(Exception ex) { |
1093 |
+ |
unexpectedException(); |
1094 |
+ |
} finally { |
1095 |
+ |
joinPool(e); |
1096 |
+ |
} |
1097 |
+ |
} |
1098 |
+ |
|
1099 |
+ |
/** |
1100 |
+ |
* timed invokeAll(c) returns results of all completed tasks |
1101 |
+ |
*/ |
1102 |
+ |
public void testTimedInvokeAll5() { |
1103 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1104 |
+ |
try { |
1105 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1106 |
+ |
l.add(new StringTask()); |
1107 |
+ |
l.add(new StringTask()); |
1108 |
+ |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1109 |
+ |
assertEquals(2, result.size()); |
1110 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1111 |
+ |
assertSame(TEST_STRING, it.next().get()); |
1112 |
+ |
} catch (ExecutionException success) { |
1113 |
+ |
} catch(Exception ex) { |
1114 |
+ |
unexpectedException(); |
1115 |
+ |
} finally { |
1116 |
+ |
joinPool(e); |
1117 |
+ |
} |
1118 |
+ |
} |
1119 |
+ |
|
1120 |
+ |
/** |
1121 |
+ |
* timed invokeAll(c) cancels tasks not completed by timeout |
1122 |
+ |
*/ |
1123 |
+ |
public void testTimedInvokeAll6() { |
1124 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1125 |
+ |
try { |
1126 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1127 |
+ |
l.add(new StringTask()); |
1128 |
+ |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1129 |
+ |
l.add(new StringTask()); |
1130 |
+ |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
1131 |
+ |
assertEquals(3, result.size()); |
1132 |
+ |
Iterator<Future<String>> it = result.iterator(); |
1133 |
+ |
Future<String> f1 = it.next(); |
1134 |
+ |
Future<String> f2 = it.next(); |
1135 |
+ |
Future<String> f3 = it.next(); |
1136 |
+ |
assertTrue(f1.isDone()); |
1137 |
+ |
assertTrue(f2.isDone()); |
1138 |
+ |
assertTrue(f3.isDone()); |
1139 |
+ |
assertFalse(f1.isCancelled()); |
1140 |
+ |
assertTrue(f2.isCancelled()); |
1141 |
+ |
} catch(Exception ex) { |
1142 |
+ |
unexpectedException(); |
1143 |
+ |
} finally { |
1144 |
+ |
joinPool(e); |
1145 |
+ |
} |
1146 |
+ |
} |
1147 |
+ |
|
1148 |
+ |
|
1149 |
|
} |