public void Calling_ReleaseReadLock_without_any_lock_held_should_throw()
 {
     using (var readWriteLock = new TinyReadWriteLock(Guid.NewGuid().ToString(), 1))
     {
         Assert.Throws <SemaphoreFullException>(() => readWriteLock.ReleaseReadLock());
     }
 }
Example #2
0
        public void Calling_ReleaseReadLock_should_release_lock()
        {
            using var readWriteLock = new TinyReadWriteLock(Guid.NewGuid().ToString(), 1);

            readWriteLock.AcquireReadLock();
            Assert.True(readWriteLock.IsReaderLockHeld);

            readWriteLock.ReleaseReadLock();
            Assert.False(readWriteLock.IsReaderLockHeld);
        }
        public void Calling_AcquireReadLock_then_AquireWriteLock_should_wait_for_other_lock()
        {
            var lockId = Guid.NewGuid().ToString();

            using (var readWriteLock1 = new TinyReadWriteLock(lockId, 2))
                using (var readWriteLock2 = new TinyReadWriteLock(lockId, 2))
                {
                    readWriteLock1.AcquireReadLock();

                    Task.Factory.StartNew(() => readWriteLock2.AcquireWriteLock());
                    Thread.Sleep(10);

                    Assert.True(readWriteLock1.IsReaderLockHeld);
                    Assert.False(readWriteLock2.IsWriterLockHeld);

                    readWriteLock1.ReleaseReadLock();
                    Thread.Sleep(10);

                    Assert.False(readWriteLock1.IsReaderLockHeld);
                    Assert.True(readWriteLock2.IsWriterLockHeld);
                }
        }
Example #4
0
        public void Calling_AcquireReadLock_then_AquireWriteLock_should_wait_for_other_lock()
        {
            var lockId = Guid.NewGuid().ToString();

            using var readWriteLock1 = new TinyReadWriteLock(lockId, 2);
            using var readWriteLock2 = new TinyReadWriteLock(lockId, 2);

            readWriteLock1.AcquireReadLock();

            var writeLockTask = Task.Run(() => readWriteLock2.AcquireWriteLock());

            WaitForTaskToStart(writeLockTask);

            Assert.True(readWriteLock1.IsReaderLockHeld);
            Assert.False(readWriteLock2.IsWriterLockHeld);

            readWriteLock1.ReleaseReadLock();

            writeLockTask.Wait();

            Assert.False(readWriteLock1.IsReaderLockHeld);
            Assert.True(readWriteLock2.IsWriterLockHeld);
        }