示例#1
0
        public void WriteLock_should_be_exclusive()
        {
            var lockId = Guid.NewGuid().ToString();

            using var readWriteLock1 = new TinyReadWriteLock(lockId, 2, TimeSpan.FromMilliseconds(0));
            using var readWriteLock2 = new TinyReadWriteLock(lockId, 2, TimeSpan.FromMilliseconds(0));

            // Aquire the first lock
            readWriteLock1.AcquireWriteLock();

            // The second lock should now throw TimeoutException
            Should.Throw <TimeoutException>(() => readWriteLock2.AcquireWriteLock());

            // Make sure the expected locks are held
            readWriteLock1.IsWriterLockHeld.ShouldBeTrue();
            readWriteLock2.IsWriterLockHeld.ShouldBeFalse();

            // By releasing the first lock, the second lock should now be able to be held
            readWriteLock1.ReleaseWriteLock();
            readWriteLock2.AcquireWriteLock();

            // Make sure the expected locks are held
            readWriteLock1.IsWriterLockHeld.ShouldBeFalse();
            readWriteLock2.IsWriterLockHeld.ShouldBeTrue();
        }
示例#2
0
        public void Calling_ReleaseWriteLock_should_release_locks()
        {
            using var readWriteLock = new TinyReadWriteLock(Guid.NewGuid().ToString(), 2);

            readWriteLock.AcquireWriteLock();
            readWriteLock.IsWriterLockHeld.ShouldBeTrue();

            readWriteLock.ReleaseWriteLock();
            readWriteLock.IsWriterLockHeld.ShouldBeFalse();
        }
示例#3
0
        public void Calling_AcquireWriteLock_then_AquireReadLock_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.AcquireWriteLock();

            var readLockTask = Task.Run(() => readWriteLock2.AcquireReadLock());

            WaitForTaskToStart(readLockTask);

            readWriteLock1.IsWriterLockHeld.ShouldBeTrue();
            readWriteLock2.IsReaderLockHeld.ShouldBeFalse();

            readWriteLock1.ReleaseWriteLock();

            readLockTask.Wait();

            readWriteLock1.IsWriterLockHeld.ShouldBeFalse();
            readWriteLock2.IsReaderLockHeld.ShouldBeTrue();
        }