jdk/test/java/util/concurrent/ConcurrentHashMap/ConcurrentContainsKeyTest.java
author psandoz
Thu, 05 Dec 2013 09:44:53 +0100
changeset 21981 48b31d370bc9
child 32991 b27c76b82713
permissions -rw-r--r--
8028564: Concurrent calls to CHM.put can fail to add the key/value to the map Reviewed-by: psandoz, chegar, alanb Contributed-by: Doug Lea <dl@cs.oswego.edu>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
     1
/*
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
     4
 *
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
     7
 * published by the Free Software Foundation.
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
     8
 *
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    13
 * accompanied this code).
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    14
 *
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    18
 *
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    21
 * questions.
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    22
 */
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    23
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    24
import org.testng.annotations.Test;
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    25
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    26
import java.util.concurrent.CompletableFuture;
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    27
import java.util.concurrent.CompletionException;
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    28
import java.util.concurrent.ConcurrentHashMap;
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    29
import java.util.concurrent.CountDownLatch;
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    30
import java.util.function.Supplier;
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    31
import java.util.stream.IntStream;
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    32
import java.util.stream.Stream;
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    33
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    34
/**
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    35
 * @test
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    36
 * @bug 8028564
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    37
 * @run testng ConcurrentContainsKeyTest
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    38
 * @summary Test that entries are always present in the map,
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    39
 * when entries are held within one bin that is a tree
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    40
 */
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    41
@Test
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    42
public class ConcurrentContainsKeyTest {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    43
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    44
    // The number of entries for each thread to place in a map
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    45
    // Should be > ConcurrentHashMap.TREEIFY_THRESHOLD but small
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    46
    // enough to allow for enough iteration overlap by multiple threads
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    47
    private static final int N = Integer.getInteger("n", 16);
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    48
    // The number of rounds each thread performs per entry
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    49
    private static final int R = Integer.getInteger("r", 32);
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    50
    // The number of iterations of the test
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    51
    private static final int I = Integer.getInteger("i", 256);
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    52
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    53
    // Object to be placed in the concurrent map
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    54
    static class X implements Comparable<X> {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    55
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    56
        private final int a;
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    57
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    58
        X(int a) {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    59
            this.a = a;
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    60
        }
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    61
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    62
        public int compareTo(X o) {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    63
            return this.a - o.a;
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    64
        }
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    65
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    66
        public int hashCode() {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    67
            // Return the same hash code to guarantee collisions
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    68
            return 0;
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    69
        }
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    70
    }
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    71
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    72
    @Test
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    73
    public void testContainsKey() {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    74
        X[] content = IntStream.range(0, N).mapToObj(i -> new X(i)).toArray(X[]::new);
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    75
        // Create map with an initial size >= ConcurrentHashMap.TREEIFY_THRESHOLD
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    76
        // ensuring tree'ification will occur for a small number of entries
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    77
        // with the same hash code
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    78
        ConcurrentHashMap<Object, Object> m = new ConcurrentHashMap<>(64);
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    79
        Stream.of(content).forEach(x -> m.put(x, x));
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    80
        test(content, m);
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    81
    }
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    82
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    83
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    84
    private static void test(X[] content, ConcurrentHashMap<Object, Object> m) {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    85
        for (int i = 0; i < I; i++) {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    86
            testOnce(content, m);
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    87
        }
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    88
    }
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    89
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    90
    static class AssociationFailure extends RuntimeException {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    91
        AssociationFailure(String message) {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    92
            super(message);
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    93
        }
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    94
    }
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    95
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    96
    private static void testOnce(Object[] content, ConcurrentHashMap<Object, Object> m) {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    97
        CountDownLatch s = new CountDownLatch(1);
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    98
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
    99
        Supplier<Runnable> sr = () -> () -> {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   100
            try {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   101
                s.await();
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   102
            }
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   103
            catch (InterruptedException e) {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   104
            }
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   105
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   106
            for (int i = 0; i < R * N; i++) {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   107
                Object o = content[i % content.length];
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   108
                if (!m.containsKey(o)) {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   109
                    throw new AssociationFailure("CHM.containsKey failed: entry does not exist");
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   110
                }
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   111
            }
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   112
        };
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   113
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   114
        int ps = Runtime.getRuntime().availableProcessors();
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   115
        Stream<CompletableFuture> runners = IntStream.range(0, ps)
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   116
                .mapToObj(i -> sr.get())
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   117
                .map(CompletableFuture::runAsync);
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   118
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   119
        CompletableFuture all = CompletableFuture.allOf(
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   120
                runners.toArray(CompletableFuture[]::new));
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   121
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   122
        // Trigger the runners to start checking key membership
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   123
        s.countDown();
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   124
        try {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   125
            all.join();
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   126
        }
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   127
        catch (CompletionException e) {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   128
            Throwable t = e.getCause();
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   129
            if (t instanceof AssociationFailure) {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   130
                throw (AssociationFailure) t;
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   131
            }
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   132
            else {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   133
                throw e;
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   134
            }
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   135
        }
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   136
    }
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents:
diff changeset
   137
}