1package test.thread; 2 3import org.testng.Assert; 4import org.testng.collections.Lists; 5import org.testng.collections.Maps; 6import org.testng.collections.Sets; 7 8import test.SimpleBaseTest; 9 10import java.util.List; 11import java.util.Map; 12import java.util.Set; 13 14public class BaseThreadTest extends SimpleBaseTest { 15 static private Set<Long> m_threadIds; 16 static private Map<String, Long> m_suitesMap; 17 static private List<String> m_strings; 18 19 static void initThreadLog() { 20 m_threadIds = Sets.newHashSet(); 21 m_suitesMap = Maps.newHashMap(); 22 m_strings = Lists.newArrayList(); 23 } 24 25 protected void logString(String s) { 26 synchronized(m_strings) { 27 log("BaseThreadTest", "Logging string:" + s); 28 m_strings.add(s); 29 } 30 } 31 32 public static List<String> getStrings() { 33 return m_strings; 34 } 35 36 protected void logCurrentThread() { 37 logThread(Thread.currentThread().getId()); 38 } 39 40 protected void logThread(long threadId) { 41 synchronized(m_threadIds) { 42 log("BaseThreadTest", "Logging thread:" + threadId); 43 m_threadIds.add(threadId); 44 } 45 } 46 47 protected void logSuite(String suiteName, long time) { 48 synchronized(m_suitesMap) { 49 m_suitesMap.put(suiteName, time); 50 } 51 } 52 53 static int getThreadCount() { 54 synchronized(m_threadIds) { 55 return m_threadIds.size(); 56 } 57 } 58 59 static Map<String, Long> getSuitesMap() { 60 return m_suitesMap; 61 } 62 63 protected void log(String cls, String s) { 64 if (false) { 65 System.out.println("[" + cls + "] thread:" + Thread.currentThread().getId() 66 + " hash:" + hashCode() + " " + s); 67 } 68 } 69 70 protected void verifyThreads(int expected) { 71 Assert.assertEquals(getThreadCount(), expected, 72 "Ran on " + getThreadCount() + " threads instead of " + expected); 73 } 74} 75