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

Comparing jsr166/src/test/tck/CountDownLatchTest.java (file contents):
Revision 1.12 by jsr166, Sat Nov 21 02:33:20 2009 UTC vs.
Revision 1.21 by jsr166, Tue May 31 16:16:23 2011 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
10   import java.util.*;
11 < import java.util.concurrent.*;
11 > import java.util.concurrent.CountDownLatch;
12   import static java.util.concurrent.TimeUnit.MILLISECONDS;
13  
14   public class CountDownLatchTest extends JSR166TestCase {
15      public static void main(String[] args) {
16 <        junit.textui.TestRunner.run (suite());
16 >        junit.textui.TestRunner.run(suite());
17      }
18      public static Test suite() {
19          return new TestSuite(CountDownLatchTest.class);
# Line 56 | Line 56 | public class CountDownLatchTest extends
56       */
57      public void testAwait() {
58          final CountDownLatch l = new CountDownLatch(2);
59 +        final CountDownLatch pleaseCountDown = new CountDownLatch(1);
60  
61 <        Thread t = new Thread(new Runnable() {
62 <                public void run() {
63 <                    try {
64 <                        threadAssertTrue(l.getCount() > 0);
65 <                        l.await();
66 <                        threadAssertTrue(l.getCount() == 0);
67 <                    } catch (InterruptedException e) {
67 <                        threadUnexpectedException();
68 <                    }
69 <                }
70 <            });
71 <        t.start();
72 <        try {
73 <            assertEquals(l.getCount(), 2);
74 <            Thread.sleep(SHORT_DELAY_MS);
75 <            l.countDown();
76 <            assertEquals(l.getCount(), 1);
77 <            l.countDown();
78 <            assertEquals(l.getCount(), 0);
79 <            t.join();
80 <        } catch (InterruptedException e) {
81 <            unexpectedException();
82 <        }
83 <    }
61 >        Thread t = newStartedThread(new CheckedRunnable() {
62 >            public void realRun() throws InterruptedException {
63 >                assertEquals(2, l.getCount());
64 >                pleaseCountDown.countDown();
65 >                l.await();
66 >                assertEquals(0, l.getCount());
67 >            }});
68  
69 +        await(pleaseCountDown);
70 +        assertEquals(2, l.getCount());
71 +        l.countDown();
72 +        assertEquals(1, l.getCount());
73 +        assertThreadStaysAlive(t);
74 +        l.countDown();
75 +        assertEquals(0, l.getCount());
76 +        awaitTermination(t);
77 +    }
78  
79      /**
80       * timed await returns after countDown to zero
81       */
82      public void testTimedAwait() {
83          final CountDownLatch l = new CountDownLatch(2);
84 +        final CountDownLatch pleaseCountDown = new CountDownLatch(1);
85  
86 <        Thread t = new Thread(new Runnable() {
87 <                public void run() {
88 <                    try {
89 <                        threadAssertTrue(l.getCount() > 0);
90 <                        threadAssertTrue(l.await(SMALL_DELAY_MS, MILLISECONDS));
91 <                    } catch (InterruptedException e) {
92 <                        threadUnexpectedException();
93 <                    }
94 <                }
95 <            });
96 <        t.start();
97 <        try {
98 <            assertEquals(l.getCount(), 2);
99 <            Thread.sleep(SHORT_DELAY_MS);
100 <            l.countDown();
101 <            assertEquals(l.getCount(), 1);
108 <            l.countDown();
109 <            assertEquals(l.getCount(), 0);
110 <            t.join();
111 <        } catch (InterruptedException e) {
112 <            unexpectedException();
113 <        }
86 >        Thread t = newStartedThread(new CheckedRunnable() {
87 >            public void realRun() throws InterruptedException {
88 >                assertEquals(2, l.getCount());
89 >                pleaseCountDown.countDown();
90 >                assertTrue(l.await(LONG_DELAY_MS, MILLISECONDS));
91 >                assertEquals(0, l.getCount());
92 >            }});
93 >
94 >        await(pleaseCountDown);
95 >        assertEquals(2, l.getCount());
96 >        l.countDown();
97 >        assertEquals(1, l.getCount());
98 >        assertThreadStaysAlive(t);
99 >        l.countDown();
100 >        assertEquals(0, l.getCount());
101 >        awaitTermination(t);
102      }
103  
104      /**
105       * await throws IE if interrupted before counted down
106       */
107 <    public void testAwait_InterruptedException() {
107 >    public void testAwait_Interruptible() {
108          final CountDownLatch l = new CountDownLatch(1);
109 <        Thread t = new Thread(new Runnable() {
110 <                public void run() {
111 <                    try {
112 <                        threadAssertTrue(l.getCount() > 0);
113 <                        l.await();
114 <                        threadShouldThrow();
115 <                    } catch (InterruptedException success) {}
116 <                }
117 <            });
118 <        t.start();
119 <        try {
120 <            assertEquals(l.getCount(), 1);
121 <            t.interrupt();
122 <            t.join();
123 <        } catch (InterruptedException e) {
124 <            unexpectedException();
125 <        }
109 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
110 >        Thread t = newStartedThread(new CheckedRunnable() {
111 >            public void realRun() throws InterruptedException {
112 >                Thread.currentThread().interrupt();
113 >                try {
114 >                    l.await();
115 >                    shouldThrow();
116 >                } catch (InterruptedException success) {}
117 >                assertFalse(Thread.interrupted());
118 >
119 >                pleaseInterrupt.countDown();
120 >                try {
121 >                    l.await();
122 >                    shouldThrow();
123 >                } catch (InterruptedException success) {}
124 >                assertFalse(Thread.interrupted());
125 >
126 >                assertEquals(1, l.getCount());
127 >            }});
128 >
129 >        await(pleaseInterrupt);
130 >        assertThreadStaysAlive(t);
131 >        t.interrupt();
132 >        awaitTermination(t);
133      }
134  
135      /**
136       * timed await throws IE if interrupted before counted down
137       */
138 <    public void testTimedAwait_InterruptedException() {
138 >    public void testTimedAwait_Interruptible() {
139          final CountDownLatch l = new CountDownLatch(1);
140 <        Thread t = new Thread(new Runnable() {
141 <                public void run() {
142 <                    try {
143 <                        threadAssertTrue(l.getCount() > 0);
144 <                        l.await(MEDIUM_DELAY_MS, MILLISECONDS);
145 <                        threadShouldThrow();
146 <                    } catch (InterruptedException success) {}
147 <                }
148 <            });
149 <        t.start();
150 <        try {
151 <            Thread.sleep(SHORT_DELAY_MS);
152 <            assertEquals(l.getCount(), 1);
153 <            t.interrupt();
154 <            t.join();
155 <        } catch (InterruptedException e) {
156 <            unexpectedException();
157 <        }
140 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
141 >        Thread t = newStartedThread(new CheckedRunnable() {
142 >            public void realRun() throws InterruptedException {
143 >                Thread.currentThread().interrupt();
144 >                try {
145 >                    l.await(LONG_DELAY_MS, MILLISECONDS);
146 >                    shouldThrow();
147 >                } catch (InterruptedException success) {}
148 >                assertFalse(Thread.interrupted());
149 >
150 >                pleaseInterrupt.countDown();
151 >                try {
152 >                    l.await(LONG_DELAY_MS, MILLISECONDS);
153 >                    shouldThrow();
154 >                } catch (InterruptedException success) {}
155 >                assertFalse(Thread.interrupted());
156 >
157 >                assertEquals(1, l.getCount());
158 >            }});
159 >
160 >        await(pleaseInterrupt);
161 >        assertThreadStaysAlive(t);
162 >        t.interrupt();
163 >        awaitTermination(t);
164      }
165  
166      /**
167       * timed await times out if not counted down before timeout
168       */
169 <    public void testAwaitTimeout() {
169 >    public void testAwaitTimeout() throws InterruptedException {
170          final CountDownLatch l = new CountDownLatch(1);
171 <        Thread t = new Thread(new Runnable() {
172 <                public void run() {
173 <                    try {
174 <                        threadAssertTrue(l.getCount() > 0);
175 <                        threadAssertFalse(l.await(SHORT_DELAY_MS, MILLISECONDS));
176 <                        threadAssertTrue(l.getCount() > 0);
177 <                    } catch (InterruptedException ie) {
178 <                        threadUnexpectedException();
179 <                    }
179 <                }
180 <            });
181 <        t.start();
182 <        try {
183 <            assertEquals(l.getCount(), 1);
184 <            t.join();
185 <        } catch (InterruptedException e) {
186 <            unexpectedException();
187 <        }
171 >        Thread t = newStartedThread(new CheckedRunnable() {
172 >            public void realRun() throws InterruptedException {
173 >                assertEquals(1, l.getCount());
174 >                assertFalse(l.await(timeoutMillis(), MILLISECONDS));
175 >                assertEquals(1, l.getCount());
176 >            }});
177 >
178 >        awaitTermination(t);
179 >        assertEquals(1, l.getCount());
180      }
181  
182      /**
# Line 192 | Line 184 | public class CountDownLatchTest extends
184       */
185      public void testToString() {
186          CountDownLatch s = new CountDownLatch(2);
187 <        String us = s.toString();
196 <        assertTrue(us.indexOf("Count = 2") >= 0);
187 >        assertTrue(s.toString().contains("Count = 2"));
188          s.countDown();
189 <        String s1 = s.toString();
199 <        assertTrue(s1.indexOf("Count = 1") >= 0);
189 >        assertTrue(s.toString().contains("Count = 1"));
190          s.countDown();
191 <        String s2 = s.toString();
202 <        assertTrue(s2.indexOf("Count = 0") >= 0);
191 >        assertTrue(s.toString().contains("Count = 0"));
192      }
193  
194   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines