ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/Collection8Test.java
(Generate patch)

Comparing jsr166/src/test/tck/Collection8Test.java (file contents):
Revision 1.25 by jsr166, Tue Nov 15 00:08:25 2016 UTC vs.
Revision 1.28 by jsr166, Fri Nov 18 17:23:29 2016 UTC

# Line 25 | Line 25 | import java.util.concurrent.CountDownLat
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  
# Line 302 | Line 304 | public class Collection8Test extends JSR
304              switch (rnd.nextInt(4)) {
305              case 0: survivors.addAll(c); break;
306              case 1: survivors.addAll(Arrays.asList(c.toArray())); break;
307 <            case 2: c.forEach(e -> survivors.add(e)); break;
307 >            case 2: c.forEach(survivors::add); break;
308              case 3: for (Object e : c) survivors.add(e); break;
309              }
310              assertTrue(orig.containsAll(accepts));
# Line 353 | Line 355 | public class Collection8Test extends JSR
355          ArrayList forEached = new ArrayList();
356          ArrayList removeIfed = new ArrayList();
357          for (Object x : c) iterated.add(x);
358 <        c.iterator().forEachRemaining(e -> iteratedForEachRemaining.add(e));
358 >        c.iterator().forEachRemaining(iteratedForEachRemaining::add);
359          for (Spliterator s = c.spliterator();
360 <             s.tryAdvance(e -> tryAdvanced.add(e)); ) {}
361 <        c.spliterator().forEachRemaining(e -> spliterated.add(e));
362 <        c.forEach(e -> forEached.add(e));
360 >             s.tryAdvance(tryAdvanced::add); ) {}
361 >        c.spliterator().forEachRemaining(spliterated::add);
362 >        c.forEach(forEached::add);
363          c.removeIf(e -> { removeIfed.add(e); return false; });
364          boolean ordered =
365              c.spliterator().hasCharacteristics(Spliterator.ORDERED);
# Line 532 | Line 534 | public class Collection8Test extends JSR
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 Runnable[] frobbers = {
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 >        };
579 >        final List<Runnable> tasks =
580 >            Arrays.stream(frobbers)
581 >            .filter(task -> rnd.nextBoolean()) // random subset
582 >            .map(task -> (Runnable) () -> {
583 >                     threadsStarted.arriveAndAwaitAdvance();
584 >                     while (!done.get())
585 >                         task.run();
586 >                 })
587 >            .collect(Collectors.toList());
588          final ExecutorService pool = Executors.newCachedThreadPool();
589          try (PoolCleaner cleaner = cleaner(pool, done)) {
590 <            final CountDownLatch threadsStarted = new CountDownLatch(2);
591 <            Runnable checkElt = () -> {
592 <                threadsStarted.countDown();
593 <                while (!done.get())
594 <                    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);
590 >            threadsStarted.bulkRegister(tasks.size());
591 >            futures = tasks.stream()
592 >                .map(pool::submit)
593 >                .collect(Collectors.toList());
594 >            threadsStarted.arriveAndDeregister();
595              Thread.sleep(testDurationMillis);
596          }
597 <        assertNull(f1.get(0L, MILLISECONDS));
598 <        assertNull(f2.get(0L, MILLISECONDS));
597 >        for (Future future : futures)
598 >            assertNull(future.get(0L, MILLISECONDS));
599      }
600  
601   //     public void testCollection8DebugFail() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines