Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ThreadMonitor |
|
| 2.5;2.5 |
1 | /* | |
2 | * Copyright 2007-2009 The Kuali Foundation | |
3 | * | |
4 | * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php | |
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 | package org.kuali.rice.test; | |
17 | ||
18 | import java.util.ArrayList; | |
19 | import java.util.List; | |
20 | ||
21 | import org.junit.Assert; | |
22 | ||
23 | /** | |
24 | * Some tests will spawn threads which we want to wait for completion on before we move onto the | |
25 | * next test. The ThreadMonitor is a place where those outstanding thread can be stored | |
26 | * and handled by the test harnesses tearDown method. | |
27 | * | |
28 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
29 | * | |
30 | */ | |
31 | 0 | public class ThreadMonitor { |
32 | ||
33 | 0 | private static List<Thread> threads = new ArrayList<Thread>(); |
34 | ||
35 | public static void addThread(Thread thread) { | |
36 | 0 | threads.add(thread); |
37 | 0 | } |
38 | ||
39 | /** | |
40 | * Waits for all outstanding monitored threads to complete. If the | |
41 | * specified timeout is exceeded for any given thread then the test | |
42 | * will fail. | |
43 | * | |
44 | * @param maxWait maximum number of milliseconds to wait for any particular thread to die | |
45 | */ | |
46 | public static void tearDown(long maxWait) { | |
47 | 0 | Thread thread = null; |
48 | try { | |
49 | 0 | for (Thread t : threads) { |
50 | 0 | thread = t; |
51 | 0 | thread.join(maxWait); |
52 | } | |
53 | 0 | } catch (InterruptedException e) { |
54 | 0 | Assert.fail("Failed to wait for test thread to complete: " + (thread == null ? null : thread.getName())); |
55 | } finally { | |
56 | 0 | threads.clear(); |
57 | 0 | } |
58 | 0 | } |
59 | ||
60 | } |