@@ -433,8 +433,10 @@ private void initMethods() {
433
433
//
434
434
// Calculate groups methods
435
435
//
436
- Map <String , List <ITestNGMethod >> beforeGroupMethods = MethodGroupsHelper .findGroupsMethods (m_classMap .values (), true );
437
- Map <String , List <ITestNGMethod >> afterGroupMethods = MethodGroupsHelper .findGroupsMethods (m_classMap .values (), false );
436
+ Map <String , List <ITestNGMethod >> beforeGroupMethods =
437
+ MethodGroupsHelper .findGroupsMethods (m_classMap .values (), true );
438
+ Map <String , List <ITestNGMethod >> afterGroupMethods =
439
+ MethodGroupsHelper .findGroupsMethods (m_classMap .values (), false );
438
440
439
441
//
440
442
// Walk through all the TestClasses, store their method
@@ -717,7 +719,8 @@ private void privateRun(XmlTest xmlTest) {
717
719
|| XmlSuite .PARALLEL_CLASSES .equals (parallelMode )
718
720
|| XmlSuite .PARALLEL_INSTANCES .equals (parallelMode );
719
721
720
- if (!parallel ) {
722
+ // @@@
723
+ if (false ) { // (!parallel) {
721
724
// sequential
722
725
computeTestLists (sequentialList , parallelList , sequentialMapList );
723
726
@@ -767,27 +770,40 @@ private void privateRun(XmlTest xmlTest) {
767
770
}
768
771
else {
769
772
// parallel
770
- int threadCount = xmlTest .getThreadCount ();
773
+ int threadCount = parallel ? xmlTest .getThreadCount () : 1 ;
771
774
// Make sure we create a graph based on the intercepted methods, otherwise an interceptor
772
775
// removing methods would cause the graph never to terminate (because it would expect
773
776
// termination from methods that never get invoked).
774
777
DynamicGraph <ITestNGMethod > graph = createDynamicGraph (intercept (m_allTestMethods ));
775
- if (graph .getNodeCount () > 0 ) {
776
- GraphThreadPoolExecutor <ITestNGMethod > executor =
777
- new GraphThreadPoolExecutor <ITestNGMethod >(graph , this ,
778
- threadCount , threadCount , 0 , TimeUnit .MILLISECONDS ,
779
- new LinkedBlockingQueue <Runnable >());
780
- executor .run ();
781
- try {
782
- long timeOut = m_xmlTest .getTimeOut (XmlTest .DEFAULT_TIMEOUT_MS );
783
- Utils .log ("TestRunner" , 2 , "Starting executor for test " + m_xmlTest .getName ()
784
- + " with time out:" + timeOut + " milliseconds." );
785
- executor .awaitTermination (timeOut , TimeUnit .MILLISECONDS );
786
- executor .shutdownNow ();
787
- } catch (InterruptedException e ) {
788
- e .printStackTrace ();
778
+ // if (parallel) {
779
+ if (graph .getNodeCount () > 0 ) {
780
+ GraphThreadPoolExecutor <ITestNGMethod > executor =
781
+ new GraphThreadPoolExecutor <ITestNGMethod >(graph , this ,
782
+ threadCount , threadCount , 0 , TimeUnit .MILLISECONDS ,
783
+ new LinkedBlockingQueue <Runnable >());
784
+ executor .run ();
785
+ // if (parallel) {
786
+ try {
787
+ long timeOut = m_xmlTest .getTimeOut (XmlTest .DEFAULT_TIMEOUT_MS );
788
+ Utils .log ("TestRunner" , 2 , "Starting executor for test " + m_xmlTest .getName ()
789
+ + " with time out:" + timeOut + " milliseconds." );
790
+ executor .awaitTermination (timeOut , TimeUnit .MILLISECONDS );
791
+ executor .shutdownNow ();
792
+ } catch (InterruptedException e ) {
793
+ e .printStackTrace ();
794
+ }
795
+ // }
789
796
}
790
- }
797
+ // } else {
798
+ // List<ITestNGMethod> freeNodes = graph.getFreeNodes();
799
+ // while (! freeNodes.isEmpty()) {
800
+ // List<IWorker<ITestNGMethod>> runnables = createWorkers(freeNodes);
801
+ // for (IWorker<ITestNGMethod> r : runnables) {
802
+ // r.run();
803
+ // }
804
+ // freeNodes = graph.getFreeNodes();
805
+ // }
806
+ // }
791
807
}
792
808
}
793
809
@@ -869,7 +885,7 @@ public int compare(XmlClass arg0, XmlClass arg1) {
869
885
* be put in the same worker in order to run in the same thread.
870
886
*/
871
887
@ Override
872
- public List <IWorker <ITestNGMethod >> createWorkers (Set <ITestNGMethod > methods ) {
888
+ public List <IWorker <ITestNGMethod >> createWorkers (List <ITestNGMethod > methods ) {
873
889
List <IWorker <ITestNGMethod >> result ;
874
890
if (XmlSuite .PARALLEL_INSTANCES .equals (m_xmlTest .getParallel ())) {
875
891
result = createInstanceBasedParallelWorkers (methods );
@@ -882,7 +898,7 @@ public List<IWorker<ITestNGMethod>> createWorkers(Set<ITestNGMethod> methods) {
882
898
/**
883
899
* Create workers for parallel="classes" and similar cases.
884
900
*/
885
- private List <IWorker <ITestNGMethod >> createClassBasedParallelWorkers (Set <ITestNGMethod > methods ) {
901
+ private List <IWorker <ITestNGMethod >> createClassBasedParallelWorkers (List <ITestNGMethod > methods ) {
886
902
List <IWorker <ITestNGMethod >> result = Lists .newArrayList ();
887
903
// Methods that belong to classes with a sequential=true or parallel=classes
888
904
// attribute must all be run in the same worker
@@ -949,7 +965,7 @@ private List<IWorker<ITestNGMethod>> createClassBasedParallelWorkers(Set<ITestNG
949
965
* Create workers for parallel="instances".
950
966
*/
951
967
private List <IWorker <ITestNGMethod >>
952
- createInstanceBasedParallelWorkers (Set <ITestNGMethod > methods ) {
968
+ createInstanceBasedParallelWorkers (List <ITestNGMethod > methods ) {
953
969
List <IWorker <ITestNGMethod >> result = Lists .newArrayList ();
954
970
ListMultiMap <Object , ITestNGMethod > lmm = Maps .newListMultiMap ();
955
971
for (ITestNGMethod m : methods ) {
@@ -1273,48 +1289,64 @@ private boolean containsString(Map<String, String> regexps, String group) {
1273
1289
1274
1290
private DynamicGraph <ITestNGMethod > createDynamicGraph (ITestNGMethod [] methods ) {
1275
1291
DynamicGraph <ITestNGMethod > result = new DynamicGraph <ITestNGMethod >();
1292
+ result .setComparator (new Comparator <ITestNGMethod >() {
1293
+ @ Override
1294
+ public int compare (ITestNGMethod o1 , ITestNGMethod o2 ) {
1295
+ return o1 .getPriority () - o2 .getPriority ();
1296
+ }
1297
+ });
1298
+
1299
+ // If preserve-order was specified and the class order is A, B
1300
+ // create a new set of dependencies where each method of B depends
1301
+ // on all the methods of A
1302
+ ListMultiMap <ITestNGMethod , ITestNGMethod > classDependencies = null ;
1303
+ if ("true" .equalsIgnoreCase (getCurrentXmlTest ().getPreserveOrder ())) {
1304
+ classDependencies = createClassDependencies (methods , getCurrentXmlTest ());
1305
+ }
1306
+
1276
1307
Map <String , ITestNGMethod > map = Maps .newHashMap ();
1277
1308
ListMultiMap <String , ITestNGMethod > groups = Maps .newListMultiMap ();
1278
1309
1279
1310
for (ITestNGMethod m : methods ) {
1280
- map .put (m .getTestClass ().getName () + "." + m .getMethodName (), m );
1311
+ map .put (/* m.getTestClass().getName() + "." + */ m .getMethodName (), m );
1281
1312
for (String g : m .getGroups ()) {
1282
1313
groups .put (g , m );
1283
1314
}
1284
1315
}
1285
1316
1286
1317
// A map of each priority and the list of methods that have this priority
1287
- ListMultiMap <Integer , ITestNGMethod > methodsByPriority = Maps .newListMultiMap ();
1288
- for (ITestNGMethod m : methods ) {
1289
- methodsByPriority .put (m .getPriority (), m );
1290
- }
1318
+ // ListMultiMap<Integer, ITestNGMethod> methodsByPriority = Maps.newListMultiMap();
1319
+ // for (ITestNGMethod m : methods) {
1320
+ // methodsByPriority.put(m.getPriority(), m);
1321
+ // }
1291
1322
1292
1323
// The priority map will contain at least one entry for all the methods that have
1293
1324
// a priority of zero (the default). If it has more than one entry, then we know that
1294
1325
// some test methods specified priorities, so we need to create dependencies
1295
1326
// that reflect the priority order.
1296
- boolean hasPriorities = methodsByPriority .getSize () > 1 ;
1327
+ // boolean hasPriorities = methodsByPriority.getSize() > 1;
1297
1328
1298
1329
for (ITestNGMethod m : methods ) {
1299
1330
result .addNode (m );
1300
1331
1301
1332
// Priority
1302
- if (hasPriorities ) {
1303
- for (Map .Entry <Integer , List <ITestNGMethod >> e : methodsByPriority .getEntrySet ()) {
1304
- if (e .getKey () < m .getPriority ()) {
1305
- for (ITestNGMethod dm : e .getValue ()) {
1306
- result .addEdge (m , dm );
1307
- }
1308
- }
1309
- }
1310
- }
1333
+ // if (hasPriorities) {
1334
+ // for (Map.Entry<Integer, List<ITestNGMethod>> e : methodsByPriority.getEntrySet()) {
1335
+ // if (e.getKey() < m.getPriority()) {
1336
+ // for (ITestNGMethod dm : e.getValue()) {
1337
+ // result.addEdge(m, dm);
1338
+ // }
1339
+ // }
1340
+ // }
1341
+ // }
1311
1342
1312
1343
// Dependent methods
1313
1344
{
1314
1345
String [] dependentMethods = m .getMethodsDependedUpon ();
1315
1346
if (dependentMethods != null ) {
1316
1347
for (String d : dependentMethods ) {
1317
- ITestNGMethod dm = map .get (d );
1348
+ String shortMethodName = d .substring (d .lastIndexOf ("." ) + 1 );
1349
+ ITestNGMethod dm = map .get (shortMethodName );
1318
1350
if (dm == null ) {
1319
1351
throw new TestNGException ("Method \" " + m
1320
1352
+ "\" depends on nonexistent method \" " + d + "\" " );
@@ -1339,6 +1371,67 @@ private DynamicGraph<ITestNGMethod> createDynamicGraph(ITestNGMethod[] methods)
1339
1371
}
1340
1372
}
1341
1373
1374
+ // Preserve order
1375
+ if (classDependencies != null ) {
1376
+ for (Map .Entry <ITestNGMethod , List <ITestNGMethod >> es : classDependencies .getEntrySet ()) {
1377
+ for (ITestNGMethod dm : es .getValue ()) {
1378
+ // System.out.println("@@@ Dep from:" + m + " to " + dm);
1379
+ result .addEdge (dm , es .getKey ());
1380
+ }
1381
+ }
1382
+ }
1383
+ }
1384
+
1385
+ List <ITestNGMethod > n = result .getFreeNodes ();
1386
+ return result ;
1387
+ }
1388
+
1389
+ private ListMultiMap <ITestNGMethod , ITestNGMethod > createClassDependencies (
1390
+ ITestNGMethod [] methods , XmlTest test )
1391
+ {
1392
+
1393
+ Map <String , List <ITestNGMethod >> classes = Maps .newHashMap ();
1394
+ List <XmlClass > sortedClasses = Lists .newArrayList ();
1395
+
1396
+ for (XmlClass c : test .getXmlClasses ()) {
1397
+ classes .put (c .getName (), new ArrayList <ITestNGMethod >());
1398
+ sortedClasses .add (c );
1399
+ }
1400
+
1401
+ // Sort the classes based on their order of appearance in the XML
1402
+ Collections .sort (sortedClasses , new Comparator <XmlClass >() {
1403
+ @ Override
1404
+ public int compare (XmlClass arg0 , XmlClass arg1 ) {
1405
+ return arg0 .getIndex () - arg1 .getIndex ();
1406
+ }
1407
+ });
1408
+
1409
+ Map <String , Integer > indexedClasses1 = Maps .newHashMap ();
1410
+ Map <Integer , String > indexedClasses2 = Maps .newHashMap ();
1411
+ int i = 0 ;
1412
+ for (XmlClass c : sortedClasses ) {
1413
+ indexedClasses1 .put (c .getName (), i );
1414
+ indexedClasses2 .put (i , c .getName ());
1415
+ i ++;
1416
+ }
1417
+
1418
+ ListMultiMap <String , ITestNGMethod > methodsFromClass = Maps .newListMultiMap ();
1419
+ for (ITestNGMethod m : methods ) {
1420
+ methodsFromClass .put (m .getTestClass ().getName (), m );
1421
+ }
1422
+
1423
+ ListMultiMap <ITestNGMethod , ITestNGMethod > result = Maps .newListMultiMap ();
1424
+ for (ITestNGMethod m : methods ) {
1425
+ int index = indexedClasses1 .get (m .getTestClass ().getName ());
1426
+ if (index > 0 ) {
1427
+ // Make this method depend on all the methods of the class in the previous
1428
+ // index
1429
+ String classDependedUpon = indexedClasses2 .get (index - 1 );
1430
+ List <ITestNGMethod > methodsDependedUpon = methodsFromClass .get (classDependedUpon );
1431
+ for (ITestNGMethod mdu : methodsDependedUpon ) {
1432
+ result .put (mdu , m );
1433
+ }
1434
+ }
1342
1435
}
1343
1436
1344
1437
return result ;
0 commit comments