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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines