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.2 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.24 by jsr166, Sat May 13 23:50:00 2017 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.*;
9 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 >
11 > import java.util.concurrent.CountDownLatch;
12 >
13 > import junit.framework.Test;
14 > import junit.framework.TestSuite;
15  
16   public class CountDownLatchTest extends JSR166TestCase {
17      public static void main(String[] args) {
18 <        junit.textui.TestRunner.run (suite());  
18 >        main(suite(), args);
19      }
20      public static Test suite() {
21 <        return new TestSuite(CountDownLatchTest.class);
21 >        return new TestSuite(CountDownLatchTest.class);
22      }
23  
24 <    public void testConstructor(){
24 >    /**
25 >     * negative constructor argument throws IAE
26 >     */
27 >    public void testConstructor() {
28          try {
29              new CountDownLatch(-1);
30 <            fail("should throw IllegalArgumentException");
31 <        } catch(IllegalArgumentException success){}
25 <    }
26 <
27 <    public void testGetCount(){
28 <        final CountDownLatch l = new CountDownLatch(2);
29 <        assertEquals(2, l.getCount());
30 <        l.countDown();
31 <        assertEquals(1, l.getCount());
32 <    }
33 <
34 <    public void testAwait(){
35 <        final CountDownLatch l = new CountDownLatch(2);
36 <
37 <        Thread t = new Thread(new Runnable(){
38 <                public void run(){
39 <                    try {
40 <                        l.await();
41 <                    } catch(InterruptedException e){
42 <                        threadFail("unexpected exception");
43 <                    }
44 <                }
45 <            });
46 <        t.start();
47 <        try {
48 <            assertEquals(l.getCount(), 2);
49 <            Thread.sleep(SHORT_DELAY_MS);
50 <            l.countDown();
51 <            assertEquals(l.getCount(), 1);
52 <            l.countDown();
53 <            assertEquals(l.getCount(), 0);
54 <            t.join();
55 <        } catch (InterruptedException e){
56 <            fail("unexpected exception");
57 <        }
58 <    }
59 <    
60 <
61 <    public void testTimedAwait(){
62 <        final CountDownLatch l = new CountDownLatch(2);
63 <
64 <        Thread t = new Thread(new Runnable(){
65 <                public void run(){
66 <                    try {
67 <                        threadAssertTrue(l.await(SMALL_DELAY_MS, TimeUnit.MILLISECONDS));
68 <                    } catch(InterruptedException e){
69 <                        threadFail("unexpected exception");
70 <                    }
71 <                }
72 <            });
73 <        t.start();
74 <        try {
75 <            assertEquals(l.getCount(), 2);
76 <            Thread.sleep(SHORT_DELAY_MS);
77 <            l.countDown();
78 <            assertEquals(l.getCount(), 1);
79 <            l.countDown();
80 <            assertEquals(l.getCount(), 0);
81 <            t.join();
82 <        } catch (InterruptedException e){
83 <            fail("unexpected exception");
84 <        }
30 >            shouldThrow();
31 >        } catch (IllegalArgumentException success) {}
32      }
86    
33  
34 <
35 <
36 <    public void testAwait_InterruptedException(){
34 >    /**
35 >     * getCount returns initial count and decreases after countDown
36 >     */
37 >    public void testGetCount() {
38 >        final CountDownLatch l = new CountDownLatch(2);
39 >        assertEquals(2, l.getCount());
40 >        l.countDown();
41 >        assertEquals(1, l.getCount());
42 >    }
43 >
44 >    /**
45 >     * countDown decrements count when positive and has no effect when zero
46 >     */
47 >    public void testCountDown() {
48          final CountDownLatch l = new CountDownLatch(1);
49 <        Thread t = new Thread(new Runnable(){
50 <                public void run(){
51 <                    try {
52 <                        l.await();
53 <                        threadFail("should throw");
54 <                    } catch(InterruptedException success){}
55 <                }
56 <            });
57 <        t.start();
58 <        try {
59 <            assertEquals(l.getCount(), 1);
60 <            t.interrupt();
61 <            t.join();
62 <        } catch (InterruptedException e){
63 <            fail("unexpected exception");
64 <        }
65 <    }
66 <
67 <    public void testTimedAwait_InterruptedException(){
49 >        assertEquals(1, l.getCount());
50 >        l.countDown();
51 >        assertEquals(0, l.getCount());
52 >        l.countDown();
53 >        assertEquals(0, l.getCount());
54 >    }
55 >
56 >    /**
57 >     * await returns after countDown to zero, but not before
58 >     */
59 >    public void testAwait() {
60 >        final CountDownLatch l = new CountDownLatch(2);
61 >        final CountDownLatch pleaseCountDown = new CountDownLatch(1);
62 >
63 >        Thread t = newStartedThread(new CheckedRunnable() {
64 >            public void realRun() throws InterruptedException {
65 >                assertEquals(2, l.getCount());
66 >                pleaseCountDown.countDown();
67 >                l.await();
68 >                assertEquals(0, l.getCount());
69 >            }});
70 >
71 >        await(pleaseCountDown);
72 >        assertEquals(2, l.getCount());
73 >        l.countDown();
74 >        assertEquals(1, l.getCount());
75 >        assertThreadBlocks(t, Thread.State.WAITING);
76 >        l.countDown();
77 >        assertEquals(0, l.getCount());
78 >        awaitTermination(t);
79 >    }
80 >
81 >    /**
82 >     * timed await returns after countDown to zero
83 >     */
84 >    public void testTimedAwait() {
85 >        final CountDownLatch l = new CountDownLatch(2);
86 >        final CountDownLatch pleaseCountDown = new CountDownLatch(1);
87 >
88 >        Thread t = newStartedThread(new CheckedRunnable() {
89 >            public void realRun() throws InterruptedException {
90 >                assertEquals(2, l.getCount());
91 >                pleaseCountDown.countDown();
92 >                assertTrue(l.await(LONG_DELAY_MS, MILLISECONDS));
93 >                assertEquals(0, l.getCount());
94 >            }});
95 >
96 >        await(pleaseCountDown);
97 >        assertEquals(2, l.getCount());
98 >        l.countDown();
99 >        assertEquals(1, l.getCount());
100 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
101 >        l.countDown();
102 >        assertEquals(0, l.getCount());
103 >        awaitTermination(t);
104 >    }
105 >
106 >    /**
107 >     * await throws IE if interrupted before counted down
108 >     */
109 >    public void testAwait_Interruptible() {
110          final CountDownLatch l = new CountDownLatch(1);
111 <        Thread t = new Thread(new Runnable(){
112 <                public void run(){
113 <                    try {
114 <                        l.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
115 <                        threadFail("should throw");                        
116 <                    } catch(InterruptedException success){}
117 <                }
118 <            });
119 <        t.start();
120 <        try {
121 <            Thread.sleep(SHORT_DELAY_MS);
122 <            assertEquals(l.getCount(), 1);
123 <            t.interrupt();
124 <            t.join();
125 <        } catch (InterruptedException e){
126 <            fail("unexpected exception");
127 <        }
128 <    }
129 <
130 <    public void testAwaitTimeout(){
111 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
112 >        Thread t = newStartedThread(new CheckedRunnable() {
113 >            public void realRun() throws InterruptedException {
114 >                Thread.currentThread().interrupt();
115 >                try {
116 >                    l.await();
117 >                    shouldThrow();
118 >                } catch (InterruptedException success) {}
119 >                assertFalse(Thread.interrupted());
120 >
121 >                pleaseInterrupt.countDown();
122 >                try {
123 >                    l.await();
124 >                    shouldThrow();
125 >                } catch (InterruptedException success) {}
126 >                assertFalse(Thread.interrupted());
127 >
128 >                assertEquals(1, l.getCount());
129 >            }});
130 >
131 >        await(pleaseInterrupt);
132 >        assertThreadBlocks(t, Thread.State.WAITING);
133 >        t.interrupt();
134 >        awaitTermination(t);
135 >    }
136 >
137 >    /**
138 >     * timed await throws IE if interrupted before counted down
139 >     */
140 >    public void testTimedAwait_Interruptible() {
141          final CountDownLatch l = new CountDownLatch(1);
142 <        Thread t = new Thread(new Runnable(){
143 <                public void run(){
144 <                    try {
145 <                        threadAssertFalse(l.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
146 <                    } catch(InterruptedException ie){
147 <                        threadFail("unexpected exception");
148 <                    }
149 <                }
150 <            });
151 <        t.start();
152 <        try {
153 <            assertEquals(l.getCount(), 1);
154 <            t.join();
155 <        } catch (InterruptedException e){
156 <            fail("unexpected exception");
157 <        }
142 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
143 >        Thread t = newStartedThread(new CheckedRunnable() {
144 >            public void realRun() throws InterruptedException {
145 >                Thread.currentThread().interrupt();
146 >                try {
147 >                    l.await(LONG_DELAY_MS, MILLISECONDS);
148 >                    shouldThrow();
149 >                } catch (InterruptedException success) {}
150 >                assertFalse(Thread.interrupted());
151 >
152 >                pleaseInterrupt.countDown();
153 >                try {
154 >                    l.await(LONG_DELAY_MS, MILLISECONDS);
155 >                    shouldThrow();
156 >                } catch (InterruptedException success) {}
157 >                assertFalse(Thread.interrupted());
158 >
159 >                assertEquals(1, l.getCount());
160 >            }});
161 >
162 >        await(pleaseInterrupt);
163 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
164 >        t.interrupt();
165 >        awaitTermination(t);
166 >    }
167 >
168 >    /**
169 >     * timed await times out if not counted down before timeout
170 >     */
171 >    public void testAwaitTimeout() throws InterruptedException {
172 >        final CountDownLatch l = new CountDownLatch(1);
173 >        Thread t = newStartedThread(new CheckedRunnable() {
174 >            public void realRun() throws InterruptedException {
175 >                assertEquals(1, l.getCount());
176 >                assertFalse(l.await(timeoutMillis(), MILLISECONDS));
177 >                assertEquals(1, l.getCount());
178 >            }});
179 >
180 >        awaitTermination(t);
181 >        assertEquals(1, l.getCount());
182 >    }
183 >
184 >    /**
185 >     * toString indicates current count
186 >     */
187 >    public void testToString() {
188 >        CountDownLatch s = new CountDownLatch(2);
189 >        assertTrue(s.toString().contains("Count = 2"));
190 >        s.countDown();
191 >        assertTrue(s.toString().contains("Count = 1"));
192 >        s.countDown();
193 >        assertTrue(s.toString().contains("Count = 0"));
194      }
195  
196   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines