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.1 by jsr166, Sun Jun 14 20:58:14 2015 UTC vs.
Revision 1.3 by jsr166, Sun Oct 9 05:25:09 2016 UTC

# Line 10 | Line 10 | import static java.util.concurrent.TimeU
10   import java.util.ArrayList;
11   import java.util.Collection;
12   import java.util.Collections;
13 + import java.util.concurrent.CountDownLatch;
14   import java.util.concurrent.Executors;
15   import java.util.concurrent.ExecutorService;
16   import java.util.concurrent.Future;
# Line 41 | Line 42 | public class Collection8Test extends JSR
42      /**
43       * stream().forEach returns elements in the collection
44       */
45 <    public void testForEach() throws Throwable {
45 >    public void testStreamForEach() throws Throwable {
46          final Collection c = impl.emptyCollection();
47          final AtomicLong count = new AtomicLong(0L);
48          final Object x = impl.makeElement(1);
# Line 68 | Line 69 | public class Collection8Test extends JSR
69          assertTrue(found.isEmpty());
70      }
71  
72 +    public void testStreamForEachConcurrentStressTest() throws Throwable {
73 +        if (!impl.isConcurrent()) return;
74 +        final Collection c = impl.emptyCollection();
75 +        final long testDurationMillis = timeoutMillis();
76 +        final AtomicBoolean done = new AtomicBoolean(false);
77 +        final Object elt = impl.makeElement(1);
78 +        final Future<?> f1, f2;
79 +        final ExecutorService pool = Executors.newCachedThreadPool();
80 +        try (PoolCleaner cleaner = cleaner(pool, done)) {
81 +            final CountDownLatch threadsStarted = new CountDownLatch(2);
82 +            Runnable checkElt = () -> {
83 +                threadsStarted.countDown();
84 +                while (!done.get())
85 +                    c.stream().forEach((x) -> { assertSame(x, elt); }); };
86 +            Runnable addRemove = () -> {
87 +                threadsStarted.countDown();
88 +                while (!done.get()) {
89 +                    assertTrue(c.add(elt));
90 +                    assertTrue(c.remove(elt));
91 +                }};
92 +            f1 = pool.submit(checkElt);
93 +            f2 = pool.submit(addRemove);
94 +            Thread.sleep(testDurationMillis);
95 +        }
96 +        assertNull(f1.get(0L, MILLISECONDS));
97 +        assertNull(f2.get(0L, MILLISECONDS));
98 +    }
99 +
100 +    /**
101 +     * collection.forEach returns elements in the collection
102 +     */
103 +    public void testForEach() throws Throwable {
104 +        final Collection c = impl.emptyCollection();
105 +        final AtomicLong count = new AtomicLong(0L);
106 +        final Object x = impl.makeElement(1);
107 +        final Object y = impl.makeElement(2);
108 +        final ArrayList found = new ArrayList();
109 +        Consumer<Object> spy = (o) -> { found.add(o); };
110 +        c.forEach(spy);
111 +        assertTrue(found.isEmpty());
112 +
113 +        assertTrue(c.add(x));
114 +        c.forEach(spy);
115 +        assertEquals(Collections.singletonList(x), found);
116 +        found.clear();
117 +
118 +        assertTrue(c.add(y));
119 +        c.forEach(spy);
120 +        assertEquals(2, found.size());
121 +        assertTrue(found.contains(x));
122 +        assertTrue(found.contains(y));
123 +        found.clear();
124 +
125 +        c.clear();
126 +        c.forEach(spy);
127 +        assertTrue(found.isEmpty());
128 +    }
129 +
130      public void testForEachConcurrentStressTest() throws Throwable {
131          if (!impl.isConcurrent()) return;
132          final Collection c = impl.emptyCollection();
133 <        final long testDurationMillis = SHORT_DELAY_MS;
133 >        final long testDurationMillis = timeoutMillis();
134          final AtomicBoolean done = new AtomicBoolean(false);
135          final Object elt = impl.makeElement(1);
136 <        ExecutorService pool = Executors.newCachedThreadPool();
137 <        Runnable checkElt = () -> {
138 <            while (!done.get())
139 <                c.stream().forEach((x) -> { assertSame(x, elt); }); };
140 <        Runnable addRemove = () -> {
141 <            while (!done.get()) {
142 <                assertTrue(c.add(elt));
143 <                assertTrue(c.remove(elt));
144 <            }};
145 <        Future<?> f1 = pool.submit(checkElt);
146 <        Future<?> f2 = pool.submit(addRemove);
147 <        Thread.sleep(testDurationMillis);
148 <        done.set(true);
149 <        pool.shutdown();
150 <        assertTrue(pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
151 <        assertNull(f1.get(LONG_DELAY_MS, MILLISECONDS));
152 <        assertNull(f2.get(LONG_DELAY_MS, MILLISECONDS));
136 >        final Future<?> f1, f2;
137 >        final ExecutorService pool = Executors.newCachedThreadPool();
138 >        try (PoolCleaner cleaner = cleaner(pool, done)) {
139 >            final CountDownLatch threadsStarted = new CountDownLatch(2);
140 >            Runnable checkElt = () -> {
141 >                threadsStarted.countDown();
142 >                while (!done.get())
143 >                    c.forEach((x) -> { assertSame(x, elt); }); };
144 >            Runnable addRemove = () -> {
145 >                threadsStarted.countDown();
146 >                while (!done.get()) {
147 >                    assertTrue(c.add(elt));
148 >                    assertTrue(c.remove(elt));
149 >                }};
150 >            f1 = pool.submit(checkElt);
151 >            f2 = pool.submit(addRemove);
152 >            Thread.sleep(testDurationMillis);
153 >        }
154 >        assertNull(f1.get(0L, MILLISECONDS));
155 >        assertNull(f2.get(0L, MILLISECONDS));
156      }
157  
158      // public void testCollection8DebugFail() { fail(); }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines