test/jdk/java/util/concurrent/tck/MapTest.java
author dl
Fri, 01 Nov 2019 09:04:04 -0700
changeset 58892 35bac2745d04
parent 58138 1e4270f875ee
permissions -rw-r--r--
8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once Reviewed-by: martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
     1
/*
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
     3
 *
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
     6
 * published by the Free Software Foundation.
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
     7
 *
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
     8
 * This code is distributed in the hope that it will be useful, but WITHOUT
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
     9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    10
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    11
 * version 2 for more details (a copy is included in the LICENSE file that
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    12
 * accompanied this code).
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    13
 *
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License version
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    15
 * 2 along with this work; if not, write to the Free Software Foundation,
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    16
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    17
 *
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    18
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    19
 * or visit www.oracle.com if you need additional information or have any
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    20
 * questions.
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    21
 */
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    22
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    23
/*
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    24
 * This file is available under and governed by the GNU General Public
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    25
 * License version 2 only, as published by the Free Software Foundation.
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    26
 * However, the following notice accompanied the original version of this
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    27
 * file:
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    28
 *
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    29
 * Written by Doug Lea and Martin Buchholz with assistance from
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    30
 * members of JCP JSR-166 Expert Group and released to the public
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    31
 * domain, as explained at
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    32
 * http://creativecommons.org/publicdomain/zero/1.0/
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    33
 */
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    34
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    35
import java.util.ArrayList;
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    36
import java.util.Iterator;
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    37
import java.util.List;
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    38
import java.util.Map;
58892
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
    39
import java.util.concurrent.CompletableFuture;
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    40
import java.util.concurrent.ThreadLocalRandom;
58892
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
    41
import java.util.concurrent.atomic.AtomicBoolean;
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
    42
import java.util.concurrent.atomic.AtomicLong;
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
    43
import java.util.function.BiFunction;
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
    44
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
    45
import junit.framework.Test;
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    46
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    47
/**
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    48
 * Contains tests applicable to all Map implementations.
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    49
 */
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    50
public class MapTest extends JSR166TestCase {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    51
    final MapImplementation impl;
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    52
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    53
    /** Tests are parameterized by a Map implementation. */
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    54
    MapTest(MapImplementation impl, String methodName) {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    55
        super(methodName);
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    56
        this.impl = impl;
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    57
    }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    58
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    59
    public static Test testSuite(MapImplementation impl) {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    60
        return newTestSuite(
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    61
            parameterizedTestSuite(MapTest.class,
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    62
                                   MapImplementation.class,
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    63
                                   impl));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    64
    }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    65
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    66
    public void testImplSanity() {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    67
        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    68
        {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    69
            Map m = impl.emptyMap();
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    70
            assertTrue(m.isEmpty());
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    71
            assertEquals(0, m.size());
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    72
            Object k = impl.makeKey(rnd.nextInt());
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    73
            Object v = impl.makeValue(rnd.nextInt());
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    74
            m.put(k, v);
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    75
            assertFalse(m.isEmpty());
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    76
            assertEquals(1, m.size());
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    77
            assertTrue(m.containsKey(k));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    78
            assertTrue(m.containsValue(v));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    79
        }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    80
        {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    81
            Map m = impl.emptyMap();
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    82
            Object v = impl.makeValue(rnd.nextInt());
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    83
            if (impl.permitsNullKeys()) {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    84
                m.put(null, v);
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    85
                assertTrue(m.containsKey(null));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    86
                assertTrue(m.containsValue(v));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    87
            } else {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    88
                assertThrows(NullPointerException.class, () -> m.put(null, v));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    89
            }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    90
        }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    91
        {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    92
            Map m = impl.emptyMap();
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    93
            Object k = impl.makeKey(rnd.nextInt());
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    94
            if (impl.permitsNullValues()) {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    95
                m.put(k, null);
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    96
                assertTrue(m.containsKey(k));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    97
                assertTrue(m.containsValue(null));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    98
            } else {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
    99
                assertThrows(NullPointerException.class, () -> m.put(k, null));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   100
            }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   101
        }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   102
        {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   103
            Map m = impl.emptyMap();
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   104
            Object k = impl.makeKey(rnd.nextInt());
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   105
            Object v1 = impl.makeValue(rnd.nextInt());
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   106
            Object v2 = impl.makeValue(rnd.nextInt());
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   107
            m.put(k, v1);
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   108
            if (impl.supportsSetValue()) {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   109
                ((Map.Entry)(m.entrySet().iterator().next())).setValue(v2);
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   110
                assertSame(v2, m.get(k));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   111
                assertTrue(m.containsKey(k));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   112
                assertTrue(m.containsValue(v2));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   113
                assertFalse(m.containsValue(v1));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   114
            } else {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   115
                assertThrows(UnsupportedOperationException.class,
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   116
                             () -> ((Map.Entry)(m.entrySet().iterator().next())).setValue(v2));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   117
            }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   118
        }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   119
    }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   120
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   121
    /**
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   122
     * Tests and extends the scenario reported in
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   123
     * https://bugs.openjdk.java.net/browse/JDK-8186171
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   124
     * HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   125
     * ant -Djsr166.tckTestClass=HashMapTest -Djsr166.methodFilter=testBug8186171 -Djsr166.runsPerTest=1000 tck
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   126
     */
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   127
    public void testBug8186171() {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   128
        if (!impl.supportsSetValue()) return;
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54682
diff changeset
   129
        if (!atLeastJava10()) return; // jdk9 is no longer maintained
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   130
        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   131
        final boolean permitsNullValues = impl.permitsNullValues();
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   132
        final Object v1 = (permitsNullValues && rnd.nextBoolean())
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   133
            ? null : impl.makeValue(1);
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   134
        final Object v2 = (permitsNullValues && rnd.nextBoolean() && v1 != null)
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   135
            ? null : impl.makeValue(2);
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   136
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47304
diff changeset
   137
        // If true, always lands in first bucket in hash tables.
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   138
        final boolean poorHash = rnd.nextBoolean();
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   139
        class Key implements Comparable<Key> {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   140
            final int i;
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   141
            Key(int i) { this.i = i; }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   142
            public int hashCode() { return poorHash ? 0 : super.hashCode(); }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   143
            public int compareTo(Key x) {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   144
                return Integer.compare(this.i, x.i);
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   145
            }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   146
        }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   147
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   148
        // Both HashMap and ConcurrentHashMap have:
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   149
        // TREEIFY_THRESHOLD = 8; UNTREEIFY_THRESHOLD = 6;
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   150
        final int size = rnd.nextInt(1, 25);
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   151
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   152
        List<Key> keys = new ArrayList<>();
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   153
        for (int i = size; i-->0; ) keys.add(new Key(i));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   154
        Key keyToFrob = keys.get(rnd.nextInt(keys.size()));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   155
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   156
        Map<Key, Object> m = impl.emptyMap();
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   157
        for (Key key : keys) m.put(key, v1);
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   158
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   159
        for (Iterator<Map.Entry<Key, Object>> it = m.entrySet().iterator();
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   160
             it.hasNext(); ) {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   161
            Map.Entry<Key, Object> entry = it.next();
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   162
            if (entry.getKey() == keyToFrob)
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   163
                entry.setValue(v2); // does this have the expected effect?
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   164
            else
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   165
                it.remove();
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   166
        }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   167
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   168
        assertFalse(m.containsValue(v1));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   169
        assertTrue(m.containsValue(v2));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   170
        assertTrue(m.containsKey(keyToFrob));
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   171
        assertEquals(1, m.size());
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   172
    }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   173
53710
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   174
    /**
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   175
     * "Missing" test found while investigating JDK-8210280.
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   176
     * ant -Djsr166.tckTestClass=HashMapTest -Djsr166.methodFilter=testBug8210280 -Djsr166.runsPerTest=1000000 tck
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   177
     */
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   178
    public void testBug8210280() {
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   179
        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   180
        final int size1 = rnd.nextInt(32);
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   181
        final int size2 = rnd.nextInt(128);
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   182
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   183
        final Map m1 = impl.emptyMap();
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   184
        for (int i = 0; i < size1; i++) {
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   185
            int elt = rnd.nextInt(1024 * i, 1024 * (i + 1));
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   186
            assertNull(m1.put(impl.makeKey(elt), impl.makeValue(elt)));
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   187
        }
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   188
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   189
        final Map m2 = impl.emptyMap();
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   190
        for (int i = 0; i < size2; i++) {
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   191
            int elt = rnd.nextInt(Integer.MIN_VALUE + 1024 * i,
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   192
                                  Integer.MIN_VALUE + 1024 * (i + 1));
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   193
            assertNull(m2.put(impl.makeKey(elt), impl.makeValue(-elt)));
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   194
        }
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   195
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   196
        final Map m1Copy = impl.emptyMap();
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   197
        m1Copy.putAll(m1);
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   198
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   199
        m1.putAll(m2);
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   200
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   201
        for (Object elt : m2.keySet())
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   202
            assertEquals(m2.get(elt), m1.get(elt));
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   203
        for (Object elt : m1Copy.keySet())
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   204
            assertSame(m1Copy.get(elt), m1.get(elt));
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   205
        assertEquals(size1 + size2, m1.size());
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   206
    }
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 48541
diff changeset
   207
54682
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   208
    /**
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   209
     * 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   210
     */
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   211
    public void testClone() {
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   212
        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   213
        final int size = rnd.nextInt(4);
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   214
        final Map map = impl.emptyMap();
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   215
        for (int i = 0; i < size; i++)
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   216
            map.put(impl.makeKey(i), impl.makeValue(i));
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   217
        final Map clone = cloneableClone(map);
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   218
        if (clone == null) return;      // not cloneable?
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   219
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   220
        assertEquals(size, map.size());
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   221
        assertEquals(size, clone.size());
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   222
        assertEquals(map.isEmpty(), clone.isEmpty());
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   223
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   224
        clone.put(impl.makeKey(-1), impl.makeValue(-1));
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   225
        assertEquals(size, map.size());
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   226
        assertEquals(size + 1, clone.size());
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   227
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   228
        clone.clear();
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   229
        assertEquals(size, map.size());
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   230
        assertEquals(0, clone.size());
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   231
        assertTrue(clone.isEmpty());
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   232
    }
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 53710
diff changeset
   233
58892
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   234
    /**
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   235
     * Concurrent access by compute methods behaves as expected
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   236
     */
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   237
    public void testConcurrentAccess() throws Throwable {
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   238
        final Map map = impl.emptyMap();
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   239
        final long testDurationMillis = expensiveTests ? 1000 : 2;
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   240
        final int nTasks = impl.isConcurrent()
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   241
            ? ThreadLocalRandom.current().nextInt(1, 10)
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   242
            : 1;
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   243
        final AtomicBoolean done = new AtomicBoolean(false);
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   244
        final boolean remappingFunctionCalledAtMostOnce
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   245
            = impl.remappingFunctionCalledAtMostOnce();
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   246
        final List<CompletableFuture> futures = new ArrayList<>();
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   247
        final AtomicLong expectedSum = new AtomicLong(0);
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   248
        final Action[] tasks = {
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   249
            // repeatedly increment values using compute()
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   250
            () -> {
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   251
                long[] invocations = new long[2];
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   252
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   253
                BiFunction<Object, Object, Object> incValue = (k, v) -> {
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   254
                    invocations[1]++;
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   255
                    int vi = (v == null) ? 1 : impl.valueToInt(v) + 1;
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   256
                    return impl.makeValue(vi);
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   257
                };
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   258
                while (!done.getAcquire()) {
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   259
                    invocations[0]++;
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   260
                    Object key = impl.makeKey(3 * rnd.nextInt(10));
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   261
                    map.compute(key, incValue);
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   262
                }
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   263
                if (remappingFunctionCalledAtMostOnce)
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   264
                    assertEquals(invocations[0], invocations[1]);
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   265
                expectedSum.getAndAdd(invocations[0]);
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   266
            },
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   267
            // repeatedly increment values using computeIfPresent()
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   268
            () -> {
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   269
                long[] invocations = new long[2];
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   270
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   271
                BiFunction<Object, Object, Object> incValue = (k, v) -> {
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   272
                    invocations[1]++;
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   273
                    int vi = impl.valueToInt(v) + 1;
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   274
                    return impl.makeValue(vi);
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   275
                };
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   276
                while (!done.getAcquire()) {
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   277
                    Object key = impl.makeKey(3 * rnd.nextInt(10));
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   278
                    if (map.computeIfPresent(key, incValue) != null)
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   279
                        invocations[0]++;
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   280
                }
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   281
                if (remappingFunctionCalledAtMostOnce)
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   282
                    assertEquals(invocations[0], invocations[1]);
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   283
                expectedSum.getAndAdd(invocations[0]);
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   284
            },
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   285
        };
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   286
        for (int i = nTasks; i--> 0; ) {
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   287
            Action task = chooseRandomly(tasks);
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   288
            futures.add(CompletableFuture.runAsync(checkedRunnable(task)));
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   289
        }
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   290
        Thread.sleep(testDurationMillis);
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   291
        done.setRelease(true);
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   292
        for (var future : futures)
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   293
            checkTimedGet(future, null);
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   294
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   295
        long sum = map.values().stream().mapToLong(x -> (int) x).sum();
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   296
        assertEquals(expectedSum.get(), sum);
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   297
    }
35bac2745d04 8231592: Clarify that ConcurrentHashMap compute methods mapping functions execute at most once
dl
parents: 58138
diff changeset
   298
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   299
//     public void testFailsIntentionallyForDebugging() {
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   300
//         fail(impl.klazz().getSimpleName());
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   301
//     }
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents:
diff changeset
   302
}