jdk/test/java/util/WeakHashMap/GCDuringIteration.java
changeset 34435 5beaa7987970
parent 32991 b27c76b82713
child 36230 ffcbf28059ee
equal deleted inserted replaced
34434:16dd60e78af4 34435:5beaa7987970
     1 /*
     1 /*
     2  * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 6499848
    26  * @bug 6499848
       
    27  * @library /lib/testlibrary/
       
    28  * @build jdk.testlibrary.RandomFactory
       
    29  * @run main GCDuringIteration
    27  * @summary Check that iterators work properly in the presence of
    30  * @summary Check that iterators work properly in the presence of
    28  *          concurrent finalization and removal of elements.
    31  *          concurrent finalization and removal of elements.
    29  * @key randomness
    32  * @key randomness intermittent
    30  */
    33  */
    31 
    34 
    32 import java.util.*;
    35 import java.util.*;
    33 import java.util.concurrent.CountDownLatch;
    36 import java.util.concurrent.CountDownLatch;
       
    37 import jdk.testlibrary.RandomFactory;
    34 
    38 
    35 public class GCDuringIteration {
    39 public class GCDuringIteration {
    36     private static void waitForFinalizersToRun() {
    40     private static void waitForFinalizersToRun() {
    37         for (int i = 0; i < 2; i++)
    41         for (int i = 0; i < 2; i++)
    38             tryWaitForFinalizersToRun();
    42             tryWaitForFinalizersToRun();
    63         check(! map.isEmpty());
    67         check(! map.isEmpty());
    64         equal(map.keySet().iterator().next(), k);
    68         equal(map.keySet().iterator().next(), k);
    65         equal(map.values().iterator().next(), v);
    69         equal(map.values().iterator().next(), v);
    66     }
    70     }
    67 
    71 
       
    72     static final Random rnd = RandomFactory.getRandom();
       
    73 
    68     void checkIterator(final Iterator<Map.Entry<Foo, Integer>> it, int first) {
    74     void checkIterator(final Iterator<Map.Entry<Foo, Integer>> it, int first) {
    69         final Random rnd = new Random();
       
    70         for (int i = first; i >= 0; --i) {
    75         for (int i = first; i >= 0; --i) {
    71             if (rnd.nextBoolean()) check(it.hasNext());
    76             if (rnd.nextBoolean()) check(it.hasNext());
    72             equal(it.next().getValue(), i);
    77             equal(it.next().getValue(), i);
    73         }
    78         }
    74         if (rnd.nextBoolean())
    79         if (rnd.nextBoolean())
    84 
    89 
    85     void test(String[] args) throws Throwable {
    90     void test(String[] args) throws Throwable {
    86         final int n = 10;
    91         final int n = 10;
    87         // Create array of strong refs
    92         // Create array of strong refs
    88         final Foo[] foos = new Foo[2*n];
    93         final Foo[] foos = new Foo[2*n];
    89         final Map<Foo,Integer> map = new WeakHashMap<Foo,Integer>(foos.length);
    94         final Map<Foo,Integer> map = new WeakHashMap<>(foos.length);
    90         check(map.isEmpty());
    95         check(map.isEmpty());
    91         equal(map.size(), 0);
    96         equal(map.size(), 0);
    92 
    97 
    93         for (int i = 0; i < foos.length; i++) {
    98         for (int i = 0; i < foos.length; i++) {
    94             Foo foo = new Foo();
    99             Foo foo = new Foo();