test/jdk/java/lang/ThreadLocal/ReplaceStaleEntry.java
author mli
Thu, 17 Oct 2019 13:18:01 +0800
changeset 58660 7a81cc7ca25c
permissions -rw-r--r--
8209824: Improve the code coverage for ThreadLocal Reviewed-by: dholmes, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58660
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
     1
/*
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
     4
 *
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
     8
 *
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    13
 * accompanied this code).
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    14
 *
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    18
 *
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    21
 * questions.
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    22
 */
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    23
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    24
/*
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    25
 * @test
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    26
 * @bug 8209824
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    27
 * @summary per latest JDK code coverage report, 2 methods replaceStaleEntry
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    28
 *          and prevIndex in ThreadLocal.ThreadLocalMap are not touched
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    29
 *          by any JDK regression tests, this is to trigger the code paths.
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    30
 * @modules java.base/java.lang:+open
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    31
 * @run testng ReplaceStaleEntry
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    32
 */
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    33
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    34
import java.lang.reflect.Field;
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    35
import java.util.HashMap;
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    36
import java.util.Map;
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    37
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    38
import static org.testng.Assert.assertEquals;
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    39
import org.testng.annotations.BeforeClass;
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    40
import org.testng.annotations.Test;
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    41
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    42
public class ReplaceStaleEntry {
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    43
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    44
    public static int INITIAL_CAPACITY;
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    45
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    46
    @BeforeClass
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    47
    public static void setup() throws Exception {
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    48
        Class<?> clazz = Class.forName("java.lang.ThreadLocal$ThreadLocalMap");
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    49
        Field f = clazz.getDeclaredField("INITIAL_CAPACITY");
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    50
        f.setAccessible(true);
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    51
        INITIAL_CAPACITY = f.getInt(null);
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    52
        System.out.println("INITIAL_CAPACITY: " + INITIAL_CAPACITY);
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    53
    }
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    54
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    55
    /**
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    56
     * This test triggers the code path to replaceStaleEntry, so as prevIndex is
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    57
     * triggered too as it's called by replaceStaleEntry.
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    58
     * The code paths must be triggered by reusing an already used entry in the
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    59
     * map's entry table. The code below
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    60
     *  1. first occupies the entries
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    61
     *  2. then expunges the entries by removing strong references to thread locals
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    62
     *  3. then reuse some of entries by adding more thread locals to the thread
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    63
     * and, the above steps are run for several times by adding more and more
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    64
     * thread locals, also trigger rehash of the map.
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    65
     */
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    66
    @Test
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    67
    public static void test() {
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    68
        Map<Integer, ThreadLocal> locals = new HashMap<>();
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    69
        for (int j = 1; j < 32; j *= 2) {
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    70
            int loop = INITIAL_CAPACITY * j;
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    71
            for (int i = 0; i < loop; i++) {
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    72
                ThreadLocal l = new ThreadLocal<Integer>();
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    73
                l.set(i);
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    74
                locals.put(i, l);
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    75
            }
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    76
            locals.keySet().stream().forEach(i -> {
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    77
                assertEquals((int)locals.get(i).get(), (int)i, "Unexpected thread local value!");
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    78
            });
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    79
            locals.clear();
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    80
            System.gc();
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    81
        }
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    82
    }
7a81cc7ca25c 8209824: Improve the code coverage for ThreadLocal
mli
parents:
diff changeset
    83
}