Skip to content

Commit 11ea2c0

Browse files
author
Vincent Potucek
committed
[tryout fix] concurrency bug in NamespacedHierarchicalStore#computeIfAbsent(Object, Object, Function) junit-team#5171 junit-team#5209
Signed-off-by: Vincent Potucek <[email protected]>
1 parent ecf18c0 commit 11ea2c0

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

platform-tests/src/test/java/org/junit/platform/engine/support/store/NamespacedHierarchicalStoreTests.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import java.util.concurrent.CountDownLatch;
3535
import java.util.concurrent.TimeUnit;
3636
import java.util.concurrent.atomic.AtomicBoolean;
37-
import java.util.concurrent.CountDownLatch;
3837
import java.util.concurrent.atomic.AtomicInteger;
3938
import java.util.function.Function;
4039

@@ -711,7 +710,9 @@ public int hashCode() {
711710
// Force all CollidingKey instances to have the same hash code
712711
return 42;
713712
}
714-
}@SuppressWarnings("deprecation")
713+
}
714+
715+
@SuppressWarnings("deprecation")
715716
@Test
716717
void getOrComputeIfAbsentDoesNotDeadlockWithCollidingKeys() throws Exception {
717718
var store = new NamespacedHierarchicalStore<String>(null);
@@ -768,18 +769,18 @@ void getOrComputeIfAbsentDoesNotDeadlockWithCollidingKeys2() throws Exception {
768769
var firstThreadTimedOut = new AtomicBoolean(false);
769770

770771
Thread first = new Thread(
771-
() -> localStore.getOrComputeIfAbsent(namespace, new CollidingKey("k1"), __ -> {
772-
firstComputationStarted.countDown();
773-
try {
774-
if (!secondComputationAllowedToFinish.await(200, TimeUnit.MILLISECONDS)) {
775-
firstThreadTimedOut.set(true);
776-
}
777-
}
778-
catch (InterruptedException e) {
779-
Thread.currentThread().interrupt();
772+
() -> localStore.getOrComputeIfAbsent(namespace, new CollidingKey("k1"), __ -> {
773+
firstComputationStarted.countDown();
774+
try {
775+
if (!secondComputationAllowedToFinish.await(200, TimeUnit.MILLISECONDS)) {
776+
firstThreadTimedOut.set(true);
780777
}
781-
return "value1";
782-
}));
778+
}
779+
catch (InterruptedException e) {
780+
Thread.currentThread().interrupt();
781+
}
782+
return "value1";
783+
}));
783784

784785
Thread second = new Thread(() -> {
785786
try {
@@ -801,7 +802,7 @@ void getOrComputeIfAbsentDoesNotDeadlockWithCollidingKeys2() throws Exception {
801802
second.join(1000);
802803

803804
assertThat(firstThreadTimedOut).as(
804-
"getOrComputeIfAbsent should not block subsequent computations on colliding keys").isFalse();
805+
"getOrComputeIfAbsent should not block subsequent computations on colliding keys").isFalse();
805806
}
806807
}
807808

@@ -845,7 +846,7 @@ void computeIfAbsentCanDeadlockWithCollidingKeys() throws Exception {
845846
second.join(1000);
846847

847848
assertThat(firstThreadTimedOut).as(
848-
"computeIfAbsent should not block subsequent computations on colliding keys").isFalse();
849+
"computeIfAbsent should not block subsequent computations on colliding keys").isFalse();
849850
}
850851
}
851852

0 commit comments

Comments
 (0)