1 /*
2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 import org.testng.annotations.Test;
25 
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.concurrent.CompletableFuture;
29 import java.util.concurrent.CompletionException;
30 import java.util.concurrent.ConcurrentHashMap;
31 import java.util.concurrent.ConcurrentMap;
32 import java.util.concurrent.CountDownLatch;
33 import java.util.concurrent.ThreadLocalRandom;
34 import java.util.function.BiConsumer;
35 import java.util.function.Supplier;
36 import java.util.stream.IntStream;
37 import java.util.stream.Stream;
38 
39 /**
40  * @test
41  * @bug 8028564
42  * @run testng ConcurrentAssociateTest
43  * @summary Test that association operations, such as put and compute,
44  * place entries in the map
45  */
46 @Test
47 public class ConcurrentAssociateTest {
48 
49     // The number of entries for each thread to place in a map
50     private static final int N = Integer.getInteger("n", 128);
51     // The number of iterations of the test
52     private static final int I = Integer.getInteger("i", 256);
53 
54     // Object to be placed in the concurrent map
55     static class X {
56         // Limit the hash code to trigger collisions
57         int hc = ThreadLocalRandom.current().nextInt(1, 9);
58 
hashCode()59         public int hashCode() { return hc; }
60     }
61 
62     @Test
testPut()63     public void testPut() {
64         test("CHM.put", (m, o) -> m.put(o, o));
65     }
66 
67     @Test
testCompute()68     public void testCompute() {
69         test("CHM.compute", (m, o) -> m.compute(o, (k, v) -> o));
70     }
71 
72     @Test
testComputeIfAbsent()73     public void testComputeIfAbsent() {
74         test("CHM.computeIfAbsent", (m, o) -> m.computeIfAbsent(o, (k) -> o));
75     }
76 
77     @Test
testMerge()78     public void testMerge() {
79         test("CHM.merge", (m, o) -> m.merge(o, o, (v1, v2) -> v1));
80     }
81 
82     @Test
testPutAll()83     public void testPutAll() {
84         test("CHM.putAll", (m, o) -> {
85             Map<Object, Object> hm = new HashMap<>();
86             hm.put(o, o);
87             m.putAll(hm);
88         });
89     }
90 
test(String desc, BiConsumer<ConcurrentMap<Object, Object>, Object> associator)91     private static void test(String desc, BiConsumer<ConcurrentMap<Object, Object>, Object> associator) {
92         for (int i = 0; i < I; i++) {
93             testOnce(desc, associator);
94         }
95     }
96 
97     static class AssociationFailure extends RuntimeException {
AssociationFailure(String message)98         AssociationFailure(String message) {
99             super(message);
100         }
101     }
102 
testOnce(String desc, BiConsumer<ConcurrentMap<Object, Object>, Object> associator)103     private static void testOnce(String desc, BiConsumer<ConcurrentMap<Object, Object>, Object> associator) {
104         ConcurrentHashMap<Object, Object> m = new ConcurrentHashMap<>();
105         CountDownLatch s = new CountDownLatch(1);
106 
107         Supplier<Runnable> sr = () -> () -> {
108             try {
109                 s.await();
110             }
111             catch (InterruptedException e) {
112             }
113 
114             for (int i = 0; i < N; i++) {
115                 Object o = new X();
116                 associator.accept(m, o);
117                 if (!m.containsKey(o)) {
118                     throw new AssociationFailure(desc + " failed: entry does not exist");
119                 }
120             }
121         };
122 
123         int ps = Runtime.getRuntime().availableProcessors();
124         Stream<CompletableFuture> runners = IntStream.range(0, ps)
125                 .mapToObj(i -> sr.get())
126                 .map(CompletableFuture::runAsync);
127 
128         CompletableFuture all = CompletableFuture.allOf(
129                 runners.toArray(CompletableFuture[]::new));
130 
131         // Trigger the runners to start associating
132         s.countDown();
133         try {
134             all.join();
135         } catch (CompletionException e) {
136             Throwable t = e.getCause();
137             if (t instanceof AssociationFailure) {
138                 throw (AssociationFailure) t;
139             }
140             else {
141                 throw e;
142             }
143         }
144     }
145 }
146