Skip to content

Commit

Permalink
Abort for invalid combinations
Browse files Browse the repository at this point in the history
GraphThreadPoolExecutor will NOT work if user is
trying to use a shared thread pool for just their
data driven tests (or) for all tests (both regular and data driven)
.

So if these combinations are found, abort execution.
  • Loading branch information
krmahadevan committed Dec 22, 2023
1 parent d01a4f1 commit ad0fb1c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
9 changes: 9 additions & 0 deletions testng-core/src/main/java/org/testng/TestNG.java
Original file line number Diff line number Diff line change
Expand Up @@ -1209,9 +1209,11 @@ public List<ISuite> runSuitesLocally() {
// Create a map with XmlSuite as key and corresponding SuiteRunner as value
for (XmlSuite xmlSuite : m_suites) {
if (m_configuration.isShareThreadPoolForDataProviders()) {
abortIfUsingGraphThreadPoolExecutor("Shared thread-pool for data providers");
xmlSuite.setShareThreadPoolForDataProviders(true);
}
if (m_configuration.useGlobalThreadPool()) {
abortIfUsingGraphThreadPoolExecutor("Global thread-pool");
xmlSuite.shouldUseGlobalThreadPool(true);
}
createSuiteRunners(suiteRunnerMap, xmlSuite);
Expand Down Expand Up @@ -1262,6 +1264,13 @@ private static void error(String s) {
LOGGER.error(s);
}

private static void abortIfUsingGraphThreadPoolExecutor(String prefix) {
if (RuntimeBehavior.favourCustomThreadPoolExecutor()) {
throw new UnsupportedOperationException(
prefix + " is NOT COMPATIBLE with TestNG's custom thread pool executor");
}
}

/**
* @return the verbose level, checking in order: the verbose level on the suite, the verbose level
* on the TestNG object, or 1.
Expand Down
15 changes: 15 additions & 0 deletions testng-core/src/test/java/test/dataprovider/DataProviderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.collections.Lists;
import org.testng.internal.RuntimeBehavior;
import org.testng.internal.collections.Pair;
import org.testng.internal.reflect.MethodMatcherException;
import org.testng.xml.XmlClass;
Expand Down Expand Up @@ -609,6 +610,20 @@ public void ensureSequentialDataProviderWithRetryAnalyserWorks() {
runTest(false);
}

@Test(
description = "GITHUB-2980",
expectedExceptions = UnsupportedOperationException.class,
expectedExceptionsMessageRegExp =
"Shared thread-pool for data providers is NOT COMPATIBLE with TestNG's custom thread pool executor")
public void ensureErrorShownWhenUsedWithGraphThreadPoolExecutor() {
try {
System.setProperty(RuntimeBehavior.FAVOR_CUSTOM_THREAD_POOL_EXECUTOR, "true");
runDataProviderTest(true);
} finally {
System.setProperty(RuntimeBehavior.FAVOR_CUSTOM_THREAD_POOL_EXECUTOR, "false");
}
}

@Test(description = "GITHUB-2980", dataProvider = "dataProviderForIssue2980")
public void ensureWeCanShareThreadPoolForDataProviders(
boolean flag, Pair<List<String>, Integer> pair) {
Expand Down
15 changes: 15 additions & 0 deletions testng-core/src/test/java/test/thread/SharedThreadPoolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.testng.TestNG;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import org.testng.internal.RuntimeBehavior;
import org.testng.xml.XmlSuite;
import test.SimpleBaseTest;
import test.thread.issue2019.TestClassSample;
Expand Down Expand Up @@ -37,6 +38,20 @@ public void ensureCommonThreadPoolIsNotUsed() {
.hasSizeGreaterThanOrEqualTo(3);
}

@Test(
description = "GITHUB-2019",
expectedExceptions = UnsupportedOperationException.class,
expectedExceptionsMessageRegExp =
"Global thread-pool is NOT COMPATIBLE with TestNG's custom thread pool executor")
public void ensureErrorShownWhenUsedWithGraphThreadPoolExecutor() {
try {
System.setProperty(RuntimeBehavior.FAVOR_CUSTOM_THREAD_POOL_EXECUTOR, "true");
runSimpleTest(true);
} finally {
System.setProperty(RuntimeBehavior.FAVOR_CUSTOM_THREAD_POOL_EXECUTOR, "false");
}
}

private static List<Long> runSimpleTest(boolean useSharedGlobalThreadPool) {
TestNG testng = create(TestClassSample.class);
testng.shouldUseGlobalThreadPool(useSharedGlobalThreadPool);
Expand Down

0 comments on commit ad0fb1c

Please sign in to comment.