@Test
public class MultiThreadTest {
private String id;
@Test(dataProvider = "dp")
public void threadTest(String id) {
System.out.println("BEFORE SETTING (THREAD: " + getCurrentThreadNumber() + ")------------------> " + id);
this.id = id;
setSleep();
if (!id.equals(this.id)) {
Assert.fail("original: " + id + " actual: " + this.id);
}
}
private void setSleep() {
int thread = getCurrentThreadNumber();
if (thread % 2 == 0) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("AFTER SLEEP (THREAD: " + getCurrentThreadNumber() + ")------------------> " + id);
}
private int getCurrentThreadNumber() {
String s = Thread.currentThread().getName().substring(12) + " ";
int thread = Integer.parseInt(s.trim());
return thread;
}
@DataProvider(name = "dp", parallel = true)
public Object[][] iterativeSwagger(Method testMethod) {
Object[][] data = null;
int rowCount = 0;
List<String> rowList = new ArrayList<>();
String file = "./data.txt";
try (Stream<String> inputStream = Files.lines(Paths.get(file))) {
rowList = inputStream.collect(Collectors.toList());
rowCount = rowList.size();
data = new Object[rowCount][1];
String columnValues;
for (int row = 0; row < rowList.size(); row++) {
columnValues = rowList.get(row);
data[row][0] = columnValues;
}
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
}
75510277
75510300
NA
NA
75510211
75510277
75510218
75510277
75510277
75510214
75510255
75510266
75510277
NA
75510277
75510300
NA
NA
75510277
75510287
75510274
75510278
75510279
75510277
75510274
75510273
75510277
NA
Now when I run this test in multi threaded mode, a few tests fails because the class level variable "id" that I'm setting is different from the obtained one through data provider. The test perfectly works fine when I run using "parallel = false" . .. Also, If I run the same test without the class level variable "id" and make it a method level variable it works perfectly fine.
How do I achieve this? Please help.
FYI, in your code, the id
variable is not class level (static), it’s a regular instance field (not the answer to your question but a correction to your description of the problem).
Could you reformat your code to make it more readable?
--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+unsubscribe@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
--Cédric
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
public class Test {private Map<Long, String> idMap = new HashMap<>();...}
@Test(dataProvider = "dp")public void threadTest(String id) {
long tId = Thread.currentThread().getId();
idMap.put(tId, id);
setSleep();
if (!id.equals(idMap.get(tId)) {
In your example, there is no benefit to storing id at the class instance level so I can only presume your actual use case is more complicated....}}
--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+unsubscribe@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.