• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package test.thread;
2 
3 import org.testng.Assert;
4 import org.testng.collections.Lists;
5 import org.testng.collections.Maps;
6 import org.testng.collections.Sets;
7 
8 import test.SimpleBaseTest;
9 
10 import java.util.List;
11 import java.util.Map;
12 import java.util.Set;
13 
14 public 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 
initThreadLog()19   static void initThreadLog() {
20     m_threadIds = Sets.newHashSet();
21     m_suitesMap = Maps.newHashMap();
22     m_strings = Lists.newArrayList();
23   }
24 
logString(String s)25   protected void logString(String s) {
26     synchronized(m_strings) {
27       log("BaseThreadTest", "Logging string:" + s);
28       m_strings.add(s);
29     }
30   }
31 
getStrings()32   public static List<String> getStrings() {
33     return m_strings;
34   }
35 
logCurrentThread()36   protected void logCurrentThread() {
37     logThread(Thread.currentThread().getId());
38   }
39 
logThread(long threadId)40   protected void logThread(long threadId) {
41     synchronized(m_threadIds) {
42       log("BaseThreadTest", "Logging thread:" + threadId);
43       m_threadIds.add(threadId);
44     }
45   }
46 
logSuite(String suiteName, long time)47   protected void logSuite(String suiteName, long time) {
48     synchronized(m_suitesMap) {
49       m_suitesMap.put(suiteName, time);
50     }
51   }
52 
getThreadCount()53   static int getThreadCount() {
54     synchronized(m_threadIds) {
55       return m_threadIds.size();
56     }
57   }
58 
getSuitesMap()59   static Map<String, Long> getSuitesMap() {
60     return m_suitesMap;
61   }
62 
log(String cls, String s)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 
verifyThreads(int expected)70   protected void verifyThreads(int expected) {
71     Assert.assertEquals(getThreadCount(), expected,
72         "Ran on " + getThreadCount() + " threads instead of " + expected);
73   }
74 }
75