public void TestLockInterruptibly2() { ReentrantLock locker = new ReentrantLock(); try { locker.LockInterruptibly(); } catch (Exception e) { UnexpectedException(e); } Thread t = new Thread(InterruptedLockRunnable); try { t.Start(locker); t.Interrupt(); Assert.IsTrue(locker.IsLocked); Assert.IsTrue(locker.IsHeldByCurrentThread); t.Join(); } catch (Exception e) { UnexpectedException(e); } }
public void TestGetQueueLengthFair() { ReentrantLock locker = new ReentrantLock(true); Thread t1 = new Thread(InterruptedLockRunnable); Thread t2 = new Thread(InterruptibleLockRunnable); try { Assert.AreEqual(0, locker.QueueLength); locker.Lock(); t1.Start(locker); Thread.Sleep(SHORT_DELAY_MS); Assert.AreEqual(1, locker.QueueLength); t2.Start(locker); Thread.Sleep(SHORT_DELAY_MS); Assert.AreEqual(2, locker.QueueLength); t1.Interrupt(); Thread.Sleep(SHORT_DELAY_MS); Assert.AreEqual(1, locker.QueueLength); locker.UnLock(); Thread.Sleep(SHORT_DELAY_MS); Assert.AreEqual(0, locker.QueueLength); t1.Join(); t2.Join(); } catch (Exception e) { UnexpectedException(e); } }
public void TestConstructor() { ReentrantLock rl = new ReentrantLock(); Assert.IsFalse(rl.IsFair); ReentrantLock r2 = new ReentrantLock(true); Assert.True(r2.IsFair); }
public void TestLock() { ReentrantLock rl = new ReentrantLock(); rl.Lock(); Assert.IsTrue(rl.IsLocked); rl.UnLock(); }
public void TestHasQueuedThread() { ReentrantLock sync = new ReentrantLock(); Thread t1 = new Thread(InterruptedLockRunnable); Thread t2 = new Thread(InterruptibleLockRunnable); try { Assert.IsFalse(sync.HasQueuedThread(t1)); Assert.IsFalse(sync.HasQueuedThread(t2)); sync.Lock(); t1.Start(); Thread.Sleep(SHORT_DELAY_MS); Assert.IsTrue(sync.HasQueuedThread(t1)); t2.Start(); Thread.Sleep(SHORT_DELAY_MS); Assert.IsTrue(sync.HasQueuedThread(t1)); Assert.IsTrue(sync.HasQueuedThread(t2)); t1.Interrupt(); Thread.Sleep(SHORT_DELAY_MS); Assert.IsFalse(sync.HasQueuedThread(t1)); Assert.IsTrue(sync.HasQueuedThread(t2)); sync.UnLock(); Thread.Sleep(SHORT_DELAY_MS); Assert.IsFalse(sync.HasQueuedThread(t1)); Thread.Sleep(SHORT_DELAY_MS); Assert.IsFalse(sync.HasQueuedThread(t2)); t1.Join(); t2.Join(); } catch (Exception e) { UnexpectedException(e); } }
public void TestAwaitLockCount() { ReentrantLock locker = new ReentrantLock(); Condition c = locker.NewCondition(); Pair data = new Pair(locker, c); Thread t1 = new Thread(TestAwaitLockCountRunnable1); Thread t2 = new Thread(TestAwaitLockCountRunnable2); try { t1.Start(data); t2.Start(data); Thread.Sleep(SHORT_DELAY_MS); locker.Lock(); c.SignalAll(); locker.UnLock(); t1.Join(SHORT_DELAY_MS); t2.Join(SHORT_DELAY_MS); Assert.IsFalse(t1.IsAlive); Assert.IsFalse(t2.IsAlive); } catch (Exception e) { UnexpectedException(e); } }
public void TestGetWaitQueueLength() { ReentrantLock locker = new ReentrantLock(); Condition c = locker.NewCondition(); Pair data = new Pair(locker, c); Thread t1 = new Thread(TestGetWaitQueueLengthRunnable1); Thread t2 = new Thread(TestGetWaitQueueLengthRunnable2); try { t1.Start(data); Thread.Sleep(SHORT_DELAY_MS); t2.Start(data); Thread.Sleep(SHORT_DELAY_MS); locker.Lock(); Assert.IsTrue(locker.HasWaiters(c)); Assert.AreEqual(2, locker.GetWaitQueueLength(c)); c.SignalAll(); locker.UnLock(); Thread.Sleep(SHORT_DELAY_MS); locker.Lock(); Assert.IsFalse(locker.HasWaiters(c)); Assert.AreEqual(0, locker.GetWaitQueueLength(c)); locker.UnLock(); t1.Join(SHORT_DELAY_MS); t2.Join(SHORT_DELAY_MS); Assert.IsFalse(t1.IsAlive); Assert.IsFalse(t2.IsAlive); } catch (Exception e) { UnexpectedException(e); } }
public void TestFairLock() { ReentrantLock rl = new ReentrantLock(true); rl.Lock(); Assert.IsTrue(rl.IsLocked); rl.UnLock(); }
public void TestTryLock() { ReentrantLock rl = new ReentrantLock(); Assert.IsTrue(rl.TryLock()); Assert.IsTrue(rl.IsLocked); rl.UnLock(); }
public void TestToString() { ReentrantLock locker = new ReentrantLock(); String us = locker.ToString(); Assert.IsTrue(us.IndexOf("Unlocked") >= 0); locker.Lock(); String ls = locker.ToString(); Assert.IsTrue(ls.IndexOf("Locked") >= 0); }
private void InterruptibleLockRunnable(object state) { ReentrantLock locker = state as ReentrantLock; try { locker.LockInterruptibly(); } catch (ThreadInterruptedException) { } }
private void TestTryLockTimeoutRunnable(Object state) { ReentrantLock locker = state as ReentrantLock; try { ThreadAssertFalse(locker.TryLock(1)); } catch (Exception e) { ThreadUnexpectedException(e); } }
public void TestUnlockThreadStateException() { ReentrantLock rl = new ReentrantLock(); try { rl.UnLock(); ShouldThrow(); } catch (ThreadStateException) { } }
private void TestInterruptedException2Runnable(Object state) { ReentrantLock locker = state as ReentrantLock; try { locker.TryLock(MEDIUM_DELAY_MS); ThreadShouldThrow(); } catch (ThreadInterruptedException) { } }
public void TestHasQueuedThreadNRE() { ReentrantLock sync = new ReentrantLock(); try { sync.HasQueuedThread(null); ShouldThrow(); } catch (NullReferenceException) { } }
private void TestIsLockedRunnable(Object state) { ReentrantLock locker = state as ReentrantLock; locker.Lock(); try { Thread.Sleep(SMALL_DELAY_MS); } catch (Exception e) { ThreadUnexpectedException(e); } locker.UnLock(); }
public void TestAwaitTimeout() { ReentrantLock locker = new ReentrantLock(); Condition c = locker.NewCondition(); try { locker.Lock(); c.Await(SHORT_DELAY_MS); locker.UnLock(); } catch (Exception e) { UnexpectedException(e); } }
public void TestAwaitUntilTimeout() { ReentrantLock locker = new ReentrantLock(); Condition c = locker.NewCondition(); try { locker.Lock(); c.AwaitUntil(DateTime.Now.AddMilliseconds(100)); locker.UnLock(); } catch (Exception e) { UnexpectedException(e); } }
public void TestGetHoldCount() { ReentrantLock locker = new ReentrantLock(); for (int i = 1; i <= SIZE; i++) { locker.Lock(); Assert.AreEqual(i, locker.HoldCount); } for (int i = SIZE; i > 0; i--) { locker.UnLock(); Assert.AreEqual(i - 1, locker.HoldCount); } }
private void TestAwaitTimedInterruptRunnable(Object state) { Pair data = state as Pair; ReentrantLock locker = data.first as ReentrantLock; Condition c = data.second as Condition; try { locker.Lock(); c.Await(1000); // 1 sec locker.UnLock(); ThreadShouldThrow(); } catch (ThreadInterruptedException) { } }
private void TestAwaitUntilInterruptRunnable(Object state) { Pair data = state as Pair; ReentrantLock locker = data.first as ReentrantLock; Condition c = data.second as Condition; try { locker.Lock(); c.AwaitUntil(DateTime.Now.AddMilliseconds(10000)); locker.UnLock(); ThreadShouldThrow(); } catch (ThreadInterruptedException) { } }
public void TestGetWaitQueueLengthNRE() { ReentrantLock locker = new ReentrantLock(); try { locker.GetWaitQueueLength(null); ShouldThrow(); } catch (NullReferenceException) { } catch (Exception e) { UnexpectedException(e); } }
public void TestHasWaitersNRE() { ReentrantLock locker = new ReentrantLock(); try { locker.HasWaiters(null); ShouldThrow(); } catch (NullReferenceException) { } catch (Exception e) { UnexpectedException(e); } }
private void TestAwaitRunnable(Object state) { Pair data = state as Pair; ReentrantLock locker = data.first as ReentrantLock; Condition c = data.second as Condition; try { locker.Lock(); c.Await(); locker.UnLock(); } catch (ThreadInterruptedException e) { ThreadUnexpectedException(e); } }
public void TestAwaitWithTimeout() { ReentrantLock locker = new ReentrantLock(); Condition c = locker.NewCondition(); try { locker.Lock(); long t = c.Await(1); Assert.IsTrue(t <= 0); locker.UnLock(); } catch (Exception e) { UnexpectedException(e); } }
private void TestGetWaitingThreadsRunnable2(Object state) { Pair data = state as Pair; ReentrantLock locker = data.first as ReentrantLock; Condition c = data.second as Condition; try { locker.Lock(); ThreadAssertFalse(locker.GetWaitingThreads(c).IsEmpty()); c.Await(); locker.UnLock(); } catch (ThreadInterruptedException e) { ThreadUnexpectedException(e); } }
public void TestTryLockTimeout() { ReentrantLock locker = new ReentrantLock(); locker.Lock(); Thread t = new Thread(TestTryLockTimeoutRunnable); try { t.Start(locker); t.Join(); locker.UnLock(); } catch (Exception e) { UnexpectedException(e); } }
public void TestHasWaitersTSE() { ReentrantLock locker = new ReentrantLock(); Condition c = (locker.NewCondition()); try { locker.HasWaiters(c); ShouldThrow(); } catch (ThreadStateException) { } catch (Exception e) { UnexpectedException(e); } }
public void TestInterruptedException2() { ReentrantLock locker = new ReentrantLock(); locker.Lock(); Thread t = new Thread(TestInterruptedException2Runnable); try { t.Start(locker); t.Interrupt(); t.Join(); } catch (Exception e) { UnexpectedException(e); } }
public void TestGetWaitQueueLengthTSE() { ReentrantLock locker = new ReentrantLock(); Condition c = (locker.NewCondition()); try { locker.GetWaitQueueLength(c); ShouldThrow(); } catch (ThreadStateException) { } catch (Exception e) { UnexpectedException(e); } }
public void TestSignalIllegalMonitor() { ReentrantLock locker = new ReentrantLock(); Condition c = locker.NewCondition(); try { c.Signal(); ShouldThrow(); } catch (ThreadStateException) { } catch (Exception e) { UnexpectedException(e); } }
public void TestIsLocked() { ReentrantLock locker = new ReentrantLock(); locker.Lock(); Assert.IsTrue(locker.IsLocked); locker.UnLock(); Assert.IsFalse(locker.IsLocked); Thread t = new Thread(TestIsLockedRunnable); try { t.Start(locker); Thread.Sleep(SHORT_DELAY_MS); Assert.IsTrue(locker.IsLocked); t.Join(); Assert.IsFalse(locker.IsLocked); } catch(Exception e) { UnexpectedException(e); } }
public void TestLockInterruptibly1() { ReentrantLock locker = new ReentrantLock(); locker.Lock(); Thread t = new Thread(InterruptedLockRunnable); try { t.Start(locker); Thread.Sleep(SHORT_DELAY_MS); t.Interrupt(); Thread.Sleep(SHORT_DELAY_MS); locker.UnLock(); t.Join(); } catch(Exception e) { UnexpectedException(e); } }
public void TestLockInterruptibly2() { ReentrantLock locker = new ReentrantLock(); try { locker.LockInterruptibly(); } catch(Exception e) { UnexpectedException(e); } Thread t = new Thread(InterruptedLockRunnable); try { t.Start(locker); t.Interrupt(); Assert.IsTrue(locker.IsLocked); Assert.IsTrue(locker.IsHeldByCurrentThread); t.Join(); } catch(Exception e) { UnexpectedException(e); } }
public void TestInterruptedException2() { ReentrantLock locker = new ReentrantLock(); locker.Lock(); Thread t = new Thread(TestInterruptedException2Runnable); try { t.Start(locker); t.Interrupt(); t.Join(); } catch(Exception e) { UnexpectedException(e); } }
public void TestGetHoldCount() { ReentrantLock locker = new ReentrantLock(); for(int i = 1; i <= SIZE; i++) { locker.Lock(); Assert.AreEqual(i,locker.HoldCount); } for(int i = SIZE; i > 0; i--) { locker.UnLock(); Assert.AreEqual(i-1,locker.HoldCount); } }
public void TestAwait() { ReentrantLock locker = new ReentrantLock(); Condition c = locker.NewCondition(); Pair data = new Pair(locker, c); Thread t = new Thread(TestAwaitRunnable); try { t.Start(data); Thread.Sleep(MEDIUM_DELAY_MS); locker.Lock(); c.Signal(); locker.UnLock(); t.Join(SMALL_DELAY_MS); Assert.IsFalse(t.IsAlive); } catch (Exception e) { UnexpectedException(e); } }
public void TestHasWaitersIAE() { ReentrantLock locker = new ReentrantLock(); Condition c = (locker.NewCondition()); ReentrantLock locker2 = new ReentrantLock(); try { locker2.HasWaiters(c); ShouldThrow(); } catch (ArgumentException) { } catch (Exception e) { UnexpectedException(e); } }
public void TestTryLockTimeout() { ReentrantLock locker = new ReentrantLock(); locker.Lock(); Thread t = new Thread(TestTryLockTimeoutRunnable); try { t.Start(locker); t.Join(); locker.UnLock(); } catch(Exception e) { UnexpectedException(e); } }
public void TestHasQueuedThread() { ReentrantLock sync = new ReentrantLock(); Thread t1 = new Thread(InterruptedLockRunnable); Thread t2 = new Thread(InterruptibleLockRunnable); try { Assert.IsFalse(sync.HasQueuedThread(t1)); Assert.IsFalse(sync.HasQueuedThread(t2)); sync.Lock(); t1.Start(); Thread.Sleep(SHORT_DELAY_MS); Assert.IsTrue(sync.HasQueuedThread(t1)); t2.Start(); Thread.Sleep(SHORT_DELAY_MS); Assert.IsTrue(sync.HasQueuedThread(t1)); Assert.IsTrue(sync.HasQueuedThread(t2)); t1.Interrupt(); Thread.Sleep(SHORT_DELAY_MS); Assert.IsFalse(sync.HasQueuedThread(t1)); Assert.IsTrue(sync.HasQueuedThread(t2)); sync.UnLock(); Thread.Sleep(SHORT_DELAY_MS); Assert.IsFalse(sync.HasQueuedThread(t1)); Thread.Sleep(SHORT_DELAY_MS); Assert.IsFalse(sync.HasQueuedThread(t2)); t1.Join(); t2.Join(); } catch(Exception e) { UnexpectedException(e); } }
public void TestGetQueueLengthFair() { ReentrantLock locker = new ReentrantLock(true); Thread t1 = new Thread(InterruptedLockRunnable); Thread t2 = new Thread(InterruptibleLockRunnable); try { Assert.AreEqual(0, locker.QueueLength); locker.Lock(); t1.Start(locker); Thread.Sleep(SHORT_DELAY_MS); Assert.AreEqual(1, locker.QueueLength); t2.Start(locker); Thread.Sleep(SHORT_DELAY_MS); Assert.AreEqual(2, locker.QueueLength); t1.Interrupt(); Thread.Sleep(SHORT_DELAY_MS); Assert.AreEqual(1, locker.QueueLength); locker.UnLock(); Thread.Sleep(SHORT_DELAY_MS); Assert.AreEqual(0, locker.QueueLength); t1.Join(); t2.Join(); } catch(Exception e) { UnexpectedException(e); } }
public void TestAwaitUntilInterrupt() { ReentrantLock locker = new ReentrantLock(); Condition c = locker.NewCondition(); Pair data = new Pair(locker, c); Thread t = new Thread(TestAwaitUntilInterruptRunnable); try { t.Start(data); Thread.Sleep(SHORT_DELAY_MS); t.Interrupt(); t.Join(SHORT_DELAY_MS); Assert.IsFalse(t.IsAlive); } catch (Exception e) { UnexpectedException(e); } }
public void TestUnlockThreadStateException() { ReentrantLock rl = new ReentrantLock(); try { rl.UnLock(); ShouldThrow(); } catch(ThreadStateException) { } }