Skip to content

Commit

Permalink
-Dtestng.thread.affinity=true do not work when running multiple insta…
Browse files Browse the repository at this point in the history
…nce of test in parallel

Closes testng-team#2321
  • Loading branch information
bj-9527 committed Sep 17, 2020
1 parent 08c8313 commit 24f743a
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/main/java/org/testng/DependencyMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.testng.collections.ListMultiMap;
import org.testng.collections.Lists;
import org.testng.collections.Maps;
import org.testng.xml.XmlSuite;

import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -82,13 +83,22 @@ private static boolean hasInstance(
ITestNGMethod baseClassMethod, ITestNGMethod derivedClassMethod) {
Object baseInstance = baseClassMethod.getInstance();
Object derivedInstance = derivedClassMethod.getInstance();
return derivedInstance != null || baseInstance != null;
boolean result = derivedInstance != null || baseInstance != null;
boolean params = null != baseClassMethod.getFactoryMethodParamsInfo() && null != derivedClassMethod.getFactoryMethodParamsInfo().getParameters();
if (result && params && System.getProperty("testng.thread.affinity", "false").equals("true")) {
return baseClassMethod.getFactoryMethodParamsInfo().getParameters()[0] == derivedClassMethod.getFactoryMethodParamsInfo().getParameters()[0];
}
return result;
}

private static boolean isSameInstance(
ITestNGMethod baseClassMethod, ITestNGMethod derivedClassMethod) {
Object baseInstance = baseClassMethod.getInstance();
Object derivedInstance = derivedClassMethod.getInstance();
boolean result = derivedInstance != null && baseInstance != null;
if (result && null != baseClassMethod.getFactoryMethodParamsInfo() && System.getProperty("testng.thread.affinity", "false").equals("true")) {
return baseInstance.getClass().isAssignableFrom(derivedInstance.getClass()) && baseClassMethod.getFactoryMethodParamsInfo().getParameters()[0] == derivedClassMethod.getFactoryMethodParamsInfo().getParameters()[0];
}
return derivedInstance != null
&& baseInstance != null
&& baseInstance.getClass().isAssignableFrom(derivedInstance.getClass());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/testng/internal/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class Version {

public static final String VERSION = "7.1.1-SNAPSHOT";
public static final String VERSION = "7.4.0-SNAPSHOT";

public static String getVersionString() {
return VERSION;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ private void mapNodeToWorker(List<IWorker<T>> runnables, List<T> freeNodes) {
long current = w.getThreadIdToRunOn();
runnable.setThreadIdToRunOn(current);
}
mapping.put(freeNode, runnable);
if (runnable.toString().contains(freeNode.toString())) {
mapping.put(freeNode, runnable);
}
}
}
}
Expand Down
50 changes: 50 additions & 0 deletions src/test/java/test/testng2321/TestMultipleInstance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package test.testng2321;

import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;

public class TestMultipleInstance {
private int part;

private long threadId;
@BeforeSuite
public void setProperty() {
System.setProperty("testng.thread.affinity", "true");
}

@Factory(dataProvider = "dp")
public TestMultipleInstance(int part) {
this.part = part;
}

@Test
public void independent() {
threadId = Thread.currentThread().getId();
System.err.println(getClass().getSimpleName() + " part - " + part + " independent() running on " + threadId);
}

@Test(dependsOnMethods = "independent")
public void dependent() {
long currentThreadId = Thread.currentThread().getId();
System.err.println(getClass().getSimpleName() + " part - " + part + " dependent() running on " + currentThreadId);
Assert.assertEquals(currentThreadId, threadId, "Thread Ids didn't match");
}


@DataProvider(name = "dp")
public static Object[][] getData() {
return new Object[][]{
{1},
{2}
};
}

@AfterSuite
public void removeProperty() {
System.clearProperty("testng.thread.affinity");
}
}
9 changes: 9 additions & 0 deletions src/test/resources/2321.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite verbose="2" name="test_parallel_mode" parallel="instances">
<test thread-count="5" verbose="2" name="test_data_provider_in_instance_parallel_mode" parallel="instances">
<classes>
<class name="test.testng2321.TestMultipleInstance"/>
</classes>
</test>
</suite>

0 comments on commit 24f743a

Please sign in to comment.