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.24 by jsr166, Mon Nov 14 23:52:22 2016 UTC vs.
Revision 1.27 by jsr166, Tue Nov 15 23:08:30 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 400 | Line 402 | public class Collection8Test extends JSR
402      public void testRemoveAfterForEachRemaining() {
403          Collection c = impl.emptyCollection();
404          ThreadLocalRandom rnd = ThreadLocalRandom.current();
405 <        {
405 >        testCollection: {
406              int n = 3 + rnd.nextInt(2);
407              for (int i = 0; i < n; i++) c.add(impl.makeElement(i));
408              Iterator it = c.iterator();
# Line 413 | Line 415 | public class Collection8Test extends JSR
415                  if (c instanceof java.util.concurrent.ArrayBlockingQueue) {
416                      assertIteratorExhausted(it);
417                  } else {
418 <                    it.remove();
418 >                    try { it.remove(); }
419 >                    catch (UnsupportedOperationException ok) {
420 >                        break testCollection;
421 >                    }
422                      assertEquals(n - 1, c.size());
423                      for (int i = 0; i < n - 1; i++)
424                          assertTrue(c.contains(impl.makeElement(i)));
# Line 529 | 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)); };
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);
590 >            threadsStarted.bulkRegister(tasks.size());
591 >            futures = tasks.stream()
592 >                .map(task -> pool.submit(task))
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