Esempio n. 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);
            }
        }
Esempio n. 2
0
 public void TestLock()
 {
     ReentrantLock rl = new ReentrantLock();
     rl.Lock();
     Assert.IsTrue(rl.IsLocked);
     rl.UnLock();
 }
Esempio n. 3
0
        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);
            }
        }
Esempio n. 4
0
        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);
            }
        }
Esempio n. 5
0
        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);
            }
        }
Esempio n. 6
0
        public void TestFairLock()
        {
            ReentrantLock rl = new ReentrantLock(true);

            rl.Lock();
            Assert.IsTrue(rl.IsLocked);
            rl.UnLock();
        }
Esempio n. 7
0
        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);
        }
Esempio n. 8
0
        private void TestAwaitLockCountRunnable2(Object state)
        {
            Pair          data   = state as Pair;
            ReentrantLock locker = data.first as ReentrantLock;
            Condition     c      = data.second as Condition;

            try
            {
                locker.Lock();
                locker.Lock();
                ThreadAssertEquals(2, locker.HoldCount);
                c.Await();
                ThreadAssertEquals(2, locker.HoldCount);
                locker.UnLock();
                locker.UnLock();
            }
            catch (ThreadInterruptedException e)
            {
                ThreadUnexpectedException(e);
            }
        }
Esempio n. 9
0
        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();
        }
Esempio n. 10
0
        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);
            }
        }
Esempio n. 11
0
        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);
            }
        }
Esempio n. 12
0
        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);
            }
        }
Esempio n. 13
0
        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)
            {
            }
        }
Esempio n. 14
0
        private void TestSignalAllRunnable2(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);
            }
        }
Esempio n. 15
0
        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);
            }
        }
Esempio n. 16
0
        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)
            {
            }
        }
Esempio n. 17
0
        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);
            }
        }
Esempio n. 18
0
        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);
            }
        }
Esempio n. 19
0
        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);
            }
        }
Esempio n. 20
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);
            }
        }
Esempio n. 21
0
        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);
            }
        }
Esempio n. 22
0
        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);
            }
        }
Esempio n. 23
0
        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);
            }
        }
Esempio n. 24
0
        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);
            }
        }
Esempio n. 25
0
        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);
            }
        }
Esempio n. 26
0
        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);
            }
        }
Esempio n. 27
0
 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);
 }
Esempio n. 28
0
 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);
     }
 }
Esempio n. 29
0
 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);
     }
 }
Esempio n. 30
0
 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);
     }
 }
Esempio n. 31
0
 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);
     }
 }
Esempio n. 32
0
 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);
     }
 }
Esempio n. 33
0
        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);
            }
        }
Esempio n. 34
0
        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);
            }
        }
Esempio n. 35
0
        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);
            }
        }
Esempio n. 36
0
        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);
            }
        }
Esempio n. 37
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);
            }
        }