Debian Codegraph @

sourceTest/java/ch/systemsx/cisd/base/namedthread/NamingThreadPoolExecutorTest.java

raw
1 /*
2 * Copyright 2007 - 2018 ETH Zuerich, CISD and SIS.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package ch.systemsx.cisd.base.namedthread;
18
19 import static org.testng.AssertJUnit.assertEquals;
20 import static org.testng.AssertJUnit.assertFalse;
21 import static org.testng.AssertJUnit.assertTrue;
22 import static org.testng.AssertJUnit.fail;
23
24 import java.lang.reflect.InvocationTargetException;
25 import java.lang.reflect.Method;
26 import java.util.Collections;
27 import java.util.HashSet;
28 import java.util.Set;
29 import java.util.concurrent.Callable;
30 import java.util.concurrent.ExecutionException;
31 import java.util.concurrent.Future;
32 import java.util.concurrent.Semaphore;
33 import java.util.concurrent.ThreadFactory;
34 import java.util.concurrent.ThreadPoolExecutor;
35 import java.util.concurrent.TimeUnit;
36
37 import org.testng.annotations.Test;
38
39 import ch.systemsx.cisd.base.BuildAndEnvironmentInfo;
40 import ch.systemsx.cisd.base.tests.Retry10;
41
42 /**
43 * Test cases for the {@link NamingThreadPoolExecutor}.
44 *
45 * @author Bernd Rinn
46 */
47 public class NamingThreadPoolExecutorTest
48 {
49
50 private final static String name = "This is the pool name";
51
52 @Test
53 public void testNamedPool() throws Throwable
54 {
55 final ThreadPoolExecutor eservice =
56 new NamingThreadPoolExecutor(name).corePoolSize(1).maximumPoolSize(2).daemonize();
57 assertEquals(1, eservice.getCorePoolSize());
58 assertEquals(2, eservice.getMaximumPoolSize());
59 final Future<?> future = eservice.submit(new Runnable()
60 {
61 @Override
62 public void run()
63 {
64 assertEquals(name + "-T1", Thread.currentThread().getName());
65 }
66 });
67 try
68 {
69 future.get(200L, TimeUnit.MILLISECONDS);
70 } catch (ExecutionException ex)
71 {
72 throw ex.getCause();
73 }
74 }
75
76 @Test
77 public void testDaemonize()
78 {
79 final NamingThreadPoolExecutor eservice =
80 new NamingThreadPoolExecutor(name).corePoolSize(1).maximumPoolSize(2);
81 assertFalse(eservice.getThreadFactory().isCreateDaemonThreads());
82 eservice.daemonize();
83 assertTrue(eservice.getThreadFactory().isCreateDaemonThreads());
84 }
85
86 @Test
87 public void testSetNamedThreadFactory()
88 {
89 final NamingThreadPoolExecutor eservice =
90 new NamingThreadPoolExecutor(name).corePoolSize(1).maximumPoolSize(2).daemonize();
91 final NamingThreadFactory factory = new NamingThreadFactory("name");
92 eservice.setThreadFactory(factory);
93 assertEquals(factory, eservice.getThreadFactory());
94 }
95
96 @Test
97 public void testSetThreadFactory()
98 {
99 final ThreadPoolExecutor eservice =
100 new NamingThreadPoolExecutor(name).corePoolSize(1).maximumPoolSize(2).daemonize();
101 final ThreadFactory factory = new NamingThreadFactory("name");
102 eservice.setThreadFactory(factory);
103 assertEquals(factory, eservice.getThreadFactory());
104 }
105
106 @Test(expectedExceptions = IllegalArgumentException.class)
107 public void testSetThreadFactoryFailed()
108 {
109 final ThreadPoolExecutor eservice =
110 new NamingThreadPoolExecutor(name).corePoolSize(1).maximumPoolSize(2).daemonize();
111 final ThreadFactory factory = new ThreadFactory()
112 {
113 @Override
114 public Thread newThread(Runnable r)
115 {
116 return null; // Doesn't matter, never used
117 }
118 };
119 // It needs to be NamingThreadFactory, thus it will throw an IllegalArgumentException.
120 eservice.setThreadFactory(factory);
121 }
122
123 @Test(groups = "slow")
124 public void testThreadDefaultNames() throws Throwable
125 {
126 final int max = 10;
127 final ThreadPoolExecutor eservice =
128 new NamingThreadPoolExecutor(name).corePoolSize(max).maximumPoolSize(max).daemonize();
129 assertEquals(max, eservice.getCorePoolSize());
130 assertEquals(max, eservice.getMaximumPoolSize());
131 final Set<String> expectedThreadNameSet = new HashSet<String>();
132 for (int i = 1; i <= max; ++i)
133 {
134 expectedThreadNameSet.add(name + "-T" + i);
135 }
136 final Set<String> threadNameSet = Collections.synchronizedSet(new HashSet<String>());
137 final Set<Future<?>> futureSet = new HashSet<Future<?>>();
138 for (int i = 0; i < max; ++i)
139 {
140 futureSet.add(eservice.submit(new Runnable()
141 {
142 @Override
143 public void run()
144 {
145 threadNameSet.add(Thread.currentThread().getName());
146 try
147 {
148 Thread.sleep(20L);
149 } catch (InterruptedException ex)
150 {
151 fail("We got interrupted.");
152 }
153 }
154 }));
155 }
156 for (Future<?> future : futureSet)
157 {
158 try
159 {
160 future.get(400L, TimeUnit.MILLISECONDS);
161 } catch (ExecutionException ex)
162 {
163 throw ex.getCause();
164 }
165 }
166 assertEquals(expectedThreadNameSet, threadNameSet);
167 }
168
169 @Test(groups = "slow")
170 public void testSubmitNamedRunnable() throws Throwable
171 {
172 final String runnableName = "This is the special runnable name";
173 final ThreadPoolExecutor eservice =
174 new NamingThreadPoolExecutor(name).corePoolSize(1).maximumPoolSize(1).daemonize();
175 assertEquals(1, eservice.getCorePoolSize());
176 assertEquals(1, eservice.getMaximumPoolSize());
177 final Future<?> future = eservice.submit(new NamedRunnable()
178 {
179 @Override
180 public void run()
181 {
182 assertEquals(name + "-T1::" + runnableName, Thread.currentThread().getName());
183 }
184
185 @Override
186 public String getRunnableName()
187 {
188 return runnableName;
189 }
190 });
191 try
192 {
193 future.get(200L, TimeUnit.MILLISECONDS);
194 } catch (ExecutionException ex)
195 {
196 throw ex.getCause();
197 }
198 }
199
200 @Test(groups = "slow")
201 public void testExecuteNamedRunnable() throws Throwable
202 {
203 final String runnableName = "This is the special runnable name";
204 final ThreadPoolExecutor eservice =
205 new NamingThreadPoolExecutor(name).corePoolSize(1).maximumPoolSize(1).daemonize();
206 assertEquals(1, eservice.getCorePoolSize());
207 assertEquals(1, eservice.getMaximumPoolSize());
208 final Semaphore sem = new Semaphore(0);
209 eservice.execute(new NamedRunnable()
210 {
211 @Override
212 public void run()
213 {
214 assertEquals(name + "-T1::" + runnableName, Thread.currentThread().getName());
215 sem.release();
216 }
217
218 @Override
219 public String getRunnableName()
220 {
221 return runnableName;
222 }
223 });
224 assertTrue(sem.tryAcquire(200L, TimeUnit.MILLISECONDS));
225 }
226
227 interface MyRunnable extends Runnable, IRunnableNameProvider
228 {
229 }
230
231 @Test(groups = "slow")
232 public void testExecuteNamedMyRunnable() throws Throwable
233 {
234 final String runnableName = "This is the special runnable name";
235 final ThreadPoolExecutor eservice =
236 new NamingThreadPoolExecutor(name).corePoolSize(1).maximumPoolSize(1).daemonize();
237 assertEquals(1, eservice.getCorePoolSize());
238 assertEquals(1, eservice.getMaximumPoolSize());
239 final Semaphore sem = new Semaphore(0);
240 eservice.execute(new MyRunnable()
241 {
242 @Override
243 public void run()
244 {
245 assertEquals(name + "-T1::" + runnableName, Thread.currentThread().getName());
246 sem.release();
247 }
248
249 @Override
250 public String getRunnableName()
251 {
252 return runnableName;
253 }
254 });
255 assertTrue(sem.tryAcquire(200L, TimeUnit.MILLISECONDS));
256 }
257
258 @Test(groups = "slow")
259 public void testSubmitNamedCallable() throws Throwable
260 {
261 final String callableName = "This is the special callable name";
262 final ThreadPoolExecutor eservice =
263 new NamingThreadPoolExecutor(name).corePoolSize(1).maximumPoolSize(1).daemonize();
264 assertEquals(1, eservice.getCorePoolSize());
265 assertEquals(1, eservice.getMaximumPoolSize());
266 final Future<?> future = eservice.submit(new NamedCallable<Object>()
267 {
268 @Override
269 public Object call() throws Exception
270 {
271 assertEquals(name + "-T1::" + callableName, Thread.currentThread().getName());
272 return null;
273 }
274
275 @Override
276 public String getCallableName()
277 {
278 return callableName;
279 }
280 });
281 try
282 {
283 future.get(200L, TimeUnit.MILLISECONDS);
284 } catch (ExecutionException ex)
285 {
286 throw ex.getCause();
287 }
288 }
289
290 interface MyCallable extends Callable<Object>, ICallableNameProvider
291 {
292 }
293
294 @Test(groups = "slow")
295 public void testSubmitMyNamedCallable() throws Throwable
296 {
297 final String callableName = "This is the special callable name";
298 final ThreadPoolExecutor eservice =
299 new NamingThreadPoolExecutor(name).corePoolSize(1).maximumPoolSize(1).daemonize();
300 assertEquals(1, eservice.getCorePoolSize());
301 assertEquals(1, eservice.getMaximumPoolSize());
302 final Future<?> future = eservice.submit(new MyCallable()
303 {
304 @Override
305 public Object call() throws Exception
306 {
307 assertEquals(name + "-T1::" + callableName, Thread.currentThread().getName());
308 return null;
309 }
310
311 @Override
312 public String getCallableName()
313 {
314 return callableName;
315 }
316 });
317 try
318 {
319 future.get(200L, TimeUnit.MILLISECONDS);
320 } catch (ExecutionException ex)
321 {
322 throw ex.getCause();
323 }
324 }
325
326 @Test(groups = "slow", retryAnalyzer = Retry10.class)
327 public void testSubmitNamedCallables() throws Throwable
328 {
329 final String callableName1 = "This is the first special callable name";
330 final ThreadPoolExecutor eservice =
331 new NamingThreadPoolExecutor(name).corePoolSize(1).maximumPoolSize(1).daemonize();
332 assertEquals(1, eservice.getCorePoolSize());
333 assertEquals(1, eservice.getMaximumPoolSize());
334 final Future<?> future1 = eservice.submit(new NamedCallable<Object>()
335 {
336 @Override
337 public Object call() throws Exception
338 {
339 assertEquals(name + "-T1::" + callableName1, Thread.currentThread().getName());
340 return null;
341 }
342
343 @Override
344 public String getCallableName()
345 {
346 return callableName1;
347 }
348 });
349 try
350 {
351 future1.get(200L, TimeUnit.MILLISECONDS);
352 } catch (ExecutionException ex)
353 {
354 throw ex.getCause();
355 }
356 // On Linux x64, Java 1.6 we get a RejectedExecutionException if we continue immediately.
357 Thread.sleep(200L);
358 final String callableName2 = "This is the second special callable name";
359 final Future<?> future2 = eservice.submit(new NamedCallable<Object>()
360 {
361 @Override
362 public Object call() throws Exception
363 {
364 assertEquals(name + "-T1::" + callableName2, Thread.currentThread().getName());
365 return null;
366 }
367
368 @Override
369 public String getCallableName()
370 {
371 return callableName2;
372 }
373 });
374 try
375 {
376 future2.get(200L, TimeUnit.MILLISECONDS);
377 } catch (ExecutionException ex)
378 {
379 throw ex.getCause();
380 }
381 }
382
383
384 @Test(groups = "slow", retryAnalyzer = Retry10.class)
385 public void testSubmitQueuedNamedCallables() throws Throwable
386 {
387 final String callableName1 = "This is the first special callable name";
388 final ThreadPoolExecutor eservice =
389 new NamingThreadPoolExecutor(name, 1).corePoolSize(1).maximumPoolSize(1).daemonize();
390 assertEquals(1, eservice.getCorePoolSize());
391 assertEquals(1, eservice.getMaximumPoolSize());
392 final Future<?> future1 = eservice.submit(new NamedCallable<Object>()
393 {
394 @Override
395 public Object call() throws Exception
396 {
397 assertEquals(name + "-T1::" + callableName1, Thread.currentThread().getName());
398 Thread.sleep(100L);
399 return null;
400 }
401
402 @Override
403 public String getCallableName()
404 {
405 return callableName1;
406 }
407 });
408 final Future<?> future2 = eservice.submit(new NamedCallable<Object>()
409 {
410 @Override
411 public Object call() throws Exception
412 {
413 assertEquals(name + "-T1::" + callableName1, Thread.currentThread().getName());
414 Thread.sleep(100L);
415 return null;
416 }
417
418 @Override
419 public String getCallableName()
420 {
421 return callableName1;
422 }
423 });
424 try
425 {
426 future1.get(200L, TimeUnit.MILLISECONDS);
427 } catch (ExecutionException ex)
428 {
429 throw ex.getCause();
430 }
431 try
432 {
433 future2.get(200L, TimeUnit.MILLISECONDS);
434 } catch (ExecutionException ex)
435 {
436 throw ex.getCause();
437 }
438 }
439
440 public static void main(String[] args) throws Throwable
441 {
442 System.out.println(BuildAndEnvironmentInfo.INSTANCE);
443 System.out.println("Test class: " + NamingThreadPoolExecutorTest.class.getSimpleName());
444 System.out.println();
445 final NamingThreadPoolExecutorTest test = new NamingThreadPoolExecutorTest();
446 for (Method m : NamingThreadPoolExecutorTest.class.getMethods())
447 {
448 final Test testAnnotation = m.getAnnotation(Test.class);
449 if (testAnnotation == null)
450 {
451 continue;
452 }
453 if (m.getParameterTypes().length == 0)
454 {
455 System.out.println("Running " + m.getName());
456 try
457 {
458 m.invoke(test);
459 } catch (InvocationTargetException wrapperThrowable)
460 {
461 final Throwable th = wrapperThrowable.getCause();
462 boolean exceptionFound = false;
463 for (Class<?> expectedExClazz : testAnnotation.expectedExceptions())
464 {
465 if (expectedExClazz == th.getClass())
466 {
467 exceptionFound = true;
468 break;
469 }
470 }
471 if (exceptionFound == false)
472 {
473 throw th;
474 }
475 }
476 }
477 }
478 System.out.println("Tests OK!");
479 }
480
481 }