Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SUREFIRE-1967] Fix parallel test ordering to prevent high resource c…
…onsumption Before the change, TestNG run from Surefire can execute `@BeforeClass` on many different test classes/instances, without invoking `@AfterClass` yet, leading to high resource utilization. This is not observed when tests are invoked via a suite file. This is because `XmlClass.m_index` field is used by TestNG to order test execution and this field used not to be set by Surefire. This commit lets Surefire initialize `XmlClass` object in a similar manner to how TestNG suite file XML parser does.
- Loading branch information
Showing
10 changed files
with
295 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
.../org/apache/maven/surefire/its/jiras/Surefire1967CheckTestNgMethodParallelOrderingIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package org.apache.maven.surefire.its.jiras; | ||
|
||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase; | ||
import org.junit.Test; | ||
|
||
import static org.apache.maven.surefire.its.fixture.HelperAssertions.assumeJavaMaxVersion; | ||
|
||
/** | ||
* Test TestNG setup and teardown ordering with parallelism | ||
* | ||
* @author findepi | ||
*/ | ||
public class Surefire1967CheckTestNgMethodParallelOrderingIT | ||
extends SurefireJUnit4IntegrationTestCase | ||
{ | ||
@Test | ||
public void testNgParallelOrdering() | ||
{ | ||
unpack( "surefire-1967-testng-method-parallel-ordering" ) | ||
.sysProp( "testNgVersion", "7.3.0" ) | ||
.executeTest() | ||
.verifyErrorFree( 12 ); | ||
} | ||
|
||
// Since the test ordering guarantees currently depend on reflection, it's useful to test with | ||
// some older version too. | ||
@Test | ||
public void testNgParallelOrderingWithVersion6() | ||
{ | ||
unpack( "surefire-1967-testng-method-parallel-ordering" ) | ||
.sysProp( "testNgVersion", "6.10" ) | ||
.executeTest() | ||
.verifyErrorFree( 12 ); | ||
} | ||
|
||
// TestNG 6.2.1 is the newest version that doesn't have XmlClass.setIndex method yet. | ||
// Note that the problem of wrong setup methods ordering (SUREFIRE-1967) was not observed on that version. | ||
// This is likely because SUREFIRE-1967 is related to a change in TestNG 6.3, where preserve-order became true by | ||
// default (https://github.com/cbeust/testng/commit/8849b3406ef2184ceb6002768a2d087d7a8de8d5). | ||
@Test | ||
public void testNgParallelOrderingWithEarlyVersion6() | ||
{ | ||
unpack( "surefire-1967-testng-method-parallel-ordering" ) | ||
.sysProp( "testNgVersion", "6.2.1" ) | ||
.executeTest() | ||
.verifyErrorFree( 12 ); | ||
} | ||
|
||
// TestNG 5.13+ already has XmlClass.m_index field, but doesn't have XmlClass.setIndex method. | ||
// Note that the problem of wrong setup methods ordering (SUREFIRE-1967) was not observed on that version. | ||
// This is likely because SUREFIRE-1967 is related to a change in TestNG 6.3, where preserve-order became true by | ||
// default (https://github.com/cbeust/testng/commit/8849b3406ef2184ceb6002768a2d087d7a8de8d5). | ||
@Test | ||
public void testNgParallelOrderingWithVersion5() | ||
{ | ||
// TestNG 5.13 does not work with Java 17 | ||
assumeJavaMaxVersion( 16 ); | ||
|
||
unpack( "surefire-1967-testng-method-parallel-ordering" ) | ||
.sysProp( "testNgVersion", "5.13" ) | ||
.executeTest() | ||
.verifyErrorFree( 12 ); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
surefire-its/src/test/resources/surefire-1967-testng-method-parallel-ordering/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ Licensed to the Apache Software Foundation (ASF) under one | ||
~ or more contributor license agreements. See the NOTICE file | ||
~ distributed with this work for additional information | ||
~ regarding copyright ownership. The ASF licenses this file | ||
~ to you under the Apache License, Version 2.0 (the | ||
~ "License"); you may not use this file except in compliance | ||
~ with the License. You may obtain a copy of the License at | ||
~ | ||
~ http://www.apache.org/licenses/LICENSE-2.0 | ||
~ | ||
~ Unless required by applicable law or agreed to in writing, | ||
~ software distributed under the License is distributed on an | ||
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
~ KIND, either express or implied. See the License for the | ||
~ specific language governing permissions and limitations | ||
~ under the License. | ||
--> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.apache.maven.surefire</groupId> | ||
<artifactId>it-parent</artifactId> | ||
<version>1.0</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
|
||
<groupId>org.apache.maven.plugins.surefire</groupId> | ||
<artifactId>surefire-1967-testng-method-parallel-ordering</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<name>TestNG parallel ordering</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.testng</groupId> | ||
<artifactId>testng</artifactId> | ||
<version>${testNgVersion}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>${surefire.version}</version> | ||
<configuration> | ||
<threadCount>2</threadCount> | ||
<parallel>methods</parallel> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
58 changes: 58 additions & 0 deletions
58
...fire-1967-testng-method-parallel-ordering/src/test/java/testng/parallelOrdering/Base.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package testng.parallelOrdering; | ||
|
||
import org.testng.annotations.AfterClass; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public abstract class Base | ||
{ | ||
private static final AtomicInteger resources = new AtomicInteger(); | ||
|
||
// This simulates resource allocation | ||
@BeforeClass | ||
public void setupAllocateResources() | ||
{ | ||
int concurrentResources = resources.incrementAndGet(); | ||
if (concurrentResources > 2) { | ||
throw new IllegalStateException("Tests execute in two threads, so there should be at most 2 resources allocated, got: " + concurrentResources); | ||
} | ||
} | ||
|
||
// This simulates freeing resources | ||
@AfterClass(alwaysRun = true) | ||
public void tearDownReleaseResources() | ||
{ | ||
resources.decrementAndGet(); | ||
} | ||
|
||
@Test | ||
public void test1() | ||
throws Exception | ||
{ | ||
sleepShortly(); | ||
} | ||
|
||
@Test | ||
public void test2() | ||
throws Exception | ||
{ | ||
sleepShortly(); | ||
} | ||
|
||
@Test | ||
public void test3() | ||
throws Exception | ||
{ | ||
sleepShortly(); | ||
} | ||
|
||
// Sleep random time to let tests interleave. Keep sleep short not to extend tests duration too much. | ||
private void sleepShortly() | ||
throws InterruptedException | ||
{ | ||
Thread.sleep(ThreadLocalRandom.current().nextInt(3)); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...967-testng-method-parallel-ordering/src/test/java/testng/parallelOrdering/TestClass1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package testng.parallelOrdering; | ||
|
||
public class TestClass1 extends Base {} |
3 changes: 3 additions & 0 deletions
3
...967-testng-method-parallel-ordering/src/test/java/testng/parallelOrdering/TestClass2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package testng.parallelOrdering; | ||
|
||
public class TestClass2 extends Base {} |
3 changes: 3 additions & 0 deletions
3
...967-testng-method-parallel-ordering/src/test/java/testng/parallelOrdering/TestClass3.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package testng.parallelOrdering; | ||
|
||
public class TestClass3 extends Base {} |
3 changes: 3 additions & 0 deletions
3
...967-testng-method-parallel-ordering/src/test/java/testng/parallelOrdering/TestClass4.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package testng.parallelOrdering; | ||
|
||
public class TestClass4 extends Base {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters