Skip to content

Commit

Permalink
Avoid using hashCode
Browse files Browse the repository at this point in the history
When this test is run with the below JVM flags
It fails because the hashCode is set to be constant

-Duser.timezone="America/New_York" 
-Duser.country=FR 
-Duser.language=fr 
-XX:+UnlockExperimentalVMOptions 
-XX:hashCode=2 
-Duser.country=FR 
-Duser.language=fr 
-XX:+UnlockDiagnosticVMOptions -XX:+StressGCM -XX:+StressLCM -XX:+StressIGVN -XX:+StressCCP
  • Loading branch information
krmahadevan committed Mar 6, 2024
1 parent eb80671 commit 832cff4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class RetryAnalyzerTest extends SimpleBaseTest {
@Test(description = "GITHUB-2798")
public void ensureNoDuplicateRetryAnalyzerInstancesAreCreated() {
create(TestClassSample.class).run();
Map<Integer, Long> collected =
Map<String, Long> collected =
HashCodeAwareRetryAnalyzer.hashCodes.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
assertThat(collected.keySet())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
import org.testng.internal.AutoCloseableLock;

public class HashCodeAwareRetryAnalyzer implements IRetryAnalyzer {

public static final List<Integer> hashCodes = new ArrayList<>();
public static final List<String> hashCodes = new ArrayList<>();

private static final AutoCloseableLock lock = new AutoCloseableLock();

private final UUID id = UUID.randomUUID();

int cnt = 0;
static final int threshold = 2;

@Override
public boolean retry(ITestResult result) {
try (AutoCloseableLock ignore = lock.lock()) {
hashCodes.add(this.hashCode());
hashCodes.add(id.toString());
return cnt++ < threshold;
}
}
Expand Down

0 comments on commit 832cff4

Please sign in to comment.