1 /* 2 * Copyright (c) 2013, 2016, 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.lang.management.ManagementFactory; 27 import java.lang.management.ThreadInfo; 28 import java.lang.management.ThreadMXBean; 29 import java.util.HashMap; 30 import java.util.Map; 31 import java.util.concurrent.CompletableFuture; 32 import java.util.concurrent.ConcurrentHashMap; 33 import java.util.concurrent.ConcurrentMap; 34 import java.util.concurrent.CountDownLatch; 35 import java.util.concurrent.ThreadLocalRandom; 36 import java.util.concurrent.TimeUnit; 37 import java.util.concurrent.TimeoutException; 38 import java.util.function.BiConsumer; 39 import java.util.function.Supplier; 40 import java.util.stream.IntStream; 41 import java.util.stream.Stream; 42 43 /** 44 * @test 45 * @bug 8028564 46 * @run testng/timeout=1200 ConcurrentAssociateTest 47 * @summary Test that association operations, such as put and compute, 48 * place entries in the map 49 * @modules java.management 50 */ 51 @Test 52 public class ConcurrentAssociateTest { 53 54 /** Maximum time (in seconds) to wait for a test method to complete. */ 55 private static final int TIMEOUT = Integer.getInteger("timeout", 200); 56 57 /** The number of entries for each thread to place in a map. */ 58 private static final int N = Integer.getInteger("n", 128); 59 60 /** The number of iterations of the test. */ 61 private static final int I = Integer.getInteger("i", 64); 62 63 /** Objects to be placed in the concurrent map. */ 64 static class X { 65 // Limit the hash code to trigger collisions 66 final int hc = ThreadLocalRandom.current().nextInt(1, 9); 67 hashCode()68 public int hashCode() { return hc; } 69 } 70 71 @Test testPut()72 public void testPut() throws Throwable { 73 test("CHM.put", (m, o) -> m.put(o, o)); 74 } 75 76 @Test testCompute()77 public void testCompute() throws Throwable { 78 test("CHM.compute", (m, o) -> m.compute(o, (k, v) -> o)); 79 } 80 81 @Test testComputeIfAbsent()82 public void testComputeIfAbsent() throws Throwable { 83 test("CHM.computeIfAbsent", (m, o) -> m.computeIfAbsent(o, (k) -> o)); 84 } 85 86 @Test testMerge()87 public void testMerge() throws Throwable { 88 test("CHM.merge", (m, o) -> m.merge(o, o, (v1, v2) -> v1)); 89 } 90 91 @Test testPutAll()92 public void testPutAll() throws Throwable { 93 test("CHM.putAll", (m, o) -> { 94 Map<Object, Object> hm = new HashMap<>(); 95 hm.put(o, o); 96 m.putAll(hm); 97 }); 98 } 99 test(String desc, BiConsumer<ConcurrentMap<Object, Object>, Object> associator)100 private static void test(String desc, BiConsumer<ConcurrentMap<Object, Object>, Object> associator) throws Throwable { 101 for (int i = 0; i < I; i++) { 102 testOnce(desc, associator); 103 } 104 } 105 106 static class AssociationFailure extends RuntimeException { AssociationFailure(String message)107 AssociationFailure(String message) { 108 super(message); 109 } 110 } 111 testOnce(String desc, BiConsumer<ConcurrentMap<Object, Object>, Object> associator)112 private static void testOnce(String desc, BiConsumer<ConcurrentMap<Object, Object>, Object> associator) throws Throwable { 113 ConcurrentHashMap<Object, Object> m = new ConcurrentHashMap<>(); 114 CountDownLatch s = new CountDownLatch(1); 115 116 Supplier<Runnable> sr = () -> () -> { 117 try { 118 if (!s.await(TIMEOUT, TimeUnit.SECONDS)) { 119 dumpTestThreads(); 120 throw new AssertionError("timed out"); 121 } 122 } 123 catch (InterruptedException e) { 124 } 125 126 for (int i = 0; i < N; i++) { 127 Object o = new X(); 128 associator.accept(m, o); 129 if (!m.containsKey(o)) { 130 throw new AssociationFailure(desc + " failed: entry does not exist"); 131 } 132 } 133 }; 134 135 // Bound concurrency to avoid degenerate performance 136 int ps = Math.min(Runtime.getRuntime().availableProcessors(), 8); 137 Stream<CompletableFuture> runners = IntStream.range(0, ps) 138 .mapToObj(i -> sr.get()) 139 .map(CompletableFuture::runAsync); 140 141 CompletableFuture all = CompletableFuture.allOf( 142 runners.toArray(CompletableFuture[]::new)); 143 144 // Trigger the runners to start associating 145 s.countDown(); 146 147 try { 148 all.get(TIMEOUT, TimeUnit.SECONDS); 149 } catch (TimeoutException e) { 150 dumpTestThreads(); 151 throw e; 152 } catch (Throwable e) { 153 dumpTestThreads(); 154 Throwable cause = e.getCause(); 155 if (cause instanceof AssociationFailure) { 156 throw cause; 157 } 158 throw e; 159 } 160 } 161 162 /** 163 * A debugging tool to print stack traces of most threads, as jstack does. 164 * Uninteresting threads are filtered out. 165 */ dumpTestThreads()166 static void dumpTestThreads() { 167 ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); 168 System.err.println("------ stacktrace dump start ------"); 169 for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) { 170 final String name = info.getThreadName(); 171 String lockName; 172 if ("Signal Dispatcher".equals(name)) 173 continue; 174 if ("Reference Handler".equals(name) 175 && (lockName = info.getLockName()) != null 176 && lockName.startsWith("java.lang.ref.Reference$Lock")) 177 continue; 178 if ("Finalizer".equals(name) 179 && (lockName = info.getLockName()) != null 180 && lockName.startsWith("java.lang.ref.ReferenceQueue$Lock")) 181 continue; 182 System.err.print(info); 183 } 184 System.err.println("------ stacktrace dump end ------"); 185 } 186 } 187