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 |
|
|
64 |
|
} |
65 |
|
|
66 |
|
/** Checks properties of empty collections. */ |
67 |
< |
public void testEmptyMeansEmpty() throws InterruptedException { |
67 |
> |
public void testEmptyMeansEmpty() throws Throwable { |
68 |
|
Collection c = impl.emptyCollection(); |
69 |
|
emptyMeansEmpty(c); |
70 |
|
|
71 |
< |
if (c instanceof java.io.Serializable) |
72 |
< |
emptyMeansEmpty(serialClone(c)); |
71 |
> |
if (c instanceof java.io.Serializable) { |
72 |
> |
try { |
73 |
> |
emptyMeansEmpty(serialClonePossiblyFailing(c)); |
74 |
> |
} catch (java.io.NotSerializableException ex) { |
75 |
> |
// excusable when we have a serializable wrapper around |
76 |
> |
// a non-serializable collection, as can happen with: |
77 |
> |
// Vector.subList() => wrapped AbstractList$RandomAccessSubList |
78 |
> |
if (testImplementationDetails |
79 |
> |
&& (! c.getClass().getName().matches( |
80 |
> |
"java.util.Collections.*"))) |
81 |
> |
throw ex; |
82 |
> |
} |
83 |
> |
} |
84 |
|
|
85 |
|
Collection clone = cloneableClone(c); |
86 |
|
if (clone != null) |
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 { |
362 |
– |
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 |
> |
/** |
554 |
> |
* Motley crew of threads concurrently randomly hammer the collection. |
555 |
> |
*/ |
556 |
> |
public void testDetectRaces() throws Throwable { |
557 |
|
if (!impl.isConcurrent()) return; |
558 |
+ |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
559 |
|
final Collection c = impl.emptyCollection(); |
560 |
< |
final long testDurationMillis = timeoutMillis(); |
560 |
> |
final long testDurationMillis |
561 |
> |
= expensiveTests ? LONG_DELAY_MS : timeoutMillis(); |
562 |
|
final AtomicBoolean done = new AtomicBoolean(false); |
563 |
< |
final Object elt = impl.makeElement(1); |
564 |
< |
final Future<?> f1, f2; |
563 |
> |
final Object one = impl.makeElement(1); |
564 |
> |
final Object two = impl.makeElement(2); |
565 |
> |
final Consumer checkSanity = x -> assertTrue(x == one || x == two); |
566 |
> |
final Consumer<Object[]> checkArraySanity = array -> { |
567 |
> |
// assertTrue(array.length <= 2); // duplicates are permitted |
568 |
> |
for (Object x : array) assertTrue(x == one || x == two); |
569 |
> |
}; |
570 |
> |
final Object[] emptyArray = |
571 |
> |
(Object[]) java.lang.reflect.Array.newInstance(one.getClass(), 0); |
572 |
> |
final List<Future<?>> futures; |
573 |
> |
final Phaser threadsStarted = new Phaser(1); // register this thread |
574 |
> |
final Runnable[] frobbers = { |
575 |
> |
() -> c.forEach(checkSanity), |
576 |
> |
() -> c.stream().forEach(checkSanity), |
577 |
> |
() -> c.parallelStream().forEach(checkSanity), |
578 |
> |
() -> c.spliterator().trySplit(), |
579 |
> |
() -> { |
580 |
> |
Spliterator s = c.spliterator(); |
581 |
> |
s.tryAdvance(checkSanity); |
582 |
> |
s.trySplit(); |
583 |
> |
}, |
584 |
> |
() -> { |
585 |
> |
Spliterator s = c.spliterator(); |
586 |
> |
do {} while (s.tryAdvance(checkSanity)); |
587 |
> |
}, |
588 |
> |
() -> { for (Object x : c) checkSanity.accept(x); }, |
589 |
> |
() -> checkArraySanity.accept(c.toArray()), |
590 |
> |
() -> checkArraySanity.accept(c.toArray(emptyArray)), |
591 |
> |
() -> { |
592 |
> |
assertTrue(c.add(one)); |
593 |
> |
assertTrue(c.contains(one)); |
594 |
> |
assertTrue(c.remove(one)); |
595 |
> |
assertFalse(c.contains(one)); |
596 |
> |
}, |
597 |
> |
() -> { |
598 |
> |
assertTrue(c.add(two)); |
599 |
> |
assertTrue(c.contains(two)); |
600 |
> |
assertTrue(c.remove(two)); |
601 |
> |
assertFalse(c.contains(two)); |
602 |
> |
}, |
603 |
> |
}; |
604 |
> |
final List<Runnable> tasks = |
605 |
> |
Arrays.stream(frobbers) |
606 |
> |
.filter(task -> rnd.nextBoolean()) // random subset |
607 |
> |
.map(task -> (Runnable) () -> { |
608 |
> |
threadsStarted.arriveAndAwaitAdvance(); |
609 |
> |
while (!done.get()) |
610 |
> |
task.run(); |
611 |
> |
}) |
612 |
> |
.collect(Collectors.toList()); |
613 |
|
final ExecutorService pool = Executors.newCachedThreadPool(); |
614 |
|
try (PoolCleaner cleaner = cleaner(pool, done)) { |
615 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
616 |
< |
Runnable checkElt = () -> { |
617 |
< |
threadsStarted.countDown(); |
618 |
< |
while (!done.get()) |
619 |
< |
c.forEach(x -> assertSame(x, elt)); }; |
535 |
< |
Runnable addRemove = () -> { |
536 |
< |
threadsStarted.countDown(); |
537 |
< |
while (!done.get()) { |
538 |
< |
assertTrue(c.add(elt)); |
539 |
< |
assertTrue(c.remove(elt)); |
540 |
< |
}}; |
541 |
< |
f1 = pool.submit(checkElt); |
542 |
< |
f2 = pool.submit(addRemove); |
615 |
> |
threadsStarted.bulkRegister(tasks.size()); |
616 |
> |
futures = tasks.stream() |
617 |
> |
.map(pool::submit) |
618 |
> |
.collect(Collectors.toList()); |
619 |
> |
threadsStarted.arriveAndDeregister(); |
620 |
|
Thread.sleep(testDurationMillis); |
621 |
|
} |
622 |
< |
assertNull(f1.get(0L, MILLISECONDS)); |
623 |
< |
assertNull(f2.get(0L, MILLISECONDS)); |
622 |
> |
for (Future future : futures) |
623 |
> |
assertNull(future.get(0L, MILLISECONDS)); |
624 |
> |
} |
625 |
> |
|
626 |
> |
/** |
627 |
> |
* Spliterators are either IMMUTABLE or truly late-binding or, if |
628 |
> |
* concurrent, use the same "late-binding style" of returning |
629 |
> |
* elements added between creation and first use. |
630 |
> |
*/ |
631 |
> |
public void testLateBindingStyle() { |
632 |
> |
if (!testImplementationDetails) return; |
633 |
> |
if (impl.klazz() == ArrayList.class) return; // for jdk8 |
634 |
> |
// Immutable (snapshot) spliterators are exempt |
635 |
> |
if (impl.emptyCollection().spliterator() |
636 |
> |
.hasCharacteristics(Spliterator.IMMUTABLE)) |
637 |
> |
return; |
638 |
> |
final Object one = impl.makeElement(1); |
639 |
> |
{ |
640 |
> |
final Collection c = impl.emptyCollection(); |
641 |
> |
final Spliterator split = c.spliterator(); |
642 |
> |
c.add(one); |
643 |
> |
assertTrue(split.tryAdvance(e -> { assertSame(e, one); })); |
644 |
> |
assertFalse(split.tryAdvance(e -> { throw new AssertionError(); })); |
645 |
> |
assertTrue(c.contains(one)); |
646 |
> |
} |
647 |
> |
{ |
648 |
> |
final AtomicLong count = new AtomicLong(0); |
649 |
> |
final Collection c = impl.emptyCollection(); |
650 |
> |
final Spliterator split = c.spliterator(); |
651 |
> |
c.add(one); |
652 |
> |
split.forEachRemaining( |
653 |
> |
e -> { assertSame(e, one); count.getAndIncrement(); }); |
654 |
> |
assertEquals(1L, count.get()); |
655 |
> |
assertFalse(split.tryAdvance(e -> { throw new AssertionError(); })); |
656 |
> |
assertTrue(c.contains(one)); |
657 |
> |
} |
658 |
|
} |
659 |
|
|
660 |
|
// public void testCollection8DebugFail() { |