1 |
dl |
1.1 |
/* |
2 |
dl |
1.6 |
* 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 |
jsr166 |
1.19 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.10 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
jsr166 |
1.24 |
import static java.util.concurrent.TimeUnit.DAYS; |
10 |
|
|
import static java.util.concurrent.TimeUnit.HOURS; |
11 |
|
|
import static java.util.concurrent.TimeUnit.MICROSECONDS; |
12 |
|
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
|
|
import static java.util.concurrent.TimeUnit.MINUTES; |
14 |
|
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
15 |
|
|
import static java.util.concurrent.TimeUnit.SECONDS; |
16 |
|
|
|
17 |
jsr166 |
1.26 |
import java.time.temporal.ChronoUnit; |
18 |
jsr166 |
1.22 |
import java.util.concurrent.CountDownLatch; |
19 |
|
|
import java.util.concurrent.TimeUnit; |
20 |
jsr166 |
1.24 |
|
21 |
|
|
import junit.framework.Test; |
22 |
|
|
import junit.framework.TestSuite; |
23 |
dl |
1.1 |
|
24 |
dl |
1.3 |
public class TimeUnitTest extends JSR166TestCase { |
25 |
dl |
1.1 |
public static void main(String[] args) { |
26 |
jsr166 |
1.25 |
main(suite(), args); |
27 |
dl |
1.1 |
} |
28 |
jsr166 |
1.10 |
|
29 |
dl |
1.1 |
public static Test suite() { |
30 |
jsr166 |
1.14 |
return new TestSuite(TimeUnitTest.class); |
31 |
dl |
1.1 |
} |
32 |
|
|
|
33 |
dl |
1.9 |
// (loops to 88888 check increments at all time divisions.) |
34 |
|
|
|
35 |
dl |
1.4 |
/** |
36 |
dl |
1.9 |
* convert correctly converts sample values across the units |
37 |
dl |
1.4 |
*/ |
38 |
dl |
1.1 |
public void testConvert() { |
39 |
dl |
1.9 |
for (long t = 0; t < 88888; ++t) { |
40 |
jsr166 |
1.10 |
assertEquals(t*60*60*24, |
41 |
jsr166 |
1.23 |
SECONDS.convert(t, DAYS)); |
42 |
jsr166 |
1.10 |
assertEquals(t*60*60, |
43 |
jsr166 |
1.23 |
SECONDS.convert(t, HOURS)); |
44 |
jsr166 |
1.10 |
assertEquals(t*60, |
45 |
jsr166 |
1.23 |
SECONDS.convert(t, MINUTES)); |
46 |
jsr166 |
1.10 |
assertEquals(t, |
47 |
jsr166 |
1.23 |
SECONDS.convert(t, SECONDS)); |
48 |
jsr166 |
1.10 |
assertEquals(t, |
49 |
jsr166 |
1.23 |
SECONDS.convert(1000L*t, MILLISECONDS)); |
50 |
jsr166 |
1.10 |
assertEquals(t, |
51 |
jsr166 |
1.23 |
SECONDS.convert(1000000L*t, MICROSECONDS)); |
52 |
jsr166 |
1.10 |
assertEquals(t, |
53 |
jsr166 |
1.23 |
SECONDS.convert(1000000000L*t, NANOSECONDS)); |
54 |
dl |
1.9 |
|
55 |
jsr166 |
1.10 |
assertEquals(1000L*t*60*60*24, |
56 |
jsr166 |
1.23 |
MILLISECONDS.convert(t, DAYS)); |
57 |
jsr166 |
1.10 |
assertEquals(1000L*t*60*60, |
58 |
jsr166 |
1.23 |
MILLISECONDS.convert(t, HOURS)); |
59 |
jsr166 |
1.10 |
assertEquals(1000L*t*60, |
60 |
jsr166 |
1.23 |
MILLISECONDS.convert(t, MINUTES)); |
61 |
jsr166 |
1.10 |
assertEquals(1000L*t, |
62 |
jsr166 |
1.23 |
MILLISECONDS.convert(t, SECONDS)); |
63 |
jsr166 |
1.10 |
assertEquals(t, |
64 |
jsr166 |
1.23 |
MILLISECONDS.convert(t, MILLISECONDS)); |
65 |
jsr166 |
1.10 |
assertEquals(t, |
66 |
jsr166 |
1.23 |
MILLISECONDS.convert(1000L*t, MICROSECONDS)); |
67 |
jsr166 |
1.10 |
assertEquals(t, |
68 |
jsr166 |
1.23 |
MILLISECONDS.convert(1000000L*t, NANOSECONDS)); |
69 |
dl |
1.9 |
|
70 |
jsr166 |
1.10 |
assertEquals(1000000L*t*60*60*24, |
71 |
jsr166 |
1.23 |
MICROSECONDS.convert(t, DAYS)); |
72 |
jsr166 |
1.10 |
assertEquals(1000000L*t*60*60, |
73 |
jsr166 |
1.23 |
MICROSECONDS.convert(t, HOURS)); |
74 |
jsr166 |
1.10 |
assertEquals(1000000L*t*60, |
75 |
jsr166 |
1.23 |
MICROSECONDS.convert(t, MINUTES)); |
76 |
jsr166 |
1.10 |
assertEquals(1000000L*t, |
77 |
jsr166 |
1.23 |
MICROSECONDS.convert(t, SECONDS)); |
78 |
jsr166 |
1.10 |
assertEquals(1000L*t, |
79 |
jsr166 |
1.23 |
MICROSECONDS.convert(t, MILLISECONDS)); |
80 |
jsr166 |
1.10 |
assertEquals(t, |
81 |
jsr166 |
1.23 |
MICROSECONDS.convert(t, MICROSECONDS)); |
82 |
jsr166 |
1.10 |
assertEquals(t, |
83 |
jsr166 |
1.23 |
MICROSECONDS.convert(1000L*t, NANOSECONDS)); |
84 |
dl |
1.9 |
|
85 |
jsr166 |
1.10 |
assertEquals(1000000000L*t*60*60*24, |
86 |
jsr166 |
1.23 |
NANOSECONDS.convert(t, DAYS)); |
87 |
jsr166 |
1.10 |
assertEquals(1000000000L*t*60*60, |
88 |
jsr166 |
1.23 |
NANOSECONDS.convert(t, HOURS)); |
89 |
jsr166 |
1.10 |
assertEquals(1000000000L*t*60, |
90 |
jsr166 |
1.23 |
NANOSECONDS.convert(t, MINUTES)); |
91 |
jsr166 |
1.10 |
assertEquals(1000000000L*t, |
92 |
jsr166 |
1.23 |
NANOSECONDS.convert(t, SECONDS)); |
93 |
jsr166 |
1.10 |
assertEquals(1000000L*t, |
94 |
jsr166 |
1.23 |
NANOSECONDS.convert(t, MILLISECONDS)); |
95 |
jsr166 |
1.10 |
assertEquals(1000L*t, |
96 |
jsr166 |
1.23 |
NANOSECONDS.convert(t, MICROSECONDS)); |
97 |
jsr166 |
1.10 |
assertEquals(t, |
98 |
jsr166 |
1.23 |
NANOSECONDS.convert(t, NANOSECONDS)); |
99 |
dl |
1.1 |
} |
100 |
|
|
} |
101 |
|
|
|
102 |
dl |
1.4 |
/** |
103 |
dl |
1.5 |
* toNanos correctly converts sample values in different units to |
104 |
|
|
* nanoseconds |
105 |
dl |
1.4 |
*/ |
106 |
dl |
1.1 |
public void testToNanos() { |
107 |
dl |
1.9 |
for (long t = 0; t < 88888; ++t) { |
108 |
jsr166 |
1.10 |
assertEquals(t*1000000000L*60*60*24, |
109 |
jsr166 |
1.23 |
DAYS.toNanos(t)); |
110 |
jsr166 |
1.10 |
assertEquals(t*1000000000L*60*60, |
111 |
jsr166 |
1.23 |
HOURS.toNanos(t)); |
112 |
jsr166 |
1.10 |
assertEquals(t*1000000000L*60, |
113 |
jsr166 |
1.23 |
MINUTES.toNanos(t)); |
114 |
jsr166 |
1.10 |
assertEquals(1000000000L*t, |
115 |
jsr166 |
1.23 |
SECONDS.toNanos(t)); |
116 |
jsr166 |
1.10 |
assertEquals(1000000L*t, |
117 |
jsr166 |
1.23 |
MILLISECONDS.toNanos(t)); |
118 |
jsr166 |
1.10 |
assertEquals(1000L*t, |
119 |
jsr166 |
1.23 |
MICROSECONDS.toNanos(t)); |
120 |
jsr166 |
1.10 |
assertEquals(t, |
121 |
jsr166 |
1.23 |
NANOSECONDS.toNanos(t)); |
122 |
dl |
1.3 |
} |
123 |
|
|
} |
124 |
|
|
|
125 |
dl |
1.4 |
/** |
126 |
dl |
1.5 |
* toMicros correctly converts sample values in different units to |
127 |
|
|
* microseconds |
128 |
dl |
1.4 |
*/ |
129 |
dl |
1.3 |
public void testToMicros() { |
130 |
dl |
1.9 |
for (long t = 0; t < 88888; ++t) { |
131 |
jsr166 |
1.10 |
assertEquals(t*1000000L*60*60*24, |
132 |
jsr166 |
1.23 |
DAYS.toMicros(t)); |
133 |
jsr166 |
1.10 |
assertEquals(t*1000000L*60*60, |
134 |
jsr166 |
1.23 |
HOURS.toMicros(t)); |
135 |
jsr166 |
1.10 |
assertEquals(t*1000000L*60, |
136 |
jsr166 |
1.23 |
MINUTES.toMicros(t)); |
137 |
jsr166 |
1.10 |
assertEquals(1000000L*t, |
138 |
jsr166 |
1.23 |
SECONDS.toMicros(t)); |
139 |
jsr166 |
1.10 |
assertEquals(1000L*t, |
140 |
jsr166 |
1.23 |
MILLISECONDS.toMicros(t)); |
141 |
jsr166 |
1.10 |
assertEquals(t, |
142 |
jsr166 |
1.23 |
MICROSECONDS.toMicros(t)); |
143 |
jsr166 |
1.10 |
assertEquals(t, |
144 |
jsr166 |
1.23 |
NANOSECONDS.toMicros(t*1000L)); |
145 |
dl |
1.3 |
} |
146 |
|
|
} |
147 |
|
|
|
148 |
dl |
1.4 |
/** |
149 |
dl |
1.5 |
* toMillis correctly converts sample values in different units to |
150 |
|
|
* milliseconds |
151 |
dl |
1.4 |
*/ |
152 |
dl |
1.3 |
public void testToMillis() { |
153 |
dl |
1.9 |
for (long t = 0; t < 88888; ++t) { |
154 |
jsr166 |
1.10 |
assertEquals(t*1000L*60*60*24, |
155 |
jsr166 |
1.23 |
DAYS.toMillis(t)); |
156 |
jsr166 |
1.10 |
assertEquals(t*1000L*60*60, |
157 |
jsr166 |
1.23 |
HOURS.toMillis(t)); |
158 |
jsr166 |
1.10 |
assertEquals(t*1000L*60, |
159 |
jsr166 |
1.23 |
MINUTES.toMillis(t)); |
160 |
jsr166 |
1.10 |
assertEquals(1000L*t, |
161 |
jsr166 |
1.23 |
SECONDS.toMillis(t)); |
162 |
jsr166 |
1.10 |
assertEquals(t, |
163 |
jsr166 |
1.23 |
MILLISECONDS.toMillis(t)); |
164 |
jsr166 |
1.10 |
assertEquals(t, |
165 |
jsr166 |
1.23 |
MICROSECONDS.toMillis(t*1000L)); |
166 |
jsr166 |
1.10 |
assertEquals(t, |
167 |
jsr166 |
1.23 |
NANOSECONDS.toMillis(t*1000000L)); |
168 |
dl |
1.3 |
} |
169 |
|
|
} |
170 |
|
|
|
171 |
dl |
1.4 |
/** |
172 |
dl |
1.5 |
* toSeconds correctly converts sample values in different units to |
173 |
|
|
* seconds |
174 |
dl |
1.4 |
*/ |
175 |
dl |
1.3 |
public void testToSeconds() { |
176 |
dl |
1.9 |
for (long t = 0; t < 88888; ++t) { |
177 |
jsr166 |
1.10 |
assertEquals(t*60*60*24, |
178 |
jsr166 |
1.23 |
DAYS.toSeconds(t)); |
179 |
jsr166 |
1.10 |
assertEquals(t*60*60, |
180 |
jsr166 |
1.23 |
HOURS.toSeconds(t)); |
181 |
jsr166 |
1.10 |
assertEquals(t*60, |
182 |
jsr166 |
1.23 |
MINUTES.toSeconds(t)); |
183 |
jsr166 |
1.10 |
assertEquals(t, |
184 |
jsr166 |
1.23 |
SECONDS.toSeconds(t)); |
185 |
jsr166 |
1.10 |
assertEquals(t, |
186 |
jsr166 |
1.23 |
MILLISECONDS.toSeconds(t*1000L)); |
187 |
jsr166 |
1.10 |
assertEquals(t, |
188 |
jsr166 |
1.23 |
MICROSECONDS.toSeconds(t*1000000L)); |
189 |
jsr166 |
1.10 |
assertEquals(t, |
190 |
jsr166 |
1.23 |
NANOSECONDS.toSeconds(t*1000000000L)); |
191 |
dl |
1.9 |
} |
192 |
|
|
} |
193 |
dl |
1.3 |
|
194 |
dl |
1.9 |
/** |
195 |
|
|
* toMinutes correctly converts sample values in different units to |
196 |
|
|
* minutes |
197 |
|
|
*/ |
198 |
|
|
public void testToMinutes() { |
199 |
|
|
for (long t = 0; t < 88888; ++t) { |
200 |
jsr166 |
1.10 |
assertEquals(t*60*24, |
201 |
jsr166 |
1.23 |
DAYS.toMinutes(t)); |
202 |
jsr166 |
1.10 |
assertEquals(t*60, |
203 |
jsr166 |
1.23 |
HOURS.toMinutes(t)); |
204 |
jsr166 |
1.10 |
assertEquals(t, |
205 |
jsr166 |
1.23 |
MINUTES.toMinutes(t)); |
206 |
jsr166 |
1.10 |
assertEquals(t, |
207 |
jsr166 |
1.23 |
SECONDS.toMinutes(t*60)); |
208 |
jsr166 |
1.10 |
assertEquals(t, |
209 |
jsr166 |
1.23 |
MILLISECONDS.toMinutes(t*1000L*60)); |
210 |
jsr166 |
1.10 |
assertEquals(t, |
211 |
jsr166 |
1.23 |
MICROSECONDS.toMinutes(t*1000000L*60)); |
212 |
jsr166 |
1.10 |
assertEquals(t, |
213 |
jsr166 |
1.23 |
NANOSECONDS.toMinutes(t*1000000000L*60)); |
214 |
dl |
1.9 |
} |
215 |
|
|
} |
216 |
|
|
|
217 |
|
|
/** |
218 |
|
|
* toHours correctly converts sample values in different units to |
219 |
|
|
* hours |
220 |
|
|
*/ |
221 |
|
|
public void testToHours() { |
222 |
|
|
for (long t = 0; t < 88888; ++t) { |
223 |
jsr166 |
1.10 |
assertEquals(t*24, |
224 |
jsr166 |
1.23 |
DAYS.toHours(t)); |
225 |
jsr166 |
1.10 |
assertEquals(t, |
226 |
jsr166 |
1.23 |
HOURS.toHours(t)); |
227 |
jsr166 |
1.10 |
assertEquals(t, |
228 |
jsr166 |
1.23 |
MINUTES.toHours(t*60)); |
229 |
jsr166 |
1.10 |
assertEquals(t, |
230 |
jsr166 |
1.23 |
SECONDS.toHours(t*60*60)); |
231 |
jsr166 |
1.10 |
assertEquals(t, |
232 |
jsr166 |
1.23 |
MILLISECONDS.toHours(t*1000L*60*60)); |
233 |
jsr166 |
1.10 |
assertEquals(t, |
234 |
jsr166 |
1.23 |
MICROSECONDS.toHours(t*1000000L*60*60)); |
235 |
jsr166 |
1.10 |
assertEquals(t, |
236 |
jsr166 |
1.23 |
NANOSECONDS.toHours(t*1000000000L*60*60)); |
237 |
dl |
1.9 |
} |
238 |
|
|
} |
239 |
|
|
|
240 |
|
|
/** |
241 |
|
|
* toDays correctly converts sample values in different units to |
242 |
|
|
* days |
243 |
|
|
*/ |
244 |
|
|
public void testToDays() { |
245 |
|
|
for (long t = 0; t < 88888; ++t) { |
246 |
jsr166 |
1.10 |
assertEquals(t, |
247 |
jsr166 |
1.23 |
DAYS.toDays(t)); |
248 |
jsr166 |
1.10 |
assertEquals(t, |
249 |
jsr166 |
1.23 |
HOURS.toDays(t*24)); |
250 |
jsr166 |
1.10 |
assertEquals(t, |
251 |
jsr166 |
1.23 |
MINUTES.toDays(t*60*24)); |
252 |
jsr166 |
1.10 |
assertEquals(t, |
253 |
jsr166 |
1.23 |
SECONDS.toDays(t*60*60*24)); |
254 |
jsr166 |
1.10 |
assertEquals(t, |
255 |
jsr166 |
1.23 |
MILLISECONDS.toDays(t*1000L*60*60*24)); |
256 |
jsr166 |
1.10 |
assertEquals(t, |
257 |
jsr166 |
1.23 |
MICROSECONDS.toDays(t*1000000L*60*60*24)); |
258 |
jsr166 |
1.10 |
assertEquals(t, |
259 |
jsr166 |
1.23 |
NANOSECONDS.toDays(t*1000000000L*60*60*24)); |
260 |
dl |
1.1 |
} |
261 |
|
|
} |
262 |
|
|
|
263 |
dl |
1.4 |
/** |
264 |
jsr166 |
1.10 |
* convert saturates positive too-large values to Long.MAX_VALUE |
265 |
dl |
1.5 |
* and negative to LONG.MIN_VALUE |
266 |
dl |
1.4 |
*/ |
267 |
dl |
1.1 |
public void testConvertSaturate() { |
268 |
|
|
assertEquals(Long.MAX_VALUE, |
269 |
jsr166 |
1.23 |
NANOSECONDS.convert(Long.MAX_VALUE / 2, SECONDS)); |
270 |
dl |
1.1 |
assertEquals(Long.MIN_VALUE, |
271 |
jsr166 |
1.23 |
NANOSECONDS.convert(-Long.MAX_VALUE / 4, SECONDS)); |
272 |
dl |
1.9 |
assertEquals(Long.MAX_VALUE, |
273 |
jsr166 |
1.23 |
NANOSECONDS.convert(Long.MAX_VALUE / 2, MINUTES)); |
274 |
dl |
1.9 |
assertEquals(Long.MIN_VALUE, |
275 |
jsr166 |
1.23 |
NANOSECONDS.convert(-Long.MAX_VALUE / 4, MINUTES)); |
276 |
dl |
1.9 |
assertEquals(Long.MAX_VALUE, |
277 |
jsr166 |
1.23 |
NANOSECONDS.convert(Long.MAX_VALUE / 2, HOURS)); |
278 |
dl |
1.9 |
assertEquals(Long.MIN_VALUE, |
279 |
jsr166 |
1.23 |
NANOSECONDS.convert(-Long.MAX_VALUE / 4, HOURS)); |
280 |
dl |
1.9 |
assertEquals(Long.MAX_VALUE, |
281 |
jsr166 |
1.23 |
NANOSECONDS.convert(Long.MAX_VALUE / 2, DAYS)); |
282 |
dl |
1.9 |
assertEquals(Long.MIN_VALUE, |
283 |
jsr166 |
1.23 |
NANOSECONDS.convert(-Long.MAX_VALUE / 4, DAYS)); |
284 |
dl |
1.1 |
} |
285 |
|
|
|
286 |
dl |
1.4 |
/** |
287 |
jsr166 |
1.10 |
* toNanos saturates positive too-large values to Long.MAX_VALUE |
288 |
dl |
1.5 |
* and negative to LONG.MIN_VALUE |
289 |
dl |
1.4 |
*/ |
290 |
dl |
1.1 |
public void testToNanosSaturate() { |
291 |
jsr166 |
1.13 |
assertEquals(Long.MAX_VALUE, |
292 |
jsr166 |
1.23 |
MILLISECONDS.toNanos(Long.MAX_VALUE / 2)); |
293 |
jsr166 |
1.13 |
assertEquals(Long.MIN_VALUE, |
294 |
jsr166 |
1.23 |
MILLISECONDS.toNanos(-Long.MAX_VALUE / 3)); |
295 |
dl |
1.1 |
} |
296 |
|
|
|
297 |
dl |
1.4 |
/** |
298 |
jsr166 |
1.20 |
* toString returns name of unit |
299 |
dl |
1.4 |
*/ |
300 |
dl |
1.1 |
public void testToString() { |
301 |
jsr166 |
1.23 |
assertEquals("SECONDS", SECONDS.toString()); |
302 |
dl |
1.1 |
} |
303 |
|
|
|
304 |
jsr166 |
1.20 |
/** |
305 |
|
|
* name returns name of unit |
306 |
|
|
*/ |
307 |
|
|
public void testName() { |
308 |
jsr166 |
1.23 |
assertEquals("SECONDS", SECONDS.name()); |
309 |
jsr166 |
1.20 |
} |
310 |
jsr166 |
1.10 |
|
311 |
dl |
1.1 |
/** |
312 |
jsr166 |
1.17 |
* Timed wait without holding lock throws |
313 |
|
|
* IllegalMonitorStateException |
314 |
dl |
1.1 |
*/ |
315 |
jsr166 |
1.21 |
public void testTimedWait_IllegalMonitorException() { |
316 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
317 |
jsr166 |
1.13 |
public void realRun() throws InterruptedException { |
318 |
|
|
Object o = new Object(); |
319 |
jsr166 |
1.23 |
TimeUnit tu = MILLISECONDS; |
320 |
jsr166 |
1.21 |
|
321 |
jsr166 |
1.13 |
try { |
322 |
jsr166 |
1.21 |
tu.timedWait(o, LONG_DELAY_MS); |
323 |
jsr166 |
1.13 |
threadShouldThrow(); |
324 |
jsr166 |
1.21 |
} catch (IllegalMonitorStateException success) {} |
325 |
|
|
}}); |
326 |
dl |
1.1 |
|
327 |
jsr166 |
1.21 |
awaitTermination(t); |
328 |
dl |
1.1 |
} |
329 |
jsr166 |
1.10 |
|
330 |
dl |
1.1 |
/** |
331 |
dl |
1.5 |
* timedWait throws InterruptedException when interrupted |
332 |
dl |
1.4 |
*/ |
333 |
jsr166 |
1.21 |
public void testTimedWait_Interruptible() { |
334 |
|
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
335 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
336 |
jsr166 |
1.14 |
public void realRun() throws InterruptedException { |
337 |
jsr166 |
1.13 |
Object o = new Object(); |
338 |
jsr166 |
1.23 |
TimeUnit tu = MILLISECONDS; |
339 |
jsr166 |
1.13 |
|
340 |
jsr166 |
1.21 |
Thread.currentThread().interrupt(); |
341 |
|
|
try { |
342 |
|
|
synchronized (o) { |
343 |
|
|
tu.timedWait(o, LONG_DELAY_MS); |
344 |
|
|
} |
345 |
|
|
shouldThrow(); |
346 |
|
|
} catch (InterruptedException success) {} |
347 |
|
|
assertFalse(Thread.interrupted()); |
348 |
|
|
|
349 |
|
|
pleaseInterrupt.countDown(); |
350 |
|
|
try { |
351 |
|
|
synchronized (o) { |
352 |
|
|
tu.timedWait(o, LONG_DELAY_MS); |
353 |
|
|
} |
354 |
|
|
shouldThrow(); |
355 |
|
|
} catch (InterruptedException success) {} |
356 |
|
|
assertFalse(Thread.interrupted()); |
357 |
jsr166 |
1.13 |
}}); |
358 |
jsr166 |
1.21 |
|
359 |
|
|
await(pleaseInterrupt); |
360 |
|
|
assertThreadStaysAlive(t); |
361 |
jsr166 |
1.13 |
t.interrupt(); |
362 |
jsr166 |
1.21 |
awaitTermination(t); |
363 |
dl |
1.1 |
} |
364 |
jsr166 |
1.10 |
|
365 |
dl |
1.1 |
/** |
366 |
dl |
1.5 |
* timedJoin throws InterruptedException when interrupted |
367 |
dl |
1.4 |
*/ |
368 |
jsr166 |
1.21 |
public void testTimedJoin_Interruptible() { |
369 |
|
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
370 |
|
|
final Thread s = newStartedThread(new CheckedInterruptedRunnable() { |
371 |
jsr166 |
1.13 |
public void realRun() throws InterruptedException { |
372 |
jsr166 |
1.21 |
Thread.sleep(LONG_DELAY_MS); |
373 |
jsr166 |
1.13 |
}}); |
374 |
jsr166 |
1.21 |
final Thread t = newStartedThread(new CheckedRunnable() { |
375 |
jsr166 |
1.14 |
public void realRun() throws InterruptedException { |
376 |
jsr166 |
1.23 |
TimeUnit tu = MILLISECONDS; |
377 |
jsr166 |
1.21 |
Thread.currentThread().interrupt(); |
378 |
|
|
try { |
379 |
|
|
tu.timedJoin(s, LONG_DELAY_MS); |
380 |
|
|
shouldThrow(); |
381 |
|
|
} catch (InterruptedException success) {} |
382 |
|
|
assertFalse(Thread.interrupted()); |
383 |
|
|
|
384 |
|
|
pleaseInterrupt.countDown(); |
385 |
|
|
try { |
386 |
|
|
tu.timedJoin(s, LONG_DELAY_MS); |
387 |
|
|
shouldThrow(); |
388 |
|
|
} catch (InterruptedException success) {} |
389 |
|
|
assertFalse(Thread.interrupted()); |
390 |
jsr166 |
1.18 |
}}); |
391 |
jsr166 |
1.21 |
|
392 |
|
|
await(pleaseInterrupt); |
393 |
|
|
assertThreadStaysAlive(t); |
394 |
jsr166 |
1.13 |
t.interrupt(); |
395 |
jsr166 |
1.21 |
awaitTermination(t); |
396 |
jsr166 |
1.13 |
s.interrupt(); |
397 |
jsr166 |
1.21 |
awaitTermination(s); |
398 |
dl |
1.1 |
} |
399 |
jsr166 |
1.10 |
|
400 |
dl |
1.1 |
/** |
401 |
jsr166 |
1.17 |
* timedSleep throws InterruptedException when interrupted |
402 |
dl |
1.4 |
*/ |
403 |
jsr166 |
1.21 |
public void testTimedSleep_Interruptible() { |
404 |
|
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
405 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
406 |
jsr166 |
1.14 |
public void realRun() throws InterruptedException { |
407 |
jsr166 |
1.23 |
TimeUnit tu = MILLISECONDS; |
408 |
jsr166 |
1.21 |
Thread.currentThread().interrupt(); |
409 |
|
|
try { |
410 |
|
|
tu.sleep(LONG_DELAY_MS); |
411 |
|
|
shouldThrow(); |
412 |
|
|
} catch (InterruptedException success) {} |
413 |
|
|
assertFalse(Thread.interrupted()); |
414 |
|
|
|
415 |
|
|
pleaseInterrupt.countDown(); |
416 |
|
|
try { |
417 |
|
|
tu.sleep(LONG_DELAY_MS); |
418 |
|
|
shouldThrow(); |
419 |
|
|
} catch (InterruptedException success) {} |
420 |
|
|
assertFalse(Thread.interrupted()); |
421 |
jsr166 |
1.13 |
}}); |
422 |
dl |
1.1 |
|
423 |
jsr166 |
1.21 |
await(pleaseInterrupt); |
424 |
|
|
assertThreadStaysAlive(t); |
425 |
jsr166 |
1.13 |
t.interrupt(); |
426 |
jsr166 |
1.21 |
awaitTermination(t); |
427 |
dl |
1.1 |
} |
428 |
dl |
1.2 |
|
429 |
dl |
1.4 |
/** |
430 |
jsr166 |
1.13 |
* a deserialized serialized unit is the same instance |
431 |
dl |
1.4 |
*/ |
432 |
jsr166 |
1.13 |
public void testSerialization() throws Exception { |
433 |
jsr166 |
1.27 |
for (TimeUnit x : TimeUnit.values()) |
434 |
|
|
assertSame(x, serialClone(x)); |
435 |
dl |
1.2 |
} |
436 |
|
|
|
437 |
jsr166 |
1.26 |
/** |
438 |
|
|
* tests for toChronoUnit. |
439 |
|
|
*/ |
440 |
|
|
public void testToChronoUnit() throws Exception { |
441 |
|
|
assertSame(ChronoUnit.NANOS, NANOSECONDS.toChronoUnit()); |
442 |
|
|
assertSame(ChronoUnit.MICROS, MICROSECONDS.toChronoUnit()); |
443 |
|
|
assertSame(ChronoUnit.MILLIS, MILLISECONDS.toChronoUnit()); |
444 |
|
|
assertSame(ChronoUnit.SECONDS, SECONDS.toChronoUnit()); |
445 |
|
|
assertSame(ChronoUnit.MINUTES, MINUTES.toChronoUnit()); |
446 |
|
|
assertSame(ChronoUnit.HOURS, HOURS.toChronoUnit()); |
447 |
|
|
assertSame(ChronoUnit.DAYS, DAYS.toChronoUnit()); |
448 |
|
|
|
449 |
|
|
// Every TimeUnit has a defined ChronoUnit equivalent |
450 |
|
|
for (TimeUnit x : TimeUnit.values()) |
451 |
|
|
assertSame(x, TimeUnit.of(x.toChronoUnit())); |
452 |
|
|
} |
453 |
|
|
|
454 |
|
|
/** |
455 |
|
|
* tests for TimeUnit.of(ChronoUnit). |
456 |
|
|
*/ |
457 |
|
|
public void testTimeUnitOf() throws Exception { |
458 |
|
|
assertSame(NANOSECONDS, TimeUnit.of(ChronoUnit.NANOS)); |
459 |
|
|
assertSame(MICROSECONDS, TimeUnit.of(ChronoUnit.MICROS)); |
460 |
|
|
assertSame(MILLISECONDS, TimeUnit.of(ChronoUnit.MILLIS)); |
461 |
|
|
assertSame(SECONDS, TimeUnit.of(ChronoUnit.SECONDS)); |
462 |
|
|
assertSame(MINUTES, TimeUnit.of(ChronoUnit.MINUTES)); |
463 |
|
|
assertSame(HOURS, TimeUnit.of(ChronoUnit.HOURS)); |
464 |
|
|
assertSame(DAYS, TimeUnit.of(ChronoUnit.DAYS)); |
465 |
|
|
|
466 |
|
|
assertThrows(NullPointerException.class, |
467 |
|
|
() -> TimeUnit.of((ChronoUnit)null)); |
468 |
|
|
|
469 |
|
|
// ChronoUnits either round trip to their TimeUnit |
470 |
|
|
// equivalents, or throw IllegalArgumentException. |
471 |
|
|
for (ChronoUnit cu : ChronoUnit.values()) { |
472 |
|
|
final TimeUnit tu; |
473 |
|
|
try { |
474 |
|
|
tu = TimeUnit.of(cu); |
475 |
|
|
} catch (IllegalArgumentException acceptable) { |
476 |
|
|
continue; |
477 |
|
|
} |
478 |
|
|
assertSame(cu, tu.toChronoUnit()); |
479 |
|
|
} |
480 |
|
|
} |
481 |
|
|
|
482 |
dl |
1.1 |
} |