Repository: groovy
Updated Branches:
  refs/heads/master b71c37dbb -> ca615fa57


Add `computeIfAbsentConcurrently` test for `ConcurrentLinkedHashMap`


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/ca615fa5
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/ca615fa5
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/ca615fa5

Branch: refs/heads/master
Commit: ca615fa57202dc3f1c3a9884d8e8d33053209102
Parents: b71c37d
Author: sunlan <[email protected]>
Authored: Mon Jan 8 23:23:04 2018 +0800
Committer: sunlan <[email protected]>
Committed: Mon Jan 8 23:23:23 2018 +0800

----------------------------------------------------------------------
 .../ConcurrentLinkedHashMapTest.java            | 38 +++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/ca615fa5/src/test/java/org/apache/groovy/util/concurrentlinkedhashmap/ConcurrentLinkedHashMapTest.java
----------------------------------------------------------------------
diff --git 
a/src/test/java/org/apache/groovy/util/concurrentlinkedhashmap/ConcurrentLinkedHashMapTest.java
 
b/src/test/java/org/apache/groovy/util/concurrentlinkedhashmap/ConcurrentLinkedHashMapTest.java
index 465d071..8659a33 100644
--- 
a/src/test/java/org/apache/groovy/util/concurrentlinkedhashmap/ConcurrentLinkedHashMapTest.java
+++ 
b/src/test/java/org/apache/groovy/util/concurrentlinkedhashmap/ConcurrentLinkedHashMapTest.java
@@ -21,9 +21,10 @@ package org.apache.groovy.util.concurrentlinkedhashmap;
 
 import org.junit.Test;
 
+import java.util.concurrent.CountDownLatch;
+
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
 
 public class ConcurrentLinkedHashMapTest {
     @Test
@@ -46,4 +47,39 @@ public class ConcurrentLinkedHashMapTest {
         assertArrayEquals(new Integer[] {3, 4, 5}, m.values().toArray(new 
Integer[0]));
     }
 
+    @Test
+    public void computeIfAbsentConcurrently() throws InterruptedException {
+        final ConcurrentLinkedHashMap m = new 
ConcurrentLinkedHashMap.Builder<>()
+                .maximumWeightedCapacity(3)
+                .build();
+
+        final int threadNum = 20;
+        final CountDownLatch countDownLatch = new CountDownLatch(1);
+        final CountDownLatch countDownLatch2 = new CountDownLatch(threadNum);
+
+        for (int i = 0; i < threadNum; i++) {
+            final int num = i;
+            new Thread(() -> {
+                try {
+                    countDownLatch.await();
+
+                    if (num != 0 && num != 1 && num != 2) {
+                        Thread.sleep(500);
+                    }
+
+                    m.computeIfAbsent(num % 3, k -> num);
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
+                } finally {
+                    countDownLatch2.countDown();
+                }
+            }).start();
+        }
+
+        countDownLatch.countDown();
+        countDownLatch2.await();
+
+        assertArrayEquals(new Integer[] {0, 1, 2}, m.keySet().toArray(new 
Integer[0]));
+        assertArrayEquals(new Integer[] {0, 1, 2}, m.values().toArray(new 
Integer[0]));
+    }
 }
\ No newline at end of file

Reply via email to