ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantLockTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ReentrantLockTest.java (file contents):
Revision 1.1 by dl, Sun Aug 31 19:24:55 2003 UTC vs.
Revision 1.3 by dl, Sun Sep 7 20:39:11 2003 UTC

# Line 8 | Line 8
8   import junit.framework.*;
9   import java.util.concurrent.locks.*;
10   import java.util.concurrent.*;
11 + import java.io.*;
12  
13   public class ReentrantLockTest extends TestCase {
14      static int HOLD_COUNT_TEST_LIMIT = 20;
# Line 35 | Line 36 | public class ReentrantLockTest extends T
36              rl.unlock();
37              fail("Should of thown Illegal Monitor State Exception");
38  
39 <        }catch(IllegalMonitorStateException sucess){}
39 >        } catch(IllegalMonitorStateException success){}
40  
41  
42      }
# Line 54 | Line 55 | public class ReentrantLockTest extends T
55                      try{
56                          lock.lockInterruptibly();
57                          fail("should throw");
58 <                    }catch(InterruptedException sucess){}
58 >                    } catch(InterruptedException success){}
59                  }
60              });
61          t.start();
# Line 76 | Line 77 | public class ReentrantLockTest extends T
77                      try{
78                          lock.tryLock(1000,TimeUnit.MILLISECONDS);
79                          fail("should throw");
80 <                    }catch(InterruptedException sucess){}
80 >                    } catch(InterruptedException success){}
81                  }
82              });
83          t.start();
84          t.interrupt();
85      }
86  
87 +
88 +    public void testTryLockWhenLocked() {
89 +        final ReentrantLock lock = new ReentrantLock();
90 +        lock.lock();
91 +        Thread t = new Thread(new Runnable() {
92 +                public void run(){
93 +                    assertFalse(lock.tryLock());
94 +                }
95 +            });
96 +        try {
97 +            t.start();
98 +            t.join();
99 +            lock.unlock();
100 +        } catch(Exception e){
101 +            fail("unexpected exception");
102 +        }
103 +    }
104 +
105 +    public void testTryLock_Timeout(){
106 +        final ReentrantLock lock = new ReentrantLock();
107 +        lock.lock();
108 +        Thread t = new Thread(new Runnable() {
109 +                public void run(){
110 +                    try {
111 +                        assertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS));
112 +                    } catch (Exception ex) {
113 +                        fail("unexpected exception");
114 +                    }
115 +                }
116 +            });
117 +        try {
118 +            t.start();
119 +            t.join();
120 +            lock.unlock();
121 +        } catch(Exception e){
122 +            fail("unexpected exception");
123 +        }
124 +    }
125      
126      public void testGetHoldCount() {
127          ReentrantLock lock = new ReentrantLock();
# Line 124 | Line 163 | public class ReentrantLockTest extends T
163          } catch(Exception e){
164              fail("unexpected exception");
165          }
127
166      }
167  
130    /*
131     * current thread locks interruptibly the thread
132     * another thread tries to aquire the lock and blocks
133     * on the call. interrupt the attempted aquireLock
134     * assert that the first lock() call actually locked the lock
135     * assert that the current thread is the one holding the lock
136     */
168  
169 <    public void testLockedInterruptibly() {
169 >    public void testLockInterruptibly() {
170          final ReentrantLock lock = new ReentrantLock();
171 <        try {lock.lockInterruptibly();} catch(Exception e) {}
171 >        try {
172 >            lock.lockInterruptibly();
173 >        } catch(Exception e) {
174 >            fail("unexpected exception");
175 >        }
176          Thread t = new Thread(new Runnable() {
177                  public void run() {
178                      try {
179                          lock.lockInterruptibly();
180 <                        fail("Failed to generate an Interrupted Exception");
180 >                        fail("should throw");
181                      }
182                      catch(InterruptedException e) {}
183                  }
184              });
185 <        t.start();
186 <        t.interrupt();
187 <        assertTrue(lock.isLocked());
188 <        assertTrue(lock.isHeldByCurrentThread());
185 >        try {
186 >            t.start();
187 >            t.interrupt();
188 >            assertTrue(lock.isLocked());
189 >            assertTrue(lock.isHeldByCurrentThread());
190 >            t.join();
191 >        } catch(Exception e){
192 >            fail("unexpected exception");
193 >        }
194 >    }
195 >
196 >    public void testAwait_IllegalMonitor() {
197 >        final ReentrantLock lock = new ReentrantLock();
198 >        final Condition c = lock.newCondition();
199 >        try {
200 >            c.await();
201 >            fail("should throw");
202 >        }
203 >        catch (IllegalMonitorStateException success) {
204 >        }
205 >        catch (Exception ex) {
206 >            fail("should throw IMSE");
207 >        }
208 >    }
209 >
210 >    public void testSignal_IllegalMonitor() {
211 >        final ReentrantLock lock = new ReentrantLock();
212 >        final Condition c = lock.newCondition();
213 >        try {
214 >            c.signal();
215 >            fail("should throw");
216 >        }
217 >        catch (IllegalMonitorStateException success) {
218 >        }
219 >        catch (Exception ex) {
220 >            fail("should throw IMSE");
221 >        }
222 >    }
223 >
224 >    public void testAwaitNanos_Timeout() {
225 >        final ReentrantLock lock = new ReentrantLock();
226 >        final Condition c = lock.newCondition();
227 >        try {
228 >            lock.lock();
229 >            long t = c.awaitNanos(100);
230 >            assertTrue(t <= 0);
231 >            lock.unlock();
232 >        }
233 >        catch (Exception ex) {
234 >            fail("unexpected exception");
235 >        }
236 >    }
237 >
238 >    public void testAwait_Timeout() {
239 >        final ReentrantLock lock = new ReentrantLock();
240 >        final Condition c = lock.newCondition();
241 >        try {
242 >            lock.lock();
243 >            assertFalse(c.await(10, TimeUnit.MILLISECONDS));
244 >            lock.unlock();
245 >        }
246 >        catch (Exception ex) {
247 >            fail("unexpected exception");
248 >        }
249 >    }
250 >
251 >    public void testAwaitUntil_Timeout() {
252 >        final ReentrantLock lock = new ReentrantLock();
253 >        final Condition c = lock.newCondition();
254 >        try {
255 >            lock.lock();
256 >            java.util.Date d = new java.util.Date();
257 >            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
258 >            lock.unlock();
259 >        }
260 >        catch (Exception ex) {
261 >            fail("unexpected exception");
262 >        }
263 >    }
264 >
265 >    public void testAwait() {
266 >        final ReentrantLock lock = new ReentrantLock();
267 >        final Condition c = lock.newCondition();
268 >        Thread t = new Thread(new Runnable() {
269 >                public void run() {
270 >                    try {
271 >                        lock.lock();
272 >                        c.await();
273 >                        lock.unlock();
274 >                    }
275 >                    catch(InterruptedException e) {
276 >                        fail("unexpected exception");
277 >                    }
278 >                }
279 >            });
280 >
281 >        try {
282 >            t.start();
283 >            Thread.sleep(SHORT_DELAY_MS);
284 >            lock.lock();
285 >            c.signal();
286 >            lock.unlock();
287 >            t.join(SHORT_DELAY_MS);
288 >            assertFalse(t.isAlive());
289 >        }
290 >        catch (Exception ex) {
291 >            fail("unexpected exception");
292 >        }
293 >    }
294 >
295 >    public void testAwaitUninterruptibly() {
296 >        final ReentrantLock lock = new ReentrantLock();
297 >        final Condition c = lock.newCondition();
298 >        Thread t = new Thread(new Runnable() {
299 >                public void run() {
300 >                    lock.lock();
301 >                    c.awaitUninterruptibly();
302 >                    lock.unlock();
303 >                }
304 >            });
305 >
306 >        try {
307 >            t.start();
308 >            Thread.sleep(SHORT_DELAY_MS);
309 >            t.interrupt();
310 >            lock.lock();
311 >            c.signal();
312 >            lock.unlock();
313 >            t.join(SHORT_DELAY_MS);
314 >            assertFalse(t.isAlive());
315 >        }
316 >        catch (Exception ex) {
317 >            fail("unexpected exception");
318 >        }
319 >    }
320 >
321 >    public void testAwait_Interrupt() {
322 >        final ReentrantLock lock = new ReentrantLock();
323 >        final Condition c = lock.newCondition();
324 >        Thread t = new Thread(new Runnable() {
325 >                public void run() {
326 >                    try {
327 >                        lock.lock();
328 >                        c.await();
329 >                        lock.unlock();
330 >                        fail("should throw");
331 >                    }
332 >                    catch(InterruptedException success) {
333 >                    }
334 >                }
335 >            });
336 >
337 >        try {
338 >            t.start();
339 >            Thread.sleep(SHORT_DELAY_MS);
340 >            t.interrupt();
341 >            t.join(SHORT_DELAY_MS);
342 >            assertFalse(t.isAlive());
343 >        }
344 >        catch (Exception ex) {
345 >            fail("unexpected exception");
346 >        }
347 >    }
348 >
349 >    public void testAwaitNanos_Interrupt() {
350 >        final ReentrantLock lock = new ReentrantLock();
351 >        final Condition c = lock.newCondition();
352 >        Thread t = new Thread(new Runnable() {
353 >                public void run() {
354 >                    try {
355 >                        lock.lock();
356 >                        c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
357 >                        lock.unlock();
358 >                        fail("should throw");
359 >                    }
360 >                    catch(InterruptedException success) {
361 >                    }
362 >                }
363 >            });
364 >
365 >        try {
366 >            t.start();
367 >            Thread.sleep(SHORT_DELAY_MS);
368 >            t.interrupt();
369 >            t.join(SHORT_DELAY_MS);
370 >            assertFalse(t.isAlive());
371 >        }
372 >        catch (Exception ex) {
373 >            fail("unexpected exception");
374 >        }
375 >    }
376 >
377 >    public void testAwaitUntil_Interrupt() {
378 >        final ReentrantLock lock = new ReentrantLock();
379 >        final Condition c = lock.newCondition();
380 >        Thread t = new Thread(new Runnable() {
381 >                public void run() {
382 >                    try {
383 >                        lock.lock();
384 >                        java.util.Date d = new java.util.Date();
385 >                        c.awaitUntil(new java.util.Date(d.getTime() + 10000));
386 >                        lock.unlock();
387 >                        fail("should throw");
388 >                    }
389 >                    catch(InterruptedException success) {
390 >                    }
391 >                }
392 >            });
393 >
394 >        try {
395 >            t.start();
396 >            Thread.sleep(SHORT_DELAY_MS);
397 >            t.interrupt();
398 >            t.join(SHORT_DELAY_MS);
399 >            assertFalse(t.isAlive());
400 >        }
401 >        catch (Exception ex) {
402 >            fail("unexpected exception");
403 >        }
404 >    }
405 >
406 >    public void testSignalAll() {
407 >        final ReentrantLock lock = new ReentrantLock();
408 >        final Condition c = lock.newCondition();
409 >        Thread t1 = new Thread(new Runnable() {
410 >                public void run() {
411 >                    try {
412 >                        lock.lock();
413 >                        c.await();
414 >                        lock.unlock();
415 >                    }
416 >                    catch(InterruptedException e) {
417 >                        fail("unexpected exception");
418 >                    }
419 >                }
420 >            });
421 >
422 >        Thread t2 = new Thread(new Runnable() {
423 >                public void run() {
424 >                    try {
425 >                        lock.lock();
426 >                        c.await();
427 >                        lock.unlock();
428 >                    }
429 >                    catch(InterruptedException e) {
430 >                        fail("unexpected exception");
431 >                    }
432 >                }
433 >            });
434 >
435 >        try {
436 >            t1.start();
437 >            t2.start();
438 >            Thread.sleep(SHORT_DELAY_MS);
439 >            lock.lock();
440 >            c.signalAll();
441 >            lock.unlock();
442 >            t1.join(SHORT_DELAY_MS);
443 >            t2.join(SHORT_DELAY_MS);
444 >            assertFalse(t1.isAlive());
445 >            assertFalse(t2.isAlive());
446 >        }
447 >        catch (Exception ex) {
448 >            fail("unexpected exception");
449 >        }
450 >    }
451 >
452 >    public void testSerialization() {
453 >        ReentrantLock l = new ReentrantLock();
454 >        l.lock();
455 >        l.unlock();
456 >
457 >        try {
458 >            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
459 >            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
460 >            out.writeObject(l);
461 >            out.close();
462 >
463 >            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
464 >            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
465 >            ReentrantLock r = (ReentrantLock) in.readObject();
466 >            r.lock();
467 >            r.unlock();
468 >        } catch(Exception e){
469 >            e.printStackTrace();
470 >            fail("unexpected exception");
471 >        }
472      }
155    
473  
474   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines