25 |
|
import java.util.concurrent.Executors; |
26 |
|
import java.util.concurrent.ExecutorService; |
27 |
|
import java.util.concurrent.Future; |
28 |
+ |
import java.util.concurrent.Phaser; |
29 |
|
import java.util.concurrent.ThreadLocalRandom; |
30 |
|
import java.util.concurrent.atomic.AtomicBoolean; |
31 |
|
import java.util.concurrent.atomic.AtomicLong; |
32 |
|
import java.util.concurrent.atomic.AtomicReference; |
33 |
|
import java.util.function.Consumer; |
34 |
|
import java.util.function.Predicate; |
35 |
+ |
import java.util.stream.Collectors; |
36 |
|
|
37 |
|
import junit.framework.Test; |
38 |
|
|
534 |
|
assertTrue(found.isEmpty()); |
535 |
|
} |
536 |
|
|
537 |
< |
public void testForEachConcurrentStressTest() throws Throwable { |
537 |
> |
/** |
538 |
> |
* Motley crew of threads concurrently randomly hammer the collection. |
539 |
> |
*/ |
540 |
> |
public void testDetectRaces() throws Throwable { |
541 |
|
if (!impl.isConcurrent()) return; |
542 |
+ |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
543 |
|
final Collection c = impl.emptyCollection(); |
544 |
|
final long testDurationMillis = timeoutMillis(); |
545 |
|
final AtomicBoolean done = new AtomicBoolean(false); |
546 |
< |
final Object elt = impl.makeElement(1); |
547 |
< |
final Future<?> f1, f2; |
546 |
> |
final Object one = impl.makeElement(1); |
547 |
> |
final Object two = impl.makeElement(2); |
548 |
> |
final List<Future<?>> futures; |
549 |
> |
final Phaser threadsStarted = new Phaser(1); // register this thread |
550 |
> |
final List<Runnable> tasks = List.<Runnable>of( |
551 |
> |
() -> c.forEach(x -> assertTrue(x == one || x == two)), |
552 |
> |
() -> c.stream().forEach(x -> assertTrue(x == one || x == two)), |
553 |
> |
() -> c.spliterator().trySplit(), |
554 |
> |
() -> { |
555 |
> |
Spliterator s = c.spliterator(); |
556 |
> |
s.tryAdvance(x -> assertTrue(x == one || x == two)); |
557 |
> |
s.trySplit(); |
558 |
> |
}, |
559 |
> |
() -> { |
560 |
> |
Spliterator s = c.spliterator(); |
561 |
> |
do {} while (s.tryAdvance(x -> assertTrue(x == one || x == two))); |
562 |
> |
}, |
563 |
> |
() -> { |
564 |
> |
for (Object x : c) assertTrue(x == one || x == two); |
565 |
> |
}, |
566 |
> |
() -> { |
567 |
> |
assertTrue(c.add(one)); |
568 |
> |
assertTrue(c.contains(one)); |
569 |
> |
assertTrue(c.remove(one)); |
570 |
> |
assertFalse(c.contains(one)); |
571 |
> |
}, |
572 |
> |
() -> { |
573 |
> |
assertTrue(c.add(two)); |
574 |
> |
assertTrue(c.contains(two)); |
575 |
> |
assertTrue(c.remove(two)); |
576 |
> |
assertFalse(c.contains(two)); |
577 |
> |
}) |
578 |
> |
.stream() |
579 |
> |
.filter(task -> rnd.nextBoolean()) // random subset |
580 |
> |
.map(task -> (Runnable) () -> { |
581 |
> |
threadsStarted.arriveAndAwaitAdvance(); |
582 |
> |
while (!done.get()) |
583 |
> |
task.run(); |
584 |
> |
}) |
585 |
> |
.collect(Collectors.toList()); |
586 |
|
final ExecutorService pool = Executors.newCachedThreadPool(); |
587 |
|
try (PoolCleaner cleaner = cleaner(pool, done)) { |
588 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
589 |
< |
Runnable checkElt = () -> { |
590 |
< |
threadsStarted.countDown(); |
591 |
< |
while (!done.get()) |
592 |
< |
c.forEach(x -> assertSame(x, elt)); }; |
549 |
< |
Runnable addRemove = () -> { |
550 |
< |
threadsStarted.countDown(); |
551 |
< |
while (!done.get()) { |
552 |
< |
assertTrue(c.add(elt)); |
553 |
< |
assertTrue(c.remove(elt)); |
554 |
< |
}}; |
555 |
< |
f1 = pool.submit(checkElt); |
556 |
< |
f2 = pool.submit(addRemove); |
588 |
> |
threadsStarted.bulkRegister(tasks.size()); |
589 |
> |
futures = tasks.stream() |
590 |
> |
.map(task -> pool.submit(task)) |
591 |
> |
.collect(Collectors.toList()); |
592 |
> |
threadsStarted.arriveAndDeregister(); |
593 |
|
Thread.sleep(testDurationMillis); |
594 |
|
} |
595 |
< |
assertNull(f1.get(0L, MILLISECONDS)); |
596 |
< |
assertNull(f2.get(0L, MILLISECONDS)); |
595 |
> |
for (Future future : futures) |
596 |
> |
assertNull(future.get(0L, MILLISECONDS)); |
597 |
|
} |
598 |
|
|
599 |
|
// public void testCollection8DebugFail() { |