jdk/test/java/util/WeakHashMap/GCDuringIteration.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7976 f273c0d04215
child 30046 cf2c86e1819e
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7976
diff changeset
     2
 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2799
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2799
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2799
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6499848
2799
0441b5d34aa0 6842023: Improve test reliability, Increase timeout factor on jtreg tests, etc.
ohair
parents: 2
diff changeset
    27
 * @ignore until 6842353 is resolved
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary Check that iterators work properly in the presence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *          concurrent finalization and removal of elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.concurrent.CountDownLatch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class GCDuringIteration {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    36
    private static void waitForFinalizersToRun() {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    37
        for (int i = 0; i < 2; i++)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    38
            tryWaitForFinalizersToRun();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    39
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    41
    private static void tryWaitForFinalizersToRun() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        System.gc();
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    43
        final CountDownLatch fin = new CountDownLatch(1);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    44
        new Object() { protected void finalize() { fin.countDown(); }};
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    45
        System.gc();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    46
        try { fin.await(); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        catch (InterruptedException ie) { throw new Error(ie); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // A class with the traditional pessimal hashCode implementation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // to ensure that all instances end up in the same bucket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static class Foo { public int hashCode() { return 42; }}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    <K,V> void put(Map<K,V> map, K k, V v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        check(! map.containsKey(k));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        equal(map.get(k), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        equal(map.put(k, v), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        equal(map.get(k), v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        check(map.containsKey(k));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        equal(map.put(k, v), v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        equal(map.get(k), v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        check(map.containsKey(k));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        check(! map.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        equal(map.keySet().iterator().next(), k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        equal(map.values().iterator().next(), v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    void checkIterator(final Iterator<Map.Entry<Foo, Integer>> it, int first) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        final Random rnd = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        for (int i = first; i >= 0; --i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            if (rnd.nextBoolean()) check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            equal(it.next().getValue(), i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (rnd.nextBoolean())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            THROWS(NoSuchElementException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                   new F(){void f(){it.next();}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (rnd.nextBoolean())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            check(! it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    <K,V> V firstValue(Map<K,V> map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        return map.values().iterator().next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    void test(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        final int n = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // Create array of strong refs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        final Foo[] foos = new Foo[2*n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        final Map<Foo,Integer> map = new WeakHashMap<Foo,Integer>(foos.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        check(map.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        equal(map.size(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        for (int i = 0; i < foos.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            Foo foo = new Foo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            foos[i] = foo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            put(map, foo, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        equal(map.size(), foos.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            int first = firstValue(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            final Iterator<Map.Entry<Foo,Integer>> it = map.entrySet().iterator();
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   103
            foos[first] = null;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   104
            for (int i = 0; i < 10 && map.size() != first; i++)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   105
                tryWaitForFinalizersToRun();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            equal(map.size(), first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            checkIterator(it, first-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            equal(map.size(), first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            equal(firstValue(map), first-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            int first = firstValue(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            final Iterator<Map.Entry<Foo,Integer>> it = map.entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            it.next();          // protects first entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            System.out.println(map.values());
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   117
            foos[first] = null;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   118
            tryWaitForFinalizersToRun()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            equal(map.size(), first+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            System.out.println(map.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            checkIterator(it, first-1);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   122
            // first entry no longer protected
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   123
            for (int i = 0; i < 10 && map.size() != first; i++)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   124
                tryWaitForFinalizersToRun();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            equal(map.size(), first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            equal(firstValue(map), first-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            int first = firstValue(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            final Iterator<Map.Entry<Foo,Integer>> it = map.entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            it.next();          // protects first entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            System.out.println(map.values());
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   134
            foos[first] = foos[first-1] = null;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   135
            tryWaitForFinalizersToRun();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            equal(map.size(), first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            equal(firstValue(map), first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            System.out.println(map.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            checkIterator(it, first-2);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   140
            // first entry no longer protected
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   141
            for (int i = 0; i < 10 && map.size() != first-1; i++)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   142
                tryWaitForFinalizersToRun();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            equal(map.size(), first-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            equal(firstValue(map), first-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            int first = firstValue(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            final Iterator<Map.Entry<Foo,Integer>> it = map.entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            it.next();          // protects first entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            it.hasNext();       // protects second entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            System.out.println(map.values());
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   153
            foos[first] = foos[first-1] = null;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   154
            tryWaitForFinalizersToRun();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            equal(firstValue(map), first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            equal(map.size(), first+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            System.out.println(map.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            checkIterator(it, first-1);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   159
            // first entry no longer protected
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   160
            for (int i = 0; i < 10 && map.size() != first-1; i++)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   161
                tryWaitForFinalizersToRun();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            equal(map.size(), first-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            equal(firstValue(map), first-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            int first = firstValue(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            final Iterator<Map.Entry<Foo,Integer>> it = map.entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            it.next();          // protects first entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            System.out.println(map.values());
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   171
            foos[first] = foos[first-1] = null;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   172
            tryWaitForFinalizersToRun();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            equal(firstValue(map), first-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            equal(map.size(), first-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            System.out.println(map.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            checkIterator(it, first-2);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   178
            // first entry no longer protected
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   179
            for (int i = 0; i < 10 && map.size() != first-1; i++)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   180
                tryWaitForFinalizersToRun();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            equal(map.size(), first-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            equal(firstValue(map), first-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            int first = firstValue(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            final Iterator<Map.Entry<Foo,Integer>> it = map.entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            it.next();          // protects first entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            it.hasNext();       // protects second entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            System.out.println(map.values());
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   192
            foos[first] = foos[first-1] = null;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   193
            tryWaitForFinalizersToRun();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            equal(firstValue(map), first-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            equal(map.size(), first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            System.out.println(map.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            checkIterator(it, first-1);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   198
            for (int i = 0; i < 10 && map.size() != first-1; i++)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   199
                tryWaitForFinalizersToRun();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            equal(map.size(), first-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            equal(firstValue(map), first-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            int first = firstValue(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            final Iterator<Map.Entry<Foo,Integer>> it = map.entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            it.hasNext();       // protects first entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            Arrays.fill(foos, null);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   209
            tryWaitForFinalizersToRun();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            equal(map.size(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            System.out.println(map.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            equal(it.next().getValue(), first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            check(! it.hasNext());
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   214
            for (int i = 0; i < 10 && map.size() != 0; i++)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   215
                tryWaitForFinalizersToRun();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            equal(map.size(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            check(map.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    void fail(String msg) {System.err.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        new GCDuringIteration().instanceMain(args);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    void instanceMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        try {test(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    abstract class F {abstract void f() throws Throwable;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    void THROWS(Class<? extends Throwable> k, F... fs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        for (F f : fs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            try {f.f(); fail("Expected " + k.getName() + " not thrown");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                if (k.isAssignableFrom(t.getClass())) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                else unexpected(t);}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
}