Skip to content

Commit

Permalink
Adding a test for KeyAwareAutoCloseableLockTest
Browse files Browse the repository at this point in the history
  • Loading branch information
krmahadevan committed Feb 27, 2024
1 parent 9d0ba52 commit cc23c7b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.testng.internal;

import java.io.Closeable;
import java.util.Objects;
import java.util.concurrent.locks.ReentrantLock;

/**
Expand All @@ -16,8 +17,25 @@ public AutoCloseableLock lock() {
return this;
}

public boolean isHeldByCurrentThread() {
return internalLock.isHeldByCurrentThread();
}

@Override
public void close() {
internalLock.unlock();
}

@Override
public boolean equals(Object object) {
if (this == object) return true;
if (object == null || getClass() != object.getClass()) return false;
AutoCloseableLock that = (AutoCloseableLock) object;
return Objects.equals(internalLock, that.internalLock);
}

@Override
public int hashCode() {
return Objects.hash(internalLock);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,27 @@ public static class AutoReleasable implements AutoCloseable {

AutoReleasable(AutoCloseableLock lock, Runnable cleanupAction) {
this.lock = Objects.requireNonNull(lock);
this.cleanupAction = Objects.requireNonNull(cleanupAction);
this.cleanupAction =
this.lock.isHeldByCurrentThread() ? () -> {} : Objects.requireNonNull(cleanupAction);
}

@Override
public void close() {
lock.close();
cleanupAction.run();
}

@Override
public boolean equals(Object object) {
if (this == object) return true;
if (object == null || getClass() != object.getClass()) return false;
AutoReleasable that = (AutoReleasable) object;
return Objects.equals(lock, that.lock);
}

@Override
public int hashCode() {
return Objects.hash(lock);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ && doesTaskHavePreRequisites()

for (IMethodInstance testMethodInstance : m_methodInstances) {
ITestNGMethod testMethod = testMethodInstance.getMethod();
Object key = testMethod.getInstance();
String key = Integer.toString(testMethod.getInstance().toString().hashCode());
if (canInvokeBeforeClassMethods()) {
try (KeyAwareAutoCloseableLock.AutoReleasable ignore = lock.lockForObject(key)) {
invokeBeforeClassMethods(testMethod.getTestClass(), testMethodInstance);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.testng.internal;

import static org.assertj.core.api.Assertions.assertThat;

import org.testng.annotations.Test;

public class KeyAwareAutoCloseableLockTest {

@Test
public void ensureLockIsReEntrant() {
String key = "TestNG";
KeyAwareAutoCloseableLock lock = new KeyAwareAutoCloseableLock();
KeyAwareAutoCloseableLock.AutoReleasable outer, inner1, inner2;
try (KeyAwareAutoCloseableLock.AutoReleasable ignore = lock.lockForObject(key)) {
outer = ignore;
try (KeyAwareAutoCloseableLock.AutoReleasable ignore1 = lock.lockForObject(key)) {
inner1 = ignore1;
}
try (KeyAwareAutoCloseableLock.AutoReleasable ignore2 = lock.lockForObject(key)) {
inner2 = ignore2;
}
}
assertThat(outer).isEqualTo(inner1);
assertThat(inner1).isEqualTo(inner2);
}
}
1 change: 1 addition & 0 deletions testng-core/src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<class name="org.testng.internal.TestListenerHelperTest"/>
<class name="org.testng.internal.GroupsHelperTest"/>
<class name="org.testng.internal.DynamicGraphHelperTest"/>
<class name="org.testng.internal.KeyAwareAutoCloseableLockTest"/>
<class name="test.ReflectionHelperTest"/>
<class name="test.privatemethod.PrivateMethodTest$PrivateMethodInnerTest"/>
<class name="test.multiple.TestMultiple"/>
Expand Down

0 comments on commit cc23c7b

Please sign in to comment.