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/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
|
11 |
|
import java.util.concurrent.*; |
12 |
|
import java.io.*; |
13 |
|
|
14 |
< |
public class TimeUnitTest extends TestCase { |
14 |
< |
static public boolean DEBUG = false; |
15 |
< |
|
16 |
< |
|
14 |
> |
public class TimeUnitTest extends JSR166TestCase { |
15 |
|
public static void main(String[] args) { |
16 |
|
junit.textui.TestRunner.run(suite()); |
17 |
|
} |
20 |
|
return new TestSuite(TimeUnitTest.class); |
21 |
|
} |
22 |
|
|
23 |
+ |
/** |
24 |
+ |
* convert correctly converts sample values across the four units |
25 |
+ |
*/ |
26 |
|
public void testConvert() { |
27 |
|
for (long t = 0; t < 10; ++t) { |
28 |
|
assertEquals(t, |
76 |
|
} |
77 |
|
} |
78 |
|
|
79 |
+ |
/** |
80 |
+ |
* toNanos correctly converts sample values in different units to |
81 |
+ |
* nanoseconds |
82 |
+ |
*/ |
83 |
|
public void testToNanos() { |
84 |
|
for (long t = 0; t < 10; ++t) { |
85 |
|
assertEquals(1000000000 * t, |
90 |
|
assertEquals(1000 * t, |
91 |
|
TimeUnit.MICROSECONDS.toNanos(t)); |
92 |
|
assertEquals(t, |
93 |
< |
TimeUnit.SECONDS.NANOSECONDS.toNanos(t)); |
93 |
> |
TimeUnit.NANOSECONDS.toNanos(t)); |
94 |
|
} |
95 |
|
} |
96 |
|
|
97 |
+ |
/** |
98 |
+ |
* toMicros correctly converts sample values in different units to |
99 |
+ |
* microseconds |
100 |
+ |
*/ |
101 |
+ |
public void testToMicros() { |
102 |
+ |
for (long t = 0; t < 10; ++t) { |
103 |
+ |
assertEquals(1000000 * t, |
104 |
+ |
TimeUnit.SECONDS.toMicros(t)); |
105 |
|
|
106 |
+ |
assertEquals(1000 * t, |
107 |
+ |
TimeUnit.MILLISECONDS.toMicros(t)); |
108 |
+ |
assertEquals(t, |
109 |
+ |
TimeUnit.MICROSECONDS.toMicros(t)); |
110 |
+ |
assertEquals(t, |
111 |
+ |
TimeUnit.NANOSECONDS.toMicros(t * 1000)); |
112 |
+ |
} |
113 |
+ |
} |
114 |
+ |
|
115 |
+ |
/** |
116 |
+ |
* toMillis correctly converts sample values in different units to |
117 |
+ |
* milliseconds |
118 |
+ |
*/ |
119 |
+ |
public void testToMillis() { |
120 |
+ |
for (long t = 0; t < 10; ++t) { |
121 |
+ |
assertEquals(1000 * t, |
122 |
+ |
TimeUnit.SECONDS.toMillis(t)); |
123 |
+ |
|
124 |
+ |
assertEquals(t, |
125 |
+ |
TimeUnit.MILLISECONDS.toMillis(t)); |
126 |
+ |
assertEquals(t, |
127 |
+ |
TimeUnit.MICROSECONDS.toMillis(t * 1000)); |
128 |
+ |
assertEquals(t, |
129 |
+ |
TimeUnit.NANOSECONDS.toMillis(t * 1000000)); |
130 |
+ |
} |
131 |
+ |
} |
132 |
+ |
|
133 |
+ |
/** |
134 |
+ |
* toSeconds correctly converts sample values in different units to |
135 |
+ |
* seconds |
136 |
+ |
*/ |
137 |
+ |
public void testToSeconds() { |
138 |
+ |
for (long t = 0; t < 10; ++t) { |
139 |
+ |
assertEquals(t, |
140 |
+ |
TimeUnit.SECONDS.toSeconds(t)); |
141 |
+ |
|
142 |
+ |
assertEquals(t, |
143 |
+ |
TimeUnit.MILLISECONDS.toSeconds(t * 1000)); |
144 |
+ |
assertEquals(t, |
145 |
+ |
TimeUnit.MICROSECONDS.toSeconds(t * 1000000)); |
146 |
+ |
assertEquals(t, |
147 |
+ |
TimeUnit.NANOSECONDS.toSeconds(t * 1000000000)); |
148 |
+ |
} |
149 |
+ |
} |
150 |
+ |
|
151 |
+ |
|
152 |
+ |
/** |
153 |
+ |
* convert saturates positive too-large values to Long.MAX_VALUE |
154 |
+ |
* and negative to LONG.MIN_VALUE |
155 |
+ |
*/ |
156 |
|
public void testConvertSaturate() { |
157 |
|
assertEquals(Long.MAX_VALUE, |
158 |
|
TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2, |
160 |
|
assertEquals(Long.MIN_VALUE, |
161 |
|
TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4, |
162 |
|
TimeUnit.SECONDS)); |
100 |
– |
|
163 |
|
} |
164 |
|
|
165 |
+ |
/** |
166 |
+ |
* toNanos saturates positive too-large values to Long.MAX_VALUE |
167 |
+ |
* and negative to LONG.MIN_VALUE |
168 |
+ |
*/ |
169 |
|
public void testToNanosSaturate() { |
170 |
|
assertEquals(Long.MAX_VALUE, |
171 |
|
TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2)); |
172 |
|
assertEquals(Long.MIN_VALUE, |
173 |
|
TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3)); |
108 |
– |
|
174 |
|
} |
175 |
|
|
176 |
|
|
177 |
+ |
/** |
178 |
+ |
* toString returns string containing common name of unit |
179 |
+ |
*/ |
180 |
|
public void testToString() { |
181 |
|
String s = TimeUnit.SECONDS.toString(); |
182 |
|
assertTrue(s.indexOf("econd") >= 0); |
183 |
|
} |
184 |
|
|
117 |
– |
// Exception tests |
185 |
|
|
186 |
|
/** |
187 |
< |
* This test specifically to catch the unreported exception |
187 |
> |
* Timed wait without holding lock throws |
188 |
> |
* IllegalMonitorStateException |
189 |
|
*/ |
190 |
< |
public void testTimedWaitForUnreportedIllegalMonitorException() { |
190 |
> |
public void testTimedWait_IllegalMonitorException() { |
191 |
|
//created a new thread with anonymous runnable |
192 |
|
|
193 |
|
Thread t = new Thread(new Runnable() { |
195 |
|
Object o = new Object(); |
196 |
|
TimeUnit tu = TimeUnit.MILLISECONDS; |
197 |
|
try { |
198 |
< |
tu.timedWait(o,40000); |
199 |
< |
fail("should throw"); |
198 |
> |
tu.timedWait(o,LONG_DELAY_MS); |
199 |
> |
threadShouldThrow(); |
200 |
|
} |
201 |
|
catch (InterruptedException ie) { |
202 |
< |
fail("should not throw IE here"); |
202 |
> |
threadUnexpectedException(); |
203 |
|
} |
204 |
|
catch(IllegalMonitorStateException success) { |
205 |
|
} |
208 |
|
}); |
209 |
|
t.start(); |
210 |
|
try { |
211 |
< |
Thread.sleep(100); |
211 |
> |
Thread.sleep(SHORT_DELAY_MS); |
212 |
|
t.interrupt(); |
213 |
|
t.join(); |
214 |
|
} catch(Exception e) { |
215 |
< |
fail("Unexpected exception"); |
215 |
> |
unexpectedException(); |
216 |
|
} |
217 |
|
} |
218 |
|
|
219 |
|
/** |
220 |
< |
* Test to verify that timedWait will throw InterruptedException. |
153 |
< |
* Thread t waits on timedWait while the main thread interrupts it. |
154 |
< |
* Note: This does not throw IllegalMonitorException since timeWait |
155 |
< |
* is synchronized on o |
220 |
> |
* timedWait throws InterruptedException when interrupted |
221 |
|
*/ |
222 |
|
public void testTimedWait() { |
223 |
|
Thread t = new Thread(new Runnable() { |
227 |
|
TimeUnit tu = TimeUnit.MILLISECONDS; |
228 |
|
try { |
229 |
|
synchronized(o) { |
230 |
< |
tu.timedWait(o,1000); |
230 |
> |
tu.timedWait(o,MEDIUM_DELAY_MS); |
231 |
|
} |
232 |
< |
fail("should throw"); |
232 |
> |
threadShouldThrow(); |
233 |
|
} |
234 |
|
catch(InterruptedException success) {} |
235 |
|
catch(IllegalMonitorStateException failure) { |
236 |
< |
fail("should not throw"); |
236 |
> |
threadUnexpectedException(); |
237 |
|
} |
238 |
|
} |
239 |
|
}); |
240 |
|
t.start(); |
241 |
|
try { |
242 |
< |
Thread.sleep(100); |
242 |
> |
Thread.sleep(SHORT_DELAY_MS); |
243 |
|
t.interrupt(); |
244 |
|
t.join(); |
245 |
|
} catch(Exception e) { |
246 |
< |
fail("Unexpected exception"); |
246 |
> |
unexpectedException(); |
247 |
|
} |
248 |
|
} |
249 |
|
|
250 |
|
|
251 |
|
/** |
252 |
< |
* Test to verify that timedJoin will throw InterruptedException. |
188 |
< |
* Thread t waits on timedJoin while the main thread interrupts it. |
252 |
> |
* timedJoin throws InterruptedException when interrupted |
253 |
|
*/ |
254 |
|
public void testTimedJoin() { |
255 |
|
Thread t = new Thread(new Runnable() { |
258 |
|
try { |
259 |
|
Thread s = new Thread(new Runnable() { |
260 |
|
public void run() { |
261 |
< |
try{ |
262 |
< |
Thread.sleep(1000); |
263 |
< |
}catch(InterruptedException success){} |
261 |
> |
try { |
262 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
263 |
> |
} catch(InterruptedException success){} |
264 |
|
} |
265 |
|
}); |
266 |
|
s.start(); |
267 |
< |
tu.timedJoin(s,1000); |
268 |
< |
fail("should throw"); |
267 |
> |
tu.timedJoin(s,MEDIUM_DELAY_MS); |
268 |
> |
threadShouldThrow(); |
269 |
|
} |
270 |
|
catch(Exception e) {} |
271 |
|
} |
272 |
|
}); |
273 |
|
t.start(); |
274 |
|
try { |
275 |
< |
Thread.sleep(100); |
275 |
> |
Thread.sleep(SHORT_DELAY_MS); |
276 |
|
t.interrupt(); |
277 |
|
t.join(); |
278 |
|
} catch(Exception e) { |
279 |
< |
fail("Unexpected exception"); |
279 |
> |
unexpectedException(); |
280 |
|
} |
281 |
|
} |
282 |
|
|
283 |
|
/** |
284 |
< |
* Test to verify that timedSleep will throw InterruptedException. |
221 |
< |
* Thread t waits on timedSleep while the main thread interrupts it. |
284 |
> |
* timedSleep throws InterruptedException when interrupted |
285 |
|
*/ |
286 |
|
public void testTimedSleep() { |
287 |
|
//created a new thread with anonymous runnable |
290 |
|
public void run() { |
291 |
|
TimeUnit tu = TimeUnit.MILLISECONDS; |
292 |
|
try { |
293 |
< |
tu.sleep(1000); |
294 |
< |
fail("should throw"); |
293 |
> |
tu.sleep(MEDIUM_DELAY_MS); |
294 |
> |
threadShouldThrow(); |
295 |
|
} |
296 |
|
catch(InterruptedException success) {} |
297 |
|
} |
298 |
|
}); |
299 |
|
t.start(); |
300 |
|
try { |
301 |
< |
Thread.sleep(100); |
301 |
> |
Thread.sleep(SHORT_DELAY_MS); |
302 |
|
t.interrupt(); |
303 |
|
t.join(); |
304 |
|
} catch(Exception e) { |
305 |
< |
fail("Unexpected exception"); |
305 |
> |
unexpectedException(); |
306 |
|
} |
307 |
|
} |
308 |
|
|
309 |
+ |
/** |
310 |
+ |
* a deserialized serialized unit is equal |
311 |
+ |
*/ |
312 |
|
public void testSerialization() { |
313 |
|
TimeUnit q = TimeUnit.MILLISECONDS; |
314 |
|
|
325 |
|
assertEquals(q.toString(), r.toString()); |
326 |
|
} catch(Exception e){ |
327 |
|
e.printStackTrace(); |
328 |
< |
fail("unexpected exception"); |
328 |
> |
unexpectedException(); |
329 |
|
} |
330 |
|
} |
331 |
|
|