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