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)); |
354 |
|
ArrayList tryAdvanced = new ArrayList(); |
355 |
|
ArrayList spliterated = new ArrayList(); |
356 |
|
ArrayList forEached = new ArrayList(); |
357 |
+ |
ArrayList streamForEached = new ArrayList(); |
358 |
+ |
ConcurrentLinkedQueue parallelStreamForEached = new ConcurrentLinkedQueue(); |
359 |
|
ArrayList removeIfed = new ArrayList(); |
360 |
|
for (Object x : c) iterated.add(x); |
361 |
< |
c.iterator().forEachRemaining(e -> iteratedForEachRemaining.add(e)); |
361 |
> |
c.iterator().forEachRemaining(iteratedForEachRemaining::add); |
362 |
|
for (Spliterator s = c.spliterator(); |
363 |
< |
s.tryAdvance(e -> tryAdvanced.add(e)); ) {} |
364 |
< |
c.spliterator().forEachRemaining(e -> spliterated.add(e)); |
365 |
< |
c.forEach(e -> forEached.add(e)); |
363 |
> |
s.tryAdvance(tryAdvanced::add); ) {} |
364 |
> |
c.spliterator().forEachRemaining(spliterated::add); |
365 |
> |
c.forEach(forEached::add); |
366 |
> |
c.stream().forEach(streamForEached::add); |
367 |
> |
c.parallelStream().forEach(parallelStreamForEached::add); |
368 |
|
c.removeIf(e -> { removeIfed.add(e); return false; }); |
369 |
|
boolean ordered = |
370 |
|
c.spliterator().hasCharacteristics(Spliterator.ORDERED); |
371 |
|
if (c instanceof List || c instanceof Deque) |
372 |
|
assertTrue(ordered); |
373 |
+ |
HashSet cset = new HashSet(c); |
374 |
+ |
assertEquals(cset, new HashSet(parallelStreamForEached)); |
375 |
|
if (ordered) { |
376 |
|
assertEquals(iterated, iteratedForEachRemaining); |
377 |
|
assertEquals(iterated, tryAdvanced); |
378 |
|
assertEquals(iterated, spliterated); |
379 |
|
assertEquals(iterated, forEached); |
380 |
+ |
assertEquals(iterated, streamForEached); |
381 |
|
assertEquals(iterated, removeIfed); |
382 |
|
} else { |
373 |
– |
HashSet cset = new HashSet(c); |
383 |
|
assertEquals(cset, new HashSet(iterated)); |
384 |
|
assertEquals(cset, new HashSet(iteratedForEachRemaining)); |
385 |
|
assertEquals(cset, new HashSet(tryAdvanced)); |
386 |
|
assertEquals(cset, new HashSet(spliterated)); |
387 |
|
assertEquals(cset, new HashSet(forEached)); |
388 |
+ |
assertEquals(cset, new HashSet(streamForEached)); |
389 |
|
assertEquals(cset, new HashSet(removeIfed)); |
390 |
|
} |
391 |
|
if (c instanceof Deque) { |
410 |
|
public void testRemoveAfterForEachRemaining() { |
411 |
|
Collection c = impl.emptyCollection(); |
412 |
|
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
413 |
< |
{ |
413 |
> |
testCollection: { |
414 |
|
int n = 3 + rnd.nextInt(2); |
415 |
|
for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); |
416 |
|
Iterator it = c.iterator(); |
423 |
|
if (c instanceof java.util.concurrent.ArrayBlockingQueue) { |
424 |
|
assertIteratorExhausted(it); |
425 |
|
} else { |
426 |
< |
it.remove(); |
426 |
> |
try { it.remove(); } |
427 |
> |
catch (UnsupportedOperationException ok) { |
428 |
> |
break testCollection; |
429 |
> |
} |
430 |
|
assertEquals(n - 1, c.size()); |
431 |
|
for (int i = 0; i < n - 1; i++) |
432 |
|
assertTrue(c.contains(impl.makeElement(i))); |
542 |
|
assertTrue(found.isEmpty()); |
543 |
|
} |
544 |
|
|
545 |
< |
public void testForEachConcurrentStressTest() throws Throwable { |
545 |
> |
/** |
546 |
> |
* Motley crew of threads concurrently randomly hammer the collection. |
547 |
> |
*/ |
548 |
> |
public void testDetectRaces() throws Throwable { |
549 |
|
if (!impl.isConcurrent()) return; |
550 |
+ |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
551 |
|
final Collection c = impl.emptyCollection(); |
552 |
|
final long testDurationMillis = timeoutMillis(); |
553 |
|
final AtomicBoolean done = new AtomicBoolean(false); |
554 |
< |
final Object elt = impl.makeElement(1); |
555 |
< |
final Future<?> f1, f2; |
554 |
> |
final Object one = impl.makeElement(1); |
555 |
> |
final Object two = impl.makeElement(2); |
556 |
> |
final Object[] emptyArray = |
557 |
> |
(Object[]) java.lang.reflect.Array.newInstance(one.getClass(), 0); |
558 |
> |
final List<Future<?>> futures; |
559 |
> |
final Phaser threadsStarted = new Phaser(1); // register this thread |
560 |
> |
final Consumer checkSanity = x -> assertTrue(x == one || x == two); |
561 |
> |
final Runnable[] frobbers = { |
562 |
> |
() -> c.forEach(checkSanity), |
563 |
> |
() -> c.stream().forEach(checkSanity), |
564 |
> |
() -> c.parallelStream().forEach(checkSanity), |
565 |
> |
() -> c.spliterator().trySplit(), |
566 |
> |
() -> { |
567 |
> |
Spliterator s = c.spliterator(); |
568 |
> |
s.tryAdvance(checkSanity); |
569 |
> |
s.trySplit(); |
570 |
> |
}, |
571 |
> |
() -> { |
572 |
> |
Spliterator s = c.spliterator(); |
573 |
> |
do {} while (s.tryAdvance(checkSanity)); |
574 |
> |
}, |
575 |
> |
() -> { for (Object x : c) checkSanity.accept(x); }, |
576 |
> |
() -> { for (Object x : c.toArray()) checkSanity.accept(x); }, |
577 |
> |
() -> { for (Object x : c.toArray(emptyArray)) checkSanity.accept(x); }, |
578 |
> |
() -> { |
579 |
> |
assertTrue(c.add(one)); |
580 |
> |
assertTrue(c.contains(one)); |
581 |
> |
assertTrue(c.remove(one)); |
582 |
> |
assertFalse(c.contains(one)); |
583 |
> |
}, |
584 |
> |
() -> { |
585 |
> |
assertTrue(c.add(two)); |
586 |
> |
assertTrue(c.contains(two)); |
587 |
> |
assertTrue(c.remove(two)); |
588 |
> |
assertFalse(c.contains(two)); |
589 |
> |
}, |
590 |
> |
}; |
591 |
> |
final List<Runnable> tasks = |
592 |
> |
Arrays.stream(frobbers) |
593 |
> |
.filter(task -> rnd.nextBoolean()) // random subset |
594 |
> |
.map(task -> (Runnable) () -> { |
595 |
> |
threadsStarted.arriveAndAwaitAdvance(); |
596 |
> |
while (!done.get()) |
597 |
> |
task.run(); |
598 |
> |
}) |
599 |
> |
.collect(Collectors.toList()); |
600 |
|
final ExecutorService pool = Executors.newCachedThreadPool(); |
601 |
|
try (PoolCleaner cleaner = cleaner(pool, done)) { |
602 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
603 |
< |
Runnable checkElt = () -> { |
604 |
< |
threadsStarted.countDown(); |
605 |
< |
while (!done.get()) |
606 |
< |
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); |
602 |
> |
threadsStarted.bulkRegister(tasks.size()); |
603 |
> |
futures = tasks.stream() |
604 |
> |
.map(pool::submit) |
605 |
> |
.collect(Collectors.toList()); |
606 |
> |
threadsStarted.arriveAndDeregister(); |
607 |
|
Thread.sleep(testDurationMillis); |
608 |
|
} |
609 |
< |
assertNull(f1.get(0L, MILLISECONDS)); |
610 |
< |
assertNull(f2.get(0L, MILLISECONDS)); |
609 |
> |
for (Future future : futures) |
610 |
> |
assertNull(future.get(0L, MILLISECONDS)); |
611 |
> |
} |
612 |
> |
|
613 |
> |
/** |
614 |
> |
* Spliterators are either IMMUTABLE or truly late-binding or, if |
615 |
> |
* concurrent, use the same "late-binding style" of returning |
616 |
> |
* elements added between creation and first use. |
617 |
> |
*/ |
618 |
> |
public void testLateBindingStyle() { |
619 |
> |
if (!testImplementationDetails) return; |
620 |
> |
if (impl.klazz() == ArrayList.class) return; // for jdk8 |
621 |
> |
// Immutable (snapshot) spliterators are exempt |
622 |
> |
if (impl.emptyCollection().spliterator() |
623 |
> |
.hasCharacteristics(Spliterator.IMMUTABLE)) |
624 |
> |
return; |
625 |
> |
final Object one = impl.makeElement(1); |
626 |
> |
{ |
627 |
> |
final Collection c = impl.emptyCollection(); |
628 |
> |
final Spliterator split = c.spliterator(); |
629 |
> |
c.add(one); |
630 |
> |
assertTrue(split.tryAdvance(e -> { assertSame(e, one); })); |
631 |
> |
assertFalse(split.tryAdvance(e -> { throw new AssertionError(); })); |
632 |
> |
assertTrue(c.contains(one)); |
633 |
> |
} |
634 |
> |
{ |
635 |
> |
final AtomicLong count = new AtomicLong(0); |
636 |
> |
final Collection c = impl.emptyCollection(); |
637 |
> |
final Spliterator split = c.spliterator(); |
638 |
> |
c.add(one); |
639 |
> |
split.forEachRemaining( |
640 |
> |
e -> { assertSame(e, one); count.getAndIncrement(); }); |
641 |
> |
assertEquals(1L, count.get()); |
642 |
> |
assertFalse(split.tryAdvance(e -> { throw new AssertionError(); })); |
643 |
> |
assertTrue(c.contains(one)); |
644 |
> |
} |
645 |
|
} |
646 |
|
|
647 |
|
// public void testCollection8DebugFail() { |