[Test] public void GetHoldCountReturnsNumberOfRecursiveHolds([Values(true, false)] bool isFair) { _lock = new ReentrantLock(isFair); RecursiveHolds(delegate { _lock.Lock(); }); RecursiveHolds((delegate { Assert.IsTrue(_lock.TryLock()); })); RecursiveHolds(delegate { Assert.IsTrue(_lock.TryLock(Delays.Short)); }); RecursiveHolds(delegate { _lock.LockInterruptibly(); }); }
[Test] public void TryLockSucceedsOnUnlockedLock([Values(true, false)] bool isFair) { ReentrantLock rl = new ReentrantLock(isFair); Assert.IsTrue(rl.TryLock()); Assert.IsTrue(rl.IsLocked); rl.Unlock(); }
[Test] public void TryLockChokesWhenIsLocked([Values(true, false)] bool isFair) { _lock = new ReentrantLock(isFair); _lock.Lock(); ThreadManager.StartAndAssertRegistered( "T1", delegate { Assert.IsFalse(_lock.TryLock()); }); ThreadManager.JoinAndVerify(); _lock.Unlock(); }
[Test] public void TimedTryLockTimesOutOnLockedLock([Values(true, false)] bool isFair) { _lock = new ReentrantLock(isFair); _lock.Lock(); ThreadManager.StartAndAssertRegistered( "T1", () => Assert.IsFalse(_lock.TryLock(TimeSpan.FromMilliseconds(1)))); ThreadManager.JoinAndVerify(); _lock.Unlock(); }
[Test] public void TimedTryLockIsInterruptible([Values(true, false)] bool isFair) { _lock = new ReentrantLock(isFair); _lock.Lock(); Thread t = ThreadManager.StartAndAssertRegistered( "T1", () => Assert.Throws <ThreadInterruptedException>( () => _lock.TryLock(Delays.Medium))); t.Interrupt(); ThreadManager.JoinAndVerify(t); }
[Test] public void TimedTryLockAllSucceedsFromMultipleThreads([Values(true, false)] bool isFair) { _lock = new ReentrantLock(isFair); _lock.LockInterruptibly(); ThreadStart action = delegate { Assert.IsTrue(_lock.TryLock(Delays.Long)); try { Thread.Sleep(Delays.Short); } finally { _lock.Unlock(); } }; ThreadManager.StartAndAssertRegistered("T", action, action); Assert.IsTrue(_lock.IsLocked); Assert.IsTrue(_lock.IsHeldByCurrentThread); _lock.Unlock(); ThreadManager.JoinAndVerify(); }