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.1 by dl, Sun Aug 31 19:24:54 2003 UTC vs.
Revision 1.29 by jsr166, Thu Sep 5 21:33:55 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.*;
9 < import java.util.*;
10 < import java.util.concurrent.*;
9 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
10  
11 < public class CountDownLatchTest extends TestCase{
12 <    
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      }
17    
18
21      public static Test suite() {
22 <        return new TestSuite(CountDownLatchTest.class);
21 <    }
22 <
23 <    private static long SHORT_DELAY_MS = 100;
24 <    private static long MEDIUM_DELAY_MS = 1000;
25 <    private static long LONG_DELAY_MS = 10000;
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 testAwait1(){
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 <                        fail("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 <        }
22 >        return new TestSuite(CountDownLatchTest.class);
23      }
59    
24  
25 <
26 <    public void testConstructor(){
27 <        try{
25 >    /**
26 >     * negative constructor argument throws IllegalArgumentException
27 >     */
28 >    public void testConstructor() {
29 >        try {
30              new CountDownLatch(-1);
31 <            fail("should throw IllegalArgumentException");
32 <        }catch(IllegalArgumentException success){}
31 >            shouldThrow();
32 >        } catch (IllegalArgumentException success) {}
33      }
34  
35 <    public void testAwait1_InterruptedException(){
35 >    /**
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());
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 <        Thread t = new Thread(new Runnable(){
51 <                public void run(){
52 <                    try{
53 <                        l.await();
54 <                        fail("should throw");
55 <                    }catch(InterruptedException success){}
56 <                }
57 <            });
58 <        t.start();
59 <        try{
60 <            assertEquals(l.getCount(), 1);
61 <            t.interrupt();
62 <            t.join();
63 <        }catch (InterruptedException e){
64 <            fail("unexpected exception");
65 <        }
66 <    }
67 <
68 <    public void testAwait2_InterruptedException(){
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 >     * await returns after countDown to zero, but not before
59 >     */
60 >    public void testAwait() {
61 >        final CountDownLatch l = new CountDownLatch(2);
62 >        final CountDownLatch pleaseCountDown = new CountDownLatch(1);
63 >
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 >    }
81 >
82 >    /**
83 >     * timed await returns after countDown to zero
84 >     */
85 >    public void testTimedAwait() {
86 >        final CountDownLatch l = new CountDownLatch(2);
87 >        final CountDownLatch pleaseCountDown = new CountDownLatch(1);
88 >
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 >    }
106 >
107 >    /**
108 >     * await throws InterruptedException if interrupted before counted down
109 >     */
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(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
116 <                        fail("should throw");                        
117 <                    }catch(InterruptedException success){}
118 <                }
119 <            });
120 <        t.start();
121 <        try{
122 <            Thread.sleep(SHORT_DELAY_MS);
123 <            assertEquals(l.getCount(), 1);
124 <            t.interrupt();
125 <            t.join();
126 <        }catch (InterruptedException e){
127 <            fail("unexpected exception");
128 <        }
129 <    }
130 <
131 <    public void testAwaitTimeout(){
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 >                Thread.currentThread().interrupt();
148 >                try {
149 >                    l.await(randomTimeout(), randomTimeUnit());
150 >                    shouldThrow();
151 >                } catch (InterruptedException success) {}
152 >                assertFalse(Thread.interrupted());
153 >
154 >                pleaseInterrupt.countDown();
155 >                try {
156 >                    l.await(LONGER_DELAY_MS, MILLISECONDS);
157 >                    shouldThrow();
158 >                } catch (InterruptedException success) {}
159 >                assertFalse(Thread.interrupted());
160 >
161 >                assertEquals(initialCount, l.getCount());
162 >            }});
163 >
164 >        await(pleaseInterrupt);
165 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
166 >        t.interrupt();
167 >        awaitTermination(t);
168 >    }
169 >
170 >    /**
171 >     * timed await times out if not counted down before timeout
172 >     */
173 >    public void testAwaitTimeout() throws InterruptedException {
174          final CountDownLatch l = new CountDownLatch(1);
175 <        Thread t = new Thread(new Runnable(){
176 <                public void run(){
177 <                    try{
178 <                        assertFalse(l.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
179 <                    }catch(InterruptedException ie){
180 <                        fail("unexpected exception");
181 <                    }
182 <                }
183 <            });
184 <        t.start();
185 <        try{
186 <            assertEquals(l.getCount(), 1);
187 <            t.join();
188 <        }catch (InterruptedException e){
189 <            fail("unexpected exception");
190 <        }
175 >        Thread t = newStartedThread(new CheckedRunnable() {
176 >            public void realRun() throws InterruptedException {
177 >                assertEquals(1, l.getCount());
178 >
179 >                long startTime = System.nanoTime();
180 >                assertFalse(l.await(timeoutMillis(), MILLISECONDS));
181 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
182 >
183 >                assertEquals(1, l.getCount());
184 >            }});
185 >
186 >        awaitTermination(t);
187 >        assertEquals(1, l.getCount());
188 >    }
189 >
190 >    /**
191 >     * toString indicates current count
192 >     */
193 >    public void testToString() {
194 >        CountDownLatch s = new CountDownLatch(2);
195 >        assertTrue(s.toString().contains("Count = 2"));
196 >        s.countDown();
197 >        assertTrue(s.toString().contains("Count = 1"));
198 >        s.countDown();
199 >        assertTrue(s.toString().contains("Count = 0"));
200      }
201  
202   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines