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.3 by dl, Sat Sep 20 18:20:07 2003 UTC vs.
Revision 1.20 by jsr166, Sat May 28 12:37:00 2011 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
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/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.*;
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);
19 >        return new TestSuite(CountDownLatchTest.class);
20      }
21  
22      /**
23 <     *
23 >     * negative constructor argument throws IAE
24       */
25      public void testConstructor() {
26          try {
27              new CountDownLatch(-1);
28              shouldThrow();
29 <        } catch(IllegalArgumentException success){}
29 >        } catch (IllegalArgumentException success) {}
30      }
31  
32      /**
33 <     *
33 >     * getCount returns initial count and decreases after countDown
34       */
35      public void testGetCount() {
36 <        final CountDownLatch l = new CountDownLatch(2);
37 <        assertEquals(2, l.getCount());
38 <        l.countDown();
39 <        assertEquals(1, l.getCount());
36 >        final CountDownLatch l = new CountDownLatch(2);
37 >        assertEquals(2, l.getCount());
38 >        l.countDown();
39 >        assertEquals(1, l.getCount());
40      }
41  
42      /**
43 <     *
43 >     * countDown decrements count when positive and has no effect when zero
44 >     */
45 >    public void testCountDown() {
46 >        final CountDownLatch l = new CountDownLatch(1);
47 >        assertEquals(1, l.getCount());
48 >        l.countDown();
49 >        assertEquals(0, l.getCount());
50 >        l.countDown();
51 >        assertEquals(0, l.getCount());
52 >    }
53 >
54 >    /**
55 >     * await returns after countDown to zero, but not before
56       */
57      public void testAwait() {
58 <        final CountDownLatch l = new CountDownLatch(2);
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 <                        l.await();
65 <                    } catch(InterruptedException e){
66 <                        threadUnexpectedException();
67 <                    }
68 <                }
69 <            });
70 <        t.start();
71 <        try {
72 <            assertEquals(l.getCount(), 2);
73 <            Thread.sleep(SHORT_DELAY_MS);
74 <            l.countDown();
75 <            assertEquals(l.getCount(), 1);
76 <            l.countDown();
62 <            assertEquals(l.getCount(), 0);
63 <            t.join();
64 <        } catch (InterruptedException e){
65 <            unexpectedException();
66 <        }
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      }
68    
78  
79      /**
80 <     *
80 >     * timed await returns after countDown to zero
81       */
82      public void testTimedAwait() {
83 <        final CountDownLatch l = new CountDownLatch(2);
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.await(SMALL_DELAY_MS, TimeUnit.MILLISECONDS));
90 <                    } catch(InterruptedException e){
91 <                        threadUnexpectedException();
92 <                    }
93 <                }
94 <            });
95 <        t.start();
96 <        try {
97 <            assertEquals(l.getCount(), 2);
98 <            Thread.sleep(SHORT_DELAY_MS);
99 <            l.countDown();
100 <            assertEquals(l.getCount(), 1);
101 <            l.countDown();
92 <            assertEquals(l.getCount(), 0);
93 <            t.join();
94 <        } catch (InterruptedException e){
95 <            unexpectedException();
96 <        }
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      }
98    
99
100
103  
104      /**
105 <     *
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 <                        l.await();
113 <                        threadShouldThrow();
114 <                    } catch(InterruptedException success){}
115 <                }
116 <            });
117 <        t.start();
118 <        try {
119 <            assertEquals(l.getCount(), 1);
120 <            t.interrupt();
121 <            t.join();
122 <        } catch (InterruptedException e){
123 <            unexpectedException();
124 <        }
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 <     *
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 <                        l.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
144 <                        threadShouldThrow();                        
145 <                    } catch(InterruptedException success){}
146 <                }
147 <            });
148 <        t.start();
149 <        try {
150 <            Thread.sleep(SHORT_DELAY_MS);
151 <            assertEquals(l.getCount(), 1);
152 <            t.interrupt();
153 <            t.join();
154 <        } catch (InterruptedException e){
155 <            unexpectedException();
156 <        }
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 <     *
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 <                        threadAssertFalse(l.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
175 <                    } catch(InterruptedException ie){
176 <                        threadUnexpectedException();
177 <                    }
178 <                }
179 <            });
180 <        t.start();
181 <        try {
182 <            assertEquals(l.getCount(), 1);
183 <            t.join();
184 <        } catch (InterruptedException e){
185 <            unexpectedException();
186 <        }
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 >    /**
183 >     * toString indicates current count
184 >     */
185 >    public void testToString() {
186 >        CountDownLatch s = new CountDownLatch(2);
187 >        assertTrue(s.toString().contains("Count = 2"));
188 >        s.countDown();
189 >        assertTrue(s.toString().contains("Count = 1"));
190 >        s.countDown();
191 >        assertTrue(s.toString().contains("Count = 0"));
192      }
193  
194   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines