1package test.thread;
2
3import org.testng.Assert;
4import org.testng.annotations.Test;
5import org.testng.xml.XmlSuite;
6
7import test.BaseTest;
8
9import java.util.HashMap;
10import java.util.Map;
11
12public class SequentialTest extends BaseTest {
13
14  @Test
15  public void verifySequential1() {
16    verifySequential(1);
17  }
18
19  @Test
20  public void verifySequential2() {
21    verifySequential(2);
22  }
23
24  @Test
25  public void verifySequential3() {
26    verifySequential(3);
27  }
28
29  @Test
30  public void verifySingleThreaded1() {
31    verifySingleThreaded(1);
32  }
33
34  @Test
35  public void verifySingleThreaded2() {
36    verifySingleThreaded(2);
37  }
38
39  @Test
40  public void verifySingleThreaded3() {
41    verifySingleThreaded(3);
42  }
43
44  public void verifySequential(int threadCount) {
45    runTest(threadCount,
46        SequentialSampleTest.class.getName(),
47        SequentialSample2Test.class.getName(),
48        SequentialSample3Test.class.getName());
49  }
50
51  public void verifySingleThreaded(int threadCount) {
52    runTest(threadCount,
53        SingleThreadedSampleTest.class.getName(),
54        SingleThreadedSample2Test.class.getName(),
55        SingleThreadedSample3Test.class.getName());
56  }
57
58  private void runTest(int threadCount, String... classes) {
59    Helper.reset();
60
61    for (String c : classes) {
62      addClass(c);
63    }
64    setParallel(XmlSuite.ParallelMode.METHODS);
65    setThreadCount(threadCount);
66
67    run();
68
69    Map<Long, Long>[] maps = new Map[] {
70        Helper.getMap(classes[0]),
71        Helper.getMap(classes[1]),
72        Helper.getMap(classes[2]),
73    };
74
75    for(Map m : maps) {
76      Assert.assertEquals(m.size(), 1);
77    }
78
79    long[] ids = new long[] {
80            maps[0].keySet().iterator().next(),
81            maps[1].keySet().iterator().next(),
82            maps[2].keySet().iterator().next(),
83    };
84    Map<Long, Long> verifyMap = new HashMap<>();
85
86    for (long id : ids) {
87      verifyMap.put(id, id);
88    }
89
90    Assert.assertEquals(verifyMap.size(), threadCount);
91
92    ppp("COUNT:" + threadCount  + " THREAD ID'S:" + ids[0] + " " + ids[1] + " " + ids[2]);
93  }
94
95  private static void ppp(String s) {
96    if (false) {
97      System.out.println("[SequentialTest] " + s);
98    }
99  }
100
101}
102