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

            rl.Lock();
            Assert.IsTrue(rl.IsLocked);
            rl.UnLock();
        }
예제 #7
0
        public void TestTryLock()
        {
            ReentrantLock rl = new ReentrantLock();

            Assert.IsTrue(rl.TryLock());
            Assert.IsTrue(rl.IsLocked);
            rl.UnLock();
        }
예제 #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);
            }
        }
예제 #9
0
        public void TestUnlockThreadStateException()
        {
            ReentrantLock rl = new ReentrantLock();

            try
            {
                rl.UnLock();
                ShouldThrow();
            }
            catch (ThreadStateException)
            {
            }
        }
예제 #10
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();
        }
예제 #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);
            }
        }
예제 #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);
            }
        }
예제 #13
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);
            }
        }
예제 #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);
            }
        }
예제 #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);
            }
        }
예제 #16
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)
            {
            }
        }
예제 #17
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)
            {
            }
        }
예제 #18
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);
            }
        }
예제 #19
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);
            }
        }
예제 #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);
            }
        }
예제 #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);
            }
        }
예제 #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);
            }
        }
예제 #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);
            }
        }
예제 #24
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);
            }
        }
예제 #25
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);
            }
        }
예제 #26
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);
            }
        }
예제 #27
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);
     }
 }
예제 #28
0
 public void TestUnlockThreadStateException()
 {
     ReentrantLock rl = new ReentrantLock();
     try
     {
         rl.UnLock();
         ShouldThrow();
     }
     catch(ThreadStateException)
     {
     }
 }
예제 #29
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);
     }
 }
예제 #30
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);
     }
 }
예제 #31
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);
     }
 }
예제 #32
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);
            }
        }
예제 #33
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);
            }
        }
예제 #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);
            }
        }
예제 #35
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);
            }
        }
예제 #36
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);
     }
 }