001 /* 002 * Copyright 2007-2009 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.test; 017 018 import java.util.ArrayList; 019 import java.util.List; 020 021 import org.junit.Assert; 022 023 /** 024 * Some tests will spawn threads which we want to wait for completion on before we move onto the 025 * next test. The ThreadMonitor is a place where those outstanding thread can be stored 026 * and handled by the test harnesses tearDown method. 027 * 028 * @author Kuali Rice Team (rice.collab@kuali.org) 029 * 030 */ 031 public class ThreadMonitor { 032 033 private static List<Thread> threads = new ArrayList<Thread>(); 034 035 public static void addThread(Thread thread) { 036 threads.add(thread); 037 } 038 039 /** 040 * Waits for all outstanding monitored threads to complete. If the 041 * specified timeout is exceeded for any given thread then the test 042 * will fail. 043 * 044 * @param maxWait maximum number of milliseconds to wait for any particular thread to die 045 */ 046 public static void tearDown(long maxWait) { 047 Thread thread = null; 048 try { 049 for (Thread t : threads) { 050 thread = t; 051 thread.join(maxWait); 052 } 053 } catch (InterruptedException e) { 054 Assert.fail("Failed to wait for test thread to complete: " + (thread == null ? null : thread.getName())); 055 } finally { 056 threads.clear(); 057 } 058 } 059 060 }