author | pli |
Tue, 16 Jul 2019 00:57:00 +0000 | |
changeset 55689 | 8c5c9d86e1d6 |
parent 50764 | 5637aca18f1d |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 6355660 6347106 6394004 |
|
27 |
* @summary methods taking concurrently mutating collection should work |
|
28 |
* @author Martin Buchholz |
|
29 |
*/ |
|
30 |
||
48541
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
31 |
import java.lang.reflect.Constructor; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
32 |
import java.util.ArrayList; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
33 |
import java.util.Arrays; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
34 |
import java.util.Collection; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
35 |
import java.util.Collections; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
36 |
import java.util.List; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
37 |
import java.util.PriorityQueue; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
38 |
import java.util.Vector; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
39 |
import java.util.concurrent.CopyOnWriteArrayList; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
40 |
import java.util.concurrent.PriorityBlockingQueue; |
2 | 41 |
|
42 |
@SuppressWarnings("unchecked") |
|
43 |
public class HotPotatoes { |
|
44 |
private static void realMain(String[] args) throws Throwable { |
|
45 |
testImplementation(Vector.class); |
|
46 |
testImplementation(ArrayList.class); |
|
47 |
testImplementation(PriorityQueue.class); |
|
48 |
testImplementation(PriorityBlockingQueue.class); |
|
49 |
} |
|
50 |
||
51 |
private static void testImplementation(Class<? extends Collection> implClazz) |
|
52 |
throws Throwable |
|
53 |
{ |
|
54 |
testPotato(implClazz, Vector.class); |
|
55 |
testPotato(implClazz, CopyOnWriteArrayList.class); |
|
56 |
||
57 |
final Constructor<? extends Collection> constr |
|
58 |
= implClazz.getConstructor(Collection.class); |
|
59 |
final Collection<Object> coll |
|
60 |
= constr.newInstance(Arrays.asList(new String[] {})); |
|
61 |
coll.add(1); |
|
62 |
equal(coll.toString(), "[1]"); |
|
63 |
} |
|
64 |
||
65 |
private static void testPotato(Class<? extends Collection> implClazz, |
|
66 |
Class<? extends List> argClazz) |
|
67 |
throws Throwable |
|
68 |
{ |
|
69 |
try { |
|
70 |
System.out.printf("implClazz=%s, argClazz=%s\n", |
|
71 |
implClazz.getName(), argClazz.getName()); |
|
72 |
final int iterations = 100000; |
|
50764
5637aca18f1d
8203681: Miscellaneous changes imported from jsr166 CVS 2018-06
dl
parents:
48541
diff
changeset
|
73 |
final List<Integer> list = (List<Integer>) |
5637aca18f1d
8203681: Miscellaneous changes imported from jsr166 CVS 2018-06
dl
parents:
48541
diff
changeset
|
74 |
argClazz.getDeclaredConstructor().newInstance(); |
2 | 75 |
final Integer one = Integer.valueOf(1); |
76 |
final List<Integer> oneElementList = Collections.singletonList(one); |
|
77 |
final Constructor<? extends Collection> constr |
|
78 |
= implClazz.getConstructor(Collection.class); |
|
79 |
final Thread t = new CheckedThread() { public void realRun() { |
|
80 |
for (int i = 0; i < iterations; i++) { |
|
81 |
list.add(one); |
|
82 |
list.remove(one); |
|
83 |
}}}; |
|
84 |
t.setDaemon(true); |
|
85 |
t.start(); |
|
86 |
||
87 |
for (int i = 0; i < iterations; i++) { |
|
88 |
Collection<?> coll = constr.newInstance(list); |
|
89 |
Object[] elts = coll.toArray(); |
|
90 |
check(elts.length == 0 || |
|
91 |
(elts.length == 1 && elts[0] == one)); |
|
92 |
} |
|
93 |
} catch (Throwable t) { unexpected(t); } |
|
94 |
} |
|
95 |
||
96 |
//--------------------- Infrastructure --------------------------- |
|
97 |
static volatile int passed = 0, failed = 0; |
|
98 |
static void pass() {passed++;} |
|
99 |
static void fail() {failed++; Thread.dumpStack();} |
|
100 |
static void fail(String msg) {System.out.println(msg); fail();} |
|
101 |
static void unexpected(Throwable t) {failed++; t.printStackTrace();} |
|
102 |
static void check(boolean cond) {if (cond) pass(); else fail();} |
|
103 |
static void equal(Object x, Object y) { |
|
104 |
if (x == null ? y == null : x.equals(y)) pass(); |
|
105 |
else fail(x + " not equal to " + y);} |
|
106 |
public static void main(String[] args) throws Throwable { |
|
107 |
try {realMain(args);} catch (Throwable t) {unexpected(t);} |
|
108 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
|
109 |
if (failed > 0) throw new AssertionError("Some tests failed");} |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
5506
diff
changeset
|
110 |
private abstract static class CheckedThread extends Thread { |
2 | 111 |
public abstract void realRun() throws Throwable; |
112 |
public void run() { |
|
113 |
try { realRun(); } catch (Throwable t) { unexpected(t); }}} |
|
114 |
} |