1 package test.thread; 2 3 import org.testng.Assert; 4 import org.testng.annotations.Test; 5 import org.testng.xml.XmlSuite; 6 7 import test.BaseTest; 8 9 import java.util.HashMap; 10 import java.util.Map; 11 12 public class SequentialTest extends BaseTest { 13 14 @Test verifySequential1()15 public void verifySequential1() { 16 verifySequential(1); 17 } 18 19 @Test verifySequential2()20 public void verifySequential2() { 21 verifySequential(2); 22 } 23 24 @Test verifySequential3()25 public void verifySequential3() { 26 verifySequential(3); 27 } 28 29 @Test verifySingleThreaded1()30 public void verifySingleThreaded1() { 31 verifySingleThreaded(1); 32 } 33 34 @Test verifySingleThreaded2()35 public void verifySingleThreaded2() { 36 verifySingleThreaded(2); 37 } 38 39 @Test verifySingleThreaded3()40 public void verifySingleThreaded3() { 41 verifySingleThreaded(3); 42 } 43 verifySequential(int threadCount)44 public void verifySequential(int threadCount) { 45 runTest(threadCount, 46 SequentialSampleTest.class.getName(), 47 SequentialSample2Test.class.getName(), 48 SequentialSample3Test.class.getName()); 49 } 50 verifySingleThreaded(int threadCount)51 public void verifySingleThreaded(int threadCount) { 52 runTest(threadCount, 53 SingleThreadedSampleTest.class.getName(), 54 SingleThreadedSample2Test.class.getName(), 55 SingleThreadedSample3Test.class.getName()); 56 } 57 runTest(int threadCount, String... classes)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 ppp(String s)95 private static void ppp(String s) { 96 if (false) { 97 System.out.println("[SequentialTest] " + s); 98 } 99 } 100 101 } 102