10 |
|
import java.util.concurrent.*; |
11 |
|
import java.io.*; |
12 |
|
|
13 |
< |
public class TimeUnitTest extends TestCase { |
14 |
< |
static public boolean DEBUG = false; |
15 |
< |
|
16 |
< |
|
13 |
> |
public class TimeUnitTest extends JSR166TestCase { |
14 |
|
public static void main(String[] args) { |
15 |
|
junit.textui.TestRunner.run(suite()); |
16 |
|
} |
82 |
|
assertEquals(1000 * t, |
83 |
|
TimeUnit.MICROSECONDS.toNanos(t)); |
84 |
|
assertEquals(t, |
85 |
< |
TimeUnit.SECONDS.NANOSECONDS.toNanos(t)); |
85 |
> |
TimeUnit.NANOSECONDS.toNanos(t)); |
86 |
> |
} |
87 |
> |
} |
88 |
> |
|
89 |
> |
public void testToMicros() { |
90 |
> |
for (long t = 0; t < 10; ++t) { |
91 |
> |
assertEquals(1000000 * t, |
92 |
> |
TimeUnit.SECONDS.toMicros(t)); |
93 |
> |
|
94 |
> |
assertEquals(1000 * t, |
95 |
> |
TimeUnit.MILLISECONDS.toMicros(t)); |
96 |
> |
assertEquals(t, |
97 |
> |
TimeUnit.MICROSECONDS.toMicros(t)); |
98 |
> |
assertEquals(t, |
99 |
> |
TimeUnit.NANOSECONDS.toMicros(t * 1000)); |
100 |
> |
} |
101 |
> |
} |
102 |
> |
|
103 |
> |
public void testToMillis() { |
104 |
> |
for (long t = 0; t < 10; ++t) { |
105 |
> |
assertEquals(1000 * t, |
106 |
> |
TimeUnit.SECONDS.toMillis(t)); |
107 |
> |
|
108 |
> |
assertEquals(t, |
109 |
> |
TimeUnit.MILLISECONDS.toMillis(t)); |
110 |
> |
assertEquals(t, |
111 |
> |
TimeUnit.MICROSECONDS.toMillis(t * 1000)); |
112 |
> |
assertEquals(t, |
113 |
> |
TimeUnit.NANOSECONDS.toMillis(t * 1000000)); |
114 |
> |
} |
115 |
> |
} |
116 |
> |
|
117 |
> |
public void testToSeconds() { |
118 |
> |
for (long t = 0; t < 10; ++t) { |
119 |
> |
assertEquals(t, |
120 |
> |
TimeUnit.SECONDS.toSeconds(t)); |
121 |
> |
|
122 |
> |
assertEquals(t, |
123 |
> |
TimeUnit.MILLISECONDS.toSeconds(t * 1000)); |
124 |
> |
assertEquals(t, |
125 |
> |
TimeUnit.MICROSECONDS.toSeconds(t * 1000000)); |
126 |
> |
assertEquals(t, |
127 |
> |
TimeUnit.NANOSECONDS.toSeconds(t * 1000000000)); |
128 |
|
} |
129 |
|
} |
130 |
|
|
153 |
|
assertTrue(s.indexOf("econd") >= 0); |
154 |
|
} |
155 |
|
|
117 |
– |
// Exception tests |
156 |
|
|
157 |
|
/** |
158 |
< |
* This test specifically to catch the unreported exception |
158 |
> |
* Timed wait without holding lock throws |
159 |
> |
* IllegalMonitorStateException |
160 |
|
*/ |
161 |
< |
public void testTimedWaitForUnreportedIllegalMonitorException() { |
161 |
> |
public void testTimedWait_IllegalMonitorException() { |
162 |
|
//created a new thread with anonymous runnable |
163 |
|
|
164 |
|
Thread t = new Thread(new Runnable() { |
166 |
|
Object o = new Object(); |
167 |
|
TimeUnit tu = TimeUnit.MILLISECONDS; |
168 |
|
try { |
169 |
< |
tu.timedWait(o,40000); |
169 |
> |
tu.timedWait(o,LONG_DELAY_MS); |
170 |
|
fail("should throw"); |
171 |
|
} |
172 |
|
catch (InterruptedException ie) { |
179 |
|
}); |
180 |
|
t.start(); |
181 |
|
try { |
182 |
< |
Thread.sleep(100); |
182 |
> |
Thread.sleep(SHORT_DELAY_MS); |
183 |
|
t.interrupt(); |
184 |
|
t.join(); |
185 |
|
} catch(Exception e) { |
188 |
|
} |
189 |
|
|
190 |
|
/** |
191 |
< |
* Test to verify that timedWait will throw InterruptedException. |
191 |
> |
* timedWait will throw InterruptedException. |
192 |
|
* Thread t waits on timedWait while the main thread interrupts it. |
193 |
|
* Note: This does not throw IllegalMonitorException since timeWait |
194 |
|
* is synchronized on o |
201 |
|
TimeUnit tu = TimeUnit.MILLISECONDS; |
202 |
|
try { |
203 |
|
synchronized(o) { |
204 |
< |
tu.timedWait(o,1000); |
204 |
> |
tu.timedWait(o,MEDIUM_DELAY_MS); |
205 |
|
} |
206 |
|
fail("should throw"); |
207 |
|
} |
213 |
|
}); |
214 |
|
t.start(); |
215 |
|
try { |
216 |
< |
Thread.sleep(100); |
216 |
> |
Thread.sleep(SHORT_DELAY_MS); |
217 |
|
t.interrupt(); |
218 |
|
t.join(); |
219 |
|
} catch(Exception e) { |
223 |
|
|
224 |
|
|
225 |
|
/** |
226 |
< |
* Test to verify that timedJoin will throw InterruptedException. |
226 |
> |
* timedJoin will throw InterruptedException. |
227 |
|
* Thread t waits on timedJoin while the main thread interrupts it. |
228 |
|
*/ |
229 |
|
public void testTimedJoin() { |
234 |
|
Thread s = new Thread(new Runnable() { |
235 |
|
public void run() { |
236 |
|
try{ |
237 |
< |
Thread.sleep(1000); |
237 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
238 |
|
}catch(InterruptedException success){} |
239 |
|
} |
240 |
|
}); |
241 |
|
s.start(); |
242 |
< |
tu.timedJoin(s,1000); |
242 |
> |
tu.timedJoin(s,MEDIUM_DELAY_MS); |
243 |
|
fail("should throw"); |
244 |
|
} |
245 |
|
catch(Exception e) {} |
247 |
|
}); |
248 |
|
t.start(); |
249 |
|
try { |
250 |
< |
Thread.sleep(100); |
250 |
> |
Thread.sleep(SHORT_DELAY_MS); |
251 |
|
t.interrupt(); |
252 |
|
t.join(); |
253 |
|
} catch(Exception e) { |
256 |
|
} |
257 |
|
|
258 |
|
/** |
259 |
< |
* Test to verify that timedSleep will throw InterruptedException. |
259 |
> |
* timedSleep will throw InterruptedException. |
260 |
|
* Thread t waits on timedSleep while the main thread interrupts it. |
261 |
|
*/ |
262 |
|
public void testTimedSleep() { |
266 |
|
public void run() { |
267 |
|
TimeUnit tu = TimeUnit.MILLISECONDS; |
268 |
|
try { |
269 |
< |
tu.sleep(1000); |
269 |
> |
tu.sleep(MEDIUM_DELAY_MS); |
270 |
|
fail("should throw"); |
271 |
|
} |
272 |
|
catch(InterruptedException success) {} |
274 |
|
}); |
275 |
|
t.start(); |
276 |
|
try { |
277 |
< |
Thread.sleep(100); |
277 |
> |
Thread.sleep(SHORT_DELAY_MS); |
278 |
|
t.interrupt(); |
279 |
|
t.join(); |
280 |
|
} catch(Exception e) { |