21 |
|
import java.util.Spliterator; |
22 |
|
import java.util.concurrent.BlockingDeque; |
23 |
|
import java.util.concurrent.BlockingQueue; |
24 |
+ |
import java.util.concurrent.ConcurrentLinkedQueue; |
25 |
|
import java.util.concurrent.CountDownLatch; |
26 |
|
import java.util.concurrent.Executors; |
27 |
|
import java.util.concurrent.ExecutorService; |
28 |
|
import java.util.concurrent.Future; |
29 |
+ |
import java.util.concurrent.Phaser; |
30 |
|
import java.util.concurrent.ThreadLocalRandom; |
31 |
|
import java.util.concurrent.atomic.AtomicBoolean; |
32 |
|
import java.util.concurrent.atomic.AtomicLong; |
33 |
|
import java.util.concurrent.atomic.AtomicReference; |
34 |
|
import java.util.function.Consumer; |
35 |
|
import java.util.function.Predicate; |
36 |
+ |
import java.util.stream.Collectors; |
37 |
|
|
38 |
|
import junit.framework.Test; |
39 |
|
|
305 |
|
switch (rnd.nextInt(4)) { |
306 |
|
case 0: survivors.addAll(c); break; |
307 |
|
case 1: survivors.addAll(Arrays.asList(c.toArray())); break; |
308 |
< |
case 2: c.forEach(e -> survivors.add(e)); break; |
308 |
> |
case 2: c.forEach(survivors::add); break; |
309 |
|
case 3: for (Object e : c) survivors.add(e); break; |
310 |
|
} |
311 |
|
assertTrue(orig.containsAll(accepts)); |
353 |
|
ArrayList iteratedForEachRemaining = new ArrayList(); |
354 |
|
ArrayList tryAdvanced = new ArrayList(); |
355 |
|
ArrayList spliterated = new ArrayList(); |
356 |
+ |
ArrayList splitonced = new ArrayList(); |
357 |
|
ArrayList forEached = new ArrayList(); |
358 |
+ |
ArrayList streamForEached = new ArrayList(); |
359 |
+ |
ConcurrentLinkedQueue parallelStreamForEached = new ConcurrentLinkedQueue(); |
360 |
|
ArrayList removeIfed = new ArrayList(); |
361 |
|
for (Object x : c) iterated.add(x); |
362 |
< |
c.iterator().forEachRemaining(e -> iteratedForEachRemaining.add(e)); |
362 |
> |
c.iterator().forEachRemaining(iteratedForEachRemaining::add); |
363 |
|
for (Spliterator s = c.spliterator(); |
364 |
< |
s.tryAdvance(e -> tryAdvanced.add(e)); ) {} |
365 |
< |
c.spliterator().forEachRemaining(e -> spliterated.add(e)); |
366 |
< |
c.forEach(e -> forEached.add(e)); |
364 |
> |
s.tryAdvance(tryAdvanced::add); ) {} |
365 |
> |
c.spliterator().forEachRemaining(spliterated::add); |
366 |
> |
{ // trySplit returns "strict prefix" |
367 |
> |
Spliterator s1 = c.spliterator(), s2 = s1.trySplit(); |
368 |
> |
if (s2 != null) s2.forEachRemaining(splitonced::add); |
369 |
> |
s1.forEachRemaining(splitonced::add); |
370 |
> |
} |
371 |
> |
c.forEach(forEached::add); |
372 |
> |
c.stream().forEach(streamForEached::add); |
373 |
> |
c.parallelStream().forEach(parallelStreamForEached::add); |
374 |
|
c.removeIf(e -> { removeIfed.add(e); return false; }); |
375 |
|
boolean ordered = |
376 |
|
c.spliterator().hasCharacteristics(Spliterator.ORDERED); |
377 |
|
if (c instanceof List || c instanceof Deque) |
378 |
|
assertTrue(ordered); |
379 |
+ |
HashSet cset = new HashSet(c); |
380 |
+ |
assertEquals(cset, new HashSet(parallelStreamForEached)); |
381 |
|
if (ordered) { |
382 |
|
assertEquals(iterated, iteratedForEachRemaining); |
383 |
|
assertEquals(iterated, tryAdvanced); |
384 |
|
assertEquals(iterated, spliterated); |
385 |
+ |
assertEquals(iterated, splitonced); |
386 |
|
assertEquals(iterated, forEached); |
387 |
+ |
assertEquals(iterated, streamForEached); |
388 |
|
assertEquals(iterated, removeIfed); |
389 |
|
} else { |
373 |
– |
HashSet cset = new HashSet(c); |
390 |
|
assertEquals(cset, new HashSet(iterated)); |
391 |
|
assertEquals(cset, new HashSet(iteratedForEachRemaining)); |
392 |
|
assertEquals(cset, new HashSet(tryAdvanced)); |
393 |
|
assertEquals(cset, new HashSet(spliterated)); |
394 |
+ |
assertEquals(cset, new HashSet(splitonced)); |
395 |
|
assertEquals(cset, new HashSet(forEached)); |
396 |
+ |
assertEquals(cset, new HashSet(streamForEached)); |
397 |
|
assertEquals(cset, new HashSet(removeIfed)); |
398 |
|
} |
399 |
|
if (c instanceof Deque) { |
418 |
|
public void testRemoveAfterForEachRemaining() { |
419 |
|
Collection c = impl.emptyCollection(); |
420 |
|
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
421 |
< |
{ |
421 |
> |
testCollection: { |
422 |
|
int n = 3 + rnd.nextInt(2); |
423 |
|
for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); |
424 |
|
Iterator it = c.iterator(); |
431 |
|
if (c instanceof java.util.concurrent.ArrayBlockingQueue) { |
432 |
|
assertIteratorExhausted(it); |
433 |
|
} else { |
434 |
< |
it.remove(); |
434 |
> |
try { it.remove(); } |
435 |
> |
catch (UnsupportedOperationException ok) { |
436 |
> |
break testCollection; |
437 |
> |
} |
438 |
|
assertEquals(n - 1, c.size()); |
439 |
|
for (int i = 0; i < n - 1; i++) |
440 |
|
assertTrue(c.contains(impl.makeElement(i))); |
550 |
|
assertTrue(found.isEmpty()); |
551 |
|
} |
552 |
|
|
553 |
< |
public void testForEachConcurrentStressTest() throws Throwable { |
553 |
> |
/** TODO: promote to a common utility */ |
554 |
> |
static <T> T chooseOne(T ... ts) { |
555 |
> |
return ts[ThreadLocalRandom.current().nextInt(ts.length)]; |
556 |
> |
} |
557 |
> |
|
558 |
> |
/** TODO: more random adders and removers */ |
559 |
> |
static <E> Runnable adderRemover(Collection<E> c, E e) { |
560 |
> |
return chooseOne( |
561 |
> |
() -> { |
562 |
> |
assertTrue(c.add(e)); |
563 |
> |
assertTrue(c.contains(e)); |
564 |
> |
assertTrue(c.remove(e)); |
565 |
> |
assertFalse(c.contains(e)); |
566 |
> |
}, |
567 |
> |
() -> { |
568 |
> |
assertTrue(c.add(e)); |
569 |
> |
assertTrue(c.contains(e)); |
570 |
> |
assertTrue(c.removeIf(x -> x == e)); |
571 |
> |
assertFalse(c.contains(e)); |
572 |
> |
}, |
573 |
> |
() -> { |
574 |
> |
assertTrue(c.add(e)); |
575 |
> |
assertTrue(c.contains(e)); |
576 |
> |
for (Iterator it = c.iterator();; ) |
577 |
> |
if (it.next() == e) { |
578 |
> |
try { it.remove(); } |
579 |
> |
catch (UnsupportedOperationException ok) { |
580 |
> |
c.remove(e); |
581 |
> |
} |
582 |
> |
break; |
583 |
> |
} |
584 |
> |
assertFalse(c.contains(e)); |
585 |
> |
}); |
586 |
> |
} |
587 |
> |
|
588 |
> |
/** |
589 |
> |
* Motley crew of threads concurrently randomly hammer the collection. |
590 |
> |
*/ |
591 |
> |
public void testDetectRaces() throws Throwable { |
592 |
|
if (!impl.isConcurrent()) return; |
593 |
+ |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
594 |
|
final Collection c = impl.emptyCollection(); |
595 |
< |
final long testDurationMillis = timeoutMillis(); |
595 |
> |
final long testDurationMillis |
596 |
> |
= expensiveTests ? LONG_DELAY_MS : timeoutMillis(); |
597 |
|
final AtomicBoolean done = new AtomicBoolean(false); |
598 |
< |
final Object elt = impl.makeElement(1); |
599 |
< |
final Future<?> f1, f2; |
598 |
> |
final Object one = impl.makeElement(1); |
599 |
> |
final Object two = impl.makeElement(2); |
600 |
> |
final Consumer checkSanity = x -> assertTrue(x == one || x == two); |
601 |
> |
final Consumer<Object[]> checkArraySanity = array -> { |
602 |
> |
// assertTrue(array.length <= 2); // duplicates are permitted |
603 |
> |
for (Object x : array) assertTrue(x == one || x == two); |
604 |
> |
}; |
605 |
> |
final Object[] emptyArray = |
606 |
> |
(Object[]) java.lang.reflect.Array.newInstance(one.getClass(), 0); |
607 |
> |
final List<Future<?>> futures; |
608 |
> |
final Phaser threadsStarted = new Phaser(1); // register this thread |
609 |
> |
final Runnable[] frobbers = { |
610 |
> |
() -> c.forEach(checkSanity), |
611 |
> |
() -> c.stream().forEach(checkSanity), |
612 |
> |
() -> c.parallelStream().forEach(checkSanity), |
613 |
> |
() -> c.spliterator().trySplit(), |
614 |
> |
() -> { |
615 |
> |
Spliterator s = c.spliterator(); |
616 |
> |
s.tryAdvance(checkSanity); |
617 |
> |
s.trySplit(); |
618 |
> |
}, |
619 |
> |
() -> { |
620 |
> |
Spliterator s = c.spliterator(); |
621 |
> |
do {} while (s.tryAdvance(checkSanity)); |
622 |
> |
}, |
623 |
> |
() -> { for (Object x : c) checkSanity.accept(x); }, |
624 |
> |
() -> checkArraySanity.accept(c.toArray()), |
625 |
> |
() -> checkArraySanity.accept(c.toArray(emptyArray)), |
626 |
> |
() -> { |
627 |
> |
Object[] a = new Object[5]; |
628 |
> |
Object three = impl.makeElement(3); |
629 |
> |
Arrays.fill(a, 0, a.length, three); |
630 |
> |
Object[] x = c.toArray(a); |
631 |
> |
if (x == a) |
632 |
> |
for (int i = 0; i < a.length && a[i] != null; i++) |
633 |
> |
checkSanity.accept(a[i]); |
634 |
> |
// A careful reading of the spec does not support: |
635 |
> |
// for (i++; i < a.length; i++) assertSame(three, a[i]); |
636 |
> |
else |
637 |
> |
checkArraySanity.accept(x); |
638 |
> |
}, |
639 |
> |
adderRemover(c, one), |
640 |
> |
adderRemover(c, two), |
641 |
> |
}; |
642 |
> |
final List<Runnable> tasks = |
643 |
> |
Arrays.stream(frobbers) |
644 |
> |
.filter(task -> rnd.nextBoolean()) // random subset |
645 |
> |
.map(task -> (Runnable) () -> { |
646 |
> |
threadsStarted.arriveAndAwaitAdvance(); |
647 |
> |
while (!done.get()) |
648 |
> |
task.run(); |
649 |
> |
}) |
650 |
> |
.collect(Collectors.toList()); |
651 |
|
final ExecutorService pool = Executors.newCachedThreadPool(); |
652 |
|
try (PoolCleaner cleaner = cleaner(pool, done)) { |
653 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
654 |
< |
Runnable checkElt = () -> { |
655 |
< |
threadsStarted.countDown(); |
656 |
< |
while (!done.get()) |
657 |
< |
c.forEach(x -> assertSame(x, elt)); }; |
546 |
< |
Runnable addRemove = () -> { |
547 |
< |
threadsStarted.countDown(); |
548 |
< |
while (!done.get()) { |
549 |
< |
assertTrue(c.add(elt)); |
550 |
< |
assertTrue(c.remove(elt)); |
551 |
< |
}}; |
552 |
< |
f1 = pool.submit(checkElt); |
553 |
< |
f2 = pool.submit(addRemove); |
653 |
> |
threadsStarted.bulkRegister(tasks.size()); |
654 |
> |
futures = tasks.stream() |
655 |
> |
.map(pool::submit) |
656 |
> |
.collect(Collectors.toList()); |
657 |
> |
threadsStarted.arriveAndDeregister(); |
658 |
|
Thread.sleep(testDurationMillis); |
659 |
|
} |
660 |
< |
assertNull(f1.get(0L, MILLISECONDS)); |
661 |
< |
assertNull(f2.get(0L, MILLISECONDS)); |
660 |
> |
for (Future future : futures) |
661 |
> |
assertNull(future.get(0L, MILLISECONDS)); |
662 |
> |
} |
663 |
> |
|
664 |
> |
/** |
665 |
> |
* Spliterators are either IMMUTABLE or truly late-binding or, if |
666 |
> |
* concurrent, use the same "late-binding style" of returning |
667 |
> |
* elements added between creation and first use. |
668 |
> |
*/ |
669 |
> |
public void testLateBindingStyle() { |
670 |
> |
if (!testImplementationDetails) return; |
671 |
> |
if (impl.klazz() == ArrayList.class) return; // for jdk8 |
672 |
> |
// Immutable (snapshot) spliterators are exempt |
673 |
> |
if (impl.emptyCollection().spliterator() |
674 |
> |
.hasCharacteristics(Spliterator.IMMUTABLE)) |
675 |
> |
return; |
676 |
> |
final Object one = impl.makeElement(1); |
677 |
> |
{ |
678 |
> |
final Collection c = impl.emptyCollection(); |
679 |
> |
final Spliterator split = c.spliterator(); |
680 |
> |
c.add(one); |
681 |
> |
assertTrue(split.tryAdvance(e -> { assertSame(e, one); })); |
682 |
> |
assertFalse(split.tryAdvance(e -> { throw new AssertionError(); })); |
683 |
> |
assertTrue(c.contains(one)); |
684 |
> |
} |
685 |
> |
{ |
686 |
> |
final AtomicLong count = new AtomicLong(0); |
687 |
> |
final Collection c = impl.emptyCollection(); |
688 |
> |
final Spliterator split = c.spliterator(); |
689 |
> |
c.add(one); |
690 |
> |
split.forEachRemaining( |
691 |
> |
e -> { assertSame(e, one); count.getAndIncrement(); }); |
692 |
> |
assertEquals(1L, count.get()); |
693 |
> |
assertFalse(split.tryAdvance(e -> { throw new AssertionError(); })); |
694 |
> |
assertTrue(c.contains(one)); |
695 |
> |
} |
696 |
|
} |
697 |
|
|
698 |
|
// public void testCollection8DebugFail() { |