test/jdk/java/lang/ThreadLocal/ReplaceStaleEntry.java
changeset 58660 7a81cc7ca25c
equal deleted inserted replaced
58659:4113f16d5109 58660:7a81cc7ca25c
       
     1 /*
       
     2  * Copyright (c) 2019, 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 /*
       
    25  * @test
       
    26  * @bug 8209824
       
    27  * @summary per latest JDK code coverage report, 2 methods replaceStaleEntry
       
    28  *          and prevIndex in ThreadLocal.ThreadLocalMap are not touched
       
    29  *          by any JDK regression tests, this is to trigger the code paths.
       
    30  * @modules java.base/java.lang:+open
       
    31  * @run testng ReplaceStaleEntry
       
    32  */
       
    33 
       
    34 import java.lang.reflect.Field;
       
    35 import java.util.HashMap;
       
    36 import java.util.Map;
       
    37 
       
    38 import static org.testng.Assert.assertEquals;
       
    39 import org.testng.annotations.BeforeClass;
       
    40 import org.testng.annotations.Test;
       
    41 
       
    42 public class ReplaceStaleEntry {
       
    43 
       
    44     public static int INITIAL_CAPACITY;
       
    45 
       
    46     @BeforeClass
       
    47     public static void setup() throws Exception {
       
    48         Class<?> clazz = Class.forName("java.lang.ThreadLocal$ThreadLocalMap");
       
    49         Field f = clazz.getDeclaredField("INITIAL_CAPACITY");
       
    50         f.setAccessible(true);
       
    51         INITIAL_CAPACITY = f.getInt(null);
       
    52         System.out.println("INITIAL_CAPACITY: " + INITIAL_CAPACITY);
       
    53     }
       
    54 
       
    55     /**
       
    56      * This test triggers the code path to replaceStaleEntry, so as prevIndex is
       
    57      * triggered too as it's called by replaceStaleEntry.
       
    58      * The code paths must be triggered by reusing an already used entry in the
       
    59      * map's entry table. The code below
       
    60      *  1. first occupies the entries
       
    61      *  2. then expunges the entries by removing strong references to thread locals
       
    62      *  3. then reuse some of entries by adding more thread locals to the thread
       
    63      * and, the above steps are run for several times by adding more and more
       
    64      * thread locals, also trigger rehash of the map.
       
    65      */
       
    66     @Test
       
    67     public static void test() {
       
    68         Map<Integer, ThreadLocal> locals = new HashMap<>();
       
    69         for (int j = 1; j < 32; j *= 2) {
       
    70             int loop = INITIAL_CAPACITY * j;
       
    71             for (int i = 0; i < loop; i++) {
       
    72                 ThreadLocal l = new ThreadLocal<Integer>();
       
    73                 l.set(i);
       
    74                 locals.put(i, l);
       
    75             }
       
    76             locals.keySet().stream().forEach(i -> {
       
    77                 assertEquals((int)locals.get(i).get(), (int)i, "Unexpected thread local value!");
       
    78             });
       
    79             locals.clear();
       
    80             System.gc();
       
    81         }
       
    82     }
       
    83 }