コード例 #1
0
        public void GetWaitingThreadsChokesWhenNotLocked(bool isFair)
        {
            PublicReentrantLock myLock = new PublicReentrantLock(isFair);
            ICondition          c      = (myLock.NewCondition());

            myLock.GetWaitingThreadsPublic(c);
        }
コード例 #2
0
        public void GetWaitingThreadsChokesWhenNotOwned([Values(true, false)] bool isFair)
        {
            PublicReentrantLock myLock = new PublicReentrantLock(isFair);
            ICondition          c      = (myLock.NewCondition());
            PublicReentrantLock lock2  = new PublicReentrantLock(isFair);

            lock2.GetWaitingThreadsPublic(c);
        }
コード例 #3
0
        public void GetWaitingThreadsIncludesWaitingThreads(bool isFair)
        {
            PublicReentrantLock myLock = new PublicReentrantLock(isFair);

            ICondition c = myLock.NewCondition();

            myLock.Lock();
            Assert.That(myLock.GetWaitingThreadsPublic(c).Count, Is.EqualTo(0));
            myLock.Unlock();
            Thread t1 = ThreadManager.StartAndAssertRegistered(
                "T1",
                delegate
            {
                myLock.Lock();
                Assert.That(myLock.GetWaitingThreadsPublic(c).Count, Is.EqualTo(0));
                c.Await();
                myLock.Unlock();
            });

            Thread.Sleep(Delays.Short);
            Thread t2 = ThreadManager.StartAndAssertRegistered(
                "T2",
                delegate
            {
                myLock.Lock();
                Assert.That(myLock.GetWaitingThreadsPublic(c).Count, Is.Not.EqualTo(0));
                c.Await();
                myLock.Unlock();
            });

            Thread.Sleep(Delays.Short);
            myLock.Lock();
            Assert.IsTrue(myLock.HasWaiters(c));
            Assert.IsTrue(myLock.GetWaitingThreadsPublic(c).Contains(t1));
            Assert.IsTrue(myLock.GetWaitingThreadsPublic(c).Contains(t2));
            c.SignalAll();
            myLock.Unlock();

            Thread.Sleep(Delays.Short);
            myLock.Lock();
            Assert.IsFalse(myLock.HasWaiters(c));
            Assert.IsTrue((myLock.GetWaitingThreadsPublic(c).Count == 0));
            myLock.Unlock();

            ThreadManager.JoinAndVerify();
        }
コード例 #4
0
        public void TestGetWaitingThreadsTSE()
        {
            PublicReentrantLock locker = new PublicReentrantLock();
            Condition           c      = (locker.NewCondition());

            try
            {
                locker.GetWaitingThreads(c);
                ShouldThrow();
            }
            catch (ThreadStateException)
            {
            }
            catch (Exception e)
            {
                UnexpectedException(e);
            }
        }
コード例 #5
0
        public void TestGetWaitingThreads()
        {
            PublicReentrantLock locker = new PublicReentrantLock();
            Condition           c      = locker.NewCondition();
            Pair data = new Pair(locker, c);

            Thread t1 = new Thread(TestGetWaitingThreadsRunnable1);
            Thread t2 = new Thread(TestGetWaitingThreadsRunnable2);

            try
            {
                locker.Lock();
                Assert.IsTrue(locker.GetWaitingThreads(c).IsEmpty());
                locker.UnLock();
                t1.Start(data);
                Thread.Sleep(SHORT_DELAY_MS);
                t2.Start(data);
                Thread.Sleep(SHORT_DELAY_MS);
                locker.Lock();
                Assert.IsTrue(locker.HasWaiters(c));
                Assert.IsTrue(locker.GetWaitingThreads(c).Contains(t1));
                Assert.IsTrue(locker.GetWaitingThreads(c).Contains(t2));
                c.SignalAll();
                locker.UnLock();
                Thread.Sleep(SHORT_DELAY_MS);
                locker.Lock();
                Assert.IsFalse(locker.HasWaiters(c));
                Assert.IsTrue(locker.GetWaitingThreads(c).IsEmpty());
                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);
            }
        }
コード例 #6
0
        public void TestGetWaitingThreads()
        {
            PublicReentrantLock locker = new PublicReentrantLock();
            Condition c = locker.NewCondition();
            Pair data = new Pair(locker, c);

            Thread t1 = new Thread(TestGetWaitingThreadsRunnable1);
            Thread t2 = new Thread(TestGetWaitingThreadsRunnable2);
    
            try
            {
                locker.Lock();
                Assert.IsTrue(locker.GetWaitingThreads(c).IsEmpty());
                locker.UnLock();
                t1.Start(data);
                Thread.Sleep(SHORT_DELAY_MS);
                t2.Start(data);
                Thread.Sleep(SHORT_DELAY_MS);
                locker.Lock();
                Assert.IsTrue(locker.HasWaiters(c));
                Assert.IsTrue(locker.GetWaitingThreads(c).Contains(t1));
                Assert.IsTrue(locker.GetWaitingThreads(c).Contains(t2));
                c.SignalAll();
                locker.UnLock();
                Thread.Sleep(SHORT_DELAY_MS);
                locker.Lock();
                Assert.IsFalse(locker.HasWaiters(c));
                Assert.IsTrue(locker.GetWaitingThreads(c).IsEmpty());
                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 TestGetWaitingThreadsTSE()
 {
     PublicReentrantLock locker = new PublicReentrantLock();
     Condition c = (locker.NewCondition());
     try
     {
         locker.GetWaitingThreads(c);
         ShouldThrow();
     }
     catch (ThreadStateException)
     {
     }
     catch (Exception e)
     {
         UnexpectedException(e);
     }
 }