10 |
|
import java.util.ArrayList; |
11 |
|
import java.util.Collection; |
12 |
|
import java.util.Collections; |
13 |
+ |
import java.util.concurrent.CountDownLatch; |
14 |
|
import java.util.concurrent.Executors; |
15 |
|
import java.util.concurrent.ExecutorService; |
16 |
|
import java.util.concurrent.Future; |
72 |
|
public void testForEachConcurrentStressTest() throws Throwable { |
73 |
|
if (!impl.isConcurrent()) return; |
74 |
|
final Collection c = impl.emptyCollection(); |
75 |
< |
final long testDurationMillis = SHORT_DELAY_MS; |
75 |
> |
final long testDurationMillis = timeoutMillis(); |
76 |
|
final AtomicBoolean done = new AtomicBoolean(false); |
77 |
|
final Object elt = impl.makeElement(1); |
78 |
< |
ExecutorService pool = Executors.newCachedThreadPool(); |
79 |
< |
Runnable checkElt = () -> { |
80 |
< |
while (!done.get()) |
81 |
< |
c.stream().forEach((x) -> { assertSame(x, elt); }); }; |
82 |
< |
Runnable addRemove = () -> { |
83 |
< |
while (!done.get()) { |
84 |
< |
assertTrue(c.add(elt)); |
85 |
< |
assertTrue(c.remove(elt)); |
86 |
< |
}}; |
87 |
< |
Future<?> f1 = pool.submit(checkElt); |
88 |
< |
Future<?> f2 = pool.submit(addRemove); |
89 |
< |
Thread.sleep(testDurationMillis); |
90 |
< |
done.set(true); |
91 |
< |
pool.shutdown(); |
92 |
< |
assertTrue(pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
93 |
< |
assertNull(f1.get(LONG_DELAY_MS, MILLISECONDS)); |
94 |
< |
assertNull(f2.get(LONG_DELAY_MS, MILLISECONDS)); |
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 |
|
// public void testCollection8DebugFail() { fail(); } |