示例#1
0
        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);
            }
        }
示例#2
0
        public void TestHasWaitersNRE()
        {
            ReentrantLock locker = new ReentrantLock();

            try
            {
                locker.HasWaiters(null);
                ShouldThrow();
            }
            catch (NullReferenceException)
            {
            }
            catch (Exception e)
            {
                UnexpectedException(e);
            }
        }
示例#3
0
        public void TestHasWaitersTSE()
        {
            ReentrantLock locker = new ReentrantLock();
            Condition     c      = (locker.NewCondition());

            try
            {
                locker.HasWaiters(c);
                ShouldThrow();
            }
            catch (ThreadStateException)
            {
            }
            catch (Exception e)
            {
                UnexpectedException(e);
            }
        }
示例#4
0
        private void TestGetWaitQueueLengthRunnable2(Object state)
        {
            Pair          data   = state as Pair;
            ReentrantLock locker = data.first as ReentrantLock;
            Condition     c      = data.second as Condition;

            try
            {
                locker.Lock();
                ThreadAssertTrue(locker.HasWaiters(c));
                ThreadAssertEquals(1, locker.GetWaitQueueLength(c));
                c.Await();
                locker.UnLock();
            }
            catch (ThreadInterruptedException e)
            {
                ThreadUnexpectedException(e);
            }
        }
示例#5
0
        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);
            }
        }
示例#6
0
        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);
            }
        }
示例#7
0
 public void TestHasWaitersTSE()
 {
     ReentrantLock locker = new ReentrantLock();
     Condition c = (locker.NewCondition());
     try
     {
         locker.HasWaiters(c);
         ShouldThrow();
     }
     catch (ThreadStateException)
     {
     }
     catch (Exception e)
     {
         UnexpectedException(e);
     }
 }
示例#8
0
 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);
     }
 }
示例#9
0
 public void TestHasWaitersNRE()
 {
     ReentrantLock locker = new ReentrantLock();
     try
     {
         locker.HasWaiters(null);
         ShouldThrow();
     }
     catch (NullReferenceException)
     {
     }
     catch (Exception e)
     {
         UnexpectedException(e);
     }
 }