1 package test.thread;
2 
3 import org.testng.Assert;
4 import org.testng.TestListenerAdapter;
5 import org.testng.TestNG;
6 import org.testng.annotations.Test;
7 
8 import test.SimpleBaseTest;
9 
10 import java.util.Arrays;
11 import java.util.List;
12 import java.util.Map;
13 
14 public class ParallelSuiteTest extends SimpleBaseTest {
15 
16   @Test
suitesShouldRunInParallel1()17   public void suitesShouldRunInParallel1() {
18     runTest(5, 2, 2, null, Arrays.asList(
19         getPathToResource("suite-parallel-1.xml"),
20         getPathToResource("suite-parallel-2.xml")));
21   }
22 
23   @Test
suitesShouldRunInParallel2()24   public void suitesShouldRunInParallel2() {
25     runTest(5, 3, 3, null, Arrays.asList(
26         getPathToResource("suite-parallel-0.xml")));
27   }
28 
29   @Test(description = "Number of threads (2) is less than number of suites (3)")
suitesShouldRunInParallel3()30   public void suitesShouldRunInParallel3() {
31     final int SUITE_THREAD_POOL_SIZE = 2;
32     TestListenerAdapter tla = new TestListenerAdapter();
33     TestNG tng = create();
34     tng.setSuiteThreadPoolSize(SUITE_THREAD_POOL_SIZE);
35     tng.setTestSuites(Arrays.asList(getPathToResource("suite-parallel-0.xml")));
36     tng.addListener(tla);
37 
38     BaseThreadTest.initThreadLog();
39     tng.run(); //Shouldn't not deadlock
40     Assert.assertEquals(BaseThreadTest.getThreadCount(), SUITE_THREAD_POOL_SIZE);
41   }
42 
runTest(int suiteThreadPoolSize, int expectedThreadCount, int expectedSuiteCount, Boolean randomizeSuites, List<String> paths)43   private void runTest(int suiteThreadPoolSize, int expectedThreadCount,
44         int expectedSuiteCount, Boolean randomizeSuites, List<String> paths) {
45     TestListenerAdapter tla = new TestListenerAdapter();
46     TestNG tng = create();
47     tng.setSuiteThreadPoolSize(suiteThreadPoolSize);
48     tng.setTestSuites(paths);
49     tng.addListener(tla);
50     if (null != randomizeSuites) {
51       tng.setRandomizeSuites(randomizeSuites);
52     }
53 
54     BaseThreadTest.initThreadLog();
55     tng.run();
56 
57     Assert.assertEquals(BaseThreadTest.getThreadCount(), expectedThreadCount,
58         "Thread count expected:" + expectedThreadCount
59         + " actual:" + BaseThreadTest.getThreadCount());
60     Assert.assertEquals(BaseThreadTest.getSuitesMap().keySet().size(), expectedSuiteCount);
61   }
62 
63   @Test
suitesShouldRunInParallel4()64   public void suitesShouldRunInParallel4() {
65     runTest(10, 5, 5, null, Arrays.asList(
66         getPathToResource("parallel-suites/suite-parallel-1.xml"),
67         getPathToResource("parallel-suites/suite-parallel-2.xml"),
68         getPathToResource("parallel-suites/suite-parallel-2-1.xml"),
69         getPathToResource("parallel-suites/suite-parallel-2-2.xml")));
70   }
71 
72   @Test
suitesShouldRunInParallel5()73   public void suitesShouldRunInParallel5() {
74     runTest(5, 5, 7, null, Arrays.asList(
75         getPathToResource("parallel-suites/suite-parallel-0.xml")));
76   }
77 
78   @Test(description = "Number of threads (2) is less than level of suites (3)")
suitesShouldRunInParallel6()79   public void suitesShouldRunInParallel6() {
80     runTest(2, 2, 7, null, Arrays.asList(
81           getPathToResource("parallel-suites/suite-parallel-0.xml")));
82   }
83 
84   @Test(description = "If suiteThreadPoolSize and randomizeSuites are not specified" +
85   		" suites should run in order specified in XML")
suitesShouldRunInOrder()86   public void suitesShouldRunInOrder() {
87     TestListenerAdapter tla = new TestListenerAdapter();
88     TestNG tng = create();
89     tng.setTestSuites(Arrays.asList(getPathToResource("suite-parallel-0.xml")));
90     tng.addListener(tla);
91     BaseThreadTest.initThreadLog();
92     tng.run();
93 
94     Map<String, Long> suitesMap = BaseThreadTest.getSuitesMap();
95     Assert.assertEquals(BaseThreadTest.getThreadCount(), 1);
96     Assert.assertEquals(suitesMap.keySet().size(), 3);
97 
98     final String SUITE_NAME_PREFIX = "Suite Parallel ";
99     if (suitesMap.get(SUITE_NAME_PREFIX + 1) > suitesMap.get(SUITE_NAME_PREFIX + 2)) {
100       Assert.fail("Suite " + (SUITE_NAME_PREFIX + 1) + " should have run before "
101           + (SUITE_NAME_PREFIX + 2));
102     }
103     Assert.assertTrue(suitesMap.get(SUITE_NAME_PREFIX + 2)
104           <= suitesMap.get(SUITE_NAME_PREFIX + 0));
105 
106   }
107 
108   @Test(description = "Number of threads (1) is less than number of levels of suites (2)")
suitesShouldRun1()109   public void suitesShouldRun1() {
110     runTest(1, 1, 3, true, Arrays.asList(
111           getPathToResource("suite-parallel-0.xml")));
112 
113 //    runTest(1, 1, 7, true, Arrays.asList(
114 //          getPathToResource("parallel-suites/suite-parallel-0.xml")));
115 //
116 //    runTest(2, 2, 7, true, Arrays.asList(
117 //          getPathToResource("parallel-suites/suite-parallel-0.xml")));
118   }
119 
120 }
121