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 |
|
|
public void testForEach() throws Throwable { |
46 |
|
|
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 |
|
|
public void testForEachConcurrentStressTest() throws Throwable { |
73 |
|
|
if (!impl.isConcurrent()) return; |
74 |
|
|
final Collection c = impl.emptyCollection(); |
75 |
jsr166 |
1.2 |
final long testDurationMillis = timeoutMillis(); |
76 |
jsr166 |
1.1 |
final AtomicBoolean done = new AtomicBoolean(false); |
77 |
|
|
final Object elt = impl.makeElement(1); |
78 |
jsr166 |
1.2 |
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 |
jsr166 |
1.1 |
} |
99 |
|
|
|
100 |
|
|
// public void testCollection8DebugFail() { fail(); } |
101 |
|
|
} |