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.2 by jsr166, Mon Feb 22 19:43:54 2016 UTC vs.
Revision 1.4 by jsr166, Mon Oct 17 01:55:48 2016 UTC

# Line 42 | 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 69 | Line 69 | public class Collection8Test extends JSR
69          assertTrue(found.isEmpty());
70      }
71  
72 <    public void testForEachConcurrentStressTest() throws Throwable {
72 >    public void testStreamForEachConcurrentStressTest() throws Throwable {
73          if (!impl.isConcurrent()) return;
74          final Collection c = impl.emptyCollection();
75          final long testDurationMillis = timeoutMillis();
# Line 97 | Line 97 | public class Collection8Test extends JSR
97          assertNull(f2.get(0L, MILLISECONDS));
98      }
99  
100 <    // public void testCollection8DebugFail() { fail(); }
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 = timeoutMillis();
134 >        final AtomicBoolean done = new AtomicBoolean(false);
135 >        final Object elt = impl.makeElement(1);
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() {
159 > //         fail(impl.klazz().getSimpleName());
160 > //     }
161   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines