Skip to content

Commit

Permalink
Use special classes for global locks in all cases
Browse files Browse the repository at this point in the history
  • Loading branch information
marcphilipp committed Sep 14, 2024
1 parent a512f38 commit c87721f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.junit.platform.engine.support.hierarchical.SingleLock.GlobalReadLock;
import org.junit.platform.engine.support.hierarchical.SingleLock.GlobalReadWriteLock;

/**
* @since 1.3
*/
Expand All @@ -46,23 +49,23 @@ private static Comparator<String> globalKeyFirst() {
}

private final Map<String, ReadWriteLock> locksByKey = new ConcurrentHashMap<>();
private final GlobalReadLock globalReadLock;
private final GlobalReadWriteLock globalReadWriteLock;

public LockManager() {
globalReadLock = new GlobalReadLock(toLock(GLOBAL_READ));
globalReadWriteLock = new GlobalReadWriteLock(toLock(GLOBAL_READ_WRITE));
}

ResourceLock getLockForResources(Collection<ExclusiveResource> resources) {
return toResourceLock(getDistinctSortedLocks(resources));
return toResourceLock(toDistinctSortedLocks(resources));
}

ResourceLock getLockForResource(ExclusiveResource resource) {
Lock lock = toLock(resource);
if (GLOBAL_READ.equals(resource)) {
return new SingleLock.GlobalReadLock(lock);
}
else if (GLOBAL_READ_WRITE.equals(resource)) {
return new SingleLock.GlobalReadWriteLock(lock);
}
return new SingleLock(lock);
return toResourceLock(toLock(resource));
}

private List<Lock> getDistinctSortedLocks(Collection<ExclusiveResource> resources) {
private List<Lock> toDistinctSortedLocks(Collection<ExclusiveResource> resources) {
if (resources.isEmpty()) {
return emptyList();
}
Expand Down Expand Up @@ -92,10 +95,20 @@ private ResourceLock toResourceLock(List<Lock> locks) {
case 0:
return NopLock.INSTANCE;
case 1:
return new SingleLock(locks.get(0));
return toResourceLock(locks.get(0));
default:
return new CompositeLock(locks);
}
}

private ResourceLock toResourceLock(Lock lock) {
if (lock == toLock(GLOBAL_READ)) {
return globalReadLock;
}
if (lock == toLock(GLOBAL_READ_WRITE)) {
return globalReadWriteLock;
}
return new SingleLock(lock);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*/
class LockManagerTests {

private LockManager lockManager = new LockManager();
private final LockManager lockManager = new LockManager();

@Test
void returnsNopLockWithoutExclusiveResources() {
Expand All @@ -50,7 +50,7 @@ void returnsSingleLockForSingleExclusiveResource() {
var locks = getLocks(resources, SingleLock.class);

assertThat(locks).hasSize(1);
assertThat(locks.get(0)).isInstanceOf(ReadLock.class);
assertThat(locks.getFirst()).isInstanceOf(ReadLock.class);
}

@Test
Expand All @@ -75,7 +75,7 @@ void reusesSameLockForExclusiveResourceWithSameKey() {

assertThat(locks1).hasSize(1);
assertThat(locks2).hasSize(1);
assertThat(locks1.get(0)).isSameAs(locks2.get(0));
assertThat(locks1.getFirst()).isSameAs(locks2.getFirst());
}

@Test
Expand Down Expand Up @@ -111,8 +111,26 @@ void globalLockComesFirst(LockMode globalLockMode) {
assertThat(locks.get(3)).isEqualTo(getSingleLock("foo", READ_WRITE));
}

private Lock getSingleLock(String globalResourceLockKey, LockMode read) {
return getLocks(Set.of(new ExclusiveResource(globalResourceLockKey, read)), SingleLock.class).get(0);
@Test
void usesSpecialClassForGlobalReadLock() {
var lock = lockManager.getLockForResources(List.of(ExclusiveResource.GLOBAL_READ));

assertThat(lock) //
.isInstanceOf(SingleLock.GlobalReadLock.class) //
.isSameAs(lockManager.getLockForResource(ExclusiveResource.GLOBAL_READ));
}

@Test
void usesSpecialClassForGlobalReadWriteLock() {
var lock = lockManager.getLockForResources(List.of(ExclusiveResource.GLOBAL_READ_WRITE));

assertThat(lock) //
.isInstanceOf(SingleLock.GlobalReadWriteLock.class) //
.isSameAs(lockManager.getLockForResource(ExclusiveResource.GLOBAL_READ_WRITE));
}

private Lock getSingleLock(String key, LockMode lockMode) {
return getLocks(Set.of(new ExclusiveResource(key, lockMode)), SingleLock.class).getFirst();
}

private List<Lock> getLocks(Collection<ExclusiveResource> resources, Class<? extends ResourceLock> type) {
Expand Down

0 comments on commit c87721f

Please sign in to comment.