1 |
jsr166 |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea and Martin Buchholz with assistance from |
3 |
|
|
* members of JCP JSR-166 Expert Group and released to the public |
4 |
|
|
* domain, as explained at |
5 |
|
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
9 |
|
|
|
10 |
|
|
import java.util.ArrayList; |
11 |
|
|
import java.util.Collection; |
12 |
|
|
import java.util.Collections; |
13 |
jsr166 |
1.2 |
import java.util.concurrent.CountDownLatch; |
14 |
jsr166 |
1.1 |
import java.util.concurrent.Executors; |
15 |
|
|
import java.util.concurrent.ExecutorService; |
16 |
|
|
import java.util.concurrent.Future; |
17 |
|
|
import java.util.concurrent.atomic.AtomicBoolean; |
18 |
|
|
import java.util.concurrent.atomic.AtomicLong; |
19 |
|
|
import java.util.function.Consumer; |
20 |
|
|
|
21 |
|
|
import junit.framework.Test; |
22 |
|
|
|
23 |
|
|
/** |
24 |
|
|
* Contains tests applicable to all jdk8+ Collection implementations. |
25 |
|
|
* An extension of CollectionTest. |
26 |
|
|
*/ |
27 |
|
|
public class Collection8Test extends JSR166TestCase { |
28 |
|
|
final CollectionImplementation impl; |
29 |
|
|
|
30 |
|
|
/** Tests are parameterized by a Collection implementation. */ |
31 |
|
|
Collection8Test(CollectionImplementation impl, String methodName) { |
32 |
|
|
super(methodName); |
33 |
|
|
this.impl = impl; |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
public static Test testSuite(CollectionImplementation impl) { |
37 |
|
|
return parameterizedTestSuite(Collection8Test.class, |
38 |
|
|
CollectionImplementation.class, |
39 |
|
|
impl); |
40 |
|
|
} |
41 |
|
|
|
42 |
|
|
/** |
43 |
|
|
* stream().forEach returns elements in the collection |
44 |
|
|
*/ |
45 |
jsr166 |
1.3 |
public void testStreamForEach() throws Throwable { |
46 |
jsr166 |
1.1 |
final Collection c = impl.emptyCollection(); |
47 |
|
|
final AtomicLong count = new AtomicLong(0L); |
48 |
|
|
final Object x = impl.makeElement(1); |
49 |
|
|
final Object y = impl.makeElement(2); |
50 |
|
|
final ArrayList found = new ArrayList(); |
51 |
|
|
Consumer<Object> spy = (o) -> { found.add(o); }; |
52 |
|
|
c.stream().forEach(spy); |
53 |
|
|
assertTrue(found.isEmpty()); |
54 |
|
|
|
55 |
|
|
assertTrue(c.add(x)); |
56 |
|
|
c.stream().forEach(spy); |
57 |
|
|
assertEquals(Collections.singletonList(x), found); |
58 |
|
|
found.clear(); |
59 |
|
|
|
60 |
|
|
assertTrue(c.add(y)); |
61 |
|
|
c.stream().forEach(spy); |
62 |
|
|
assertEquals(2, found.size()); |
63 |
|
|
assertTrue(found.contains(x)); |
64 |
|
|
assertTrue(found.contains(y)); |
65 |
|
|
found.clear(); |
66 |
|
|
|
67 |
|
|
c.clear(); |
68 |
|
|
c.stream().forEach(spy); |
69 |
|
|
assertTrue(found.isEmpty()); |
70 |
|
|
} |
71 |
|
|
|
72 |
jsr166 |
1.3 |
public void testStreamForEachConcurrentStressTest() throws Throwable { |
73 |
|
|
if (!impl.isConcurrent()) return; |
74 |
|
|
final Collection c = impl.emptyCollection(); |
75 |
|
|
final long testDurationMillis = timeoutMillis(); |
76 |
|
|
final AtomicBoolean done = new AtomicBoolean(false); |
77 |
|
|
final Object elt = impl.makeElement(1); |
78 |
|
|
final Future<?> f1, f2; |
79 |
|
|
final ExecutorService pool = Executors.newCachedThreadPool(); |
80 |
|
|
try (PoolCleaner cleaner = cleaner(pool, done)) { |
81 |
|
|
final CountDownLatch threadsStarted = new CountDownLatch(2); |
82 |
|
|
Runnable checkElt = () -> { |
83 |
|
|
threadsStarted.countDown(); |
84 |
|
|
while (!done.get()) |
85 |
|
|
c.stream().forEach((x) -> { assertSame(x, elt); }); }; |
86 |
|
|
Runnable addRemove = () -> { |
87 |
|
|
threadsStarted.countDown(); |
88 |
|
|
while (!done.get()) { |
89 |
|
|
assertTrue(c.add(elt)); |
90 |
|
|
assertTrue(c.remove(elt)); |
91 |
|
|
}}; |
92 |
|
|
f1 = pool.submit(checkElt); |
93 |
|
|
f2 = pool.submit(addRemove); |
94 |
|
|
Thread.sleep(testDurationMillis); |
95 |
|
|
} |
96 |
|
|
assertNull(f1.get(0L, MILLISECONDS)); |
97 |
|
|
assertNull(f2.get(0L, MILLISECONDS)); |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
/** |
101 |
|
|
* collection.forEach returns elements in the collection |
102 |
|
|
*/ |
103 |
|
|
public void testForEach() throws Throwable { |
104 |
|
|
final Collection c = impl.emptyCollection(); |
105 |
|
|
final AtomicLong count = new AtomicLong(0L); |
106 |
|
|
final Object x = impl.makeElement(1); |
107 |
|
|
final Object y = impl.makeElement(2); |
108 |
|
|
final ArrayList found = new ArrayList(); |
109 |
|
|
Consumer<Object> spy = (o) -> { found.add(o); }; |
110 |
|
|
c.forEach(spy); |
111 |
|
|
assertTrue(found.isEmpty()); |
112 |
|
|
|
113 |
|
|
assertTrue(c.add(x)); |
114 |
|
|
c.forEach(spy); |
115 |
|
|
assertEquals(Collections.singletonList(x), found); |
116 |
|
|
found.clear(); |
117 |
|
|
|
118 |
|
|
assertTrue(c.add(y)); |
119 |
|
|
c.forEach(spy); |
120 |
|
|
assertEquals(2, found.size()); |
121 |
|
|
assertTrue(found.contains(x)); |
122 |
|
|
assertTrue(found.contains(y)); |
123 |
|
|
found.clear(); |
124 |
|
|
|
125 |
|
|
c.clear(); |
126 |
|
|
c.forEach(spy); |
127 |
|
|
assertTrue(found.isEmpty()); |
128 |
|
|
} |
129 |
|
|
|
130 |
jsr166 |
1.1 |
public void testForEachConcurrentStressTest() throws Throwable { |
131 |
|
|
if (!impl.isConcurrent()) return; |
132 |
|
|
final Collection c = impl.emptyCollection(); |
133 |
jsr166 |
1.2 |
final long testDurationMillis = timeoutMillis(); |
134 |
jsr166 |
1.1 |
final AtomicBoolean done = new AtomicBoolean(false); |
135 |
|
|
final Object elt = impl.makeElement(1); |
136 |
jsr166 |
1.2 |
final Future<?> f1, f2; |
137 |
|
|
final ExecutorService pool = Executors.newCachedThreadPool(); |
138 |
|
|
try (PoolCleaner cleaner = cleaner(pool, done)) { |
139 |
|
|
final CountDownLatch threadsStarted = new CountDownLatch(2); |
140 |
|
|
Runnable checkElt = () -> { |
141 |
|
|
threadsStarted.countDown(); |
142 |
|
|
while (!done.get()) |
143 |
jsr166 |
1.3 |
c.forEach((x) -> { assertSame(x, elt); }); }; |
144 |
jsr166 |
1.2 |
Runnable addRemove = () -> { |
145 |
|
|
threadsStarted.countDown(); |
146 |
|
|
while (!done.get()) { |
147 |
|
|
assertTrue(c.add(elt)); |
148 |
|
|
assertTrue(c.remove(elt)); |
149 |
|
|
}}; |
150 |
|
|
f1 = pool.submit(checkElt); |
151 |
|
|
f2 = pool.submit(addRemove); |
152 |
|
|
Thread.sleep(testDurationMillis); |
153 |
|
|
} |
154 |
|
|
assertNull(f1.get(0L, MILLISECONDS)); |
155 |
|
|
assertNull(f2.get(0L, MILLISECONDS)); |
156 |
jsr166 |
1.1 |
} |
157 |
|
|
|
158 |
|
|
// public void testCollection8DebugFail() { fail(); } |
159 |
|
|
} |