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