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.13 by jsr166, Sat Nov 21 05:05:38 2009 UTC

# Line 54 | Line 54 | public class CountDownLatchTest extends
54      /**
55       * await returns after countDown to zero, but not before
56       */
57 <    public void testAwait() {
57 >    public void testAwait() throws InterruptedException {
58          final CountDownLatch l = new CountDownLatch(2);
59  
60 <        Thread t = new Thread(new Runnable() {
61 <                public void run() {
62 <                    try {
63 <                        threadAssertTrue(l.getCount() > 0);
64 <                        l.await();
65 <                        threadAssertTrue(l.getCount() == 0);
66 <                    } catch (InterruptedException e) {
67 <                        threadUnexpectedException();
68 <                    }
69 <                }
70 <            });
60 >        Thread t = new Thread(new CheckedRunnable() {
61 >            public void realRun() throws InterruptedException {
62 >                threadAssertTrue(l.getCount() > 0);
63 >                l.await();
64 >                threadAssertTrue(l.getCount() == 0);
65 >            }});
66 >
67          t.start();
68 <        try {
69 <            assertEquals(l.getCount(), 2);
70 <            Thread.sleep(SHORT_DELAY_MS);
71 <            l.countDown();
72 <            assertEquals(l.getCount(), 1);
73 <            l.countDown();
74 <            assertEquals(l.getCount(), 0);
79 <            t.join();
80 <        } catch (InterruptedException e) {
81 <            unexpectedException();
82 <        }
68 >        assertEquals(l.getCount(), 2);
69 >        Thread.sleep(SHORT_DELAY_MS);
70 >        l.countDown();
71 >        assertEquals(l.getCount(), 1);
72 >        l.countDown();
73 >        assertEquals(l.getCount(), 0);
74 >        t.join();
75      }
76  
77  
78      /**
79       * timed await returns after countDown to zero
80       */
81 <    public void testTimedAwait() {
81 >    public void testTimedAwait() throws InterruptedException {
82          final CountDownLatch l = new CountDownLatch(2);
83  
84 <        Thread t = new Thread(new Runnable() {
85 <                public void run() {
86 <                    try {
87 <                        threadAssertTrue(l.getCount() > 0);
88 <                        threadAssertTrue(l.await(SMALL_DELAY_MS, MILLISECONDS));
89 <                    } catch (InterruptedException e) {
98 <                        threadUnexpectedException();
99 <                    }
100 <                }
101 <            });
84 >        Thread t = new Thread(new CheckedRunnable() {
85 >            public void realRun() throws InterruptedException {
86 >                threadAssertTrue(l.getCount() > 0);
87 >                threadAssertTrue(l.await(SMALL_DELAY_MS, MILLISECONDS));
88 >            }});
89 >
90          t.start();
91 <        try {
92 <            assertEquals(l.getCount(), 2);
93 <            Thread.sleep(SHORT_DELAY_MS);
94 <            l.countDown();
95 <            assertEquals(l.getCount(), 1);
96 <            l.countDown();
97 <            assertEquals(l.getCount(), 0);
110 <            t.join();
111 <        } catch (InterruptedException e) {
112 <            unexpectedException();
113 <        }
91 >        assertEquals(l.getCount(), 2);
92 >        Thread.sleep(SHORT_DELAY_MS);
93 >        l.countDown();
94 >        assertEquals(l.getCount(), 1);
95 >        l.countDown();
96 >        assertEquals(l.getCount(), 0);
97 >        t.join();
98      }
99  
100      /**
101       * await throws IE if interrupted before counted down
102       */
103 <    public void testAwait_InterruptedException() {
103 >    public void testAwait_InterruptedException() throws InterruptedException {
104          final CountDownLatch l = new CountDownLatch(1);
105 <        Thread t = new Thread(new Runnable() {
106 <                public void run() {
107 <                    try {
108 <                        threadAssertTrue(l.getCount() > 0);
109 <                        l.await();
110 <                        threadShouldThrow();
127 <                    } catch (InterruptedException success) {}
128 <                }
129 <            });
105 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
106 >            public void realRun() throws InterruptedException {
107 >                threadAssertTrue(l.getCount() > 0);
108 >                l.await();
109 >            }});
110 >
111          t.start();
112 <        try {
113 <            assertEquals(l.getCount(), 1);
114 <            t.interrupt();
134 <            t.join();
135 <        } catch (InterruptedException e) {
136 <            unexpectedException();
137 <        }
112 >        assertEquals(l.getCount(), 1);
113 >        t.interrupt();
114 >        t.join();
115      }
116  
117      /**
118       * timed await throws IE if interrupted before counted down
119       */
120 <    public void testTimedAwait_InterruptedException() {
120 >    public void testTimedAwait_InterruptedException() throws InterruptedException {
121          final CountDownLatch l = new CountDownLatch(1);
122 <        Thread t = new Thread(new Runnable() {
123 <                public void run() {
124 <                    try {
125 <                        threadAssertTrue(l.getCount() > 0);
126 <                        l.await(MEDIUM_DELAY_MS, MILLISECONDS);
127 <                        threadShouldThrow();
151 <                    } catch (InterruptedException success) {}
152 <                }
153 <            });
122 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
123 >            public void realRun() throws InterruptedException {
124 >                threadAssertTrue(l.getCount() > 0);
125 >                l.await(MEDIUM_DELAY_MS, MILLISECONDS);
126 >            }});
127 >
128          t.start();
129 <        try {
130 <            Thread.sleep(SHORT_DELAY_MS);
131 <            assertEquals(l.getCount(), 1);
132 <            t.interrupt();
159 <            t.join();
160 <        } catch (InterruptedException e) {
161 <            unexpectedException();
162 <        }
129 >        Thread.sleep(SHORT_DELAY_MS);
130 >        assertEquals(l.getCount(), 1);
131 >        t.interrupt();
132 >        t.join();
133      }
134  
135      /**
136       * timed await times out if not counted down before timeout
137       */
138 <    public void testAwaitTimeout() {
138 >    public void testAwaitTimeout() throws InterruptedException {
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 <                        threadAssertFalse(l.await(SHORT_DELAY_MS, MILLISECONDS));
145 <                        threadAssertTrue(l.getCount() > 0);
146 <                    } catch (InterruptedException ie) {
177 <                        threadUnexpectedException();
178 <                    }
179 <                }
180 <            });
140 >        Thread t = new Thread(new CheckedRunnable() {
141 >            public void realRun() throws InterruptedException {
142 >                threadAssertTrue(l.getCount() > 0);
143 >                threadAssertFalse(l.await(SHORT_DELAY_MS, MILLISECONDS));
144 >                threadAssertTrue(l.getCount() > 0);
145 >            }});
146 >
147          t.start();
148 <        try {
149 <            assertEquals(l.getCount(), 1);
184 <            t.join();
185 <        } catch (InterruptedException e) {
186 <            unexpectedException();
187 <        }
148 >        assertEquals(l.getCount(), 1);
149 >        t.join();
150      }
151  
152      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines