Repository: groovy Updated Branches: refs/heads/GROOVY_2_6_X dcbe543a6 -> afd75dc1b
Add a test for accessing cache concurrently (cherry picked from commit 4d3d0ed) Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/afd75dc1 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/afd75dc1 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/afd75dc1 Branch: refs/heads/GROOVY_2_6_X Commit: afd75dc1bfc5a43d044e1cf23cef1e986bc748de Parents: dcbe543 Author: danielsun1106 <[email protected]> Authored: Sun Mar 4 12:46:13 2018 +0800 Committer: danielsun1106 <[email protected]> Committed: Sun Mar 4 12:48:04 2018 +0800 ---------------------------------------------------------------------- .../memoize/ConcurrentCommonCacheTest.java | 32 ++++++++++++++++++++ 1 file changed, 32 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/afd75dc1/src/test/org/codehaus/groovy/runtime/memoize/ConcurrentCommonCacheTest.java ---------------------------------------------------------------------- diff --git a/src/test/org/codehaus/groovy/runtime/memoize/ConcurrentCommonCacheTest.java b/src/test/org/codehaus/groovy/runtime/memoize/ConcurrentCommonCacheTest.java index 44c9ae2..a8d57f5 100644 --- a/src/test/org/codehaus/groovy/runtime/memoize/ConcurrentCommonCacheTest.java +++ b/src/test/org/codehaus/groovy/runtime/memoize/ConcurrentCommonCacheTest.java @@ -24,6 +24,8 @@ import org.junit.Test; import java.util.HashMap; import java.util.LinkedHashMap; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicInteger; public class ConcurrentCommonCacheTest { @Test @@ -200,4 +202,34 @@ public class ConcurrentCommonCacheTest { Assert.assertEquals("3", sc.get("c")); Assert.assertEquals("5", sc.get("d")); } + + @Test + public void testAccessCacheConcurrently() throws InterruptedException { + final ConcurrentCommonCache<Integer, Integer> m = new ConcurrentCommonCache<>(); + + final int threadNum = 30; + final CountDownLatch countDownLatch = new CountDownLatch(1); + final CountDownLatch countDownLatch2 = new CountDownLatch(threadNum); + + final AtomicInteger cnt = new AtomicInteger(0); + + for (int i = 0; i < threadNum; i++) { + new Thread(() -> { + try { + countDownLatch.await(); + + m.getAndPut(123, k -> cnt.getAndIncrement()); + } catch (InterruptedException e) { + e.printStackTrace(); + } finally { + countDownLatch2.countDown(); + } + }).start(); + } + + countDownLatch.countDown(); + countDownLatch2.await(); + + Assert.assertEquals(1, cnt.get()); + } }
