forked from testng-team/testng
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure thread safety for attribute access
Closes testng-team#2991
- Loading branch information
1 parent
0183dd8
commit a9c0174
Showing
4 changed files
with
58 additions
and
2 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
45 changes: 45 additions & 0 deletions
45
testng-core/src/test/java/test/attributes/issue2991/TestClassSample.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.attributes.issue2991; | ||
|
||
import java.util.ConcurrentModificationException; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import org.assertj.core.api.SoftAssertions; | ||
import org.testng.IAttributes; | ||
import org.testng.ITestResult; | ||
import org.testng.Reporter; | ||
import org.testng.annotations.Test; | ||
|
||
public class TestClassSample { | ||
private static final AtomicInteger counter = new AtomicInteger(1); | ||
|
||
@Test(invocationCount = 100, threadPoolSize = 20) | ||
public void sampleTest() { | ||
String key = "test-" + counter.getAndIncrement(); | ||
ITestResult itr = Reporter.getCurrentTestResult(); | ||
SoftAssertions softly = new SoftAssertions(); | ||
softly | ||
.assertThat(iterate(key, itr)) | ||
.withFailMessage("No exceptions at test result level") | ||
.isTrue(); | ||
softly | ||
.assertThat(iterate(key, itr.getTestContext())) | ||
.withFailMessage("No exceptions at <test> level") | ||
.isTrue(); | ||
softly | ||
.assertThat(iterate(key, itr.getTestContext().getSuite())) | ||
.withFailMessage("No exceptions at <suite> level") | ||
.isTrue(); | ||
softly.assertAll(); | ||
} | ||
|
||
private static boolean iterate(String key, IAttributes attributes) { | ||
try { | ||
for (String next : attributes.getAttributeNames()) { | ||
Reporter.log(attributes.getAttribute(next).toString()); | ||
} | ||
attributes.setAttribute(key, counter.get()); | ||
return true; | ||
} catch (ConcurrentModificationException e) { | ||
return false; | ||
} | ||
} | ||
} |
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