forked from testng-team/testng
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Streamline honoring of “singleThreaded” attribute
Closes testng-team#2361
- Loading branch information
1 parent
1c6cd89
commit 42f120a
Showing
8 changed files
with
126 additions
and
6 deletions.
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
8 changes: 8 additions & 0 deletions
8
src/test/java/test/thread/issue2361/AnotherChildClassExample.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,8 @@ | ||
package test.thread.issue2361; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
@Test(singleThreaded = true) | ||
public class AnotherChildClassExample extends BaseTestClassExample { | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
src/test/java/test/thread/issue2361/BaseTestClassExample.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,45 @@ | ||
package test.thread.issue2361; | ||
|
||
import static java.util.concurrent.TimeUnit.MILLISECONDS; | ||
import static org.testng.Assert.assertEquals; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
import org.testng.annotations.Test; | ||
|
||
@Test(singleThreaded = true) | ||
public class BaseTestClassExample { | ||
|
||
private final AtomicInteger currentTests = new AtomicInteger(); | ||
|
||
protected void test() { | ||
int currentTests = this.currentTests.incrementAndGet(); | ||
try { | ||
assertEquals(currentTests, 1); | ||
MILLISECONDS.sleep(10); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} finally { | ||
this.currentTests.decrementAndGet(); | ||
} | ||
} | ||
|
||
@Test | ||
public void test1() { | ||
test(); | ||
} | ||
|
||
@Test | ||
public void test2() { | ||
test(); | ||
} | ||
|
||
@Test | ||
public void test3() { | ||
test(); | ||
} | ||
|
||
@Test | ||
public void test4() { | ||
test(); | ||
} | ||
} |
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,8 @@ | ||
package test.thread.issue2361; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
@Test(singleThreaded = true) | ||
public class ChildClassExample extends BaseTestClassExample { | ||
|
||
} |
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,15 @@ | ||
package test.thread.issue2361; | ||
|
||
import org.testng.annotations.Factory; | ||
|
||
public class FactorySample { | ||
|
||
@Factory | ||
public static Object[] newInstances() { | ||
return new Object[] { | ||
new ChildClassExample(), | ||
new AnotherChildClassExample() | ||
}; | ||
} | ||
|
||
} |
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,32 @@ | ||
package test.thread.issue2361; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.testng.TestNG; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Test; | ||
import org.testng.xml.XmlSuite; | ||
import org.testng.xml.XmlSuite.ParallelMode; | ||
import org.testng.xml.XmlTest; | ||
import test.SimpleBaseTest; | ||
|
||
public class IssueTest extends SimpleBaseTest { | ||
|
||
@Test(dataProvider = "dp") | ||
public void ensureClassLevelSingleThreadedNatureGetsHonoured(Class<?> cls) { | ||
XmlSuite suite = createXmlSuite("Sample_Suite"); | ||
suite.setParallel(ParallelMode.METHODS); | ||
XmlTest xmlTest = createXmlTest(suite, "Sample_Test", cls); | ||
xmlTest.setParallel(ParallelMode.METHODS); | ||
TestNG testng = create(suite); | ||
testng.run(); | ||
Assertions.assertThat(testng.getStatus()).isEqualTo(0); | ||
} | ||
|
||
@DataProvider(name = "dp") | ||
public Object[][] getTestData() { | ||
return new Object[][] { | ||
{ChildClassExample.class}, | ||
{FactorySample.class} | ||
}; | ||
} | ||
} |
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