ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/WeakHashMap/GCDuringIteration.java
Revision: 1.1
Committed: Tue Sep 1 01:22:20 2009 UTC (14 years, 9 months ago) by jsr166
Branch: MAIN
Log Message:
import tests from openjdk

File Contents

# Content
1 /*
2 * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24 /*
25 * @test
26 * @bug 6499848
27 * @ignore until 6842353 is resolved
28 * @summary Check that iterators work properly in the presence of
29 * concurrent finalization and removal of elements.
30 */
31
32 import java.util.*;
33 import java.util.concurrent.CountDownLatch;
34
35 public class GCDuringIteration {
36 static void finalizeTillYouDrop() {
37 System.gc(); // Enqueue all finalizables
38
39 System.runFinalization(); // Drain finalizer queue
40
41 // There may be a straggler finalizable object still being
42 // finalized by the dedicated finalizer thread. Enqueue one
43 // more finalizable object, and wait for it to be finalized.
44 final CountDownLatch latch = new CountDownLatch(1);
45 new Object() { protected void finalize() { latch.countDown(); }};
46 System.gc();
47 try { latch.await(); }
48 catch (InterruptedException ie) { throw new Error(ie); }
49 }
50
51 // A class with the traditional pessimal hashCode implementation,
52 // to ensure that all instances end up in the same bucket.
53 static class Foo { public int hashCode() { return 42; }}
54
55 <K,V> void put(Map<K,V> map, K k, V v) {
56 check(! map.containsKey(k));
57 equal(map.get(k), null);
58 equal(map.put(k, v), null);
59 equal(map.get(k), v);
60 check(map.containsKey(k));
61 equal(map.put(k, v), v);
62 equal(map.get(k), v);
63 check(map.containsKey(k));
64 check(! map.isEmpty());
65 equal(map.keySet().iterator().next(), k);
66 equal(map.values().iterator().next(), v);
67 }
68
69 void checkIterator(final Iterator<Map.Entry<Foo, Integer>> it, int first) {
70 final Random rnd = new Random();
71 for (int i = first; i >= 0; --i) {
72 if (rnd.nextBoolean()) check(it.hasNext());
73 equal(it.next().getValue(), i);
74 }
75 if (rnd.nextBoolean())
76 THROWS(NoSuchElementException.class,
77 new F(){void f(){it.next();}});
78 if (rnd.nextBoolean())
79 check(! it.hasNext());
80 }
81
82 <K,V> V firstValue(Map<K,V> map) {
83 return map.values().iterator().next();
84 }
85
86 void test(String[] args) throws Throwable {
87 final int n = 10;
88 // Create array of strong refs
89 final Foo[] foos = new Foo[2*n];
90 final Map<Foo,Integer> map = new WeakHashMap<Foo,Integer>(foos.length);
91 check(map.isEmpty());
92 equal(map.size(), 0);
93
94 for (int i = 0; i < foos.length; i++) {
95 Foo foo = new Foo();
96 foos[i] = foo;
97 put(map, foo, i);
98 }
99 equal(map.size(), foos.length);
100
101 {
102 int first = firstValue(map);
103 final Iterator<Map.Entry<Foo,Integer>> it = map.entrySet().iterator();
104 foos[first] = null; finalizeTillYouDrop();
105 equal(map.size(), first);
106 checkIterator(it, first-1);
107 equal(map.size(), first);
108 equal(firstValue(map), first-1);
109 }
110
111 {
112 int first = firstValue(map);
113 final Iterator<Map.Entry<Foo,Integer>> it = map.entrySet().iterator();
114 it.next(); // protects first entry
115 System.out.println(map.values());
116 foos[first] = null; finalizeTillYouDrop();
117 equal(map.size(), first+1);
118 System.out.println(map.values());
119 checkIterator(it, first-1);
120 finalizeTillYouDrop(); // first entry no longer protected
121 equal(map.size(), first);
122 equal(firstValue(map), first-1);
123 }
124
125 {
126 int first = firstValue(map);
127 final Iterator<Map.Entry<Foo,Integer>> it = map.entrySet().iterator();
128 it.next(); // protects first entry
129 System.out.println(map.values());
130 foos[first] = foos[first-1] = null; finalizeTillYouDrop();
131 equal(map.size(), first);
132 equal(firstValue(map), first);
133 System.out.println(map.values());
134 checkIterator(it, first-2);
135 finalizeTillYouDrop(); // first entry no longer protected
136 equal(map.size(), first-1);
137 equal(firstValue(map), first-2);
138 }
139
140 {
141 int first = firstValue(map);
142 final Iterator<Map.Entry<Foo,Integer>> it = map.entrySet().iterator();
143 it.next(); // protects first entry
144 it.hasNext(); // protects second entry
145 System.out.println(map.values());
146 foos[first] = foos[first-1] = null; finalizeTillYouDrop();
147 equal(firstValue(map), first);
148 equal(map.size(), first+1);
149 System.out.println(map.values());
150 checkIterator(it, first-1);
151 finalizeTillYouDrop(); // first entry no longer protected
152 equal(map.size(), first-1);
153 equal(firstValue(map), first-2);
154 }
155
156 {
157 int first = firstValue(map);
158 final Iterator<Map.Entry<Foo,Integer>> it = map.entrySet().iterator();
159 it.next(); // protects first entry
160 System.out.println(map.values());
161 foos[first] = foos[first-1] = null; finalizeTillYouDrop();
162 it.remove();
163 equal(firstValue(map), first-2);
164 equal(map.size(), first-1);
165 System.out.println(map.values());
166 checkIterator(it, first-2);
167 finalizeTillYouDrop();
168 equal(map.size(), first-1);
169 equal(firstValue(map), first-2);
170 }
171
172 {
173 int first = firstValue(map);
174 final Iterator<Map.Entry<Foo,Integer>> it = map.entrySet().iterator();
175 it.next(); // protects first entry
176 it.remove();
177 it.hasNext(); // protects second entry
178 System.out.println(map.values());
179 foos[first] = foos[first-1] = null; finalizeTillYouDrop();
180 equal(firstValue(map), first-1);
181 equal(map.size(), first);
182 System.out.println(map.values());
183 checkIterator(it, first-1);
184 finalizeTillYouDrop();
185 equal(map.size(), first-1);
186 equal(firstValue(map), first-2);
187 }
188
189 {
190 int first = firstValue(map);
191 final Iterator<Map.Entry<Foo,Integer>> it = map.entrySet().iterator();
192 it.hasNext(); // protects first entry
193 Arrays.fill(foos, null);
194 finalizeTillYouDrop();
195 equal(map.size(), 1);
196 System.out.println(map.values());
197 equal(it.next().getValue(), first);
198 check(! it.hasNext());
199 finalizeTillYouDrop();
200 equal(map.size(), 0);
201 check(map.isEmpty());
202 }
203 }
204
205 //--------------------- Infrastructure ---------------------------
206 volatile int passed = 0, failed = 0;
207 void pass() {passed++;}
208 void fail() {failed++; Thread.dumpStack();}
209 void fail(String msg) {System.err.println(msg); fail();}
210 void unexpected(Throwable t) {failed++; t.printStackTrace();}
211 void check(boolean cond) {if (cond) pass(); else fail();}
212 void equal(Object x, Object y) {
213 if (x == null ? y == null : x.equals(y)) pass();
214 else fail(x + " not equal to " + y);}
215 public static void main(String[] args) throws Throwable {
216 new GCDuringIteration().instanceMain(args);}
217 void instanceMain(String[] args) throws Throwable {
218 try {test(args);} catch (Throwable t) {unexpected(t);}
219 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
220 if (failed > 0) throw new AssertionError("Some tests failed");}
221 abstract class F {abstract void f() throws Throwable;}
222 void THROWS(Class<? extends Throwable> k, F... fs) {
223 for (F f : fs)
224 try {f.f(); fail("Expected " + k.getName() + " not thrown");}
225 catch (Throwable t) {
226 if (k.isAssignableFrom(t.getClass())) pass();
227 else unexpected(t);}}
228 }