Path: blob/master/test/jdk/java/lang/ThreadLocal/ReplaceStaleEntry.java
41149 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 820982426* @summary per latest JDK code coverage report, 2 methods replaceStaleEntry27* and prevIndex in ThreadLocal.ThreadLocalMap are not touched28* by any JDK regression tests, this is to trigger the code paths.29* @modules java.base/java.lang:+open30* @run testng ReplaceStaleEntry31*/3233import java.lang.reflect.Field;34import java.util.HashMap;35import java.util.Map;3637import static org.testng.Assert.assertEquals;38import org.testng.annotations.BeforeClass;39import org.testng.annotations.Test;4041public class ReplaceStaleEntry {4243public static int INITIAL_CAPACITY;4445@BeforeClass46public static void setup() throws Exception {47Class<?> clazz = Class.forName("java.lang.ThreadLocal$ThreadLocalMap");48Field f = clazz.getDeclaredField("INITIAL_CAPACITY");49f.setAccessible(true);50INITIAL_CAPACITY = f.getInt(null);51System.out.println("INITIAL_CAPACITY: " + INITIAL_CAPACITY);52}5354/**55* This test triggers the code path to replaceStaleEntry, so as prevIndex is56* triggered too as it's called by replaceStaleEntry.57* The code paths must be triggered by reusing an already used entry in the58* map's entry table. The code below59* 1. first occupies the entries60* 2. then expunges the entries by removing strong references to thread locals61* 3. then reuse some of entries by adding more thread locals to the thread62* and, the above steps are run for several times by adding more and more63* thread locals, also trigger rehash of the map.64*/65@Test66public static void test() {67Map<Integer, ThreadLocal> locals = new HashMap<>();68for (int j = 1; j < 32; j *= 2) {69int loop = INITIAL_CAPACITY * j;70for (int i = 0; i < loop; i++) {71ThreadLocal l = new ThreadLocal<Integer>();72l.set(i);73locals.put(i, l);74}75locals.keySet().stream().forEach(i -> {76assertEquals((int)locals.get(i).get(), (int)i, "Unexpected thread local value!");77});78locals.clear();79System.gc();80}81}82}838485