lhotari commented on a change in pull request #9764:
URL: https://github.com/apache/pulsar/pull/9764#discussion_r584613261
##########
File path:
pulsar-common/src/test/java/org/apache/pulsar/common/util/collections/ConcurrentOpenHashMapTest.java
##########
@@ -370,6 +370,47 @@ public boolean equals(Object obj) {
assertNull(map.get(t1_b));
}
+ @Test
+ public void testDeadlockPreventionWithForEachInSnapshot() throws
InterruptedException {
+ // Given
+
+ // 2 maps
+ ConcurrentOpenHashMap<Integer, Integer> mapA = new
ConcurrentOpenHashMap<>(10000, 1);
+ ConcurrentOpenHashMap<Integer, Integer> mapB = new
ConcurrentOpenHashMap<>(10000, 1);
+
+ // and some values in both maps
+ for (int i = 0; i < 10000; i++) {
+ mapA.put(i, i);
+ mapB.put(i, i);
+ }
+
+ // WHEN
+
+ // start a background thread
+ Thread thread = new Thread(() -> {
+ // add iterations to increase chances of concurrent access on fast
machines
+ for (int i=0; i < 10; i++) {
+ // iterate entries of mapA and modify mapB
+ mapA.forEachInSnapshot((k, v) -> {
+ mapB.put(k, v);
+ });
+ }
+ });
+ thread.start();
+
+ // add iterations to increase chances of concurrent access on fast
machines
+ for (int i=0; i < 10; i++) {
+ // iterate entries of mapB and modify mapA
+ mapB.forEachInSnapshot((k, v) -> {
+ mapA.put(k, v);
+ });
+ }
+
+ // THEN
+ // background thread should complete without a deadlock
+ thread.join();
Review comment:
It's not really useful, since in the dead lock, the main thread will
never reach this line of code.
I guess it's simpler in the end to rely on TestNG's timeout feature?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]